blib/lib/File/Find/IncludesTimeRange.pm

filter: Statementstmt 18.8% 13 / 69 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 12.3% 18 / 146 SCAR 65.9
CC
31
Cov
10%
find
992.0
BEGIN
1.0
BEGIN
1.0
CRAP 727.9
↑ Summary
1 package File::Find::IncludesTimeRange;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 File::Find::IncludesTimeRange - Takes a array of time stamped items(largely meant for use with files) returns ones that include the specified time range.
11  
12 =head1 VERSION
13  
14 Version 0.1.1
15  
16 =cut
17  
18 1 our $VERSION = '0.1.1';
19  
20 =head1 SYNOPSIS
21  
22 use File::Find::IncludesTimeRange;
23 uses Time::Piece;
24 use Data::Dumper;
25  
26 my @files=(
27 'daemonlogger.1677468390.pcap',
28 'daemonlogger.1677468511.pcap',
29 'daemonlogger.1677468632.pcap',
30 'daemonlogger.1677468753.pcap',
31 'daemonlogger.1677468874.pcap',
32 'daemonlogger.1677468995.pcap',
33 'daemonlogger.1677469116.pcap',
34 'daemonlogger.1677469237.pcap',
35 'daemonlogger.1677469358.pcap',
36 'daemonlogger.1677469479.pcap',
37 'daemonlogger.1677469600.pcap',
38 'daemonlogger.1677469721.pcap',
39 );
40  
41 print Dumper(\@files);
42  
43 my $start=Time::Piece->strptime('1677468620', '%s');
44 my $end=Time::Piece->strptime( '1677468633', '%s');
45  
46 my $found=File::Find::IncludesTimeRange->find(
47 items=>\@files,
48 start=>$start,
49 end=>$end,
50 regex=>'(?<timestamp>\d\d\d\d\d\d+)(\.pcap|(?<subsec>\.\d+)\.pcap)$',
51 strptime=>'%s',
52 );
53 print Dumper($found);
54  
55 # do similar, but skip parsing the time stamp as it is already in unixtime
56 my $found=File::Find::IncludesTimeRange->find(
57 items=>\@files,
58 start=>$start,
59 end=>$end,
60 regex=>'(?<timestamp>(\d\d\d\d\d\d+|\d\d\d\d\d\+.\d+))\.pcap$',
61 ts_is_unixtime => 1,
62 );
63 print Dumper($found);
64  
65 =head1 SUBROUTINES
66  
67 =head2 find
68  
69 Searches through a list of items , finds the ones that appear to be timestamped.
70 It will then sort the found time stamps and return the ones that include the
71 specified time periods.
72  
73 There following options are taken.
74  
75 - items :: A array ref of items to examine.
76  
77 - start :: A Time::Piece object set to the start time.
78  
79 - end :: A Time::Piece object set to the end time.
80  
81 - regex :: A regex to use for matching the files. Requires uses of the named
82 group 'timestamp' for capturing the timestamp. If it includes micro
83 seconds in it, since Time::Piece->strptime does not handle those,
84 those can be captured via the group 'subsec'. They will then be
85 appended to to the epoch time of any parsed timestamp for sorting
86 purposes.
87 - Default :: (?<timestamp>\d\d\d\d\d\d+)(\.pcap|(?<subsec>\.\d+)\.pcap)$
88  
89 - strptime :: The format for use with L<Time::Piece>->strptime.
90 - Default :: %s
91  
92 - ts_is_unixtime :: Skips using Time::Piece and strptime as it is just a simple
93 numeric test. For this subsecs should be included in the
94 capture group 'timestamp' for the regex.
95  
96 =cut
97  
98 sub find {
99 0 ▶ my ( $blank, %opts ) = @_;
Subroutine find: not called CC=31 SCAR=69.0
Pod documented
100  
101 # some basic error checking
102 0 ▶ if ( !defined( $opts{start} ) ) {
Branch: 7 branches
BranchTrueFalseTotal
if (not defined $opts{'start'}) { } 0 0 0
elsif (not defined $opts{'end'}) { } 0 0 0
elsif (not defined $opts{'items'}) { } 0 0 0
elsif (ref $opts{'start'} ne 'Time::Piece') { } 0 0 0
elsif (ref $opts{'end'} ne 'Time::Piece') { } 0 0 0
elsif (ref $opts{'items'} ne 'ARRAY') { } 0 0 0
elsif ($opts{'start'} > $opts{'end'}) { } 0 0 0
103 0 die('$opts{start} is undef');
104 } elsif ( !defined( $opts{end} ) ) {
105 0 die('$opts{end} is undef');
106 } elsif ( !defined( $opts{items} ) ) {
107 0 die('$opts{items} is undef');
108 } elsif ( ref( $opts{start} ) ne 'Time::Piece' ) {
109 0 die('$opts{start} is not a Time::Piece object');
110 } elsif ( ref( $opts{end} ) ne 'Time::Piece' ) {
111 0 die('$opts{end} is not a Time::Piece object');
112 } elsif ( ref( $opts{items} ) ne 'ARRAY' ) {
113 0 die('$opts{items} is not a ARRAY');
114 } elsif ( $opts{start} > $opts{end} ) {
115 0 die('$opts{start} is greater than $opts{end}');
116 }
117  
118 0 ▶ if ( !defined( $opts{strptime} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"strptime"}) 0 0 0
119 0 $opts{strptime} = '%s';
120 }
121  
122 0 ▶ if ( !defined( $opts{regex} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"regex"}) 0 0 0
123 0 $opts{regex} = '(?<timestamp>\d\d\d\d\d\d+)(\.pcap|(?<subsec>\.\d+)\.pcap)$';
124 }
125  
126 0 my $start = $opts{start}->epoch;
127 0 my $end = $opts{end}->epoch;
128  
129 # a HoA of found timestamps
130 # each value is a array containing files for that time stamp
131 0 my $found = {};
132 0 foreach my $item ( @{ $opts{items} } ) {
133 0 ▶ if ( $item =~ /$opts{regex}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /$opts{"regex"}/) 0 0 0
134 0 my $subsec = '';
135 0 my $timestamp_raw = $+{timestamp};
136 0 ▶ if ( defined( $+{subsec} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+{"subsec"}) 0 0 0
137 0 $subsec = $+{subsec};
138 }
139  
140 0 my $timestamp;
141 my $full_timestamp;
142 0 ▶ if ( !$opts{ts_is_unixtime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'ts_is_unixtime'}) { } 0 0 0
143 # we have one we actually need to parse.... attempt to
144 # and if we can get the time stamp
145 0 eval { $timestamp = Time::Piece->strptime( $timestamp_raw, $opts{strptime} ); };
146 0 ▶ if ( !$@ && defined($timestamp) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and defined $timestamp) 0 0 0
Condition0%0/3
not $@ and defined $timestamp
!ll&&!rl&&r
000
MC/DC0%0/2
not $@ and defined $timestamp
not $@ defined $timestamp
Truth table0%0/3
not(A) and B
A= not $@B= defined $timestamp
ABresult
0X0
100
111
147 0 $full_timestamp = $timestamp->epoch . $subsec;
148 }
149 } else {
150 # if ts_is_unixtime, then no need to parse it... just go ahead and use it
151 0 $full_timestamp = $timestamp_raw;
152 }
153  
154 # only not going to be defined if the eval above failed for Time::Piece->strptime
155 0 ▶ if ( defined($full_timestamp) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $full_timestamp) 0 0 0
156 0 ▶ if ( !defined( $found->{$full_timestamp} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $found->{$full_timestamp}) 0 0 0
157 0 $found->{$full_timestamp} = [];
158 }
159 0 push( @{ $found->{$full_timestamp} }, $item );
160 }
161 } ## end if ( $item =~ /$opts{regex}/ )
162 } ## end foreach my $item ( @{ $opts{items} } )
163  
164 0 my @found_timestamps = sort( keys( %{$found} ) );
165 0 my $previous_timestamp;
166 my $previous_found;
167 0 my @timestamp_to_return;
168 0 foreach my $current_timestamp (@found_timestamps) {
169 0 ▶ if ( ( $start <= $current_timestamp ) && ( $current_timestamp <= $end ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start <= $current_timestamp and $current_timestamp <= $end) 0 0 0
Condition0%0/3
$start <= $current_timestamp and $current_timestamp <= $end
!ll&&!rl&&r
000
MC/DC0%0/2
$start <= $current_timestamp and $current_timestamp <= $end
$start <= $current_timestamp $current_timestamp <= $end
Truth table0%0/3
A and B
A= $start <= $current_timestampB= $current_timestamp <= $end
ABresult
0X0
100
111
170 0 push( @timestamp_to_return, $current_timestamp );
171  
172 # if we find one that it is between, but not equal, then add the previous as that contains the start
173 0 ▶ if ( defined($previous_timestamp) && !$previous_found && ( $start != $current_timestamp ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $previous_timestamp and not $previous_found and $start != $current_timestamp) { } 0 0 0
elsif (not $previous_found and $start == $current_timestamp) { } 0 0 0
Condition0%0/6
defined $previous_timestamp and not $previous_found and $start != $current_timestamp
!ll&&!rl&&r
000
defined $previous_timestamp and not $previous_found
!ll&&!rl&&r
000
MC/DC0%0/3
defined $previous_timestamp and not $previous_found and $start != $current_timestamp
defined $previous_timestamp not $previous_found $start != $current_timestamp
Truth table0%0/4
A and not(B) and C
A= defined $previous_timestampB= not $previous_foundC= $start != $current_timestamp
ABCresult
0XX0
10X0
1100
1111
174 0 $previous_found = 1;
175 0 push( @timestamp_to_return, $previous_timestamp );
176 ▶ } elsif ( !$previous_found && ( $start == $current_timestamp ) ) {
Condition0%0/3
not $previous_found and $start == $current_timestamp
!ll&&!rl&&r
000
MC/DC0%0/2
not $previous_found and $start == $current_timestamp
not $previous_found $start == $current_timestamp
Truth table0%0/3
not(A) and B
A= not $previous_foundB= $start == $current_timestamp
ABresult
0X0
100
111
177 0 $previous_found = 1;
178 }
179 } ## end if ( ( $start <= $current_timestamp ) && (...))
180  
181 0 $previous_timestamp = $current_timestamp;
182 } ## end foreach my $current_timestamp (@found_timestamps)
183  
184 # if we did not find anything and we have timestamps,
185 # and the last timestamp is before the end, add it...
186 #
187 # this happens when the time frame desired is after any of the timestamps
188 # such as will happen with a start of now-30 and a end of now
189 0 ▶ if ( !defined( $timestamp_to_return[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $timestamp_to_return[0] and defined $found_timestamps[0] and $found_timestamps[$#found_timestamps] <= $end) 0 0 0
Condition0%0/6
not defined $timestamp_to_return[0] and defined $found_timestamps[0] and $found_timestamps[$#found_timestamps] <= $end
!ll&&!rl&&r
000
not defined $timestamp_to_return[0] and defined $found_timestamps[0]
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $timestamp_to_return[0] and defined $found_timestamps[0] and $found_timestamps[$#found_timestamps] <= $end
not defined $timestamp_to_return[0] defined $found_timestamps[0] $found_timestamps[$#found_timestamps] <= $end
Truth table0%0/4
not(A) and B and C
A= not defined $timestamp_to_return[0]B= defined $found_timestamps[0]C= $found_timestamps[$#found_timestamps] <= $end
ABCresult
0XX0
10X0
1100
1111
190 && defined( $found_timestamps[0] )
191 && $found_timestamps[$#found_timestamps] <= $end )
192 {
193 0 push( @timestamp_to_return, $found_timestamps[$#found_timestamps] );
194 }
195  
196 0 my $to_return = [];
197  
198 # the second sort is needed as if
199 0 foreach my $item ( sort(@timestamp_to_return) ) {
200 0 foreach my $file ( @{ $found->{$item} } ) {
201 0 push( @{$to_return}, $file );
202 }
203 }
204  
205 # the file name to write to
206  
207 0 return $to_return;
208 } ## end sub find
209  
210 =head1 AUTHOR
211  
212 Zane C. Bower-Hadley, C<< <vvelox at vvelox.net> >>
213  
214 =head1 BUGS
215  
216 Please report any bugs or feature requests to C<bug-file-find-includestimerange at rt.cpan.org>, or through
217 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Find-IncludesTimeRange>. I will be notified, and then you'll
218 automatically be notified of progress on your bug as I make changes.
219  
220  
221  
222  
223 =head1 SUPPORT
224  
225 You can find documentation for this module with the perldoc command.
226  
227 perldoc File::Find::IncludesTimeRange
228  
229  
230 You can also look for information at:
231  
232 =over 4
233  
234 =item * RT: CPAN's request tracker (report bugs here)
235  
236 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Find-IncludesTimeRange>
237  
238 =item * Search CPAN
239  
240 L<https://metacpan.org/release/File-Find-IncludesTimeRange>
241  
242 =back
243  
244  
245 =head1 ACKNOWLEDGEMENTS
246  
247  
248 =head1 LICENSE AND COPYRIGHT
249  
250 This software is Copyright (c) 2023 by Zane C. Bower-Hadley.
251  
252 This is free software, licensed under:
253  
254 The Artistic License 2.0 (GPL Compatible)
255  
256  
257 =cut
258  
259 1 1; # End of File::Find::IncludesTimeRange
↑ Summary