blib/lib/File/Find/IncludesTimeRange.pm

filter: Statementstmt 18.0% 13 / 72 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 12.4% 18 / 145 CC 29 SCAR 66.4
CC
29
Cov
10%
find
870.0
BEGIN
1.0
BEGIN
1.0
CRAP 764.7
↑ 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.0.1
15  
16 =cut
17  
18 1 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 use File::Find::IncludesTimeRange;
23 usexs 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  
54 print Dumper($found);
55  
56 =head1 SUBROUTINES
57  
58 =head2 find
59  
60 Searches through a list of items , finds the ones that appear to be timestamped.
61 It will then sort the found time stamps and return the ones that include the
62 specified time periods.
63  
64 There following options are taken.
65  
66 - items :: A array ref of items to examine.
67  
68 - start :: A Time::Piece object set to the start time.
69  
70 - end :: A Time::Piece object set to the end time.
71  
72 - regex :: A regex to use for matching the files. Requires uses of the named
73 group 'timestamp' for capturing the timestamp. If it includes micro
74 seconds in it, since Time::Piece->strptime does not handle those,
75 those can be captured via the group 'subsec'. They will then be
76 appended to to the epoch time of any parsed timestamp for sorting
77 purposes.
78 - Default :: (?<timestamp>\d\d\d\d\d\d+)(\.pcap|(?<subsec>\.\d+)\.pcap)$
79  
80 - strptime :: The format for use with L<Time::Piece>->strptime.
81 - Default :: %s
82  
83 =cut
84  
85 sub find {
86 0 ▶ my ( $blank, %opts ) = @_;
Subroutine find: not called CC=29 SCAR=67.7
Pod documented
87  
88 # some basic error checking
89 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
90 0 die('$opts{start} is undef');
91 }
92 elsif ( !defined( $opts{end} ) ) {
93 0 die('$opts{end} is undef');
94 }
95 elsif ( !defined( $opts{items} ) ) {
96 0 die('$opts{items} is undef');
97 }
98 elsif ( ref( $opts{start} ) ne 'Time::Piece' ) {
99 0 die('$opts{start} is not a Time::Piece object');
100 }
101 elsif ( ref( $opts{end} ) ne 'Time::Piece' ) {
102 0 die('$opts{end} is not a Time::Piece object');
103 }
104 elsif ( ref( $opts{items} ) ne 'ARRAY' ) {
105 0 die('$opts{items} is not a ARRAY');
106 }elsif ( $opts{start} > $opts{end} ) {
107 0 die('$opts{start} is greater than $opts{end}');
108 }
109  
110 0 ▶ if (!defined($opts{strptime}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"strptime"}) 0 0 0
111 0 $opts{strptime}='%s';
112 }
113  
114 0 ▶ if (!defined($opts{regex})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"regex"}) 0 0 0
115 0 $opts{regex}='(?<timestamp>\d\d\d\d\d\d+)(\.pcap|(?<subsec>\.\d+)\.pcap)$';
116 }
117  
118 0 my $start = $opts{start}->epoch;
119 0 my $end = $opts{end}->epoch;
120  
121 0 my $found = {};
122 0 my $timestamp_save = {};
123 0 foreach my $item ( @{ $opts{items} } ) {
124 0 ▶ if ( $item =~ /$opts{regex}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /$opts{"regex"}/) 0 0 0
125 0 my $subsec = '';
126 0 my $timestamp_raw = $+{timestamp};
127 0 ▶ if ( defined( $+{subsec} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+{"subsec"}) 0 0 0
128 0 $subsec = $+{subsec};
129 }
130  
131 0 my $timestamp;
132 0 eval { $timestamp = Time::Piece->strptime( $timestamp_raw, $opts{strptime} ); };
133 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
134 0 my $full_timestamp = $timestamp->epoch . $subsec;
135 0 ▶ if ( !defined( $found->{$full_timestamp} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $found->{$full_timestamp}) 0 0 0
136 0 $found->{$full_timestamp} = [];
137 0 $timestamp_save->{$full_timestamp} = $timestamp;
138 }
139 0 push( @{ $found->{$full_timestamp} }, $item );
140 }
141 }
142 }
143  
144 0 my @found_timestamps = sort( keys( %{$found} ) );
145 0 my $previous_timestamp;
146 my $previous_found;
147 0 my $previous_t;
148 0 my @timestamp_to_return;
149 0 foreach my $current_timestamp (@found_timestamps) {
150 0 my $t = $timestamp_save->{$current_timestamp};
151  
152 0 ▶ if ( ( $opts{start} <= $t ) && ( $t <= $opts{end} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"start"} <= $t and $t <= $opts{"end"}) 0 0 0
Condition0%0/3
$opts{"start"} <= $t and $t <= $opts{"end"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"start"} <= $t and $t <= $opts{"end"}
$opts{"start"} <= $t $t <= $opts{"end"}
Truth table0%0/3
A and B
A= $opts{"start"} <= $tB= $t <= $opts{"end"}
ABresult
0X0
100
111
153 0 push( @timestamp_to_return, $current_timestamp );
154  
155 # if we find one that it is between, but not equal, then add the previous as that contains the start
156 0 ▶ if ( defined($previous_timestamp) && !$previous_found && ( $opts{start} != $t ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $previous_timestamp and not $previous_found and $opts{'start'} != $t) { } 0 0 0
elsif (not $previous_found and $opts{'start'} == $t) { } 0 0 0
Condition0%0/6
defined $previous_timestamp and not $previous_found and $opts{"start"} != $t
!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 $opts{"start"} != $t
defined $previous_timestamp not $previous_found $opts{"start"} != $t
Truth table0%0/4
A and not(B) and C
A= defined $previous_timestampB= not $previous_foundC= $opts{"start"} != $t
ABCresult
0XX0
10X0
1100
1111
157 0 $previous_found = 1;
158 0 push( @timestamp_to_return, $previous_timestamp );
159 ▶ }elsif (!$previous_found && ( $opts{start} == $t ) ) {
Condition0%0/3
not $previous_found and $opts{"start"} == $t
!ll&&!rl&&r
000
MC/DC0%0/2
not $previous_found and $opts{"start"} == $t
not $previous_found $opts{"start"} == $t
Truth table0%0/3
not(A) and B
A= not $previous_foundB= $opts{"start"} == $t
ABresult
0X0
100
111
160 0 $previous_found = 1;
161 }
162 }
163  
164 # if the time period falls between when two files was created, we will find
165 0 ▶ if (!$previous_found && defined($previous_timestamp) && ( $opts{end} < $t )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $previous_found and defined $previous_timestamp and $opts{"end"} < $t) 0 0 0
Condition0%0/6
not $previous_found and defined $previous_timestamp and $opts{"end"} < $t
!ll&&!rl&&r
000
not $previous_found and defined $previous_timestamp
!ll&&!rl&&r
000
MC/DC0%0/3
not $previous_found and defined $previous_timestamp and $opts{"end"} < $t
not $previous_found defined $previous_timestamp $opts{"end"} < $t
Truth table0%0/4
not(A) and B and C
A= not $previous_foundB= defined $previous_timestampC= $opts{"end"} < $t
ABCresult
0XX0
10X0
1100
1111
166 0 $previous_found=1;
167 0 push( @timestamp_to_return, $previous_timestamp );
168 }
169  
170 0 $previous_timestamp = $current_timestamp;
171 0 $previous_t = $t;
172 }
173  
174 0 my $to_return=[];
175 # the second sort is needed as if
176 0 foreach my $item (sort(@timestamp_to_return)) {
177 0 foreach my $file (@{ $found->{$item} }) {
178 0 push(@{ $to_return }, $file);
179 }
180 }
181  
182 0 return $to_return;
183 }
184  
185 =head1 AUTHOR
186  
187 Zane C. Bower-Hadley, C<< <vvelox at vvelox.net> >>
188  
189 =head1 BUGS
190  
191 Please report any bugs or feature requests to C<bug-file-find-includestimerange at rt.cpan.org>, or through
192 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Find-IncludesTimeRange>. I will be notified, and then you'll
193 automatically be notified of progress on your bug as I make changes.
194  
195  
196  
197  
198 =head1 SUPPORT
199  
200 You can find documentation for this module with the perldoc command.
201  
202 perldoc File::Find::IncludesTimeRange
203  
204  
205 You can also look for information at:
206  
207 =over 4
208  
209 =item * RT: CPAN's request tracker (report bugs here)
210  
211 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Find-IncludesTimeRange>
212  
213 =item * CPAN Ratings
214  
215 L<https://cpanratings.perl.org/d/File-Find-IncludesTimeRange>
216  
217 =item * Search CPAN
218  
219 L<https://metacpan.org/release/File-Find-IncludesTimeRange>
220  
221 =back
222  
223  
224 =head1 ACKNOWLEDGEMENTS
225  
226  
227 =head1 LICENSE AND COPYRIGHT
228  
229 This software is Copyright (c) 2023 by Zane C. Bower-Hadley.
230  
231 This is free software, licensed under:
232  
233 The Artistic License 2.0 (GPL Compatible)
234  
235  
236 =cut
237  
238 1 1; # End of File::Find::IncludesTimeRange
↑ Summary