<commit_before>package Tapper::Reports::Web::Util::Filter;

use DateTime;
use Data::Dumper;
use DateTime::Format::DateParse;
use Hash::Merge::Simple 'merge';

use Moose;



use common::sense;

has 'context'         => (is => 'rw');
has 'requested_day'   => (is => 'rw');
has 'dispatch'        => (is        => 'rw',
                          isa       => 'HashRef',
                          # builder   => 'build_dispatch',
                          # init_arg  => undef,
                         );

sub BUILD{
        my $self = shift;
        my $args = shift;

        $self->dispatch(
                        merge($self->dispatch, {days => \&days,
                                                date => \&date,
                                               })
                       );
}


=head2 date

Add a date to late filters. Furthermore, it sets the days value. Date
used to filter for the number of days (now provided by function
'days'). For backwards compatibility it checks whether the input to mean
day rather than days and forwards accordingly.

@param hash ref - current version of filters
@param string   - date

@return hash ref - updated filters

=cut


sub date
{
        my ($self, $filter_condition, $date) = @_;
        return $self->days($filter_condition, $date) if $date =~m/^\d+$/; # handle old date links correctly

        if (defined($self->requested_day)) {
                push @{$filter_condition->{error}}, "Time filter already exists, only using first one";
                return $filter_condition;
        }

        $filter_condition->{days} = 1;

        my $requested_day;
        my $one_day_later;
        eval {
                $requested_day = DateTime::Format::DateParse->parse_datetime( $date );
                $one_day_later = DateTime::Format::DateParse->parse_datetime( $date )->add(days => 1);
        };
        if (not defined $requested_day) {
                push @{$filter_condition->{error}}, "Can not parse date '$date'";
                return $filter_condition;
        }

        $filter_condition->{date} = $requested_day->ymd('/');
        $self->requested_day($requested_day);
        push @{$filter_condition->{late}}, {created_at => {'<='  => $one_day_later}};
        return $filter_condition;
}


sub days
{
        my ($self, $filter_condition, $days) = @_;

        if (defined($self->requested_day)) {
                push @{$filter_condition->{error}}, "Time filter already exists, only using first one";
                return $filter_condition;
        }
        $filter_condition->{days} = $days;
        my $parser = new DateTime::Format::Natural;<commit_msg>[FIX] make error message better<commit_after>package Tapper::Reports::Web::Util::Filter;

use DateTime;
use Data::Dumper;
use DateTime::Format::DateParse;
use Hash::Merge::Simple 'merge';

use Moose;



use common::sense;

has 'context'         => (is => 'rw');
has 'requested_day'   => (is => 'rw');
has 'dispatch'        => (is        => 'rw',
                          isa       => 'HashRef',
                          # builder   => 'build_dispatch',
                          # init_arg  => undef,
                         );

sub BUILD{
        my $self = shift;
        my $args = shift;

        $self->dispatch(
                        merge($self->dispatch, {days => \&days,
                                                date => \&date,
                                               })
                       );
}


=head2 date

Add a date to late filters. Furthermore, it sets the days value. Date
used to filter for the number of days (now provided by function
'days'). For backwards compatibility it checks whether the input to mean
day rather than days and forwards accordingly.

@param hash ref - current version of filters
@param string   - date

@return hash ref - updated filters

=cut


sub date
{
        my ($self, $filter_condition, $date) = @_;
        return $self->days($filter_condition, $date) if $date =~m/^\d+$/; # handle old date links correctly

        if (defined($self->requested_day)) {
                push @{$filter_condition->{error}}, "'date' and 'days' filter can not be used together.";
                return $filter_condition;
        }

        $filter_condition->{days} = 1;

        my $requested_day;
        my $one_day_later;
        eval {
                $requested_day = DateTime::Format::DateParse->parse_datetime( $date );
                $one_day_later = DateTime::Format::DateParse->parse_datetime( $date )->add(days => 1);
        };
        if (not defined $requested_day) {
                push @{$filter_condition->{error}}, "Can not parse date '$date'";
                return $filter_condition;
        }

        $filter_condition->{date} = $requested_day->ymd('/');
        $self->requested_day($requested_day);
        push @{$filter_condition->{late}}, {created_at => {'<='  => $one_day_later}};
        return $filter_condition;
}


sub days
{
        my ($self, $filter_condition, $days) = @_;

        if (defined($self->requested_day)) {
                push @{$filter_condition->{error}}, "'date' and 'days' filter can not be used together.";
                return $filter_condition;
        }
        $filter_condition->{days} = $days;
        my $parser = new DateTime::Format::Natural;
{'fraction_non_alphanumeric': 0.13310708898944193, 'fraction_numerical': 0.0011312217194570137, 'mean_word_length': 1.9455857856746253, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1542843', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1428, 'n_words': 409}