File Coverage

blib/lib/App/JobLog/Log/Event.pm
Criterion Covered Total %
statement 74 77 96.1
branch 22 28 78.5
condition 13 16 81.2
subroutine 15 16 93.7
pod 9 9 100.0
total 133 146 91.1


line stmt bran cond sub pod time code
1             package App::JobLog::Log::Event;
2             $App::JobLog::Log::Event::VERSION = '1.039';
3             # ABSTRACT: basically adds an end time to App::JobLog::Log::Line events
4              
5              
6 4     4   3046 use parent qw(App::JobLog::Log::Note);
  4         10  
  4         32  
7              
8 4     4   265 use Modern::Perl;
  4         10  
  4         33  
9 4     4   603 use Class::Autouse qw{DateTime};
  4         9  
  4         29  
10 4     4   208 use autouse 'App::JobLog::Time' => qw(now);
  4         13  
  4         31  
11 4     4   566 use autouse 'Carp' => qw(carp);
  4         10  
  4         15  
12              
13             # for debugging
14             use overload '""' => sub {
15 0 0   0   0 $_[0]->data->to_string . '-->'
16             . ( $_[0]->is_closed ? $_[0]->end : 'ongoing' );
17 4     4   549 };
  4         8  
  4         45  
18              
19              
20             sub clone {
21 1085     1085 1 1523 my ($self) = @_;
22 1085         3062 my $clone = $self->new( $self->data->clone );
23 1085 100       2882 $clone->end = $self->end->clone unless $self->is_open;
24 1085         3874 return $clone;
25             }
26              
27              
28             sub overlap {
29 4846     4846 1 7636 my ( $self, $start, $end ) = @_;
30              
31             # if this falls entirely within interval, return this
32 4846   100     13125 my $c1 = DateTime->compare( $start, $self->start ) || 0;
33 4846   100     418295 my $c2 = DateTime->compare( $end, $self->end ) || 0;
34 4846 100 100     418024 if ( $c1 <= 0 && $c2 >= 0 ) {
35 3764         11181 return $self;
36             }
37 1082 50 33     3201 return if $self->start >= $end || $start >= $self->end;
38 1082 100       96384 my $s = $c1 < 0 ? $self->start : $start;
39 1082 100       2922 my $e = $c2 < 0 ? $end : $self->end;
40 1082         2331 my $clone = $self->clone;
41 1082         3028 $clone->start = $s;
42 1082         4491 $clone->end = $e;
43 1082         5700 return $clone;
44             }
45              
46              
47             sub end : lvalue {
48 25323     25323 1 204922 $_[0]->{end};
49             }
50              
51              
52             sub cmp {
53 130     130 1 2607 my ( $self, $other ) = @_;
54 130         380 my $comparison = $self->SUPER::cmp($other);
55 130 100       11433 unless ($comparison) {
56 6 50       19 if ( $other->isa(__PACKAGE__) ) {
57 6 100       87 if ( $self->is_closed ) {
    50          
58 5 50       186 if ( $other->is_closed ) {
59 5         167 return DateTime->compare( $self->end, $other->end );
60             }
61             else {
62 0         0 return 1;
63             }
64             }
65             elsif ( $other->is_closed ) {
66 0         0 return -1;
67             }
68             else {
69 1         7 return 0;
70             }
71             }
72             }
73 124         390 return $comparison;
74             }
75              
76              
77 14387     14387 1 90782 sub is_closed { $_[0]->{end} }
78              
79              
80 14186     14186 1 30654 sub is_open { !$_[0]->is_closed }
81              
82              
83             sub duration {
84 137     137 1 219 my ($self) = @_;
85 137 100       279 my $e = $self->is_open ? now : $self->end;
86 137         596 return $e->epoch - $self->start->epoch;
87             }
88              
89              
90             sub split_days {
91 140     140 1 208 my ($self) = @_;
92 140         423 my $days_end =
93             $self->start->clone->truncate( to => 'day' )->add( days => 1 );
94 140   66     190292 my $e = $self->end || now;
95 140 100       5657 if ( $days_end < $e ) {
96 1         99 my @splits;
97 1         5 my $s = $self->start;
98 1         3 do {
99 2         975 my $clone = $self->clone;
100 2         6 $clone->start = $s;
101 2         10 $s = $days_end->clone;
102 2         30 $clone->end = $s;
103 2         4 push @splits, $clone;
104 2         7 $days_end->add( days => 1 );
105             } while ( $days_end < $e );
106 1         961 my $clone = $self->clone;
107 1         5 $clone->start = $s;
108 1         5 $clone->end = $self->end;
109 1         3 push @splits, $clone;
110 1         8 return @splits;
111             }
112             else {
113 139         13736 return $self;
114             }
115             }
116              
117              
118             sub intersects {
119 170     170 1 251 my ( $self, $other ) = @_;
120 170 100       457 if ( $self->start > $other->start ) {
121              
122             #rearrange so $self is earlier
123 127         11296 my $t = $other;
124 127         152 $other = $self;
125 127         177 $self = $t;
126             }
127 170   100     4403 return $self->is_open || $self->end > $other->start;
128             }
129              
130             1;
131              
132             __END__