File Coverage

blib/lib/Tapper/Reports/Web/Util/Filter.pm
Criterion Covered Total %
statement 54 77 70.1
branch 7 16 43.7
condition 1 3 33.3
subroutine 11 11 100.0
pod 2 4 50.0
total 75 111 67.5


line stmt bran cond sub pod time code
1             package Tapper::Reports::Web::Util::Filter;
2             our $AUTHORITY = 'cpan:TAPPER';
3             $Tapper::Reports::Web::Util::Filter::VERSION = '5.0.15';
4 13     13   7789 use DateTime;
  13         38  
  13         370  
5 13     13   74 use Data::Dumper;
  13         29  
  13         611  
6 13     13   4933 use DateTime::Format::DateParse;
  13         67083  
  13         419  
7 13     13   96 use Hash::Merge::Simple 'merge';
  13         32  
  13         575  
8 13     13   78 use Tapper::Model 'model';
  13         31  
  13         425  
9              
10 13     13   79 use Moose;
  13         26  
  13         97  
11              
12              
13              
14 13     13   84426 use common::sense;
  13         35  
  13         149  
15              
16             has 'context' => (is => 'rw');
17             has 'requested_day' => (is => 'rw');
18             has 'dispatch' => (is => 'rw',
19             isa => 'HashRef',
20             # builder => 'build_dispatch',
21             # init_arg => undef,
22             );
23              
24             sub BUILD{
25 4     4 0 12208 my $self = shift;
26 4         10 my $args = shift;
27              
28 4         177 $self->dispatch(
29             merge($self->dispatch, {days => \&days,
30             date => \&date,
31             })
32             );
33             }
34              
35              
36              
37              
38             sub date
39             {
40 2     2 1 9 my ($self, $filter_condition, $date) = @_;
41 2 50       14 return $self->days($filter_condition, $date) if $date =~m/^\d+$/; # handle old date links correctly
42              
43 2 50       67 if (defined($self->requested_day)) {
44 0         0 push @{$filter_condition->{error}}, "'date' and 'days' filter can not be used together.";
  0         0  
45 0         0 return $filter_condition;
46             }
47              
48 2         7 $filter_condition->{days} = 1;
49              
50 2         5 my $requested_day;
51             my $one_day_later;
52 2         4 eval {
53 2         15 $requested_day = DateTime::Format::DateParse->parse_datetime( $date, 'GMT' );
54 2         1822 $one_day_later = DateTime::Format::DateParse->parse_datetime( $date, 'GMT' )->add(days => 1);
55             };
56 2 50       3456 if (not defined $requested_day) {
57 0         0 push @{$filter_condition->{error}}, "Can not parse date '$date'";
  0         0  
58 0         0 return $filter_condition;
59             }
60              
61 2         13 my $dtf = model("TestrunDB")->storage->datetime_parser;
62              
63 2         182417 $filter_condition->{date} = $requested_day->ymd('/');
64 2         128 $self->requested_day($requested_day);
65 2         5 push @{$filter_condition->{late}}, {created_at => {'<=' => $dtf->format_datetime($one_day_later)}};
  2         14  
66 2         155 return $filter_condition;
67             }
68              
69              
70             sub days
71             {
72 1     1 0 5 my ($self, $filter_condition, $days) = @_;
73              
74 1 50       33 if (defined($self->requested_day)) {
75 1         2 push @{$filter_condition->{error}}, "'date' and 'days' filter can not be used together.";
  1         5  
76 1         4 return $filter_condition;
77             }
78 0         0 $filter_condition->{days} = $days;
79 0         0 my $parser = DateTime::Format::Natural->new(time_zone => 'GMT');
80 0         0 my $requested_day = $parser->parse_datetime("today at midnight");
81 0         0 $self->requested_day($requested_day);
82              
83 0         0 my $dtf = model("TestrunDB")->storage->datetime_parser;
84              
85 0         0 my $yesterday = $parser->parse_datetime("today at midnight")->subtract(days => $days);
86 0         0 $filter_condition->{early}->{created_at} = {'>' => $dtf->format_datetime($yesterday)};
87 0         0 return $filter_condition;
88             }
89              
90              
91             sub parse_filters
92             {
93 4     4 1 186 my ($self, $args, $allowed_filters) = @_;
94 4 50       24 my @args = ref($args) eq 'ARRAY' ? @$args : ();
95 4         11 my $filter_condition = {};
96 4         16 while (@args) {
97 3         7 my $key = shift @args;
98 3         7 my $value = shift @args;
99              
100 3 50 33     14 if (ref $allowed_filters eq 'ARRAY' and not grep {$_ eq $key} @$allowed_filters) {
  0         0  
101 0         0 $filter_condition->{error} = "Can not filter for $key";
102 0         0 return $filter_condition;
103             }
104              
105 3 50       102 if (not $self->dispatch->{$key}) {
106 0 0       0 my @errors = @{$filter_condition->{error} || [] };
  0         0  
107 0         0 $filter_condition = {};
108 0         0 push @errors, "Can not parse filter $key/$value. No filters applied";
109 0         0 $filter_condition->{error} = \@errors;
110 0         0 return $filter_condition;
111             }
112 3         109 $filter_condition = $self->dispatch->{$key}->($self, $filter_condition, $value);
113             }
114 4         19 return $filter_condition;
115             }
116              
117             1;
118              
119             __END__
120              
121             =pod
122              
123             =encoding UTF-8
124              
125             =head1 NAME
126              
127             Tapper::Reports::Web::Util::Filter
128              
129             =head2 date
130              
131             Add a date to late filters. Furthermore, it sets the days value. Date
132             used to filter for the number of days (now provided by function
133             'days'). For backwards compatibility it checks whether the input to mean
134             day rather than days and forwards accordingly.
135              
136             @param hash ref - current version of filters
137             @param string - date
138              
139             @return hash ref - updated filters
140              
141             =head2 parse_filters
142              
143             Parse filter arguments given by controller. The function expects an
144             array ref containing the filter arguments. This is achieved by providing
145             the arguments that Catalyst provide for a sub with :Args().
146              
147             The second argument is a list of word that are valid filters.
148              
149             The function returns a hash ref containing the following elements (not
150             necessarily all every time):
151             * early - hash ref - contains the filter conditions that can be given
152             in the initial search on reports
153             * late - array ref - list of hash refs that contain filter conditions
154             which shall be applied as $report->search{$key => $value}
155             after the initial search returned a $report already
156             * function - hash ref - functions that are to be applied as $report->$key($value);
157             * error - array ref - contains all errors that occured as strings
158             * days - int - number of days that will be shown
159             * date - string - the exact date that will be shown
160              
161             @param array ref - list of filter arguments
162             @param array ref - list of allowed filters
163              
164             @return hash ref
165              
166             =head1 AUTHORS
167              
168             =over 4
169              
170             =item *
171              
172             AMD OSRC Tapper Team <tapper@amd64.org>
173              
174             =item *
175              
176             Tapper Team <tapper-ops@amazon.com>
177              
178             =back
179              
180             =head1 COPYRIGHT AND LICENSE
181              
182             This software is Copyright (c) 2020 by Advanced Micro Devices, Inc..
183              
184             This is free software, licensed under:
185              
186             The (two-clause) FreeBSD License
187              
188             =cut