| 1 | package Data::Validate::Sanctions::Redis; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use parent 'Data::Validate::Sanctions'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Data::Validate::Sanctions::Fetcher; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use List::Util qw(max); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use JSON::MaybeUTF8 qw(encode_json_utf8 decode_json_utf8); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use YAML::XS qw(DumpFile); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 9 | our $VERSION = '0.21'; # VERSION | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | 0 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 0 | my $self = {}; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $self->{connection} = $args{connection} or die 'Redis connection is missing'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 0 | $self->{sources} = [keys Data::Validate::Sanctions::Fetcher::config(eu_token => 'dummy')->%*]; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 0 | $self->{args} = {%args}; | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 0 | $self->{last_modification} = 0; | |||||||||||||||||||||||||||||||
| 29 | 0 | $self->{last_index} = 0; | |||||||||||||||||||||||||||||||
| 30 | 0 | $self->{last_data_load} = 0; | |||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $object = bless $self, ref($class) || $class; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||
| 33 | 0 | $object->_load_data(); | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 0 | return $object; | |||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub set_sanction_file { | ||||||||||||||||||||||||||||||
|
Subroutine
set_sanction_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 39 | 0 | die 'Not applicable'; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub get_sanction_file { | ||||||||||||||||||||||||||||||
|
Subroutine
get_sanction_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 43 | 0 | die 'Not applicable'; | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub get_sanctioned_info { | ||||||||||||||||||||||||||||||
|
Subroutine
get_sanctioned_info: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 47 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | die "This function can only be called on an object" unless $self; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 0 | return Data::Validate::Sanctions::get_sanctioned_info($self, @_); | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub _load_data { | ||||||||||||||||||||||||||||||
|
Subroutine
_load_data: not called CC=21 SCAR=61.4
| |||||||||||||||||||||||||||||||||
| 55 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $self->{last_modification} //= 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"last_modification"} //= 0
MC/DC0%0/1
$self->{"last_modification"} //= 0
$self->{"last_modification"}
Truth table0%0/2
A
A= $self->{"last_modification"}
| |||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $self->{last_index} //= 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"last_index"} //= 0
MC/DC0%0/1
$self->{"last_index"} //= 0
$self->{"last_index"}
Truth table0%0/2
A
A= $self->{"last_index"}
| |||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $self->{_data} //= {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_data"} //= {}
MC/DC0%0/1
$self->{"_data"} //= {}
$self->{"_data"}
Truth table0%0/2
A
A= $self->{"_data"}
| |||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $self->{_sanctioned_name_tokens} //= {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_sanctioned_name_tokens"} //= {}
MC/DC0%0/1
$self->{"_sanctioned_name_tokens"} //= {}
$self->{"_sanctioned_name_tokens"}
Truth table0%0/2
A
A= $self->{"_sanctioned_name_tokens"}
| |||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $self->{_token_sanctioned_names} //= {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_token_sanctioned_names"} //= {}
MC/DC0%0/1
$self->{"_token_sanctioned_names"} //= {}
$self->{"_token_sanctioned_names"}
Truth table0%0/2
A
A= $self->{"_token_sanctioned_names"}
| |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return $self->{_data} if $self->{_data} and $self->{last_data_load} + $self->IGNORE_OPERATION_INTERVAL > time; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"_data"} and $self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time
MC/DC0%0/2
$self->{"_data"} and $self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time
$self->{"_data"}
$self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time
Truth table0%0/3
A and B
A= $self->{"_data"}B= $self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time
| |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 0 | my $latest_update = 0; | |||||||||||||||||||||||||||||||
| 66 | 0 | for my $source ($self->{sources}->@*) { | |||||||||||||||||||||||||||||||
| 67 | try { | ||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $self->{_data}->{$source} //= {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_data"}{$source} //= {}
MC/DC0%0/1
$self->{"_data"}{$source} //= {}
$self->{"_data"}{$source}
Truth table0%0/2
A
A= $self->{"_data"}{$source}
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 0 | my ($updated) = $self->{connection}->hget("SANCTIONS::$source" => 'updated'); | |||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $updated //= 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$updated //= 0
MC/DC0%0/1
$updated //= 0
$updated
Truth table0%0/2
A
A= $updated
| |||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $current_update_date = $self->{_data}->{$source}->{updated} // 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_data"}{$source}{"updated"} // 0
MC/DC0%0/1
$self->{"_data"}{$source}{"updated"} // 0
$self->{"_data"}{$source}{"updated"}
Truth table0%0/2
A
A= $self->{"_data"}{$source}{"updated"}
| |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | next if $current_update_date && $updated <= $current_update_date; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$current_update_date and $updated <= $current_update_date
MC/DC0%0/2
$current_update_date and $updated <= $current_update_date
$current_update_date
$updated <= $current_update_date
Truth table0%0/3
A and B
A= $current_update_dateB= $updated <= $current_update_date
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 0 | my ($content, $verified, $error) = $self->{connection}->hmget("SANCTIONS::$source", qw/content verified error/)->@*; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $self->{_data}->{$source}->{content} = decode_json_utf8($content // '[]'); | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$content // "[]"
MC/DC0%0/1
$content // "[]"
$content
Truth table0%0/2
A
A= $content
| |||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $self->{_data}->{$source}->{verified} = $verified // 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$verified // 0
MC/DC0%0/1
$verified // 0
$verified
Truth table0%0/2
A
A= $verified
| |||||||||||||||||||||||||||||||||
| 79 | 0 | $self->{_data}->{$source}->{updated} = $updated; | |||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $self->{_data}->{$source}->{error} = $error // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$error // ""
MC/DC0%0/1
$error // ""
$error
Truth table0%0/2
A
A= $error
| |||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $latest_update = $updated if $updated > $latest_update; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 0 | } catch ($e) { | |||||||||||||||||||||||||||||||
| 83 | 0 | $self->{_data}->{$source}->{content} = []; | |||||||||||||||||||||||||||||||
| 84 | 0 | $self->{_data}->{$source}->{updated} = 0; | |||||||||||||||||||||||||||||||
| 85 | 0 | $self->{_data}->{$source}->{verified} = 0; | |||||||||||||||||||||||||||||||
| 86 | 0 | $self->{_data}->{$source}->{error} = "Failed to load from Redis: $e"; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | 0 | } | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 0 | $self->{last_modification} = $latest_update; | |||||||||||||||||||||||||||||||
| 91 | 0 | $self->{last_data_load} = time; | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | return $self->{_data} if $latest_update <= $self->{last_index}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 0 | $self->_index_data(); | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 0 | foreach my $sanctioned_name (keys $self->{_index}->%*) { | |||||||||||||||||||||||||||||||
| 98 | 0 | my @tokens = Data::Validate::Sanctions::_clean_names($sanctioned_name); | |||||||||||||||||||||||||||||||
| 99 | 0 | $self->{_sanctioned_name_tokens}->{$sanctioned_name} = \@tokens; | |||||||||||||||||||||||||||||||
| 100 | 0 | foreach my $token (@tokens) { | |||||||||||||||||||||||||||||||
| 101 | 0 | $self->{_token_sanctioned_names}->{$token}->{$sanctioned_name} = 1; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 0 | return $self->{_data}; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub _save_data { | ||||||||||||||||||||||||||||||
|
Subroutine
_save_data: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 109 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 0 | for my $source ($self->{sources}->@*) { | |||||||||||||||||||||||||||||||
| 112 | 0 | $self->{_data}->{$source}->{verified} = time; | |||||||||||||||||||||||||||||||
| 113 | $self->{connection}->hmset( | ||||||||||||||||||||||||||||||||
| 114 | "SANCTIONS::$source", | ||||||||||||||||||||||||||||||||
| 115 | updated => $self->{_data}->{$source}->{updated} // 0, | ||||||||||||||||||||||||||||||||
| 116 | content => encode_json_utf8($self->{_data}->{$source}->{content} // []), | ||||||||||||||||||||||||||||||||
| 117 | verified => $self->{_data}->{$source}->{verified}, | ||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | error => $self->{_data}->{$source}->{error} // '' | ||||||||||||||||||||||||||||||
|
Condition0%0/6
$self->{"_data"}{$source}{"updated"} // 0
$self->{"_data"}{$source}{"content"} // []
$self->{"_data"}{$source}{"error"} // ""
MC/DC0%0/1
$self->{"_data"}{$source}{"updated"} // 0
$self->{"_data"}{$source}{"updated"}
MC/DC0%0/1
$self->{"_data"}{$source}{"content"} // []
$self->{"_data"}{$source}{"content"}
MC/DC0%0/1
$self->{"_data"}{$source}{"error"} // ""
$self->{"_data"}{$source}{"error"}
Truth table0%0/2
A
A= $self->{"_data"}{$source}{"updated"}
Truth table0%0/2
A
A= $self->{"_data"}{$source}{"content"}
Truth table0%0/2
A
A= $self->{"_data"}{$source}{"error"}
| |||||||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 0 | return; | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub _default_sanction_file { | ||||||||||||||||||||||||||||||
|
Subroutine
_default_sanction_file: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 126 | 0 | die 'Not applicable'; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 9 | 1; | |||||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||||||