lib/Serge/Engine/Plugin/completeness.pm

filter: Statementstmt 12.0% 13 / 108 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 33.3% 4 / 12 Podpod 0.0% 0 / 7 totaltotal 7.8% 17 / 216 SCAR 70.9
CC
38
Cov
7%
_check_can_generate
182.0
validate_data
132.0
report_new_files
132.0
CRAP 1197.0
« lib/Serge/DB.pm ↑ Summary lib/Serge/Engine/Plugin/parse_json.pm »
1 package Serge::Engine::Plugin::completeness;
2 1 ▶use parent Serge::Plugin::Base::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Serge::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Serge::Util qw(xml_escape_strref);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub name {
9 0 ▶ return 'Generate/update/delete files based on their translation completeness';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
10 }
11  
12 sub init {
13 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
14  
15 0 $self->{allowed_files} = {};
16 0 $self->{created_files} = {};
17 0 $self->{deleted_files} = {};
18  
19 0 $self->SUPER::init(@_);
20  
21 0 $self->merge_schema({
22 create_threshold => 'STRING',
23 update_threshold => 'STRING',
24  
25 bypass_languages => 'ARRAY',
26  
27 save_incomplete_to => 'STRING',
28  
29 can_delete => 'BOOLEAN',
30  
31 email_from => 'STRING',
32 email_to => 'ARRAY',
33 email_subject => 'STRING',
34 });
35  
36 0 $self->add({
37 can_generate_localized_file => \&can_generate_localized_file,
38 after_save_localized_file => \&after_save_localized_file,
39 after_job => \&report_new_files,
40 });
41 }
42  
43 sub validate_data {
44 0 ▶ my ($self) = @_;
Subroutine validate_data: not called CC=11 SCAR=48.8
Pod undocumented
45  
46 0 $self->SUPER::validate_data;
47  
48 # By default, if no parameters are provided, 'create_threshold' is set to 1,
49 # 'update_threshold' is set to 0, and 'can_delete' is undefined.
50 # This means that a file will never be created unless it is 100% complete;
51 # once it has been created, it will always be updated no matter what its
52 # current completeness ratio is (even if it drops to zero), and it will never
53 # be deleted.
54 #
55 # If 'can_detete' is set to a true value, the file will be deleted if it
56 # completeness ratio will be equal or less than update_threshold value
57  
58 0 ▶ my $create_threshold = $self->{data}->{create_threshold} = exists $self->{data}->{create_threshold} ? $self->{data}->{create_threshold} + 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'data'}{'create_threshold'} ? : 0 0 0
59 0 ▶ my $update_threshold = $self->{data}->{update_threshold} = exists $self->{data}->{update_threshold} ? $self->{data}->{update_threshold} + 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'data'}{'update_threshold'} ? : 0 0 0
60  
61 0 ▶ die "ERROR: create_threshold must be in [0..1] range" if $create_threshold < 0 or $create_threshold > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $create_threshold < 0 or $create_threshold > 1 0 0 0
Condition0%0/3
$create_threshold < 0 or $create_threshold > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$create_threshold < 0 or $create_threshold > 1
$create_threshold < 0 $create_threshold > 1
Truth table0%0/3
A or B
A= $create_threshold < 0B= $create_threshold > 1
ABresult
1X1
011
000
62 0 ▶ die "ERROR: update_threshold must be in [0..1] range" if $update_threshold < 0 or $update_threshold > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $update_threshold < 0 or $update_threshold > 1 0 0 0
Condition0%0/3
$update_threshold < 0 or $update_threshold > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$update_threshold < 0 or $update_threshold > 1
$update_threshold < 0 $update_threshold > 1
Truth table0%0/3
A or B
A= $update_threshold < 0B= $update_threshold > 1
ABresult
1X1
011
000
63 0 ▶ die "ERROR: update_threshold must be less or equal to create_threshold" if $update_threshold > $create_threshold;
Branch: 1 branch
BranchTrueFalseTotal
if $update_threshold > $create_threshold 0 0 0
64  
65 0 ▶ print "WARNING: data->email_from not defined. Will skip sending any reports.\n" unless $self->{data}->{email_from};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"}{"email_from"} 0 0 0
66 0 ▶ print "WARNING: data->email_to not defined. Will skip sending any reports.\n" unless $self->{data}->{email_to};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"}{"email_to"} 0 0 0
67  
68 0 $self->{bypass_languages} = {};
69 0 ▶ if (defined $self->{data}->{bypass_languages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"data"}{"bypass_languages"}) 0 0 0
70 map {
71 0 $self->{bypass_languages}->{$_} = 1;
72 0 } @{$self->{data}->{bypass_languages}};
73 }
74 }
75  
76 # public static method
77 sub make_key {
78 0 ▶ my ($file, $lang) = @_;
Subroutine make_key: not called CC=1 SCAR=6.9
Pod undocumented
79 0 return $lang."\001".$file;
80 }
81  
82 sub can_generate_localized_file {
83 0 ▶ my ($self, $phase, $file, $lang, $strref) = @_;
Subroutine can_generate_localized_file: not called CC=5 SCAR=34.0
Pod undocumented
84  
85 # skip checks and allow file generation if the language
86 # is in `bypass_languages` list
87 0 ▶ return 1 if exists $self->{bypass_languages}->{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"bypass_languages"}{$lang} 0 0 0
88  
89 0 my $file_id = $self->{parent}->{engine}->{db}->get_file_id($self->{parent}->{db_namespace}, $self->{parent}->{id}, $file, 1); # 1=no_create
90  
91 # sanity check: if the file is unknown, do not create it
92 0 ▶ if (!$file_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_id) 0 0 0
93 0 print "WARNING: file is not registered in the database\n";
94 0 return 0;
95 }
96  
97 0 my $fullpath = $self->{parent}->{engine}->get_full_output_path($file, $lang); # get for the default output dir
98  
99 0 my $can_generate = $self->_check_can_generate($file_id, $fullpath, $file, $lang, $strref);
100 0 ▶ return 1 if $can_generate;
Branch: 1 branch
BranchTrueFalseTotal
if $can_generate 0 0 0
101  
102 0 ▶ if ($self->{data}->{save_incomplete_to}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"save_incomplete_to"}) 0 0 0
103 # save the file information for rewriting purposes
104 0 my $newpath = $self->{parent}->{engine}->get_full_output_path($file, $lang, $self->{data}->{save_incomplete_to});
105 0 $self->{parent}->{engine}->set_full_output_path($file, $lang, $newpath); # set for the default output dir
106 0 return 1; # can generate (under the new name)
107 }
108  
109 0 return 0; # can't generate
110 }
111  
112 sub _check_can_generate {
113 0 ▶ my ($self, $file_id, $fullpath, $file, $lang, $strref) = @_;
Subroutine _check_can_generate: not called CC=13 SCAR=52.0
114  
115 # source language is always 100% translated
116 0 ▶ return 1 if $lang eq $self->{parent}->{source_language};
Branch: 1 branch
BranchTrueFalseTotal
if $lang eq $self->{"parent"}{"source_language"} 0 0 0
117  
118 0 my $create_threshold = $self->{data}->{create_threshold};
119 0 my $update_threshold = $self->{data}->{update_threshold};
120 0 my $can_delete = $self->{data}->{can_delete};
121  
122 0 my $total = $self->{parent}->{engine}->{ts_items_count}->{"$file_id:$lang"};
123  
124 0 ▶ if (-f $fullpath) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fullpath) { } 0 0 0
125 # file exists, check against delete_ratio and update_ratio
126  
127 # get the value from the database only if necessary
128 0 ▶ my $ratio = $self->{parent}->{engine}->{db}->get_file_completeness_ratio($file_id, $lang, $total) if $can_delete or $update_threshold > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $can_delete or $update_threshold > 0 0 0 0
Condition0%0/3
$can_delete or $update_threshold > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$can_delete or $update_threshold > 0
$can_delete $update_threshold > 0
Truth table0%0/3
A or B
A= $can_deleteB= $update_threshold > 0
ABresult
1X1
011
000
129 0 ▶ print "[completeness]::[_check_can_generate] existing file, file_id=$file_id, lang=$lang, total=$total, ratio=$ratio, can_delete=$can_delete, update_threshold=$update_threshold\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
130  
131 0 ▶ if ($can_delete and $ratio <= $update_threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_delete and $ratio <= $update_threshold) 0 0 0
Condition0%0/3
$can_delete and $ratio <= $update_threshold
!ll&&!rl&&r
000
MC/DC0%0/2
$can_delete and $ratio <= $update_threshold
$can_delete $ratio <= $update_threshold
Truth table0%0/3
A and B
A= $can_deleteB= $ratio <= $update_threshold
ABresult
0X0
100
111
132 0 print "Deleting the file $fullpath as it's completeness ratio ($ratio) <= threshold ($update_threshold)\n";
133 0 ▶ unlink $fullpath or print "WARNING: Failed to delete the file $fullpath: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $fullpath or print "WARNING: Failed to delete the file ${fullpath}: $!\n" 0 0 0
134 0 $self->{deleted_files}->{$fullpath} = 1;
135 0 return 0;
136 }
137  
138 0 ▶ return $ratio >= $update_threshold ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ratio >= $update_threshold ? : 0 0 0
139  
140 } else {
141 # file doesn't exist, check against create_ratio
142  
143 # get the value from the database only if necessary
144 0 ▶ my $ratio = $self->{parent}->{engine}->{db}->get_file_completeness_ratio($file_id, $lang, $total) if $create_threshold > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $create_threshold > 0 0 0 0
145 0 ▶ print "[completeness]::[_check_can_generate] new file, file_id=$file_id, lang=$lang, total=$total, ratio=$ratio, can_delete=$can_delete, create_threshold=$create_threshold\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
146  
147 0 ▶ if ($ratio >= $create_threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ratio >= $create_threshold) { } 0 0 0
148 # file is not created just yet, but is blessed for creation by this plugin,
149 # so monitor if the file is actually created (at 'after_save_localized_file' phase),
150 # and only then add it to the list of created ones
151 0 $self->{allowed_files}->{make_key($file, $lang)} = $fullpath;
152 0 return 1;
153 } else {
154 0 return 0;
155 }
156 }
157  
158 }
159  
160 sub after_save_localized_file {
161 0 ▶ my ($self, $phase, $file, $lang) = @_;
Subroutine after_save_localized_file: not called CC=2 SCAR=17.9
Pod undocumented
162  
163 0 my $key = make_key($file, $lang);
164  
165 # check if this file was actually something allowed for generation by our plugin
166 # and only then add its full path to the report
167 0 ▶ if (exists $self->{allowed_files}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"allowed_files"}{$key}) 0 0 0
168 0 $self->{created_files}->{$self->{allowed_files}->{$key}} = 1;
169 }
170 }
171  
172 sub report_new_files {
173 0 ▶ my ($self, $phase) = @_;
Subroutine report_new_files: not called CC=11 SCAR=48.8
Pod undocumented
174  
175 0 ▶ if (!$self->{data}->{email_from} || !$self->{data}->{email_to}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"data"}{"email_from"} and $self->{"data"}{"email_to"}) 0 0 0
Condition0%0/3
$self->{"data"}{"email_from"} and $self->{"data"}{"email_to"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data"}{"email_from"} and $self->{"data"}{"email_to"}
$self->{"data"}{"email_from"} $self->{"data"}{"email_to"}
Truth table0%0/3
A and B
A= $self->{"data"}{"email_from"}B= $self->{"data"}{"email_to"}
ABresult
0X0
100
111
176 0 $self->{allowed_files} = {};
177 0 $self->{created_files} = {};
178 0 $self->{deleted_files} = {};
179 0 return;
180 }
181  
182 0 my $create_threshold = $self->{data}->{create_threshold};
183 0 my $update_threshold = $self->{data}->{update_threshold};
184  
185 0 ▶ my $email_subject = $self->{data}->{email_subject} || ("[".$self->{parent}->{id}.']: ');
Condition0%0/2
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: '
l!l
00
MC/DC0%0/1
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: '
$self->{'data'}{'email_subject'}
Truth table0%0/2
A
A= $self->{'data'}{'email_subject'}
Aresult
11
00
186  
187 0 my $created_text;
188 0 foreach my $key (sort keys %{$self->{created_files}}) {
189 0 xml_escape_strref(\$key);
190 0 $created_text .= "<li style='color: green'>$key</li>\n";
191 }
192 0 ▶ if ($created_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created_text) 0 0 0
193 0 $email_subject .= ' New files created';
194 0 $created_text = qq|
195 <p>The following files were created as their completeness ratio reached $create_threshold:</p>
196  
197 <ol>
198 $created_text
199 </ol>
200 |;
201 }
202  
203 0 my $deleted_text;
204 0 foreach my $key (sort keys %{$self->{deleted_files}}) {
205 0 xml_escape_strref(\$key);
206 0 $deleted_text .= "<li style='color: red'>$key</li>\n";
207 }
208 0 ▶ if ($deleted_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deleted_text) 0 0 0
209 0 ▶ $email_subject .= ',' if ($created_text);
Branch: 1 branch
BranchTrueFalseTotal
if $created_text 0 0 0
210 0 $email_subject .= ' Stale files removed';
211 0 $deleted_text = qq|
212 <p>The following files were removed as their completeness ratio dropped below $update_threshold:</p>
213  
214 <ol>
215 $deleted_text
216 </ol>
217 |;
218 }
219  
220 0 $self->{allowed_files} = {};
221 0 $self->{created_files} = {};
222 0 $self->{deleted_files} = {};
223  
224 0 ▶ if ($created_text or $deleted_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created_text or $deleted_text) 0 0 0
Condition0%0/3
$created_text or $deleted_text
l!l&&r!l&&!r
000
MC/DC0%0/2
$created_text or $deleted_text
$created_text $deleted_text
Truth table0%0/3
A or B
A= $created_textB= $deleted_text
ABresult
1X1
011
000
225 0 my $text = qq|
226 <html>
227 <head>
228 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
229 </head>
230 <body style="font-family: sans-serif; font-size: 120%">
231  
232 <p># This is an automatically generated message.</p>
233  
234 $created_text
235  
236 $deleted_text
237  
238 </body>
239 </html>
240 |;
241  
242 Serge::Mail::send_html_message(
243 $self->{data}->{email_from}, # from
244 $self->{data}->{email_to}, # to (list)
245 0 $email_subject, # subject
246 $text # message body
247 );
248 }
249  
250 }
251  
252 1 1;
« lib/Serge/DB.pm ↑ Summary lib/Serge/Engine/Plugin/parse_json.pm »