| 1 | package Cikl::EventBuilder; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Cikl::Models::Event; | ||
| 5 | use Cikl::AddressBuilder qw/address_from_protoevent/; | ||
| 6 | use Mouse; | ||
| 7 | use namespace::autoclean; | ||
| 8 | use Try::Tiny; | ||
| 9 | use DateTime; | ||
| 10 | use Cikl::Util::TimeHelpers qw/normalize_timestamp create_strptime_parser create_default_timestamp_parser/; | ||
| 11 | |||
| 12 | has 'default_event_data' => ( | ||
| 13 | is => 'rw', | ||
| 14 | isa => 'HashRef', | ||
| 15 | required => 1, | ||
| 16 | default => sub { return {}; } | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'refresh' => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'Bool', | ||
| 22 | default => 0, | ||
| 23 | required => 1 | ||
| 24 | ); | ||
| 25 | |||
| 26 | has 'detecttime_format' => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => 'Maybe[Str]', | ||
| 29 | required => 0 | ||
| 30 | ); | ||
| 31 | |||
| 32 | has 'detecttime_zone' => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'Maybe[Str]', | ||
| 35 | required => 0 | ||
| 36 | ); | ||
| 37 | |||
| 38 | has 'detecttime_parser' => ( | ||
| 39 | is => 'ro', | ||
| 40 | init_arg => undef, | ||
| 41 | isa => 'CodeRef', | ||
| 42 | lazy => 1, | ||
| 43 | builder => '_build_detecttime_parser' | ||
| 44 | ); | ||
| 45 | |||
| 46 | sub _build_detecttime_parser { | ||
| 47 | my $self = shift; | ||
| 48 | if (defined($self->detecttime_format)) { | ||
| 49 | return create_strptime_parser( | ||
| 50 | $self->detecttime_format, $self->detecttime_zone); | ||
| 51 | } | ||
| 52 | return create_default_timestamp_parser(); | ||
| 53 | } | ||
| 54 | |||
| 55 | has 'not_before' => ( | ||
| 56 | is => 'rw', | ||
| 57 | isa => 'Int', | ||
| 58 | required => 1, | ||
| 59 | default => sub { | ||
| 60 | return DateTime->now()->subtract(days => 3)->epoch(); | ||
| 61 | } | ||
| 62 | ); | ||
| 63 | |||
| 64 | has '_now' => ( | ||
| 65 | is => 'ro', | ||
| 66 | default => sub { time() }, | ||
| 67 | init_arg => undef | ||
| 68 | ); | ||
| 69 | |||
| 70 | sub merge_default_event_data { | ||
| 71 | my $self = shift; | ||
| 72 | my $data_to_merge = shift; | ||
| 73 | my $merged_data = {%{$self->default_event_data}, %$data_to_merge}; | ||
| 74 | $self->default_event_data($merged_data); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub normalize { | ||
| 78 | my $self = shift; | ||
| 79 | my $r = shift; | ||
| 80 | |||
| 81 | my $now = $self->_now; | ||
| 82 | my $dt = $self->detecttime_parser->($r->{detecttime}, $now); | ||
| 83 | if($dt < $self->not_before) { | ||
| 84 | return(undef); | ||
| 85 | } | ||
| 86 | $r->{detecttime} = $dt; | ||
| 87 | |||
| 88 | $r->{reporttime} = $self->refresh ? $now : | ||
| 89 | normalize_timestamp($r->{reporttime}, $now); | ||
| 90 | |||
| 91 | $r->{address} = address_from_protoevent($r); | ||
| 92 | # MPR: Disabling value expansion, for now. | ||
| 93 | # foreach my $key (keys %$r){ | ||
| 94 | # my $v = $r->{$key}; | ||
| 95 | # next unless($v); | ||
| 96 | # if($v =~ /<(\S+)>/){ | ||
| 97 | # my $value_to_expand = $1; | ||
| 98 | # my $x = $r->{$value_to_expand}; | ||
| 99 | # if($x){ | ||
| 100 | # $r->{$key} =~ s/<\S+>/$x/; | ||
| 101 | # } | ||
| 102 | # } | ||
| 103 | # } | ||
| 104 | return $r; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub build_event { | ||
| 108 | my $self = shift; | ||
| 109 | my $hashref = shift; | ||
| 110 | my $err; | ||
| 111 | |||
| 112 | if (!defined($hashref)) { | ||
| 113 | die("build_event requires a hashref of arguments!"); | ||
| 114 | } | ||
| 115 | my $merged_hash = {%{$self->default_event_data}, %$hashref}; | ||
| 116 | my $normalized; | ||
| 117 | try { | ||
| 118 | $normalized = $self->normalize($merged_hash); | ||
| 119 | } catch { | ||
| 120 | $err = shift; | ||
| 121 | }; | ||
| 122 | if ($err) { | ||
| 123 | die($err); | ||
| 124 | } | ||
| 125 | if (!defined($normalized)) { | ||
| 126 | return undef; | ||
| 127 | } | ||
| 128 | |||
| 129 | my $event; | ||
| 130 | try { | ||
| 131 | $event = Cikl::Models::Event->new($normalized); | ||
| 132 | } catch { | ||
| 133 | $err = shift; | ||
| 134 | }; | ||
| 135 | if ($err) { | ||
| 136 | die("Failed to build event. Likely missing a required field: $err"); | ||
| 137 | } | ||
| 138 | return $event; | ||
| 139 | } | ||
| 140 | |||
| 141 | __PACKAGE__->meta->make_immutable; | ||
| 142 | |||
| 143 | 1; | ||
| 144 |