lib/Mail/Decency/LogParser/Aggregator.pm

filter: Statementstmt 29.8% 17 / 57 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 33.3% 3 / 9 Podpod 100% 4 / 4 totaltotal 25.2% 24 / 95 CC 17 SCAR 41.8
CC
17
Cov
22%
handle_data
132.0
print_stats
30.0
setup
6.0
CRAP 65.3
« lib/Mail/Decency/ContentFilter/MimeAttribs.pm ↑ Summary lib/Mail/Decency/ContentFilter/DSPAM.pm »
1 package Mail::Decency::LogParser::Aggregator;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends qw/
5 Mail::Decency::LogParser::Core
6 /;
7 1 with qw/
8 Mail::Decency::Core::Meta::Maintenance
9 /;
10  
11 1 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our @CUMULATE_TYPE = qw/ ip from_domain to_domain /;
16  
17 =head1 NAME
18  
19 Mail::Decency::LogParser::Aggregator
20  
21  
22 =head1 DESCRIPTION
23  
24 Aggregates events (sent, reject, bounced) by IP, sender domain and recipient domain in database.
25  
26 =head1 CLASS ATTRIBUTES
27  
28 =head2 interval_formats : ArrayRef[Str]
29  
30 Intervals in strftime format for L<DateTime>
31  
32 =cut
33  
34 0 ▶has interval_formats => ( is => 'rw', isa => 'ArrayRef[Str]', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
35  
36 =head2 maintenance_ttl : Int
37  
38 Maintenance Time To Live. In maintenance mode, all entries from the database older then (now - ttl) will be wiped.
39  
40 =cut
41 1 has maintenance_ttl => ( is => 'rw', isa => 'Int', default => 86400 * 90 );
42  
43 =head2 schema_definition : ArrayRef[Str]
44  
45 Schema for Aggregator is:
46  
47 =over
48  
49 =item * Aggregator
50  
51 =over
52  
53 =item * (ip|from_domain|to_domain)
54  
55 =over
56  
57 =item * (ip|from_domain|to_domain)
58  
59 Varchar 255 (39 for IP) ..
60  
61 =item * type
62  
63 Varchar 25 .. either sent, bounced or reject
64  
65 =item * interval
66  
67 Varchar 25 .. the interval value, such as '2010-06-17'
68  
69 =item * format
70  
71 Varchar 25 .. the interval format, such as '%Y-%m-%d'
72  
73 =item * counter
74  
75 Integer .. the amount
76  
77 =item * last_update
78  
79 Integer (timestamp) .. last time updated
80  
81 =back
82  
83 =back
84  
85 =back
86  
87 =cut
88  
89 0 ▶has schema_definition => ( is => 'ro', isa => 'HashRef[HashRef]', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 {
91 0 aggregator => {
92 ip => {
93 ip => [ varchar => 39 ],
94 type => [ varchar => 25 ], # bounced, sent, reject
95 interval => [ varchar => 25 ],
96 format => [ varchar => 25 ],
97 counter => 'integer',
98 transfer => 'integer',
99 last_update => 'integer',
100 -unique => [ 'ip', 'type', 'interval', 'format' ]
101 },
102 from_domain => {
103 from_domain => [ varchar => 255 ],
104 type => [ varchar => 25 ], # bounced, sent, reject
105 interval => [ varchar => 25 ],
106 format => [ varchar => 25 ],
107 counter => 'integer',
108 transfer => 'integer',
109 last_update => 'integer',
110 -unique => [ 'from_domain', 'type', 'interval', 'format' ]
111 },
112 to_domain => {
113 to_domain => [ varchar => 255 ],
114 type => [ varchar => 25 ], # bounced, sent, reject
115 interval => [ varchar => 25 ],
116 format => [ varchar => 25 ],
117 counter => 'integer',
118 transfer => 'integer',
119 last_update => 'integer',
120 -unique => [ 'to_domain', 'type', 'interval', 'format' ]
121 },
122 }
123 };
124 1 } );
125  
126 =head1 METHODS
127  
128  
129 =head2 setup
130  
131 =cut
132  
133 0 ▶sub setup {
Subroutine setup: not called CC=2 SCAR=17.9
Pod documented
134 0 my ( $self ) = @_;
135 0 ▶ $self->config->{ interval_formats } ||= [ '%Y-%m-%d', '%Y-%m', '%Y' ];
Condition0%0/2
$self->config->{"interval_formats"} ||= ["%Y-%m-%d", "%Y-%m", "%Y"]
l!l
00
MC/DC0%0/1
$self->config->{"interval_formats"} ||= ["%Y-%m-%d", "%Y-%m", "%Y"]
$self->config->{"interval_formats"}
Truth table0%0/2
A
A= $self->config->{"interval_formats"}
Aresult
11
00
136 0 $self->interval_formats( $self->config->{ interval_formats } );
137 }
138  
139  
140 =head2 handle_data
141  
142 Checks wheter incoming mail is whilist for final recipient
143  
144 =cut
145  
146 0 ▶sub handle_data {
Subroutine handle_data: not called CC=11 SCAR=48.8
Pod documented
147 0 my ( $self, $parsed_ref ) = @_;
148
149 # no relevant
150 return
151 0 ▶ unless ( $parsed_ref->{ reject } || $parsed_ref->{ bounced } || $parsed_ref->{ sent } );
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed_ref->{"reject"} or $parsed_ref->{"bounced"} or $parsed_ref->{"sent"} 0 0 0
Condition0%0/6
$parsed_ref->{"reject"} or $parsed_ref->{"bounced"} or $parsed_ref->{"sent"}
l!l&&r!l&&!r
000
$parsed_ref->{"reject"} or $parsed_ref->{"bounced"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$parsed_ref->{"reject"} or $parsed_ref->{"bounced"} or $parsed_ref->{"sent"}
$parsed_ref->{"reject"} $parsed_ref->{"bounced"} $parsed_ref->{"sent"}
Truth table0%0/4
A or B or C
A= $parsed_ref->{"reject"}B= $parsed_ref->{"bounced"}C= $parsed_ref->{"sent"}
ABCresult
1XX1
01X1
0011
0000
152
153 # determine attributes
154 0 my %db = map { ( $_ => $parsed_ref->{ $_ } ) }
155 0 grep { defined $parsed_ref->{ $_ } }
156 qw/ ip from_address to_address from_domain to_domain /
157 ;
158
159 # determine intervals
160 0 my $dt = DateTime->now( time_zone => 'local' );
161 0 my @intervals = ( [ 'total', 'total' ] );
162 push @intervals, [ $dt->strftime( $_ ), $_ ]
163 0 for @{ $self->interval_formats };
164
165 0 foreach my $ref( @intervals ) {
166 0 my ( $interval, $format ) = @$ref;
167
168 0 foreach my $type( grep { defined $db{ $_ } } @CUMULATE_TYPE ) {
169 0 $self->database->usr_lock;
170 my $entry_ref = $self->database->get( aggregator => $type => my $search_ref = {
171 $type => $db{ $type },
172 interval => $interval,
173 format => $format,
174 type => $parsed_ref->{ reject }
175 ? 'reject'
176 : ( $parsed_ref->{ bounced }
177 0 ▶ ? 'bounced'
Branch: 2 branches
BranchTrueFalseTotal
$parsed_ref->{'reject'} ? : 0 0 0
$parsed_ref->{'bounced'} ? : 0 0 0
Condition0%0/2
$self->database->get('aggregator', $type, my $search_ref = {$type, $db{$type}, 'interval', $interval, 'format', $format, 'type', $parsed_ref->{'reject'} ? 'reject' : ($parsed_ref->{'bounced'} ? 'bounced' : 'sent')}) || {'counter', 0, 'transfer', 0}
l!l
00
MC/DC0%0/1
$self->database->get('aggregator', $type, my $search_ref = {$type, $db{$type}, 'interval', $interval, 'format', $format, 'type', $parsed_ref->{'reject'} ? 'reject' : ($parsed_ref->{'bounced'} ? 'bounced' : 'sent')}) || {'counter', 0, 'transfer', 0}
$self->database->get('aggregator', $type, my $search_ref = {$type, $db{$type}, 'interval', $interval, 'format', $format, 'type', $parsed_ref->{'reject'} ? 'reject' : ($parsed_ref->{'bounced'} ? 'bounced' : 'sent')})
Truth table0%0/2
A
A= $self->database->get('aggregator', $type, my $search_ref = {$type, $db{$type}, 'interval', $interval, 'format', $format, 'type', $parsed_ref->{'reject'} ? 'reject' : ($parsed_ref->{'bounced'} ? 'bounced' : 'sent')})
Aresult
11
00
178 : 'sent'
179 )
180 } ) || { counter => 0, transfer => 0 };
181 0 $entry_ref->{ last_update } = time();
182 0 $entry_ref->{ counter } ++;
183 $entry_ref->{ transfer } += $parsed_ref->{ size }
184 0 ▶ if defined $parsed_ref->{ size };
Branch: 1 branch
BranchTrueFalseTotal
if defined $parsed_ref->{"size"} 0 0 0
185 0 $self->database->set( aggregator => $type => $search_ref, $entry_ref );
186 0 $self->database->usr_unlock;
187 }
188 }
189 }
190  
191  
192  
193 =head2 print_stats
194  
195 =cut
196  
197 0 ▶sub print_stats {
Subroutine print_stats: not called CC=5 SCAR=34.0
Pod documented
198 0 my ( $self ) = @_;
199
200 0 foreach my $table( @CUMULATE_TYPE ) {
201
202 0 foreach my $type( qw/ sent bounced reject / ) {
203 0 print "# $table: $type\n";
204
205 0 foreach my $format( @{ $self->interval_formats } ) {
206 0 my ( $handle, $meth ) = $self->database->search_read( aggregator => $table, {
207 type => $type,
208 format => $format
209 } );
210
211 0 ▶ while ( my $ref = $handle->$meth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $handle->$meth) 0 0 0
212 0 print "$ref->{ $table };$type;$ref->{ interval };$ref->{ counter };$format\n";
213 }
214 }
215 0 print "\n";
216 }
217 }
218
219 }
220  
221  
222  
223 =head2 maintenance
224  
225 Remove old cumulated entries. See maintenance_ttl
226  
227 =cut
228  
229 0 ▶sub maintenance {
Subroutine maintenance: not called CC=2 SCAR=17.9
Pod documented
230 0 my ( $self ) = @_;
231 0 my $obsolete_time = time() - $self->maintenance_ttl;
232
233 0 $self->logger->debug0( "Clear obsolete entries (TTL ". $self->maintenance_ttl. ")" );
234
235 0 foreach my $table( @CUMULATE_TYPE ) {
236 0 $self->database->remove( aggregator => $table, {
237 last_update => {
238 '<' => $obsolete_time
239 }
240 } );
241 }
242 }
243  
244  
245 =head1 AUTHOR
246  
247 Ulrich Kautz <uk@fortrabbit.de>
248  
249 =head1 COPYRIGHT
250  
251 Copyright (c) 2010 the L</AUTHOR> as listed above
252  
253 =head1 LICENCSE
254  
255 This library is free software and may be distributed under the same terms as perl itself.
256  
257 =cut
258  
259 1 1;
« lib/Mail/Decency/ContentFilter/MimeAttribs.pm ↑ Summary lib/Mail/Decency/ContentFilter/DSPAM.pm »