blib/lib/Punk/Observe/Warm.pm

filter: Statementstmt 82.9% 97 / 117 Branchbran 58.3% 28 / 48 Conditioncond 33.3% 11 / 33 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 88.8% 16 / 18 Podpod 100% 5 / 5 totaltotal 66.2% 159 / 240 CC 43 SCAR 29.3
CC
43
Cov
69%
__ANON__
56.0
run
25.5
cron_task
20.0
CRAP 18.7
« blib/lib/Punk/Observe/Evaluate.pm ↑ Summary blib/lib/Punk/Observe/Config.pm »
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
BranchTrueFalseTotal
my $st = "Punk::Plugin::Observe"->state_for($class) or Carp::croak('Punk::Observe::Warm: no Observe plugin state for ' . "$class - is the worker running the same application " . 'class as the server?') 2 1 3
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 || []
l!l
70
MC/DC0%0/1
$rows || []
$rows
Truth table50%1/2
A
A= $rows
Aresult
11
00
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'} || []
l!l
50
MC/DC0%0/1
$top->{'list'} || []
$top->{'list'}
Truth table50%1/2
A
A= $top->{'list'}
Aresult
11
00
46 5 my $full = Punk::Observe::Config::dashboards($db, $tenant, $d->{slug});
47 5 ▶ for my $p (@{ $full->{panels} || [] }) {
Condition50%1/2
$full->{'panels'} || []
l!l
50
MC/DC0%0/1
$full->{'panels'} || []
$full->{'panels'}
Truth table50%1/2
A
A= $full->{'panels'}
Aresult
11
00
48 13 my $src = $p->{query};
49 13 ▶ next unless defined $src && length $src;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $src and length $src 0 13 13
Condition33%1/3
defined $src and length $src
!ll&&!rl&&r
0013
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
ABresult
0X0
100
111
50 13 ▶ next if $seen{$src}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$src}++ 4 9 13
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
BranchTrueFalseTotal
my $db = $opt{"db"} or return {"tenants", 0, "queries", 0} 6 0 6
60 6 ▶ my $for = $opt{store} or return { tenants => 0, queries => 0 };
Branch: 1 branch
BranchTrueFalseTotal
my $for = $opt{"store"} or return {"tenants", 0, "queries", 0} 6 0 6
61  
62 6 ▶ my $now = defined $opt{now} ? $opt{now} : Punk::Observe::now_ns();
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'now'} ? : 6 0 6
63 6 ▶ my $depth = $opt{depth_ns} || DEPTH_NS;
Condition100%2/2
$opt{'depth_ns'} || 604800000000000
l!l
51
MC/DC100%1/1
$opt{'depth_ns'} || 604800000000000
$opt{'depth_ns'}
Truth table100%2/2
A
A= $opt{'depth_ns'}
Aresult
11
00
64 6 ▶ my $refresh = defined $opt{refresh_ns} ? $opt{refresh_ns} : REFRESH_NS;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'refresh_ns'} ? : 2 4 6
65 6 ▶ my $ttl = defined $opt{ttl} ? $opt{ttl} : TTL;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'ttl'} ? : 4 2 6
66 6 ▶ my $budget = defined $opt{budget} ? $opt{budget} : BUDGET;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'budget'} ? : 1 5 6
67 6 ▶ my $timeout = defined $opt{timeout} ? $opt{timeout} : TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'timeout'} ? : 0 6 6
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
BranchTrueFalseTotal
my $store = &$for($tenant) or next 5 0 5
78 5 ▶ my $cache = $store->{cache} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $cache = $store->{"cache"} or next 4 1 5
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
BranchTrueFalseTotal
unless (defined Punk::Observe::Cache::bucket_ns($q)) 2 4 6
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
BranchTrueFalseTotal
if ($budget and $left <= 0) 0 4 4
Condition33%1/3
$budget and $left <= 0
!ll&&!rl&&r
040
MC/DC0%0/2
$budget and $left <= 0
$budget $left <= 0
Truth table33%1/3
A and B
A= $budgetB= $left <= 0
ABresult
0X0
100
111
91 0 ▶ if ($timeout && $secs <= 0) { $tot{stopped} = 'deadline'; return \%tot }
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and $secs <= 0) 0 4 4
Condition33%1/3
$timeout and $secs <= 0
!ll&&!rl&&r
040
MC/DC0%0/2
$timeout and $secs <= 0
$timeout $secs <= 0
Truth table33%1/3
A and B
A= $timeoutB= $secs <= 0
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($r->{"stopped"} eq "budget" or $r->{"stopped"} eq "deadline") 1 3 4
Condition66%2/3
$r->{"stopped"} eq "budget" or $r->{"stopped"} eq "deadline"
l!l&&r!l&&!r
103
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"
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $q->lock("observe.warm", 60, "owner", $owner) 1 1 2
123 unless $q->lock('observe.warm', 60, owner => $owner);
124  
125 1 ▶ my $opt = $st->{warm_opts} || {};
Condition50%1/2
$st->{'warm_opts'} || {}
l!l
10
MC/DC0%0/1
$st->{'warm_opts'} || {}
$st->{'warm_opts'}
Truth table50%1/2
A
A= $st->{'warm_opts'}
Aresult
11
00
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
BranchTrueFalseTotal
if $q->can("unlock") 1 0 1
133 1 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 1 1
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
BranchTrueFalseTotal
unless defined $owner and $owner =~ /\A[0-9]+\z/ 0 0 0
Condition0%0/3
defined $owner and $owner =~ /\A[0-9]+\z/
!ll&&!rl&&r
000
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/
ABresult
0X0
100
111
143 0 ▶ my $lease = $opt{lease_seconds} || 30;
Condition0%0/2
$opt{'lease_seconds'} || 30
l!l
00
MC/DC0%0/1
$opt{'lease_seconds'} || 30
$opt{'lease_seconds'}
Truth table0%0/2
A
A= $opt{'lease_seconds'}
Aresult
11
00
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
BranchTrueFalseTotal
unless $q and $db and $for 0 0 0
Condition0%0/6
$q and $db and $for
!ll&&!rl&&r
000
$q and $db
!ll&&!rl&&r
000
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
ABCresult
0XX0
10X0
1100
1111
148 0 ▶ return 0 unless $q->lock('leader', $lease, owner => $owner);
Branch: 1 branch
BranchTrueFalseTotal
unless $q->lock("leader", $lease, "owner", $owner) 0 0 0
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
BranchTrueFalseTotal
if $err 0 0 0
156 0 ▶ return $out ? $out->{computed} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$out ? : 0 0 0
157 0 };
158 }
159  
160 5 1;
161  
162 __END__
« blib/lib/Punk/Observe/Evaluate.pm ↑ Summary blib/lib/Punk/Observe/Config.pm »