lib/Mail/Decency/Core/Stats.pm

filter: Statementstmt 17.0% 22 / 129 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 60.0% 6 / 10 Podpod 100% 2 / 2 totaltotal 14.0% 30 / 213 CC 44 SCAR 68.1
CC
44
Cov
11%
print_stats
1332.0
update_stats
42.0
__ANON__
12.0
CRAP 910.5
↑ Summary lib/Mail/Decency/ContentFilter.pm »
1 package Mail::Decency::Core::Stats;
2  
3 24 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 24 ▶use version 0.74; our $VERSION = qv( "v0.1.5" );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Mail::Decency::Core::Stats
13  
14 =head1 DESCRIPTION
15  
16 Statistic database for policy server and content filter
17  
18 =cut
19  
20 =head2 enable_stats
21  
22 Wheter enable stats or not
23  
24 =cut
25  
26 24 has enable_stats => ( is => 'rw', isa => 'Bool', default => 0 );
27  
28 13 ▶has stats_intervals => ( is => 'rw', isa => 'ArrayRef[Str]', default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 [
30 13 'hour',
31 'day',
32 'week',
33 'month',
34 'year',
35 ]
36 24 } );
37 20 ▶has stats_time_zone => ( is => 'ro', isa => 'DateTime::TimeZone', default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 20 DateTime::TimeZone::Local->TimeZone();
39 24 } );
40 24 has schema_definition => ( is => 'ro', isa => 'HashRef[HashRef]' );
41  
42  
43 =head1 MODIFIER
44  
45 =head2 init
46  
47 Update schema definition of this module
48  
49 =cut
50  
51 0 ▶after 'init' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
52 0 my ( $self ) = @_;
53
54 0 my $prefix = $self->name;
55
56 0 ▶ $self->{ schema_definition } ||= {};
Condition0%0/2
$self->{"schema_definition"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"schema_definition"} ||= {}
$self->{"schema_definition"}
Truth table0%0/2
A
A= $self->{"schema_definition"}
Aresult
11
00
57 $self->{ schema_definition }->{ stats } = {
58 0 $self->name. "_response" => {
59 module => [ varchar => 32 ],
60 period => [ varchar => 10 ],
61 start => 'integer',
62 type => [ varchar => 32 ],
63 -unique => [ qw/ module period start type / ]
64 },
65 $self->name. "_performance" => {
66 module => [ varchar => 32 ],
67 type => [ varchar => 32 ],
68 period => [ varchar => 10 ],
69 calls => [ varchar => 10 ],
70 runtime => [ 'real' ],
71 start => 'integer',
72 -unique => [ qw/ module period start type / ]
73 }
74 };
75
76 $self->enable_stats( 1 )
77 0 ▶ if $self->config->{ enable_stats };
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"enable_stats"} 0 0 0
78 24 };
79  
80 =head2 maintenance
81  
82 Clears all entries which are older then the current interval. For hour, that would mean any hourly stats before the current hour, for year that would mean any stat from the last year and so on..
83  
84 =cut
85  
86 0 ▶before 'maintenance' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
87 0 my ( $self ) = @_;
88 0 my $table = lc( $self->name );
89
90 0 my $now = DateTime->now( time_zone => $self->stats_time_zone );
91 my @intervals = map {
92 0 my $iv = $now->clone->truncate( to => $_ );
93 0 [ $_, $iv->epoch ];
94 } grep {
95 0 /^(hour|day|week|month|year)$/
96 0 } @{ $self->stats_intervals };
97
98 0 my @module_names = map { "$_" } @{ $self->childs };
99 0 ( my $server_name = ref( $self ) ) =~ s/^.*:://;
100 0 push @module_names, "${server_name}Core";
101
102 0 foreach my $interval_ref( @intervals ) {
103 0 $self->database->remove( stats => "${table}_performance" => {
104 module => \@module_names,
105 period => $interval_ref->[0],
106 start => {
107 '<' => $interval_ref->[1],
108 }
109 } );
110 0 $self->database->remove( stats => "${table}_response" => {
111 module => \@module_names,
112 period => $interval_ref->[0],
113 start => {
114 '<' => $interval_ref->[1],
115 }
116 } );
117 }
118 24 };
119  
120  
121 =head1 METHODS
122  
123 =head2 update_stats
124  
125 =cut
126  
127 0 ▶sub update_stats {
Subroutine update_stats: not called CC=6 SCAR=37.4
Pod documented
128 0 my ( $self, $module, $type, $weight_diff, $runtime ) = @_;
129
130 0 print Dumper [ $type => $weight_diff, $runtime ];
131
132 0 my $now = DateTime->now( time_zone => $self->stats_time_zone );
133 my @intervals = map {
134 0 my $iv = $now->clone->truncate( to => $_ );
135 0 [ $_, $iv->epoch ];
136 } grep {
137 0 /^(hour|day|week|month|year)$/
138 0 } @{ $self->stats_intervals };
139
140 0 eval {
141
142 0 my $table = lc( $self->name );
143 0 foreach my $interval_ref( @intervals ) {
144 0 my %search = (
145 module => "$module",
146 period => $interval_ref->[0],
147 start => $interval_ref->[1],
148 );
149
150 # increment weighting
151 0 ▶ if ( defined $weight_diff ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $weight_diff) 0 0 0
152 0 $self->database->usr_lock;
153 0 my $db_ref = $self->database->get( stats => "${table}_performance" => \%search );
154 0 ▶ $db_ref ||= { weight => 0, runtime => 0, calls => 0 };
Condition0%0/2
$db_ref ||= {"weight", 0, "runtime", 0, "calls", 0}
l!l
00
MC/DC0%0/1
$db_ref ||= {"weight", 0, "runtime", 0, "calls", 0}
$db_ref
Truth table0%0/2
A
A= $db_ref
Aresult
11
00
155 $self->database->set( stats => "${table}_performance" => \%search, {
156 weight => ( $db_ref->{ weight } || 0 ) + $weight_diff,
157 calls => $db_ref->{ calls } + 1,
158 0 ▶ runtime => $db_ref->{ runtime } + $runtime
Condition0%0/2
$db_ref->{'weight'} || 0
l!l
00
MC/DC0%0/1
$db_ref->{'weight'} || 0
$db_ref->{'weight'}
Truth table0%0/2
A
A= $db_ref->{'weight'}
Aresult
11
00
159 } );
160 0 $self->database->usr_unlock;
161 }
162
163 # increment response counter
164 0 $self->database->increment( stats => "${table}_response" => {
165 %search,
166 type => $type,
167 } );
168 }
169 };
170 0 ▶ $self->logger->error( "Error updating stats for $module / $type: $@" ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
171
172 0 return;
173 }
174  
175  
176 =head2 print_stats
177  
178 Print out stats
179  
180 =cut
181  
182 0 ▶sub print_stats {
Subroutine print_stats: not called CC=36 SCAR=71.9
Pod documented
183 0 my ( $self, $return ) = @_;
184
185 0 my $table = lc( $self->name );
186
187 0 my $now = DateTime->now( time_zone => $self->stats_time_zone );
188 my @intervals = map {
189 0 my $iv = $now->clone->truncate( to => $_ );
190 0 [ $_, $iv->epoch ];
191 } grep {
192 0 /^(hour|day|week|month|year)$/
193 0 } @{ $self->stats_intervals };
194
195 0 my @module_names = map { "$_" } @{ $self->childs };
196 0 ( my $server_name = ref( $self ) ) =~ s/^.*:://;
197 0 push @module_names, "${server_name}Core";
198
199 0 my ( %stats_weight, %stats_response ) = ();
200
201 0 foreach my $interval_ref( @intervals ) {
202 0 my ( $period, $start ) = @$interval_ref;
203
204 0 foreach my $module( @module_names ) {
205 0 my $weight_ref = $self->database->get( stats => "${table}_performance" => {
206 module => $module,
207 period => $period,
208 start => {
209 '>=' => $start,
210 }
211 } );
212 0 ▶ if ( $weight_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight_ref) 0 0 0
213 0 ▶ $stats_weight{ $module } ||= {};
Condition0%0/2
$stats_weight{$module} ||= {}
l!l
00
MC/DC0%0/1
$stats_weight{$module} ||= {}
$stats_weight{$module}
Truth table0%0/2
A
A= $stats_weight{$module}
Aresult
11
00
214 0 $stats_weight{ $module }->{ $period } = $weight_ref->{ weight };
215
216 0 ▶ if ( $period eq 'year' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($period eq "year") 0 0 0
217 $stats_weight{ $module }->{ $_ } = $weight_ref->{ $_ }
218 0 for qw/ calls runtime /;
219 }
220 }
221
222 0 my @response = $self->database->search( stats => "${table}_response" => {
223 module => $module,
224 period => $period,
225 start => {
226 '>=' => $start,
227 }
228 } );
229
230 0 foreach my $response_ref( @response ) {
231 0 ▶ next unless $response_ref->{ type };
Branch: 1 branch
BranchTrueFalseTotal
unless $response_ref->{"type"} 0 0 0
232 0 ▶ $stats_response{ $response_ref->{ type } } ||= {};
Condition0%0/2
$stats_response{$response_ref->{"type"}} ||= {}
l!l
00
MC/DC0%0/1
$stats_response{$response_ref->{"type"}} ||= {}
$stats_response{$response_ref->{"type"}}
Truth table0%0/2
A
A= $stats_response{$response_ref->{"type"}}
Aresult
11
00
233 0 ▶ $stats_response{ $response_ref->{ type } }->{ $module } ||= {};
Condition0%0/2
$stats_response{$response_ref->{"type"}}{$module} ||= {}
l!l
00
MC/DC0%0/1
$stats_response{$response_ref->{"type"}}{$module} ||= {}
$stats_response{$response_ref->{"type"}}{$module}
Truth table0%0/2
A
A= $stats_response{$response_ref->{"type"}}{$module}
Aresult
11
00
234 $stats_response{ $response_ref->{ type } }->{ $module }->{ $period }
235 0 = $response_ref->{ data };
236 }
237 }
238 }
239
240 0 my $format = '%-20s'. ( ' | %-10s' x 5 );
241
242
243 #
244 # PRINT RESPONSE
245 #
246
247 0 ▶ if ( scalar keys %stats_response ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %stats_response) 0 0 0
248
249 0 print "# **************************************\n# RESPONSE STATS\n# **************************************\n";
250 0 foreach my $response( sort keys %stats_response ) {
251 0 my @out = ( sprintf( $format, 'Module', map { ucfirst( $_ ). " ". $now->$_ } qw/ hour day week month year / ) );
252
253 0 print "\n# ****** $response ******\n";
254
255 0 my $response_ref = $stats_response{ $response };
256 0 ▶ foreach my $module( sort { $b =~ /Core$/ ? -1 : $a cmp $b } keys %$response_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
$b =~ /Core$/ ? : 0 0 0
257 push @out, sprintf( $format, $module,
258 $response_ref->{ $module }->{ hour }
259 ? sprintf( '%.1f', $response_ref->{ $module }->{ hour } )
260 : "-"
261 ,
262 $response_ref->{ $module }->{ day }
263 ? sprintf( '%.1f', $response_ref->{ $module }->{ day } )
264 : "-"
265 ,
266 $response_ref->{ $module }->{ week }
267 ? sprintf( '%.1f', $response_ref->{ $module }->{ week } )
268 : "-"
269 ,
270 $response_ref->{ $module }->{ month }
271 ? sprintf( '%.1f', $response_ref->{ $module }->{ month } )
272 : "-"
273 ,
274 $response_ref->{ $module }->{ year }
275 ? sprintf( '%.1f', $response_ref->{ $module }->{ year } )
276 0 ▶ : "-"
Branch: 5 branches
BranchTrueFalseTotal
$response_ref->{$module}{'hour'} ? : 0 0 0
$response_ref->{$module}{'day'} ? : 0 0 0
$response_ref->{$module}{'week'} ? : 0 0 0
$response_ref->{$module}{'month'} ? : 0 0 0
$response_ref->{$module}{'year'} ? : 0 0 0
277 ,
278 );
279 }
280
281 0 print join( "\n", @out ). "\n";
282 }
283 0 print "\n\n";
284 }
285
286 #
287 # PRINT WEIGHT
288 #
289 0 ▶ if ( scalar keys %stats_weight ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %stats_weight) 0 0 0
290
291 0 print "# **************************************\n# WEIGHT STATS\n# **************************************\n";
292 0 my @out = ( sprintf( $format, 'Module', map { ucfirst( $_ ). " ". $now->$_ } qw/ hour day week month year / ) );
293
294 0 ▶ foreach my $module( sort { $b =~ /Core$/ ? -1 : $a cmp $b } keys %stats_weight ) {
Branch: 1 branch
BranchTrueFalseTotal
$b =~ /Core$/ ? : 0 0 0
295 push @out, sprintf( $format, $module,
296 $stats_weight{ $module }->{ hour }
297 ? sprintf( '%.1f', $stats_weight{ $module }->{ hour } )
298 : "-"
299 ,
300 $stats_weight{ $module }->{ day }
301 ? sprintf( '%.1f', $stats_weight{ $module }->{ day } )
302 : "-"
303 ,
304 $stats_weight{ $module }->{ week }
305 ? sprintf( '%.1f', $stats_weight{ $module }->{ week } )
306 : "-"
307 ,
308 $stats_weight{ $module }->{ month }
309 ? sprintf( '%.1f', $stats_weight{ $module }->{ month } )
310 : "-"
311 ,
312 $stats_weight{ $module }->{ year }
313 ? sprintf( '%.1f', $stats_weight{ $module }->{ year } )
314 0 ▶ : "-"
Branch: 5 branches
BranchTrueFalseTotal
$stats_weight{$module}{'hour'} ? : 0 0 0
$stats_weight{$module}{'day'} ? : 0 0 0
$stats_weight{$module}{'week'} ? : 0 0 0
$stats_weight{$module}{'month'} ? : 0 0 0
$stats_weight{$module}{'year'} ? : 0 0 0
315 ,
316
317 );
318 }
319 0 print join( "\n", @out ). "\n\n\n";
320 }
321
322 #
323 # PRINT RUNTIME
324 #
325 0 ▶ if ( scalar keys %stats_weight ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %stats_weight) 0 0 0
326
327 0 print "# **************************************\n# RUNTIME STATS\n# **************************************\n";
328 0 my @out = ( sprintf( $format, 'Module', map { ucfirst( $_ ) } qw/ total calls average - - / ) );
329
330 0 ▶ foreach my $module( sort { $b =~ /Core$/ ? -1 : $a cmp $b } keys %stats_weight ) {
Branch: 1 branch
BranchTrueFalseTotal
$b =~ /Core$/ ? : 0 0 0
331 my $average = $stats_weight{ $module }->{ calls } > 0
332 ? $stats_weight{ $module }->{ runtime } / $stats_weight{ $module }->{ calls }
333 0 ▶ : 0
Branch: 1 branch
BranchTrueFalseTotal
$stats_weight{$module}{'calls'} > 0 ? : 0 0 0
334 ;
335 push @out, sprintf( $format, $module,
336 sprintf( '%.2f', $stats_weight{ $module }->{ runtime } || 0 ),
337 0 ▶ $stats_weight{ $module }->{ calls } || "-",
Condition0%0/4
$stats_weight{$module}{'runtime'} || 0
l!l
00
$stats_weight{$module}{'calls'} || '-'
l!l
00
MC/DC0%0/1
$stats_weight{$module}{'runtime'} || 0
$stats_weight{$module}{'runtime'}
MC/DC0%0/1
$stats_weight{$module}{'calls'} || '-'
$stats_weight{$module}{'calls'}
Truth table0%0/2
A
A= $stats_weight{$module}{'runtime'}
Aresult
11
00
Truth table0%0/2
A
A= $stats_weight{$module}{'calls'}
Aresult
11
00
338 sprintf( '%.4f', $average ),
339 "-",
340 "-",
341 );
342 }
343 0 print join( "\n", @out ). "\n\n\n";
344 }
345
346 #
347 # MODULE STATS
348 #
349
350 0 foreach my $module( @{ $self->childs } ) {
351 0 ▶ next unless $module->can( 'print_stats' );
Branch: 1 branch
BranchTrueFalseTotal
unless $module->can("print_stats") 0 0 0
352 0 print "# *************************************\n# Module: $module STATS\n# *************************************\n";
353 0 $module->print_stats;
354 0 print "\n\n\n";
355 }
356
357 #print Dumper( { weight => \%stats_weight, response => \%stats_response, out => \@out } );
358
359 }
360  
361  
362  
363 =head1 AUTHOR
364  
365 Ulrich Kautz <uk@fortrabbit.de>
366  
367 =head1 COPYRIGHT
368  
369 Copyright (c) 2010 the L</AUTHOR> as listed above
370  
371 =head1 LICENCSE
372  
373 This library is free software and may be distributed under the same terms as perl itself.
374  
375 =cut
376  
377  
378  
379  
380 24 1;
↑ Summary lib/Mail/Decency/ContentFilter.pm »