« blib/lib/Tapper/Reports/Web/Util/Filter/Testrun.pm
↑ Summary
blib/lib/Tapper/Reports/Web/Controller/Tapper.pm »
| 1 | package Tapper::Reports::Web::Util::Filter; | ||||||||||||||||||||||||||||
| 2 | 24 | our $AUTHORITY = 'cpan:TAPPER'; | |||||||||||||||||||||||||||
| 3 | 24 | $Tapper::Reports::Web::Util::Filter::VERSION = '5.0.17'; | |||||||||||||||||||||||||||
| 4 | 24 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 24 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 24 | ▶ | use DateTime::Format::DateParse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 24 | ▶ | use Hash::Merge::Simple 'merge'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use Tapper::Model 'model'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 24 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 24 | has 'context' => (is => 'rw'); | |||||||||||||||||||||||||||
| 15 | 24 | has 'requested_day' => (is => 'rw'); | |||||||||||||||||||||||||||
| 16 | 24 | has 'dispatch' => (is => 'rw', | |||||||||||||||||||||||||||
| 17 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 18 | # builder => 'build_dispatch', | ||||||||||||||||||||||||||||
| 19 | # init_arg => undef, | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | sub BUILD{ | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 24 | 4 | my $args = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | $self->dispatch( | |||||||||||||||||||||||||||
| 27 | merge($self->dispatch, {days => \&days, | ||||||||||||||||||||||||||||
| 28 | date => \&date, | ||||||||||||||||||||||||||||
| 29 | }) | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub date | ||||||||||||||||||||||||||||
| 37 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
date: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 2 | my ($self, $filter_condition, $date) = @_; | |||||||||||||||||||||||||||
| 39 | 2 | ▶ | return $self->days($filter_condition, $date) if $date =~m/^\d+$/; # handle old date links correctly | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | if (defined($self->requested_day)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | push @{$filter_condition->{error}}, "'date' and 'days' filter can not be used together."; | |||||||||||||||||||||||||||
| 43 | 0 | return $filter_condition; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | $filter_condition->{days} = 1; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | my $requested_day; | |||||||||||||||||||||||||||
| 49 | my $one_day_later; | ||||||||||||||||||||||||||||
| 50 | 2 | eval { | |||||||||||||||||||||||||||
| 51 | 2 | $requested_day = DateTime::Format::DateParse->parse_datetime( $date, 'GMT' ); | |||||||||||||||||||||||||||
| 52 | 2 | $one_day_later = DateTime::Format::DateParse->parse_datetime( $date, 'GMT' )->add(days => 1); | |||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | 2 | ▶ | if (not defined $requested_day) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | push @{$filter_condition->{error}}, "Can not parse date '$date'"; | |||||||||||||||||||||||||||
| 56 | 0 | return $filter_condition; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | my $dtf = model("TestrunDB")->storage->datetime_parser; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | $filter_condition->{date} = $requested_day->ymd('/'); | |||||||||||||||||||||||||||
| 62 | 2 | $self->requested_day($requested_day); | |||||||||||||||||||||||||||
| 63 | 2 | push @{$filter_condition->{late}}, {created_at => {'<=' => $dtf->format_datetime($one_day_later)}}; | |||||||||||||||||||||||||||
| 64 | 2 | return $filter_condition; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub days | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
days: called CC=2 SCAR=10.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 1 | my ($self, $filter_condition, $days) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | if (defined($self->requested_day)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1 | push @{$filter_condition->{error}}, "'date' and 'days' filter can not be used together."; | |||||||||||||||||||||||||||
| 74 | 1 | return $filter_condition; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | $filter_condition->{days} = $days; | |||||||||||||||||||||||||||
| 77 | 0 | my $parser = DateTime::Format::Natural->new(time_zone => 'GMT'); | |||||||||||||||||||||||||||
| 78 | 0 | my $requested_day = $parser->parse_datetime("today at midnight"); | |||||||||||||||||||||||||||
| 79 | 0 | $self->requested_day($requested_day); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $dtf = model("TestrunDB")->storage->datetime_parser; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $yesterday = $parser->parse_datetime("today at midnight")->subtract(days => $days); | |||||||||||||||||||||||||||
| 84 | 0 | $filter_condition->{early}->{created_at} = {'>' => $dtf->format_datetime($yesterday)}; | |||||||||||||||||||||||||||
| 85 | 0 | return $filter_condition; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub parse_filters | ||||||||||||||||||||||||||||
| 90 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_filters: called CC=7 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 4 | my ($self, $args, $allowed_filters) = @_; | |||||||||||||||||||||||||||
| 92 | 4 | ▶ | my @args = ref($args) eq 'ARRAY' ? @$args : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 4 | my $filter_condition = {}; | |||||||||||||||||||||||||||
| 94 | 4 | ▶ | while (@args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 3 | my $key = shift @args; | |||||||||||||||||||||||||||
| 96 | 3 | my $value = shift @args; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (ref $allowed_filters eq 'ARRAY' and not grep {$_ eq $key} @$allowed_filters) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $allowed_filters eq "ARRAY" and not grep({$_ eq $key;} @$allowed_filters)
MC/DC0%0/2
ref $allowed_filters eq "ARRAY" and not grep({$_ eq $key;} @$allowed_filters)
ref $allowed_filters eq "ARRAY"
not grep({$_ eq $key;} @$allowed_filters)
Truth table33%1/3
A and not(B)
A= ref $allowed_filters eq "ARRAY"B= not grep({$_ eq $key;} @$allowed_filters)
| |||||||||||||||||||||||||||||
| 99 | 0 | $filter_condition->{error} = "Can not filter for $key"; | |||||||||||||||||||||||||||
| 100 | 0 | return $filter_condition; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | ▶ | if (not $self->dispatch->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my @errors = @{$filter_condition->{error} || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$filter_condition->{'error'} || []
MC/DC0%0/1
$filter_condition->{'error'} || []
$filter_condition->{'error'}
Truth table0%0/2
A
A= $filter_condition->{'error'}
| |||||||||||||||||||||||||||||
| 105 | 0 | $filter_condition = {}; | |||||||||||||||||||||||||||
| 106 | 0 | push @errors, "Can not parse filter $key/$value. No filters applied"; | |||||||||||||||||||||||||||
| 107 | 0 | $filter_condition->{error} = \@errors; | |||||||||||||||||||||||||||
| 108 | 0 | return $filter_condition; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 3 | $filter_condition = $self->dispatch->{$key}->($self, $filter_condition, $value); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 4 | return $filter_condition; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 24 | 1; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||