| 1 | package Punk::Observe::Warm; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Punk::Observe (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Punk::Observe::Cache (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Punk::Observe::Config (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Punk::Observe::Store (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | our $VERSION = $Punk::Observe::VERSION; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | DEPTH_NS => 7 * 86_400 * 1_000_000_000, # a week back | |||||||||||||||||||||||||||||||||||||||||
| 18 | REFRESH_NS => 2 * 3600 * 1_000_000_000, # the newest two hours, always | ||||||||||||||||||||||||||||||||||||||||||
| 19 | TTL => 8 * 86_400, # outlives the depth | ||||||||||||||||||||||||||||||||||||||||||
| 20 | BUDGET => 400, # chunks computed per pass | ||||||||||||||||||||||||||||||||||||||||||
| 21 | TIMEOUT => 20, # seconds per pass | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | sub _plugin_state { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_plugin_state: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | require Punk::Plugin::Observe; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | ▶ | my $st = Punk::Plugin::Observe->state_for($class) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | or Carp::croak("Punk::Observe::Warm: no Observe plugin state for " | ||||||||||||||||||||||||||||||||||||||||||
| 29 | . "$class - is the worker running the same application " | ||||||||||||||||||||||||||||||||||||||||||
| 30 | . "class as the server?"); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | return $st; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | ▶ | sub tenants { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tenants: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | my ($db) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | my $rows = $db->dbh->selectcol_arrayref( | |||||||||||||||||||||||||||||||||||||||||
| 37 | 'SELECT DISTINCT tenant FROM dashboards ORDER BY tenant'); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | ▶ | return $rows || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rows || []
MC/DC0%0/1
$rows || []
$rows
Truth table50%1/2
A
A= $rows
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | sub queries { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
queries: called CC=8 SCAR=21.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | my ($db, $tenant) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | my $top = Punk::Observe::Config::dashboards($db, $tenant); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | my (@q, %seen); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | ▶ | for my $d (@{ $top->{list} || [] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$top->{'list'} || []
MC/DC0%0/1
$top->{'list'} || []
$top->{'list'}
Truth table50%1/2
A
A= $top->{'list'}
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | my $full = Punk::Observe::Config::dashboards($db, $tenant, $d->{slug}); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | for my $p (@{ $full->{panels} || [] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$full->{'panels'} || []
MC/DC0%0/1
$full->{'panels'} || []
$full->{'panels'}
Truth table50%1/2
A
A= $full->{'panels'}
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 13 | my $src = $p->{query}; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 13 | ▶ | next unless defined $src && length $src; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $src and length $src
MC/DC0%0/2
defined $src and length $src
defined $src
length $src
Truth table33%1/3
A and B
A= defined $srcB= length $src
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 13 | ▶ | next if $seen{$src}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 9 | push @q, $src; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 5 | return \@q; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 6 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=21 SCAR=32.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 6 | my (%opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 6 | ▶ | my $db = $opt{db} or return { tenants => 0, queries => 0 }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 6 | ▶ | my $for = $opt{store} or return { tenants => 0, queries => 0 }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 6 | ▶ | my $now = defined $opt{now} ? $opt{now} : Punk::Observe::now_ns(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | ▶ | my $depth = $opt{depth_ns} || DEPTH_NS; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opt{'depth_ns'} || 604800000000000
MC/DC100%1/1
$opt{'depth_ns'} || 604800000000000
$opt{'depth_ns'}
Truth table100%2/2
A
A= $opt{'depth_ns'}
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 6 | ▶ | my $refresh = defined $opt{refresh_ns} ? $opt{refresh_ns} : REFRESH_NS; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 6 | ▶ | my $ttl = defined $opt{ttl} ? $opt{ttl} : TTL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 6 | ▶ | my $budget = defined $opt{budget} ? $opt{budget} : BUDGET; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | ▶ | my $timeout = defined $opt{timeout} ? $opt{timeout} : TIMEOUT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | my $from = Punk::Observe::Store::nsub($now, $depth); | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 6 | my %tot = (tenants => 0, queries => 0, skipped => 0, chunks => 0, | |||||||||||||||||||||||||||||||||||||||||
| 71 | computed => 0, hits => 0, failed => 0, unstorable => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | stopped => ''); | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | my $t0 = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 6 | for my $tenant (@{ tenants($db) }) { | |||||||||||||||||||||||||||||||||||||||||
| 77 | 5 | ▶ | my $store = $for->($tenant) or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | my $cache = $store->{cache} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | $tot{tenants}++; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | for my $q (@{ queries($db, $tenant) }) { | |||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | ▶ | if (!defined Punk::Observe::Cache::bucket_ns($q)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | $tot{skipped}++; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | $tot{queries}++; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | my $left = $budget - $tot{computed}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | my $secs = $timeout - (Time::HiRes::time() - $t0); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($budget && $left <= 0) { $tot{stopped} = 'budget'; return \%tot } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$budget and $left <= 0
MC/DC0%0/2
$budget and $left <= 0
$budget
$left <= 0
Truth table33%1/3
A and B
A= $budgetB= $left <= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ($timeout && $secs <= 0) { $tot{stopped} = 'deadline'; return \%tot } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$timeout and $secs <= 0
MC/DC0%0/2
$timeout and $secs <= 0
$timeout
$secs <= 0
Truth table33%1/3
A and B
A= $timeoutB= $secs <= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | my $r = Punk::Observe::Cache::warm( | |||||||||||||||||||||||||||||||||||||||||
| 94 | $store, $q, | ||||||||||||||||||||||||||||||||||||||||||
| 95 | from => $from, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | to => $now, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | now => $now, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | cache => $cache, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | ttl => $ttl, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | refresh_ns => $refresh, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | budget => $left, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | deadline => $secs, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | $tot{$_} += $r->{$_} for qw(chunks computed hits failed unstorable); | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | ▶ | if ($r->{stopped} eq 'budget' || $r->{stopped} eq 'deadline') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$r->{"stopped"} eq "budget" or $r->{"stopped"} eq "deadline"
MC/DC50%1/2
$r->{"stopped"} eq "budget" or $r->{"stopped"} eq "deadline"
$r->{"stopped"} eq "budget"
$r->{"stopped"} eq "deadline"
Truth table66%2/3
A or B
A= $r->{"stopped"} eq "budget"B= $r->{"stopped"} eq "deadline"
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | $tot{stopped} = $r->{stopped}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | return \%tot; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 5 | return \%tot; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | ▶ | sub warm_job { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
warm_job: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my ($job, $class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | my $st = _plugin_state($class); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | my $q = $job->queue_object; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | my $owner = 0 + $$; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | ▶ | return { skipped => 'lock' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | unless $q->lock('observe.warm', 60, owner => $owner); | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | my $opt = $st->{warm_opts} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$st->{'warm_opts'} || {}
MC/DC0%0/1
$st->{'warm_opts'} || {}
$st->{'warm_opts'}
Truth table50%1/2
A
A= $st->{'warm_opts'}
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | my $out = eval { | |||||||||||||||||||||||||||||||||||||||||
| 127 | run(db => $st->{db}, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | store => sub { Punk::Plugin::Observe::store_for($st, $_[0]) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | %$opt); | |||||||||||||||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | ▶ | $q->unlock('observe.warm', $owner) if $q->can('unlock'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | ▶ | die $err if $err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub cron_task { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cron_task: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my (%opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | my $db = $opt{db}; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $for = $opt{store}; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $owner = $opt{owner}; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | $owner = $$ unless defined $owner && $owner =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $owner and $owner =~ /\A[0-9]+\z/
MC/DC0%0/2
defined $owner and $owner =~ /\A[0-9]+\z/
defined $owner
$owner =~ /\A[0-9]+\z/
Truth table0%0/3
A and B
A= defined $ownerB= $owner =~ /\A[0-9]+\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | my $lease = $opt{lease_seconds} || 30; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'lease_seconds'} || 30
MC/DC0%0/1
$opt{'lease_seconds'} || 30
$opt{'lease_seconds'}
Truth table0%0/2
A
A= $opt{'lease_seconds'}
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my ($q) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | return 0 unless $q && $db && $for; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$q and $db and $for
$q and $db
MC/DC0%0/3
$q and $db and $for
$q
$db
$for
Truth table0%0/4
A and B and C
A= $qB= $dbC= $for
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | return 0 unless $q->lock('leader', $lease, owner => $owner); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | my $out = eval { run(%opt, db => $db, store => $for) }; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | $q->renew_lock('leader', $owner, $lease); | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | die $err if $err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | return $out ? $out->{computed} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||||||||||||||||