OpenInteract2/ErrorStorage.pm

filter: Statementstmt 24.0% 53 / 220 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 44.4% 16 / 36 Podpod 0.0% 0 / 8 totaltotal 18.1% 69 / 381 CC 52 SCAR 30.0
CC
52
Cov
17%
get_by_date
56.0
get_most_recent
42.0
save
30.0
CRAP 20.2
« OpenInteract2/Datasource/DBI.pm ↑ Summary OpenInteract2/Manage/Website/ThemeDump.pm »
1 package OpenInteract2::ErrorStorage;
2  
3 # $Id: ErrorStorage.pm,v 1.2 2005/02/25 05:35:16 lachoy Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec::Functions qw( catdir catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use OpenInteract2::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 $OpenInteract2::ErrorStorage::VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
17 1 @OpenInteract2::ErrorStorage::EXPORT_OK = qw(
18 DEFAULT_RECENT_ERRORS DEFAULT_RECENT_MONTHS DEFAULT_RECENT_DAYS
19 DAY_DATE_PATTERN FILE_DATE_PATTERN ID_DATE_PATTERN
20 );
21  
22 1 ▶use constant DEFAULT_RECENT_ERRORS => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant DEFAULT_RECENT_MONTHS => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant DEFAULT_RECENT_DAYS => 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use constant DAY_DATE_PATTERN => '%Y-%m-%d';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant FILE_DATE_PATTERN => '%H%M%S-%3N';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant ID_DATE_PATTERN => '%Y%m%d-%H%M%S-%3N';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # NOTE: unlike the rest of OI2 we use a class-based logger here so
31 # that OI2::Log::OIAppender doesn't try and send messages sent BY this
32 # class TO this class.
33  
34 1 my $log = get_logger( __PACKAGE__ );
35  
36 1 my ( $PARSER );
37  
38 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
39 0 my ( $class, $error_dir ) = @_;
40 0 ▶ if ( ! $error_dir and CTX ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $error_dir and CTX()) { } 0 0 0
Condition0%0/3
not $error_dir and CTX()
!ll&&!rl&&r
000
MC/DC0%0/2
not $error_dir and CTX()
not $error_dir CTX()
Truth table0%0/3
not(A) and B
A= not $error_dirB= CTX()
ABresult
0X0
100
111
41 0 ▶ $error_dir ||= CTX->lookup_directory( 'error' );
Condition0%0/3
$error_dir ||= CTX()->lookup_directory("error")
l!l&&r!l&&!r
000
MC/DC0%0/2
$error_dir ||= CTX()->lookup_directory("error")
$error_dir CTX()->lookup_directory("error")
Truth table0%0/3
A ||= B
A= $error_dirB= CTX()->lookup_directory("error")
ABresult
1X1
011
000
42 }
43 else {
44 0 $log->warn( "A context is not available and you did not pass in ",
45 "a directory to which I can save errors. Any errors ",
46 "stored with this object will be discarded" );
47 }
48 0 $class->_initialize_parser;
49 0 return bless( { error_dir => $error_dir }, $class );
50 }
51  
52 0 ▶sub _initialize_parser {
Subroutine _initialize_parser: not called CC=3 SCAR=24.8
53 0 my ( $class ) = @_;
54 0 ▶ return if ( $PARSER );
Branch: 1 branch
BranchTrueFalseTotal
if $PARSER 0 0 0
55 0 my %params = ( pattern => DAY_DATE_PATTERN );
56 0 ▶ if ( CTX ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CTX()) 0 0 0
57 0 $params{time_zone} = CTX->timezone_object;
58 }
59 0 $PARSER = DateTime::Format::Strptime->new( %params );
60 }
61  
62  
63 0 ▶sub _now {
Subroutine _now: not called CC=2 SCAR=17.9
64 0 ▶ return ( CTX ) ? CTX->create_date() : DateTime->now();
Branch: 1 branch
BranchTrueFalseTotal
CTX() ? : 0 0 0
65 }
66  
67 0 ▶sub save {
Subroutine save: not called CC=5 SCAR=34.0
Pod undocumented
68 0 my ( $self, $error_data ) = @_;
69 0 ▶ unless ( $self->{error_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"error_dir"}) 0 0 0
70 0 $log->warn( "Will not store error -- no error directory set" );
71 0 return;
72 }
73 0 my ( $error );
74 0 ▶ if ( blessed( $error_data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error_data)) { } 0 0 0
75 0 $error = $error_data;
76 }
77 else {
78 0 ▶ $error_data ||= {};
Condition0%0/2
$error_data ||= {}
l!l
00
MC/DC0%0/1
$error_data ||= {}
$error_data
Truth table0%0/2
A
A= $error_data
Aresult
11
00
79 0 ▶ $error_data->{time} ||= _now();
Condition0%0/3
$error_data->{"time"} ||= _now()
l!l&&r!l&&!r
000
MC/DC0%0/2
$error_data->{"time"} ||= _now()
$error_data->{"time"} _now()
Truth table0%0/3
A ||= B
A= $error_data->{"time"}B= _now()
ABresult
1X1
011
000
80 0 $error = OpenInteract2::Error->new( %{ $error_data } );
81 }
82 0 return $error->save( $self->_filename_from_date( $error_data->{time} ) );
83 }
84  
85 0 ▶sub get_most_recent {
Subroutine get_most_recent: not called CC=6 SCAR=37.4
Pod undocumented
86 0 my ( $self, $num_errors, $max_days ) = @_;
87 0 ▶ $num_errors ||= DEFAULT_RECENT_ERRORS;
Condition0%0/2
$num_errors ||= 5
l!l
00
MC/DC0%0/1
$num_errors ||= 5
$num_errors
Truth table0%0/2
A
A= $num_errors
Aresult
11
00
88 0 ▶ $max_days ||= DEFAULT_RECENT_DAYS;
Condition0%0/2
$max_days ||= 30
l!l
00
MC/DC0%0/1
$max_days ||= 30
$max_days
Truth table0%0/2
A
A= $max_days
Aresult
11
00
89 0 my $current_days_back = 0;
90 0 my $start = _now();
91 0 my @errors = ();
92 0 ▶ while ( scalar @errors < $num_errors and $current_days_back < $max_days ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @errors < $num_errors and $current_days_back < $max_days) 0 0 0
Condition0%0/3
scalar @errors < $num_errors and $current_days_back < $max_days
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @errors < $num_errors and $current_days_back < $max_days
scalar @errors < $num_errors $current_days_back < $max_days
Truth table0%0/3
A and B
A= scalar @errors < $num_errorsB= $current_days_back < $max_days
ABresult
0X0
100
111
93 0 my $find_dt = $start->clone()->subtract( days => $current_days_back );
94 0 push @errors, $self->_read_by_date( $find_dt );
95 0 $current_days_back++;
96 }
97 0 ▶ if ( scalar @errors > $num_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @errors > $num_errors) 0 0 0
98 0 splice( @errors, ( scalar @errors - $num_errors ) );
99 }
100 0 return $self->_add_id( @errors );
101 }
102  
103 0 ▶sub get_by_date {
Subroutine get_by_date: not called CC=7 SCAR=40.3
Pod undocumented
104 0 my ( $self, %date_info ) = @_;
105 0 my @errors = ();
106 0 my $date_id = $date_info{date_id};
107 0 ▶ if ( $date_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_id) { } 0 0 0
108 0 my $fmt = DateTime::Format::Strptime->new(
109 pattern => ID_DATE_PATTERN,
110 );
111 0 ▶ if ( my $dt = $fmt->parse_datetime( $date_id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dt = $fmt->parse_datetime($date_id)) 0 0 0
112 0 my $file = $self->_filename_from_date( $dt );
113 0 my $error = eval {
114 0 OpenInteract2::Error->new( file_storage => $file )
115 };
116 0 ▶ push @errors, $error if ( $error );
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
117 }
118 }
119 else {
120 0 ▶ my $date = $date_info{date} || _now()->strftime( DAY_DATE_PATTERN );
Condition0%0/3
$date_info{'date'} || _now()->strftime('%Y-%m-%d')
l!l&&r!l&&!r
000
MC/DC0%0/2
$date_info{'date'} || _now()->strftime('%Y-%m-%d')
$date_info{'date'} _now()->strftime('%Y-%m-%d')
Truth table0%0/3
A || B
A= $date_info{'date'}B= _now()->strftime('%Y-%m-%d')
ABresult
1X1
011
000
121 0 ▶ my $span = $date_info{days} || 1;
Condition0%0/2
$date_info{'days'} || 1
l!l
00
MC/DC0%0/1
$date_info{'days'} || 1
$date_info{'days'}
Truth table0%0/2
A
A= $date_info{'days'}
Aresult
11
00
122 0 my @all_dates = $self->_generate_range( 'days', $date, $span - 1 );
123 0 foreach my $dt ( @all_dates ) {
124 0 push @errors, $self->_read_by_date( $dt );
125 }
126 }
127 0 return $self->_add_id( @errors );
128 }
129  
130 0 ▶sub _add_id {
Subroutine _add_id: not called CC=2 SCAR=17.9
131 0 my ( $self, @errors ) = @_;
132 0 for ( @errors ) {
133 0 $_->id( $_->time->strftime( ID_DATE_PATTERN ) );
134 }
135 0 return @errors;
136 }
137  
138 0 ▶sub _read_by_date {
Subroutine _read_by_date: not called CC=3 SCAR=24.8
139 0 my ( $self, $dt ) = @_;
140 0 my @errors = ();
141 0 foreach my $file ( $self->_files_by_date( $dt ) ) {
142 0 my $error = OpenInteract2::Error->new( file_storage => $file );
143 0 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
144 0 push @errors, $error;
145 }
146 }
147 0 return $self->_add_id( @errors );
148 }
149  
150 0 ▶sub get_breakdown_by_month {
Subroutine get_breakdown_by_month: not called CC=5 SCAR=34.0
Pod undocumented
151 0 my ( $self, %date_info ) = @_;
152 0 my $now = _now();
153 0 ▶ my $year = $date_info{year} || $now->year();
Condition0%0/3
$date_info{'year'} || $now->year
l!l&&r!l&&!r
000
MC/DC0%0/2
$date_info{'year'} || $now->year
$date_info{'year'} $now->year
Truth table0%0/3
A || B
A= $date_info{'year'}B= $now->year
ABresult
1X1
011
000
154 0 ▶ my $month = $date_info{month} || $now->month();
Condition0%0/3
$date_info{'month'} || $now->month
l!l&&r!l&&!r
000
MC/DC0%0/2
$date_info{'month'} || $now->month
$date_info{'month'} $now->month
Truth table0%0/3
A || B
A= $date_info{'month'}B= $now->month
ABresult
1X1
011
000
155 0 ▶ my $span = $date_info{months} || DEFAULT_RECENT_MONTHS;
Condition0%0/2
$date_info{'months'} || 6
l!l
00
MC/DC0%0/1
$date_info{'months'} || 6
$date_info{'months'}
Truth table0%0/2
A
A= $date_info{'months'}
Aresult
11
00
156 0 my $end = DateTime->new( year => $year, month => $month, day => $now->day );
157 0 my $start = $end->clone()->subtract( months => $span, days => 7 );
158 0 my @all_months = $self->_generate_range( 'months', $start, $end );
159 0 my %bd = ();
160 0 foreach my $dt ( @all_months ) {
161 0 my $month_spec = $dt->strftime( '%Y-%m' );
162 0 $bd{ $month_spec } = $self->count_errors( $month_spec );
163 }
164 0 return %bd;
165 }
166  
167 0 ▶sub get_breakdown_by_day {
Subroutine get_breakdown_by_day: not called CC=5 SCAR=34.0
Pod undocumented
168 0 my ( $self, %date_info ) = @_;
169 0 my $now = _now();
170 0 ▶ my $year = $date_info{year} || $now->year();
Condition0%0/3
$date_info{'year'} || $now->year
l!l&&r!l&&!r
000
MC/DC0%0/2
$date_info{'year'} || $now->year
$date_info{'year'} $now->year
Truth table0%0/3
A || B
A= $date_info{'year'}B= $now->year
ABresult
1X1
011
000
171 0 ▶ my $month = $date_info{month} || $now->month();
Condition0%0/3
$date_info{'month'} || $now->month
l!l&&r!l&&!r
000
MC/DC0%0/2
$date_info{'month'} || $now->month
$date_info{'month'} $now->month
Truth table0%0/3
A || B
A= $date_info{'month'}B= $now->month
ABresult
1X1
011
000
172 0 my $start = CTX->create_date({
173 year => $year, month => $month, day => 1
174 });
175 0 my $end = CTX->create_date({
176 last_day_of_month => 1, year => $year, month => $month
177 });
178 0 my @all_days = $self->_generate_range( 'days', $start, $end );
179 0 $log->info( "Got ", scalar( @all_days ), "days in range" );
180 0 my %bd = ();
181 0 foreach my $dt ( @all_days ) {
182 0 ▶ next if ( $dt > $now );
Branch: 1 branch
BranchTrueFalseTotal
if $dt > $now 0 0 0
183 0 $log->info( "Counting errors for: ", $dt->ymd );
184 0 $bd{ $dt->strftime( '%d' ) } = $self->count_errors( $dt );
185 }
186 0 return %bd;
187 }
188  
189  
190 0 ▶sub count_errors {
Subroutine count_errors: not called CC=4 SCAR=30.0
Pod undocumented
191 0 my ( $self, $date_spec ) = @_;
192 0 ▶ if ( blessed( $date_spec ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($date_spec)) 0 0 0
193 0 return $self->_count_errors_in_day( $date_spec );
194 }
195 0 my ( $year, $month, $day ) = split /\D/, $date_spec;
196 0 my @days = ();
197 0 ▶ if ( $day ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day) { } 0 0 0
198 0 push @days, $day
199 }
200 else {
201 0 push @days, $self->_get_error_days_in_month( "$year-$month" );
202 }
203 0 my $count = 0;
204 0 foreach my $day ( @days ) {
205 0 my $dt = $PARSER->parse_datetime( "$year-$month-$day" );
206 0 $count += $self->_count_errors_in_day( $dt );
207 }
208 0 return $count;
209 }
210  
211 0 ▶sub _get_error_days_in_month {
Subroutine _get_error_days_in_month: not called CC=3 SCAR=24.8
212 0 my ( $self, $month_spec ) = @_;
213 0 my $month_dir = catdir( $self->{error_dir}, $month_spec );
214 0 ▶ return () unless ( -d $month_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $month_dir 0 0 0
215 0 ▶ opendir( MON, $month_dir )
Branch: 1 branch
BranchTrueFalseTotal
opendir MON, $month_dir or oi_error("Cannot open '${month_dir}' for reading: $!") 0 0 0
216 || oi_error "Cannot open '$month_dir' for reading: $!";
217 0 my @days = grep /^\d+/, grep { -d "$month_dir/$_" } readdir( MON );
218 0 closedir( MON );
219 0 return sort { $b <=> $a } @days;
220 }
221  
222 0 ▶sub _count_errors_in_day {
Subroutine _count_errors_in_day: not called CC=3 SCAR=24.8
223 0 my ( $self, $dt ) = @_;
224 0 my $day_dir = $self->_dirname_from_date( $dt );
225 0 $log->info( "Trying to count errors in '$day_dir'" );
226 0 ▶ return 0 unless ( -d $day_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $day_dir 0 0 0
227 0 ▶ opendir( ERR, $day_dir )
Branch: 1 branch
BranchTrueFalseTotal
opendir ERR, $day_dir or oi_error("Cannot open '${day_dir}' for reading: $!") 0 0 0
228 || oi_error "Cannot open '$day_dir' for reading: $!";
229 0 my @errors = grep /\.txt$/, grep { -f "$day_dir/$_" } readdir( ERR );
230 0 closedir( ERR );
231 0 return scalar @errors;
232 }
233  
234 0 ▶sub remove_by_date {
Subroutine remove_by_date: not called CC=5 SCAR=34.0
Pod undocumented
235 0 my ( $self, $date, $span ) = @_;
236 0 ▶ $span ||= 1;
Condition0%0/2
$span ||= 1
l!l
00
MC/DC0%0/1
$span ||= 1
$span
Truth table0%0/2
A
A= $span
Aresult
11
00
237 0 my @all_dates = $self->_generate_range( 'days', $date, $span );
238 0 my @removed = ();
239 0 foreach my $dt ( @all_dates ) {
240 0 foreach my $file ( $self->_files_by_date( $dt ) ) {
241 0 ▶ if ( $self->_remove_file( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_remove_file($file)) 0 0 0
242 0 push @removed, $file;
243 }
244 }
245 }
246 0 return @removed;
247 }
248  
249 0 ▶sub _files_by_date {
Subroutine _files_by_date: not called CC=2 SCAR=17.9
250 0 my ( $self, $dt ) = @_;
251 0 my $date_dir = $self->_dirname_from_date( $dt );
252 0 ▶ my @files = ( -d $date_dir ) ? <$date_dir/*> : ();
Branch: 1 branch
BranchTrueFalseTotal
-d $date_dir ? : 0 0 0
253 0 return sort { $b cmp $a } @files;
254 }
255  
256 0 ▶sub _filename_from_date {
Subroutine _filename_from_date: not called CC=1 SCAR=6.9
257 0 my ( $self, $dt ) = @_;
258 0 return catfile(
259 $self->_dirname_from_date( $dt ),
260 $dt->strftime( '%H%M%S-%3N.txt' )
261 );
262 }
263  
264 0 ▶sub _dirname_from_date {
Subroutine _dirname_from_date: not called CC=1 SCAR=6.9
265 0 my ( $self, $dt ) = @_;
266 return catdir(
267 $self->{error_dir},
268 0 $dt->strftime( '%Y-%m' ),
269 $dt->strftime( '%d' )
270 );
271 }
272  
273 0 ▶sub _generate_range {
Subroutine _generate_range: not called CC=4 SCAR=30.0
274 0 my ( $self, $type, $date, $end_range ) = @_;
275 0 my @dates = ();
276 0 my $start = $self->_parse_date( $date );
277 0 push @dates, $start;
278 0 my ( $span );
279  
280 0 ▶ if ( blessed( $end_range ) ) { # treat as DateTime
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($end_range)) { } 0 0 0
281 0 my $duration = $end_range - $start;
282 0 my $method = "delta_$type";
283 0 $span = $duration->$method(); # e.g., $duration->delta_days();
284 }
285 else {
286 0 $span = $end_range;
287 }
288  
289 0 ▶ if ( $span > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span > 0) 0 0 0
290 0 for ( 1 .. $span ) {
291 0 push @dates, $start->clone()->add( $type => $_ );
292 }
293 }
294 0 return @dates;
295 }
296  
297 0 ▶sub _parse_date {
Subroutine _parse_date: not called CC=3 SCAR=24.8
298 0 my ( $self, $date ) = @_;
299 0 ▶ return $date if ( blessed( $date ) );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($date) 0 0 0
300 0 my $dt = $PARSER->parse_datetime( $date );
301 0 ▶ unless ( $dt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt) 0 0 0
302 0 oi_error "Dates for error storage must be in format ",
303 "'", DAY_DATE_PATTERN, "'. (Date given: $date)";
304 }
305 0 return $dt;
306 }
307  
308 0 ▶sub _remove_file {
Subroutine _remove_file: not called CC=3 SCAR=24.8
309 0 my ( $self, $file ) = @_;
310 0 ▶ return unless ( -f $file );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
311 0 ▶ unless ( unlink( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $file) 0 0 0
312 0 $log->warn( "Failed to remove file '$file': $!" );
313 0 return;
314 }
315 0 return $file;
316 }
317  
318 1 1;
319  
320 __END__
« OpenInteract2/Datasource/DBI.pm ↑ Summary OpenInteract2/Manage/Website/ThemeDump.pm »