| 1 | package Google::Spreadsheet::Agent::Runner; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use FindBin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 13 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 13 | extends ('Google::Spreadsheet::Agent::DB'); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 13 | has 'dry_run' => ( | |||||||||||||||||||||||||||
| 12 | is => 'rw', | ||||||||||||||||||||||||||||
| 13 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 14 | predicate => 'is_dry_run', | ||||||||||||||||||||||||||||
| 15 | clearer => 'no_dry_run' | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 13 | has 'run_in_serial' => ( | |||||||||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||||||||||
| 20 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 21 | default => 0, | ||||||||||||||||||||||||||||
| 22 | predicate => 'is_run_in_serial' | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 13 | has 'debug' => ( | |||||||||||||||||||||||||||
| 26 | is => 'rw', | ||||||||||||||||||||||||||||
| 27 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 28 | lazy => 1, # depends on dry_run if not set explicitly | ||||||||||||||||||||||||||||
| 29 | builder => '_build_debug', | ||||||||||||||||||||||||||||
| 30 | predicate => 'is_debug', | ||||||||||||||||||||||||||||
| 31 | clearer => 'no_debug' | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 13 | has 'query_fields' => ( | |||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 37 | lazy => 1, # depends on config | ||||||||||||||||||||||||||||
| 38 | builder => '_build_query_fields', | ||||||||||||||||||||||||||||
| 39 | init_arg => undef # query_fields cannot be overridden | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 13 | has 'sleep_between' => ( | |||||||||||||||||||||||||||
| 43 | is => 'rw', | ||||||||||||||||||||||||||||
| 44 | isa => 'Int', | ||||||||||||||||||||||||||||
| 45 | default => 5 | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | has 'agent_bin' => ( | ||||||||||||||||||||||||||||
| 49 | is => 'rw', | ||||||||||||||||||||||||||||
| 50 | isa => 'Str', | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | default => sub { $FindBin::Bin.'/../agent_bin' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 52 | 13 | ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 13 | has 'skip_pages' => ( | |||||||||||||||||||||||||||
| 55 | is => 'rw', | ||||||||||||||||||||||||||||
| 56 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 57 | predicate => 'has_skip_pages', | ||||||||||||||||||||||||||||
| 58 | clearer => 'no_skip_pages' | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 13 | has 'skip_pages_if' => ( | |||||||||||||||||||||||||||
| 62 | is => 'rw', | ||||||||||||||||||||||||||||
| 63 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 64 | predicate => 'has_skip_pages_if', | ||||||||||||||||||||||||||||
| 65 | clearer => 'no_skip_pages_if' | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 13 | has 'only_pages' => ( | |||||||||||||||||||||||||||
| 69 | is => 'rw', | ||||||||||||||||||||||||||||
| 70 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 71 | predicate => 'has_only_pages', | ||||||||||||||||||||||||||||
| 72 | clearer => 'no_only_pages' | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 13 | has 'only_pages_if' => ( | |||||||||||||||||||||||||||
| 76 | is => 'rw', | ||||||||||||||||||||||||||||
| 77 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 78 | predicate => 'has_only_pages_if', | ||||||||||||||||||||||||||||
| 79 | clearer => 'no_only_pages_if' | ||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 13 | has 'skip_entry' => ( | |||||||||||||||||||||||||||
| 83 | is => 'rw', | ||||||||||||||||||||||||||||
| 84 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 85 | builder => '_build_skip_entry' | ||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 13 | has 'skip_goal' => ( | |||||||||||||||||||||||||||
| 89 | is => 'rw', | ||||||||||||||||||||||||||||
| 90 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 91 | lazy => 1, # depends on agent_bin | ||||||||||||||||||||||||||||
| 92 | builder => '_build_skip_goal' | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 13 | has 'process_entries_with' => ( | |||||||||||||||||||||||||||
| 96 | is => 'rw', | ||||||||||||||||||||||||||||
| 97 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 98 | lazy => 1, | ||||||||||||||||||||||||||||
| 99 | builder => '_build_process_entries_with' | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # BUILDERS | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | die ("You cannot construct a runner with both only_pages and skip_pages\n") if ($self->only_pages && $self->skip_pages); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->only_pages and $self->skip_pages
MC/DC0%0/2
$self->only_pages and $self->skip_pages
$self->only_pages
$self->skip_pages
Truth table0%0/3
A and B
A= $self->only_pagesB= $self->skip_pages
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub _build_debug { | ||||||||||||||||||||||||||
|
Subroutine
_build_debug: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 0 | return $self->dry_run; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub _build_query_fields { | ||||||||||||||||||||||||||
|
Subroutine
_build_query_fields: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | return [ | ||||||||||||||||||||||||||||
| 119 | sort { | ||||||||||||||||||||||||||||
| 120 | $self->config->{key_fields}->{$a}->{rank} <=> $self->config->{key_fields}->{$b}->{rank} | ||||||||||||||||||||||||||||
| 121 | 0 | } keys %{$self->config->{key_fields}} | |||||||||||||||||||||||||||
| 122 | ]; | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub _build_skip_entry { | ||||||||||||||||||||||||||
|
Subroutine
_build_skip_entry: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 126 | return ( | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 128 | 0 | my $entry = shift; | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | return 1 unless ($entry->content->{ready}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | return 1 if ($entry->content->{complete}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | return; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub _build_skip_goal { | ||||||||||||||||||||||||||
|
Subroutine
_build_skip_goal: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 138 | return ( | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 140 | 0 | my ($entry, $goal) = @_; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | print "Checking ${goal}\n" if ($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | return 1 if ($entry->{$goal}); # r, 1, F cause it to be skipped | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | my $goal_script = join('/', $self->agent_bin, $goal.'_agent.pl'); | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | return 1 unless (-x $goal_script); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | ); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub _build_process_entries_with { | ||||||||||||||||||||||||||
|
Subroutine
_build_process_entries_with: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 152 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | return ( | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 156 | 0 | my $runnable_entry = shift; | |||||||||||||||||||||||||||
| 157 | 0 | my $title = join(' ', map { $runnable_entry->{$_} } @{$self->query_fields}); | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | print STDERR "Checking goals for ${title}\n" if ($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | foreach my $goal ( grep { !$self->skip_goal->($runnable_entry, $_) } keys %{$runnable_entry} ) { | |||||||||||||||||||||||||||
| 161 | 0 | $self->run_entry_goal($runnable_entry, $goal); | |||||||||||||||||||||||||||
| 162 | 0 | sleep $self->sleep_between; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 0 | ); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # METHODS | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub run_entry_goal { | ||||||||||||||||||||||||||
|
Subroutine
run_entry_goal: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 0 | my ($self, $entry, $goal) = @_; | |||||||||||||||||||||||||||
| 172 | 0 | ▶ | print STDERR "Running goal ${goal}\n" if ($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $goal_agent = join('/', $self->agent_bin, $goal.'_agent.pl'); | |||||||||||||||||||||||||||
| 175 | 0 | my @cmd = ($goal_agent); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | foreach my $query_field ( @{$self->query_fields} ) { | |||||||||||||||||||||||||||
| 178 | 0 | ▶ | next unless ($entry->{$query_field}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | push @cmd, $entry->{$query_field}; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | my $command = join(' ', @cmd); | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | $command .= '&' unless ($self->run_in_serial); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | print STDERR "${command}\n" if ($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if (-x $goal_agent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | unless ($self->dry_run) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | system($command); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | else { | ||||||||||||||||||||||||||||
| 192 | 0 | print STDERR "AGENT RUNNER DOES NOT EXIST!\n"; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | sub get_pages_to_process { | ||||||||||||||||||||||||||
|
Subroutine
get_pages_to_process: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 197 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | my %skip_page = (); | |||||||||||||||||||||||||||
| 200 | 0 | ▶ | if ($self->only_pages) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 201 | 0 | %skip_page = map { $_->title => 1 } $self->google_db->worksheets; | |||||||||||||||||||||||||||
| 202 | 0 | map { undef $skip_page{$_} } @{$self->only_pages}; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | elsif ($self->only_pages_if) { | ||||||||||||||||||||||||||||
| 205 | 0 | return grep { $self->only_pages_if->($_) } $self->google_db->worksheets; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | elsif ($self->skip_pages) { | ||||||||||||||||||||||||||||
| 208 | 0 | %skip_page = map { $_ => 1 } @{$self->skip_pages}; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | elsif ($self->skip_pages_if) { | ||||||||||||||||||||||||||||
| 211 | 0 | return grep { !$self->skip_pages_if->($_) } $self->google_db->worksheets; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | else { | ||||||||||||||||||||||||||||
| 214 | 0 | return $self->google_db->worksheets; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | return grep { !($skip_page{$_->title}) } $self->google_db->worksheets; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 0 | ▶ | sub get_runnable_entries { | ||||||||||||||||||||||||||
|
Subroutine
get_runnable_entries: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 221 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 222 | 0 | my @runnable_entries = (); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | foreach my $page ($self->get_pages_to_process) { | |||||||||||||||||||||||||||
| 225 | 0 | ▶ | print STDERR "Processing page ".$page->title."\n" if ($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 0 | push @runnable_entries, grep { !($self->skip_entry->($_)) } $page->rows; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | return @runnable_entries; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | foreach my $runnable_entry ($self->get_runnable_entries) { | |||||||||||||||||||||||||||
| 236 | 0 | $self->process_entries_with->($runnable_entry->content, $runnable_entry); | |||||||||||||||||||||||||||
| 237 | 0 | sleep $self->sleep_between; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 13 | 1; | |||||||||||||||||||||||||||
| 242 | __END__ | ||||||||||||||||||||||||||||