blib/lib/Punk/Plugin/Observe.pm

filter: Statementstmt 61.2% 577 / 942 Branchbran 43.6% 247 / 566 Conditioncond 41.5% 190 / 457 MC/DCmcdc 20.8% 54 / 259 Subroutinesub 48.2% 54 / 112 Podpod 0.0% 0 / 3 totaltotal 47.9% 1122 / 2339 CC 472 SCAR 65.5
CC
472
Cov
52%
_write
4830.0
_page
534.7
_persist
506.0
CRAP 699.1
↑ Summary blib/lib/Punk/Observe/Ingest.pm »
1 package Punk::Plugin::Observe;
2  
3 16 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use Punk::Observe ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Punk::Observe::Ingest ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Punk::Observe::Store ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Punk::Observe::View ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Punk::Observe::Live ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use Punk::Observe::Plot ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Punk::Observe::Segment ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use Punk::Observe::Tenant ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use Punk::Observe::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use Punk::Observe::Query ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use File::Raw::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 16 our $VERSION = $Punk::Observe::VERSION;
23  
24 16 our $INSECURE_ENV = 'PUNK_OBSERVE_INSECURE';
25  
26 16 my %STATE;
27 6 ▶sub state_for { return $STATE{ $_[1] } }
Subroutine state_for: called CC=1 SCAR=0.0
Pod undocumented
28  
29 16 my %ASSET_TYPE = (
30 'observe.css' => 'text/css',
31 'brush.js' => 'application/javascript',
32 'waterfall.js' => 'application/javascript',
33 'flamegraph.js' => 'application/javascript',
34 'livetail.js' => 'application/javascript',
35 'discover.js' => 'application/javascript',
36 'defer.js' => 'application/javascript',
37 'nsmath.js' => 'application/javascript',
38 'plot.js' => 'application/javascript',
39 'plotly.min.js' => 'application/javascript',
40 'moment.min.js' => 'application/javascript',
41 'daterangepicker.js' => 'application/javascript',
42 'daterangepicker.css' => 'text/css',
43 'daterange.js' => 'application/javascript',
44 );
45  
46 20 ▶sub register {
Subroutine register: called CC=10 SCAR=23.1
Pod undocumented
47 20 my ($class, $app, $opts) = @_;
48 20 ▶ $opts = {} unless ref $opts eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 0 20 20
49  
50 Carp::croak(
51 "Punk::Plugin::Observe: a guard is required - "
52 . "plugin 'Observe' => { guard => 'Web::Auth#observe_admin' }. "
53 . "An unguarded mount serves every log line this application has "
54 . "written to anybody who finds the prefix. Set $INSECURE_ENV to mean "
55 . "it.")
56 20 ▶ unless $opts->{guard} or $ENV{$INSECURE_ENV};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"guard"} or $ENV{$Punk::Plugin::Observe::INSECURE_ENV} 1 19 20
Condition100%3/3
$opts->{"guard"} or $ENV{$Punk::Plugin::Observe::INSECURE_ENV}
l!l&&r!l&&!r
1811
MC/DC100%2/2
$opts->{"guard"} or $ENV{$Punk::Plugin::Observe::INSECURE_ENV}
$opts->{"guard"} $ENV{$Punk::Plugin::Observe::INSECURE_ENV}
Truth table100%3/3
A or B
A= $opts->{"guard"}B= $ENV{$Punk::Plugin::Observe::INSECURE_ENV}
ABresult
1X1
011
000
57  
58 19 my $prefix = $opts->{prefix};
59 19 ▶ $prefix = '/observe' unless defined $prefix && length $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix and length $prefix 17 2 19
Condition66%2/3
defined $prefix and length $prefix
!ll&&!rl&&r
1702
MC/DC50%1/2
defined $prefix and length $prefix
defined $prefix length $prefix
Truth table66%2/3
A and B
A= defined $prefixB= length $prefix
ABresult
0X0
100
111
60 19 $prefix =~ s{/\z}{};
61  
62 my $st = {
63 app => $app,
64 opts => $opts,
65 prefix => $prefix,
66 store => $opts->{store},
67 19 tenant => _tenant($opts),
68 ingest => _ingest($opts),
69 limits => _limits($opts),
70 stores => {},
71 };
72 18 $st->{arena} = _arena($st);
73 18 $st->{retain_opts} = _retain($opts);
74 14 $st->{warm_opts} = _warm($opts, $st->{retain_opts});
75 14 $st->{db} = _backend($st);
76 # The alerts option carries two shapes: a seam (a coderef, or a hashref
77 # with read/write/delete or the static rules) and the POLICY hash
78 # (every, group_wait, repeat_interval). The policy is not a seam, and
79 # taken as one it became a reader that read nothing, so the alerts page
80 # said "No alert rules yet" over a store full of them.
81 $st->{seam} = { alerts => _seam(_alerts_policy_only($opts->{alerts}) ? undef : $opts->{alerts}),
82 14 ▶ dashboards => _seam($opts->{dashboards}) };
Branch: 1 branch
BranchTrueFalseTotal
_alerts_policy_only($opts->{'alerts'}) ? : 0 14 14
83 14 $st->{alerts_opts} = _alerts_opts($opts);
84  
85 14 $st->{writable} = _writable($st);
86  
87 14 _register_ui($st);
88 14 _register_ingest($st);
89 14 _register_jobs($st);
90  
91 14 ▶ my $key = ref($app) || $app;
Condition100%3/3
ref $app || $app
l!l&&r!l&&!r
1211
MC/DC100%2/2
ref $app || $app
ref $app $app
Truth table100%3/3
A || B
A= ref $appB= $app
ABresult
1X1
011
000
92 14 ▶ $STATE{$key} = $st if defined $key && length $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and length $key 3 11 14
Condition66%2/3
defined $key and length $key
!ll&&!rl&&r
1103
MC/DC50%1/2
defined $key and length $key
defined $key length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
93 14 return $st;
94 }
95  
96 16 ▶sub _alerts_opts {
Subroutine _alerts_opts: called CC=11 SCAR=24.3
97 16 my ($opts) = @_;
98 my $a = ref $opts->{alerts} eq 'HASH' && !exists $opts->{alerts}{read}
99 16 ▶ ? $opts->{alerts} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'alerts'} eq 'HASH' && !exists $opts->{'alerts'}{'read'} ? : 1 15 16
Condition100%3/3
ref $opts->{"alerts"} eq "HASH" and not exists $opts->{"alerts"}{"read"}
!ll&&!rl&&r
1411
MC/DC100%2/2
ref $opts->{"alerts"} eq "HASH" and not exists $opts->{"alerts"}{"read"}
ref $opts->{"alerts"} eq "HASH" not exists $opts->{"alerts"}{"read"}
Truth table100%3/3
A and not(B)
A= ref $opts->{"alerts"} eq "HASH"B= not exists $opts->{"alerts"}{"read"}
ABresult
0X0
100
111
100 16 my %out = ( group_wait_ns => 30_000_000_000, repeat_ns => 0,
101 every => '@every 30s' );
102 16 ▶ if (defined $a->{group_wait} && length $a->{group_wait}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a->{"group_wait"} and length $a->{"group_wait"}) 1 15 16
Condition66%2/3
defined $a->{"group_wait"} and length $a->{"group_wait"}
!ll&&!rl&&r
1501
MC/DC50%1/2
defined $a->{"group_wait"} and length $a->{"group_wait"}
defined $a->{"group_wait"} length $a->{"group_wait"}
Truth table66%2/3
A and B
A= defined $a->{"group_wait"}B= length $a->{"group_wait"}
ABresult
0X0
100
111
103 1 my ($ns) = Punk::Observe::View::min_duration($a->{group_wait});
104 1 ▶ $out{group_wait_ns} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 1 0 1
105 }
106 16 ▶ if (defined $a->{repeat_interval} && length $a->{repeat_interval}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a->{"repeat_interval"} and length $a->{"repeat_interval"}) 1 15 16
Condition66%2/3
defined $a->{"repeat_interval"} and length $a->{"repeat_interval"}
!ll&&!rl&&r
1501
MC/DC50%1/2
defined $a->{"repeat_interval"} and length $a->{"repeat_interval"}
defined $a->{"repeat_interval"} length $a->{"repeat_interval"}
Truth table66%2/3
A and B
A= defined $a->{"repeat_interval"}B= length $a->{"repeat_interval"}
ABresult
0X0
100
111
107 1 my ($ns) = Punk::Observe::View::min_duration($a->{repeat_interval});
108 1 ▶ $out{repeat_ns} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 1 0 1
109 }
110 16 ▶ $out{every} = $a->{every} if defined $a->{every} && length $a->{every};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a->{"every"} and length $a->{"every"} 1 15 16
Condition66%2/3
defined $a->{"every"} and length $a->{"every"}
!ll&&!rl&&r
1501
MC/DC50%1/2
defined $a->{"every"} and length $a->{"every"}
defined $a->{"every"} length $a->{"every"}
Truth table66%2/3
A and B
A= defined $a->{"every"}B= length $a->{"every"}
ABresult
0X0
100
111
111 16 return \%out;
112 }
113  
114 14 ▶sub _register_jobs {
Subroutine _register_jobs: called CC=16 SCAR=42.8
115 14 my ($st) = @_;
116 14 ▶ return unless $st->{db};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"db"} 12 2 14
117 2 ▶ my $own_alerts = $st->{seam}{alerts} && $st->{seam}{alerts}{read};
Condition33%1/3
$st->{'seam'}{'alerts'} && $st->{'seam'}{'alerts'}{'read'}
!ll&&!rl&&r
002
MC/DC0%0/2
$st->{'seam'}{'alerts'} && $st->{'seam'}{'alerts'}{'read'}
$st->{'seam'}{'alerts'} $st->{'seam'}{'alerts'}{'read'}
Truth table33%1/3
A && B
A= $st->{'seam'}{'alerts'}B= $st->{'seam'}{'alerts'}{'read'}
ABresult
0X0
100
111
118  
119 2 my $app = $st->{app};
120 2 ▶ my $class = ref($app) || $app;
Condition33%1/3
ref $app || $app
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $app || $app
ref $app $app
Truth table33%1/3
A || B
A= ref $appB= $app
ABresult
1X1
011
000
121 2 ▶ my $kw = ($app->can('caller_class') && $app->caller_class) || $class;
Condition33%2/6
$app->can('caller_class') && $app->caller_class || $class
l!l&&r!l&&!r
020
$app->can("caller_class") and $app->caller_class
!ll&&!rl&&r
200
MC/DC0%0/3
$app->can('caller_class') && $app->caller_class || $class
$app->can("caller_class") $app->caller_class $class
Truth table20%1/5
A and B || C
A= $app->can("caller_class")B= $app->caller_classC= $class
ABCresult
11X1
0X11
1011
0X00
1000
122 2 my $task = $kw->can('task');
123 2 my $cron = $kw->can('cron');
124 2 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
unless $task and $cron or $own_alerts 0 2 2
Condition33%2/6
$task and $cron or $own_alerts
l!l&&r!l&&!r
020
$task and $cron
!ll&&!rl&&r
200
MC/DC0%0/3
$task and $cron or $own_alerts
$task $cron $own_alerts
Truth table20%1/5
A and B or C
A= $taskB= $cronC= $own_alerts
ABCresult
11X1
0X11
1011
0X00
1000
125 "plugin 'Observe': alerting needs plugin 'Queue' - "
126 . "`use Punk::Plugin::Queue` and register plugin 'Queue' before "
127 . "plugin 'Observe', or pass an `alerts` seam to keep your own "
128 . "evaluator.") unless $task && $cron || $own_alerts;
129 2 ▶ if ($own_alerts && !($task && $cron)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($own_alerts and not $task && $cron) 2 0 2
Condition33%2/6
$own_alerts and not $task && $cron
!ll&&!rl&&r
002
$task && $cron
!ll&&!rl&&r
200
MC/DC0%0/3
$own_alerts and not $task && $cron
$own_alerts $task $cron
Truth table25%1/4
A and not(B && C)
A= $own_alertsB= $taskC= $cron
ABCresult
0XX0
1110
10X1
1101
130 Carp::croak("plugin 'Observe': retain is configured but there is no "
131 . "queue to schedule it on - register plugin 'Queue', or "
132 . "run retention yourself and drop the option")
133 2 ▶ if $st->{retain_opts};
Branch: 1 branch
BranchTrueFalseTotal
if $st->{"retain_opts"} 0 2 2
134 2 return;
135 }
136  
137 0 ▶ unless ($own_alerts) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($own_alerts) 0 0 0
138 0 $task->('observe.evaluate', '+Punk::Observe::Evaluate#evaluate_job');
139 0 $task->('observe.notify', '+Punk::Observe::Evaluate#notify_job',
140 { attempts => 5 });
141  
142 0 $cron->($st->{alerts_opts}{every}, 'observe.evaluate',
143 { name => 'observe-evaluate', args => [ $class ] });
144 }
145  
146 0 $task->('observe.health', '+Punk::Observe::Health#health_job');
147 0 $cron->('* * * * *', 'observe.health',
148 { name => 'observe-health', args => [ $class ] });
149  
150 0 ▶ if ($st->{retain_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"retain_opts"}) 0 0 0
151 0 $task->('observe.retain', '+Punk::Observe::Retain#retain_job');
152 0 $cron->($st->{retain_opts}{at}, 'observe.retain',
153 { name => 'observe-retain', args => [ $class ] });
154 }
155  
156 0 ▶ if ($st->{warm_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"warm_opts"}) 0 0 0
157 0 $task->('observe.warm', '+Punk::Observe::Warm#warm_job');
158 0 $cron->($st->{warm_opts}{every}, 'observe.warm',
159 { name => 'observe-warm', args => [ $class ] });
160 }
161 0 return 1;
162 }
163  
164 14 ▶sub _backend {
Subroutine _backend: called CC=11 SCAR=27.8
165 14 my ($st) = @_;
166 14 my $db = $st->{opts}{db};
167 14 ▶ return undef if defined $db && !$db;
Branch: 1 branch
BranchTrueFalseTotal
if defined $db and not $db 0 14 14
Condition66%2/3
defined $db and not $db
!ll&&!rl&&r
1310
MC/DC0%0/2
defined $db and not $db
defined $db not $db
Truth table66%2/3
A and not(B)
A= defined $dbB= not $db
ABresult
0X0
100
111
168  
169 14 my %arg;
170 14 ▶ if (ref $db eq 'HASH') { %arg = %$db }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $db eq 'HASH') { } 1 13 14
elsif (ref $db) { } 0 13 13
elsif (defined $db and length $db) { } 0 13 13
171 0 elsif (ref $db) { return $db }
172 0 ▶ elsif (defined $db && length $db) { $arg{dsn} = $db }
Condition33%1/3
defined $db and length $db
!ll&&!rl&&r
1300
MC/DC0%0/2
defined $db and length $db
defined $db length $db
Truth table33%1/3
A and B
A= defined $dbB= length $db
ABresult
0X0
100
111
173 else {
174 13 ▶ return undef unless defined $st->{store} && length $st->{store};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $st->{"store"} and length $st->{"store"} 12 1 13
Condition66%2/3
defined $st->{"store"} and length $st->{"store"}
!ll&&!rl&&r
1201
MC/DC50%1/2
defined $st->{"store"} and length $st->{"store"}
defined $st->{"store"} length $st->{"store"}
Truth table66%2/3
A and B
A= defined $st->{"store"}B= length $st->{"store"}
ABresult
0X0
100
111
175 1 require File::Spec;
176 $arg{dsn} = 'dbi:SQLite:dbname='
177 1 . File::Spec->catfile($st->{store}, 'config.db');
178 }
179  
180 2 my $b = eval {
181 2 require Punk::Observe::Backend;
182 2 my $o = Punk::Observe::Backend->new(%arg);
183 2 $o->migrate;
184 2 $o->disconnect;
185 2 $o;
186 };
187 2 ▶ if (!$b) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($b) 0 2 2
188 0 ▶ my $why = $@ || 'unknown error';
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
189 0 $why =~ s/\s+\z//;
190 0 Carp::carp("Punk::Plugin::Observe: the configuration store is "
191 . "unavailable, so editing is off - $why");
192 0 return undef;
193 }
194 2 return $b;
195 }
196  
197 0 ▶sub _identity {
Subroutine _identity: not called CC=5 SCAR=34.0
198 0 my ($st, $c) = @_;
199 0 ▶ my $cb = $st->{opts}{identity} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $st->{"opts"}{"identity"} or return undef 0 0 0
200 0 my $id = eval { $cb->($c) };
201 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
202 0 my $why = $@; $why =~ s/\s+\z//;
203 0 Carp::carp("Punk::Plugin::Observe: the identity seam died - $why");
204 0 return undef;
205 }
206 0 ▶ return undef unless defined $id && length $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 0 0
Condition0%0/3
defined $id and length $id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table0%0/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
207 0 return substr $id, 0, 200;
208 }
209  
210 32 ▶sub _seam {
Subroutine _seam: called CC=7 SCAR=19.7
211 32 my ($v) = @_;
212 32 ▶ return undef unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 26 6 32
213 6 ▶ return { read => $v } if ref $v eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "CODE" 3 3 6
214 3 ▶ return $v if ref $v eq 'HASH' && ($v->{read} || $v->{write} || $v->{delete});
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "HASH" and $v->{"read"} || $v->{"write"} || $v->{"delete"} 2 1 3
Condition66%6/9
ref $v eq "HASH" and $v->{"read"} || $v->{"write"} || $v->{"delete"}
!ll&&!rl&&r
012
$v->{"read"} or $v->{"write"} or $v->{"delete"}
l!l&&r!l&&!r
201
$v->{"read"} or $v->{"write"}
l!l&&r!l&&!r
201
MC/DC25%1/4
ref $v eq "HASH" and $v->{"read"} || $v->{"write"} || $v->{"delete"}
ref $v eq "HASH" $v->{"read"} $v->{"write"} $v->{"delete"}
Truth table40%2/5
A and B or C or D
A= ref $v eq "HASH"B= $v->{"read"}C= $v->{"write"}D= $v->{"delete"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
215 1 return { read => $v };
216 }
217  
218 # The alerts POLICY hash, as opposed to a seam or static rules: only the
219 # three cadence keys, and none of read, write, delete or rules.
220 23 ▶sub _alerts_policy_only {
Subroutine _alerts_policy_only: called CC=5 SCAR=16.1
221 23 my ($v) = @_;
222 23 ▶ return 0 unless ref $v eq 'HASH' && %$v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "HASH" and %$v 17 6 23
Condition100%3/3
ref $v eq "HASH" and %$v
!ll&&!rl&&r
1616
MC/DC100%2/2
ref $v eq "HASH" and %$v
ref $v eq "HASH" %$v
Truth table100%3/3
A and B
A= ref $v eq "HASH"B= %$v
ABresult
0X0
100
111
223 6 ▶ return 0 if grep { exists $v->{$_} } qw(read write delete rules silences events);
Branch: 1 branch
BranchTrueFalseTotal
if grep {exists $v->{$_};} "read", "write", "delete", "rules", "silences", "events" 3 3 6
224 3 my %policy = map { $_ => 1 } qw(every group_wait repeat_interval);
225 3 ▶ return (grep { !$policy{$_} } keys %$v) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({not $policy{$_};} keys %$v) ? : 1 2 3
226 }
227  
228 0 ▶sub _reader {
Subroutine _reader: not called CC=1 SCAR=6.9
229 0 my ($st, $kind) = @_;
230 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
231 0 my @a = @_;
232 0 ▶ my $req = ref $a[-1] eq 'HASH' ? $a[-1] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $a[-1] eq 'HASH' ? : 0 0 0
233 0 my $t = _request_tenant($st, $req->{c});
234 return $kind eq 'dashboards'
235 ? Punk::Observe::Config::dashboards($st->{db}, $t, $a[0])
236 0 ▶ : Punk::Observe::Config::alerts($st->{db}, $t, $req);
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'dashboards' ? : 0 0 0
237 0 };
238 }
239  
240 14 ▶sub _writable {
Subroutine _writable: called CC=9 SCAR=22.7
241 14 my ($st) = @_;
242 14 my $want = $st->{opts}{writable};
243 14 ▶ return 0 if defined $want && !$want;
Branch: 1 branch
BranchTrueFalseTotal
if defined $want and not $want 0 14 14
Condition33%1/3
defined $want and not $want
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $want and not $want
defined $want not $want
Truth table33%1/3
A and not(B)
A= defined $wantB= not $want
ABresult
0X0
100
111
244 return 0 unless $st->{db} || ($st->{seam}{dashboards}
245 14 ▶ && $st->{seam}{dashboards}{write});
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"db"} or $st->{"seam"}{"dashboards"} and $st->{"seam"}{"dashboards"}{"write"} 11 3 14
Condition83%5/6
$st->{"db"} or $st->{"seam"}{"dashboards"} and $st->{"seam"}{"dashboards"}{"write"}
l!l&&r!l&&!r
2111
$st->{"seam"}{"dashboards"} and $st->{"seam"}{"dashboards"}{"write"}
!ll&&!rl&&r
1101
MC/DC66%2/3
$st->{"db"} or $st->{"seam"}{"dashboards"} and $st->{"seam"}{"dashboards"}{"write"}
$st->{"db"} $st->{"seam"}{"dashboards"} $st->{"seam"}{"dashboards"}{"write"}
Truth table75%3/4
A or B and C
A= $st->{"db"}B= $st->{"seam"}{"dashboards"}C= $st->{"seam"}{"dashboards"}{"write"}
ABCresult
1XX1
0111
00X0
0100
246  
247 3 my $app = $st->{app};
248 3 ▶ my $csrf = eval { ref $app eq 'HASH' ? $app->{csrf} : $app->{csrf} };
Branch: 1 branch
BranchTrueFalseTotal
ref $app eq 'HASH' ? : 0 3 3
249 3 ▶ return 1 if $csrf;
Branch: 1 branch
BranchTrueFalseTotal
if $csrf 1 2 3
250 2 ▶ return 1 if $ENV{$INSECURE_ENV};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{$Punk::Plugin::Observe::INSECURE_ENV} 0 2 2
251 2 return 0;
252 }
253  
254 19 ▶sub _tenant {
Subroutine _tenant: called CC=4 SCAR=14.3
255 19 my ($opts) = @_;
256 19 my $t = $opts->{tenant};
257  
258 19 ▶ return { fixed => 'default', resolver => undef }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 18 1 19
259 unless defined $t;
260 1 ▶ return { fixed => 'default', resolver => $t } if ref $t eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $t eq "CODE" 0 1 1
261  
262 1 my $chk = Punk::Observe::Tenant::check($t);
263 Carp::croak("Punk::Plugin::Observe: tenant '$t' is not usable - "
264 . $chk->{reason})
265 1 ▶ unless $chk->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $chk->{"ok"} 1 0 1
266 0 return { fixed => $t, resolver => undef };
267 }
268  
269 20 ▶sub _ingest {
Subroutine _ingest: called CC=8 SCAR=20.9
270 20 my ($opts) = @_;
271 20 my $i = $opts->{ingest};
272 20 ▶ return undef unless $i;
Branch: 1 branch
BranchTrueFalseTotal
unless $i 16 4 20
273 4 ▶ $i = {} unless ref $i eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $i eq "HASH" 0 4 4
274  
275 4 my $prefix = $i->{prefix};
276 4 ▶ $prefix = '/v1' unless defined $prefix && length $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix and length $prefix 3 1 4
Condition66%2/3
defined $prefix and length $prefix
!ll&&!rl&&r
301
MC/DC50%1/2
defined $prefix and length $prefix
defined $prefix length $prefix
Truth table66%2/3
A and B
A= defined $prefixB= length $prefix
ABresult
0X0
100
111
277 4 $prefix =~ s{/\z}{};
278  
279 4 my %out = (prefix => $prefix, keys => $i->{keys});
280 4 $out{$_} = $i->{$_} for grep { defined $i->{$_} }
281 qw(max_body max_ratio max_records grpc);
282  
283 4 my %known = map { $_ => 1 }
284 qw(prefix keys max_body max_ratio max_records grpc);
285 4 my @bad = sort grep { !$known{$_} } keys %$i;
286 4 ▶ Carp::croak("Punk::Plugin::Observe: unknown ingest option"
Branch: 2 branches
BranchTrueFalseTotal
if @bad 1 3 4
@bad > 1 ? : 0 1 1
287 . (@bad > 1 ? 's' : '') . ": " . join(', ', @bad)
288 . ". Accepted: " . join(', ', sort keys %known))
289 if @bad;
290  
291 3 return \%out;
292 }
293  
294 18 ▶sub _limits {
Subroutine _limits: called CC=6 SCAR=20.3
295 18 my ($opts) = @_;
296 18 my $l = $opts->{limits};
297 18 ▶ $l = {} unless ref $l eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $l eq "HASH" 18 0 18
298 return {
299 rate_records => $l->{rate_records} || 0,
300 rate_bytes => $l->{rate_bytes} || 0,
301 series => defined $l->{series} ? $l->{series} : 1_000_000,
302 series_window => $l->{series_window},
303 storage => $l->{storage} || 0,
304 attributes => $l->{attributes},
305 18 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined $l->{'series'} ? : 0 18 18
Condition50%3/6
$l->{'rate_records'} || 0
l!l
018
$l->{'rate_bytes'} || 0
l!l
018
$l->{'storage'} || 0
l!l
018
MC/DC0%0/1
$l->{'rate_records'} || 0
$l->{'rate_records'}
MC/DC0%0/1
$l->{'rate_bytes'} || 0
$l->{'rate_bytes'}
MC/DC0%0/1
$l->{'storage'} || 0
$l->{'storage'}
Truth table50%1/2
A
A= $l->{'rate_records'}
Aresult
11
00
Truth table50%1/2
A
A= $l->{'rate_bytes'}
Aresult
11
00
Truth table50%1/2
A
A= $l->{'storage'}
Aresult
11
00
306 }
307  
308 23 ▶sub _retain {
Subroutine _retain: called CC=13 SCAR=25.7
309 23 my ($opts) = @_;
310 23 my $r = $opts->{retain};
311 23 ▶ return undef unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 11 12 23
312 12 ▶ $r = { keep => $r } unless ref $r;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r 0 12 12
313 Carp::croak("Punk::Plugin::Observe: retain needs keep => '7d' (or 30d, "
314 12 ▶ . "12h ...)") unless defined $r->{keep};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"keep"} 1 11 12
315 11 require Punk::Observe::Retain;
316 11 my $ns = Punk::Observe::Retain::parse_keep($r->{keep});
317 11 ▶ Carp::croak("Punk::Plugin::Observe: retain keep '$r->{keep}' is not a "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 2 9 11
318 . "window - a number and a unit, as in 7d")
319 unless defined $ns;
320 9 ▶ my $at = defined $r->{at} ? $r->{at} : '17 * * * *';
Branch: 1 branch
BranchTrueFalseTotal
defined $r->{'at'} ? : 2 7 9
321 9 my ($at_ok, $at_why);
322 {
323 9 local $@;
324 9 $at_ok = eval { require Punk::Queue::Cron;
325 9 Punk::Queue::Cron->check($at); 1 };
326 9 $at_why = $@;
327 }
328 9 ▶ Carp::croak("Punk::Plugin::Observe: retain at '$at' is not "
Branch: 1 branch
BranchTrueFalseTotal
unless $at_ok 1 8 9
329 . "a cron expression: $at_why") unless $at_ok;
330 8 my $bytes;
331 8 ▶ if (defined $r->{bytes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{"bytes"}) 4 4 8
332 4 my %mult = (k => 1024, m => 1024**2, g => 1024**3, t => 1024**4);
333 4 ▶ if ($r->{bytes} =~ /\A\s*(\d+(?:\.\d+)?)\s*(?:([kmgt])i?b?|b)?\s*\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"bytes"} =~ /\A\s*(\d+(?:\.\d+)?)\s*(?:([kmgt])i?b?|b)?\s*\z/i) 3 1 4
334 3 my ($n, $u) = ($1, $2);
335 3 ▶ $bytes = sprintf '%.0f', $n * ($u ? $mult{lc $u} : 1);
Branch: 1 branch
BranchTrueFalseTotal
$u ? : 2 1 3
336 }
337 4 ▶ Carp::croak("Punk::Plugin::Observe: retain bytes '$r->{bytes}' is "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and $bytes > 0 1 3 4
Condition66%2/3
defined $bytes and $bytes > 0
!ll&&!rl&&r
103
MC/DC50%1/2
defined $bytes and $bytes > 0
defined $bytes $bytes > 0
Truth table66%2/3
A and B
A= defined $bytesB= $bytes > 0
ABresult
0X0
100
111
338 . "not a size - a number and a unit, as in 500M or 2G")
339 unless defined $bytes && $bytes > 0;
340 }
341  
342 7 ▶ return { keep => $r->{keep}, keep_ns => $ns, at => $at,
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes ? : 3 4 7
343 (defined $bytes ? (bytes => $bytes) : ()) };
344 }
345  
346 # THE WARMER, ON UNLESS TURNED OFF, for the same reason the cache it fills is:
347 # a dashboard that is only fast for the people who read the documentation is a
348 # dashboard that is slow. It needs the cache, so `cache => 0` turns it off too
349 # rather than leaving a job that can only ever compute and discard.
350 14 ▶sub _warm {
Subroutine _warm: called CC=23 SCAR=41.9
351 14 my ($opts, $retain) = @_;
352 14 ▶ my $w = exists $opts->{warm} ? $opts->{warm} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'warm'} ? : 0 14 14
353 14 ▶ return undef unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 0 14 14
354 14 ▶ return undef if exists $opts->{cache} && !$opts->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"cache"} and not $opts->{"cache"} 0 14 14
Condition33%1/3
exists $opts->{"cache"} and not $opts->{"cache"}
!ll&&!rl&&r
1400
MC/DC0%0/2
exists $opts->{"cache"} and not $opts->{"cache"}
exists $opts->{"cache"} not $opts->{"cache"}
Truth table33%1/3
A and not(B)
A= exists $opts->{"cache"}B= not $opts->{"cache"}
ABresult
0X0
100
111
355 14 ▶ $w = {} unless ref $w eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $w eq "HASH" 0 14 14
356  
357 14 require Punk::Observe::Retain;
358 14 require Punk::Observe::Warm;
359 14 my %out;
360 14 my %window = (depth => 'depth_ns', refresh => 'refresh_ns');
361 14 for my $k (sort keys %window) {
362 28 ▶ next unless defined $w->{$k} && length $w->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w->{$k} and length $w->{$k} 28 0 28
Condition33%1/3
defined $w->{$k} and length $w->{$k}
!ll&&!rl&&r
2800
MC/DC0%0/2
defined $w->{$k} and length $w->{$k}
defined $w->{$k} length $w->{$k}
Truth table33%1/3
A and B
A= defined $w->{$k}B= length $w->{$k}
ABresult
0X0
100
111
363 0 my $ns = Punk::Observe::Retain::parse_keep($w->{$k});
364 0 ▶ Carp::croak("Punk::Plugin::Observe: warm $k '$w->{$k}' is not a "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 0 0 0
365 . "window - a number and a unit, as in 7d")
366 unless defined $ns;
367 0 $out{ $window{$k} } = $ns;
368 }
369 14 ▶ if (defined $w->{ttl} && length $w->{ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $w->{"ttl"} and length $w->{"ttl"}) 0 14 14
Condition33%1/3
defined $w->{"ttl"} and length $w->{"ttl"}
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $w->{"ttl"} and length $w->{"ttl"}
defined $w->{"ttl"} length $w->{"ttl"}
Truth table33%1/3
A and B
A= defined $w->{"ttl"}B= length $w->{"ttl"}
ABresult
0X0
100
111
370 0 my $ns = Punk::Observe::Retain::parse_keep($w->{ttl});
371 0 ▶ Carp::croak("Punk::Plugin::Observe: warm ttl '$w->{ttl}' is not a "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 0 0 0
372 . "window - a number and a unit, as in 8d")
373 unless defined $ns;
374 0 $out{ttl} = int($ns / 1_000_000_000);
375 }
376 14 for my $k (qw(budget timeout)) {
377 28 ▶ next unless defined $w->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w->{$k} 28 0 28
378 Carp::croak("Punk::Plugin::Observe: warm $k must be a positive number")
379 0 ▶ unless $w->{$k} =~ /\A\d+(?:\.\d+)?\z/ && $w->{$k} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->{$k} =~ /\A\d+(?:\.\d+)?\z/ and $w->{$k} > 0 0 0 0
Condition0%0/3
$w->{$k} =~ /\A\d+(?:\.\d+)?\z/ and $w->{$k} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$w->{$k} =~ /\A\d+(?:\.\d+)?\z/ and $w->{$k} > 0
$w->{$k} =~ /\A\d+(?:\.\d+)?\z/ $w->{$k} > 0
Truth table0%0/3
A and B
A= $w->{$k} =~ /\A\d+(?:\.\d+)?\z/B= $w->{$k} > 0
ABresult
0X0
100
111
380 0 $out{$k} = $w->{$k};
381 }
382  
383 # WARMING PAST THE HORIZON IS WORK OVER DATA THAT IS GONE. Retention
384 # deletes segments; a chunk covering a window with nothing left in it is
385 # computed, stored and never right again.
386 14 ▶ if ($retain && $retain->{keep_ns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retain and $retain->{"keep_ns"}) 3 11 14
Condition66%2/3
$retain and $retain->{"keep_ns"}
!ll&&!rl&&r
1103
MC/DC50%1/2
$retain and $retain->{"keep_ns"}
$retain $retain->{"keep_ns"}
Truth table66%2/3
A and B
A= $retainB= $retain->{"keep_ns"}
ABresult
0X0
100
111
387 3 ▶ my $depth = $out{depth_ns} || Punk::Observe::Warm::DEPTH_NS();
Condition33%1/3
$out{'depth_ns'} || &Punk::Observe::Warm::DEPTH_NS()
l!l&&r!l&&!r
030
MC/DC0%0/2
$out{'depth_ns'} || &Punk::Observe::Warm::DEPTH_NS()
$out{'depth_ns'} &Punk::Observe::Warm::DEPTH_NS()
Truth table33%1/3
A || B
A= $out{'depth_ns'}B= &Punk::Observe::Warm::DEPTH_NS()
ABresult
1X1
011
000
388 3 ▶ $out{depth_ns} = $retain->{keep_ns} if $retain->{keep_ns} < $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $retain->{"keep_ns"} < $depth 0 3 3
389 }
390  
391 14 ▶ my $every = defined $w->{every} ? $w->{every} : '@every 5m';
Branch: 1 branch
BranchTrueFalseTotal
defined $w->{'every'} ? : 0 14 14
392 14 my ($every_ok, $every_why);
393 {
394 14 local $@;
395 14 $every_ok = eval { require Punk::Queue::Cron;
396 14 Punk::Queue::Cron->check($every); 1 };
397 14 $every_why = $@;
398 }
399 14 ▶ Carp::croak("Punk::Plugin::Observe: warm every '$every' is "
Branch: 1 branch
BranchTrueFalseTotal
unless $every_ok 0 14 14
400 . "not a cron expression: $every_why") unless $every_ok;
401 14 $out{every} = $every;
402 14 return \%out;
403 }
404  
405 22 ▶sub _arena {
Subroutine _arena: called CC=8 SCAR=27.2
406 22 my ($st) = @_;
407 22 ▶ my $h = eval { Punk::Observe::Segment::shm_new($st->{limits}{series} || 0) };
Condition100%2/2
$st->{'limits'}{'series'} || 0
l!l
202
MC/DC100%1/1
$st->{'limits'}{'series'} || 0
$st->{'limits'}{'series'}
Truth table100%2/2
A
A= $st->{'limits'}{'series'}
Aresult
11
00
408 22 ▶ return undef unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 0 22 22
409 22 ▶ if (my $w = $st->{limits}{series_window}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = $st->{"limits"}{"series_window"}) 0 22 22
410 0 require Punk::Observe::Retain;
411 0 ▶ my $ns = $w =~ /\A\d+\z/ ? $w : Punk::Observe::Retain::parse_keep($w);
Branch: 1 branch
BranchTrueFalseTotal
$w =~ /\A\d+\z/ ? : 0 0 0
412 0 ▶ Carp::croak("Punk::Plugin::Observe: limits.series_window '$w' is not "
Branch: 1 branch
BranchTrueFalseTotal
unless $ns 0 0 0
413 . "a window - a number and a unit, as in 24h") unless $ns;
414 0 Punk::Observe::Segment::shm_window($h, $ns);
415 }
416 22 my $ok = eval { Punk::Observe::Segment::shm_stats($h) };
417 22 ▶ return { handle => $h, shared => ($ok && $ok->{shared}) ? 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
$ok && $ok->{'shared'} ? : 22 0 22
Condition33%1/3
$ok and $ok->{"shared"}
!ll&&!rl&&r
0022
MC/DC0%0/2
$ok and $ok->{"shared"}
$ok $ok->{"shared"}
Truth table33%1/3
A and B
A= $okB= $ok->{"shared"}
ABresult
0X0
100
111
418 }
419  
420 14 ▶sub _resolve_guard {
Subroutine _resolve_guard: called CC=6 SCAR=27.7
421 14 my ($st) = @_;
422 14 my $g = $st->{opts}{guard};
423 0 ▶ return sub { return } unless $g;
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless $g 1 13 14
424 13 ▶ return $g if ref $g eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $g eq "CODE" 13 0 13
425 0 my ($ctrl, $action) = split /#/, $g, 2;
426 0 ▶ Carp::croak("Punk::Plugin::Observe: guard '$g' is not Controller#action")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctrl and defined $action and length $action 0 0 0
Condition0%0/6
defined $ctrl and defined $action and length $action
!ll&&!rl&&r
000
defined $ctrl and defined $action
!ll&&!rl&&r
000
MC/DC0%0/3
defined $ctrl and defined $action and length $action
defined $ctrl defined $action length $action
Truth table0%0/4
A and B and C
A= defined $ctrlB= defined $actionC= length $action
ABCresult
0XX0
10X0
1100
1111
427 unless defined $ctrl && defined $action && length $action;
428 0 return $g;
429 }
430  
431 14 ▶sub _register_ui {
Subroutine _register_ui: called CC=5 SCAR=20.2
432 14 my ($st) = @_;
433 14 my $app = $st->{app};
434 14 my $guard = _resolve_guard($st);
435 14 ▶ return unless $app && $app->can('under');
Branch: 1 branch
BranchTrueFalseTotal
unless $app and $app->can("under") 13 1 14
Condition100%3/3
$app and $app->can("under")
!ll&&!rl&&r
1121
MC/DC100%2/2
$app and $app->can("under")
$app $app->can("under")
Truth table100%3/3
A and B
A= $appB= $app->can("under")
ABresult
0X0
100
111
436 1 my $scope = $app->under($st->{prefix} => $guard);
437 1 $st->{scope} = $scope;
438  
439 1 _build_views($st);
440 0 ▶ $scope->get('/' => sub { _page($st, 'status', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
441 0 ▶ $scope->get('/status' => sub { _page($st, 'status', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
442 0 ▶ $scope->get('/logs' => sub { _page($st, 'logs', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
443 0 ▶ $scope->get('/metrics' => sub { _page($st, 'metrics', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
444 0 ▶ $scope->get('/map' => sub { _page($st, 'map', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
445 0 ▶ $scope->get('/traces' => sub { _page($st, 'trace', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
446 0 ▶ $scope->get('/explore' => sub { _page($st, 'explore', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
447 0 ▶ $scope->get('/alerts' => sub { _page($st, 'alerts', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
448 0 ▶ $scope->get('/help' => sub { _page($st, 'help', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
449 0 ▶ $scope->get('/alerts/new' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
450 0 _page($st, 'alerts', $_[0], { template => 'alertedit', editing => 1,
451 creating => 1,
452 _alert_form_vars({}) });
453 1 });
454 0 ▶ $scope->get('/alerts/:id' => sub { _page($st, 'alerts', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
455 0 ▶ $scope->get('/alerts/:id/edit' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
456 0 my ($c) = @_;
457 my $rule = $st->{db}
458 ? Punk::Observe::Config::alert($st->{db},
459 0 ▶ _request_tenant($st, $c), scalar eval { $c->param('id') })
Branch: 1 branch
BranchTrueFalseTotal
$st->{'db'} ? : 0 0 0
460 : undef;
461 0 ▶ return $c->status(404)->text("no such rule\n") unless $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule 0 0 0
462 0 _page($st, 'alerts', $c, { template => 'alertedit', editing => 1,
463 _alert_form_vars($rule) });
464 1 });
465 0 ▶ $scope->get('/dashboards' => sub { _page($st, 'dashboard', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
466 0 ▶ $scope->get('/dashboards/new' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
467 0 _page($st, 'dashboard', $_[0], { template => 'dashedit', editing => 1,
468 creating => 1 });
469 1 });
470 0 ▶ $scope->get('/dashboards/:slug' => sub { _page($st, 'dashboard', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
471 0 ▶ $scope->get('/health' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
472 0 _page($st, 'status', $_[0], { template => 'health',
473 heading => 'Health',
474 here_status => 0, here_home => 0,
475 here_health => 1 });
476 1 });
477 0 ▶ $scope->get('/status.slow' => sub { _slow_panel($st, $_[0], 'status') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
478 0 ▶ $scope->get('/health.slow' => sub { _slow_panel($st, $_[0], 'health') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
479 0 ▶ $scope->get('/dashboards/:slug/panels/:key/slow' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
480 0 _dash_panel($st, $_[0]);
481 1 });
482 0 ▶ $scope->get('/health-targets/edit' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
483 0 _page($st, 'status', $_[0], { template => 'healthedit',
484 heading => 'Health targets',
485 here_status => 0, here_home => 0,
486 here_health => 1 });
487 1 });
488 0 ▶ $scope->get('/dashboards/:slug/edit' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
489 0 _page($st, 'dashboard', $_[0], { template => 'dashedit', editing => 1 });
490 1 });
491 1 ▶ if ($st->{writable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"writable"}) 1 0 1
492 0 ▶ $scope->post('/dashboards' => sub { _write($st, 'dashboard', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
493 $scope->post('/dashboards/:slug'
494 0 ▶ => sub { _write($st, 'dashboard', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
495 $scope->post('/dashboards/:slug/delete'
496 0 ▶ => sub { _write($st, 'dashboard_delete', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
497 $scope->post('/dashboards/:slug/panels'
498 0 ▶ => sub { _write($st, 'panel', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
499 $scope->post('/dashboards/:slug/panels/save'
500 0 ▶ => sub { _write($st, 'panels_save', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
501 $scope->post('/dashboards/:slug/panels/:id'
502 0 ▶ => sub { _write($st, 'panel', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
503 $scope->post('/dashboards/:slug/panels/:id/delete'
504 0 ▶ => sub { _write($st, 'panel_delete', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
505  
506 0 ▶ $scope->post('/views' => sub { _write($st, 'view', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
507 0 ▶ $scope->post('/views/:id/delete' => sub { _write($st, 'view_delete', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
508  
509 0 ▶ $scope->post('/alerts' => sub { _write($st, 'alert', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
510 0 ▶ $scope->post('/alerts/:id' => sub { _write($st, 'alert', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
511 0 ▶ $scope->post('/alerts/:id/delete' => sub { _write($st, 'alert_delete', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
512 0 ▶ $scope->post('/alerts/:id/silence' => sub { _write($st, 'silence', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
513 $scope->post('/alerts/silences/:id/delete'
514 0 ▶ => sub { _write($st, 'silence_delete', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
515  
516 $scope->post('/health-targets'
517 0 ▶ => sub { _write($st, 'health_target', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
518 $scope->post('/health-targets/:name/delete'
519 0 ▶ => sub { _write($st, 'health_target_delete', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
520 }
521  
522 0 ▶ $scope->get('/logs/stream' => sub { _stream($st, $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
523 0 ▶ $scope->get('/logs/:id' => sub { _page($st, 'record', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
524 0 ▶ $scope->get('/traces/:trace' => sub { _page($st, 'trace', $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
525 1 for my $asset (sort keys %ASSET_TYPE) {
526 0 ▶ $scope->get("/assets/$asset" => sub { _asset($st, $_[0], $asset) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
527 }
528 0 ▶ $scope->get('/assets/favicon.svg' => sub { _favicon($st, $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
529 1 return $scope;
530 }
531  
532 1 ▶sub _root_dir {
Subroutine _root_dir: called CC=5 SCAR=16.6
533 1 ▶ my $pm = $INC{'Punk/Plugin/Observe.pm'} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $pm = $INC{"Punk/Plugin/Observe.pm"} or return undef 1 0 1
534 1 my $dir = File::Basename::dirname($pm);
535 1 my @candidates = (File::Spec->catdir($dir, File::Spec->updir,
536 'Observe', 'root'));
537 1 my $up = $dir;
538 1 for (1 .. 5) {
539 5 $up = File::Spec->catdir($up, File::Spec->updir);
540 5 push @candidates, File::Spec->catdir($up, 'root');
541 }
542 1 for my $c (@candidates) {
543 1 ▶ return $c if -d File::Spec->catdir($c, 'templates');
Branch: 1 branch
BranchTrueFalseTotal
if -d "File::Spec"->catdir($c, "templates") 1 0 1
544 }
545 0 return undef;
546 }
547  
548 1 ▶sub _build_views {
Subroutine _build_views: called CC=4 SCAR=14.8
549 1 my ($st) = @_;
550 1 ▶ my $root = $st->{opts}{root} || _root_dir();
Condition33%1/3
$st->{'opts'}{'root'} || _root_dir()
l!l&&r!l&&!r
010
MC/DC0%0/2
$st->{'opts'}{'root'} || _root_dir()
$st->{'opts'}{'root'} _root_dir()
Truth table33%1/3
A || B
A= $st->{'opts'}{'root'}B= _root_dir()
ABresult
1X1
011
000
551 1 $st->{root} = $root;
552 1 ▶ return unless $root && eval { require Template::Stencil; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless $root and eval { do { require Template::Stencil; 1 } } 0 1 1
Condition33%1/3
$root and eval { do { require Template::Stencil; 1 } }
!ll&&!rl&&r
001
MC/DC0%0/2
$root and eval { do { require Template::Stencil; 1 } }
$root eval { do { require Template::Stencil; 1 } }
Truth table33%1/3
A and B
A= $rootB= eval { do { require Template::Stencil; 1 } }
ABresult
0X0
100
111
553 1 $st->{stencil} = Template::Stencil->new({
554 template_dir => File::Spec->catdir($root, 'templates'),
555 wrapper => 'layout.tmpl',
556 });
557 1 $st->{fragment} = Template::Stencil->new({
558 template_dir => File::Spec->catdir($root, 'templates'),
559 });
560 1 return $st->{stencil};
561 }
562  
563 11 ▶sub _fragment_html {
Subroutine _fragment_html: called CC=6 SCAR=18.2
564 11 my ($st, $tmpl, $vars) = @_;
565 11 ▶ return undef unless $st->{fragment};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"fragment"} 3 8 11
566 8 my $html = eval { $st->{fragment}->render($tmpl, $vars) };
567 8 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
568 8 ▶ return undef if defined $html && $html =~ /\A\s*\Q$tmpl\E\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $html and $html =~ /\A\s*\Q$tmpl\E\s*\z/ 1 7 8
Condition66%2/3
defined $html and $html =~ /\A\s*\Q$tmpl\E\s*\z/
!ll&&!rl&&r
071
MC/DC50%1/2
defined $html and $html =~ /\A\s*\Q$tmpl\E\s*\z/
defined $html $html =~ /\A\s*\Q$tmpl\E\s*\z/
Truth table66%2/3
A and B
A= defined $htmlB= $html =~ /\A\s*\Q$tmpl\E\s*\z/
ABresult
0X0
100
111
569 7 ▶ utf8::decode($html) if defined $html;
Branch: 1 branch
BranchTrueFalseTotal
if defined $html 7 0 7
570 7 return $html;
571 }
572  
573 5 ▶sub _ingest_vars {
Subroutine _ingest_vars: called CC=2 SCAR=7.0
574 5 my ($store, $from, $to) = @_;
575 5 my $fig = eval { Punk::Observe::Plot::ingest_figure($store, $from, $to) };
576 5 ▶ return $fig ? (ingest_plot => $fig) : ();
Branch: 1 branch
BranchTrueFalseTotal
$fig ? : 5 0 5
577 }
578  
579 2 ▶sub _health_vars {
Subroutine _health_vars: called CC=15 SCAR=53.4
580 2 my ($st, $store, $from, $to) = @_;
581 2 ▶ return () unless $st->{db};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"db"} 2 0 2
582 0 my $h = eval {
583 0 require Punk::Observe::Health;
584 Punk::Observe::Health::page_vars(
585 db => $st->{db}, store => $store,
586 0 tenant => $st->{tenant}{fixed});
587 };
588 0 ▶ return () unless ref $h eq 'ARRAY' && @$h;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "ARRAY" and @$h 0 0 0
Condition0%0/3
ref $h eq "ARRAY" and @$h
!ll&&!rl&&r
000
MC/DC0%0/2
ref $h eq "ARRAY" and @$h
ref $h eq "ARRAY" @$h
Truth table0%0/3
A and B
A= ref $h eq "ARRAY"B= @$h
ABresult
0X0
100
111
589  
590 0 ▶ my $dur = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
591 0 my ($ns, $capped) = @_;
592 0 ▶ my $s = Punk::Observe::View::fmt_dur($ns || 0);
Condition0%0/2
$ns || 0
l!l
00
MC/DC0%0/1
$ns || 0
$ns
Truth table0%0/2
A
A= $ns
Aresult
11
00
593 0 ▶ return $capped ? "over $s" : $s;
Branch: 1 branch
BranchTrueFalseTotal
$capped ? : 0 0 0
594 0 };
595 0 for my $t (@$h) {
596 0 $t->{held_s} = $dur->($t->{held}, $t->{held_min});
597 0 ▶ $t->{row_class} = $t->{ok} ? '' : 'row-error';
Branch: 1 branch
BranchTrueFalseTotal
$t->{'ok'} ? : 0 0 0
598 0 for my $c (@{ $t->{checks} }) {
599 0 $c->{held_s} = $dur->($c->{held}, $c->{held_min});
600 0 ▶ $c->{row_class} = $c->{ok} ? '' : 'row-error';
Branch: 1 branch
BranchTrueFalseTotal
$c->{'ok'} ? : 0 0 0
601 }
602 }
603 0 my %out = (health => $h);
604  
605 0 ▶ if (defined $from && defined $to && $store) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from and defined $to and $store) 0 0 0
Condition0%0/6
defined $from and defined $to and $store
!ll&&!rl&&r
000
defined $from and defined $to
!ll&&!rl&&r
000
MC/DC0%0/3
defined $from and defined $to and $store
defined $from defined $to $store
Truth table0%0/4
A and B and C
A= defined $fromB= defined $toC= $store
ABCresult
0XX0
10X0
1100
1111
606 0 my $ev = eval { Punk::Observe::Health::uptime_events(
607 store => $store, from => $from, to => $to) };
608 0 ▶ if (ref $ev eq 'ARRAY' && @$ev) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ev eq "ARRAY" and @$ev) 0 0 0
Condition0%0/3
ref $ev eq "ARRAY" and @$ev
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ev eq "ARRAY" and @$ev
ref $ev eq "ARRAY" @$ev
Truth table0%0/3
A and B
A= ref $ev eq "ARRAY"B= @$ev
ABresult
0X0
100
111
609 0 my $j = eval { Punk::Observe::Plot::timeline_figure(
610 { events => $ev, to => $to }) };
611 0 ▶ if (defined $j && length $j) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $j and length $j) 0 0 0
Condition0%0/3
defined $j and length $j
!ll&&!rl&&r
000
MC/DC0%0/2
defined $j and length $j
defined $j length $j
Truth table0%0/3
A and B
A= defined $jB= length $j
ABresult
0X0
100
111
612 0 utf8::decode($j);
613 0 $out{health_up_plot} = $j;
614 }
615 }
616 }
617 0 return %out;
618 }
619  
620 9 ▶sub _status_window {
Subroutine _status_window: called CC=3 SCAR=14.2
621 9 my ($req) = @_;
622 9 my ($from, $to) = Punk::Observe::View::window($req);
623 9 ▶ if (!defined $from || !defined $to) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from and defined $to) 0 9 9
Condition33%1/3
defined $from and defined $to
!ll&&!rl&&r
009
MC/DC0%0/2
defined $from and defined $to
defined $from defined $to
Truth table33%1/3
A and B
A= defined $fromB= defined $to
ABresult
0X0
100
111
624 0 my $now = Punk::Observe::now_ns();
625 0 $from = Punk::Observe::Store::nsub($now, 3_600 * 1_000_000_000);
626 0 $to = $now;
627 }
628 9 return ($from, $to);
629 }
630  
631 3 ▶sub _dash_panel {
Subroutine _dash_panel: called CC=4 SCAR=13.9
632 3 my ($st, $c) = @_;
633 3 my $store = store_for($st, _request_tenant($st, $c));
634 3 my $req = _params($st, $c);
635 3 $req->{panel} = eval { $c->param('key') };
636  
637 3 my $panel = eval { Punk::Observe::View->_panel($store, $req) };
638 3 ▶ return $c->status(500)->text("panel build failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
639 3 ▶ return $c->status(404)->text("that panel is not on this dashboard\n")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $panel eq "HASH" 1 2 3
640 unless ref $panel eq 'HASH';
641  
642 2 my ($w_from, $w_to) = _status_window($req);
643 my $html = _fragment_html($st, q{panelslow.tmpl},
644 { p => $panel, prefix => $st->{prefix},
645 2 from => $w_from, to => $w_to,
646 range_amp => _range_qs($req, q{&}) });
647 2 ▶ return $c->status(500)->text('fragment render failed')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 1 1 2
648 unless defined $html;
649 1 $c->header('Cache-Control' => 'no-cache');
650 1 return $c->html($html);
651 }
652  
653 2 ▶sub _slow_panel {
Subroutine _slow_panel: called CC=4 SCAR=13.9
654 2 my ($st, $c, $which) = @_;
655 2 my $store = store_for($st, _request_tenant($st, $c));
656 2 my $req = _params($st, $c);
657 my %vars = (prefix => $st->{prefix},
658 writable => $st->{writable},
659 2 range_qs => _range_qs($req, '?'),
660 range_amp => _range_qs($req, '&'));
661 2 ▶ if ($which eq 'status') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq 'status') { } 1 1 2
662 1 my ($from, $to) = _status_window($req);
663 1 %vars = (%vars, _ingest_vars($store, $from, $to));
664 }
665 else {
666 1 my ($from, $to) = _status_window($req);
667 1 %vars = (%vars, _health_vars($st, $store, $from, $to));
668 }
669 2 my $html = _fragment_html($st, "${which}slow.tmpl", \%vars);
670 2 ▶ return $c->status(500)->text('fragment render failed')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 2 2
671 unless defined $html;
672 2 $c->header('Cache-Control' => 'no-cache');
673 2 ▶ utf8::encode($html) if utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($html) 0 2 2
674 2 return $c->html($html);
675 }
676  
677 15 ▶sub _empty {
Subroutine _empty: called CC=2 SCAR=7.2
678 15 my ($st, $c) = @_;
679 return (
680 prefix => $st->{prefix}, query => '', title => '', heading => '',
681 theme => '', toolbar => '', width => 720, height => 220,
682 root_name => '', span_count => 0, duration_ms => 0, orphans => 0,
683 cycles => 0, spans => [], rows => [], series => [], nodes => [],
684 edges => [], yticks => [], back_edges => 0, error => '', hint => '',
685 refusal => '', truncated => 0, scanned => 0, tail => 0,
686 query_esc => '', from => '0', to => '0',
687 rules => [], silences => [], broken => 0, panels => [], cols => 2,
688 slug => '',
689 ingest_rate => 0, wal_depth => 0, segments => 0, compaction_lag => 0,
690 series_used => 0, series_dropping => 0, overflow_records => 0,
691 live_gaps => 0, older_cursor => '', paged => 0, next_cursor => '',
692 15 ▶ series_cap => $st->{limits}{series} || 0,
Condition50%1/2
$st->{'limits'}{'series'} || 0
l!l
015
MC/DC0%0/1
$st->{'limits'}{'series'} || 0
$st->{'limits'}{'series'}
Truth table50%1/2
A
A= $st->{'limits'}{'series'}
Aresult
11
00
693 health => [],
694 accepted => '0', accepted_bytes => '0 B',
695 rate_rejected => '0', counters_shared => 1,
696  
697 groups => [], names => [], examples => [], traces => [], flame => [],
698 attrs => [], context => [], services => [], columns => [],
699 record => {}, found => 0, empty => 0, degraded => 0, exact => 1,
700 offset => 0, shape => 'rows', total => 0, errors => 0,
701 has_severity => 0, has_duration => 0, has_value => 0, has_trace => 0,
702 errors_only => 0, min_ms => '', trace => '', flame_height => 0,
703 logs => 0, metrics => 0, traces_seen => 0, store_bytes => '0 B',
704 here_home => 0, here_map => 0, here_traces => 0, here_logs => 0,
705 here_dashboard => 0,
706 here_metrics => 0, here_explore => 0, here_alerts => 0,
707 here_status => 0,
708 range => '1h', range_all => 0, range_custom => 0, ranges => [],
709 wants_range => 0, range_qs => '', range_amp => '',
710 writable => 0, csrf_field => '', editing => 0, creating => 0,
711 saved_views => [], save_page => '', save_path => '',
712 sources => [], aggregates => [], severities => [], units => [],
713 attr_keys => [], attrs_truncated => 0, attrs_sampled => 0,
714 columns_source => '',
715 operators => [],
716 field => '', configured => 0, can_edit => 0, list => [],
717 );
718 }
719  
720 19 ▶sub store_for {
Subroutine store_for: called CC=6 SCAR=21.4
Pod undocumented
721 19 my ($st, $tenant) = @_;
722 19 ▶ return undef unless defined $st->{store} && length $st->{store};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $st->{"store"} and length $st->{"store"} 0 19 19
Condition33%1/3
defined $st->{"store"} and length $st->{"store"}
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $st->{"store"} and length $st->{"store"}
defined $st->{"store"} length $st->{"store"}
Truth table33%1/3
A and B
A= defined $st->{"store"}B= length $st->{"store"}
ABresult
0X0
100
111
723 19 ▶ $tenant = 'default' unless defined $tenant && length $tenant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tenant and length $tenant 0 19 19
Condition33%1/3
defined $tenant and length $tenant
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $tenant and length $tenant
defined $tenant length $tenant
Truth table33%1/3
A and B
A= defined $tenantB= length $tenant
ABresult
0X0
100
111
724 return $st->{stores}{$tenant} ||= Punk::Observe::Store->new(
725 dir => $st->{store},
726 tenant => $tenant,
727 seal_bytes => $st->{opts}{seal_bytes},
728 max_rows => $st->{opts}{max_rows},
729 19 ▶ cache => _query_cache($st),
Condition66%2/3
$st->{"stores"}{$tenant} ||= "Punk::Observe::Store"->new("dir", $st->{"store"}, "tenant", $tenant, "seal_bytes", $st->{"opts"}{"seal_bytes"}, "max_rows", $st->{"opts"}{"max_rows"}, "cache", _query_cache($st))
l!l&&r!l&&!r
1630
MC/DC0%0/2
$st->{"stores"}{$tenant} ||= "Punk::Observe::Store"->new("dir", $st->{"store"}, "tenant", $tenant, "seal_bytes", $st->{"opts"}{"seal_bytes"}, "max_rows", $st->{"opts"}{"max_rows"}, "cache", _query_cache($st))
$st->{"stores"}{$tenant} "Punk::Observe::Store"->new("dir", $st->{"store"}, "tenant", $tenant, "seal_bytes", $st->{"opts"}{"seal_bytes"}, "max_rows", $st->{"opts"}{"max_rows"}, "cache", _query_cache($st))
Truth table66%2/3
A ||= B
A= $st->{"stores"}{$tenant}B= "Punk::Observe::Store"->new("dir", $st->{"store"}, "tenant", $tenant, "seal_bytes", $st->{"opts"}{"seal_bytes"}, "max_rows", $st->{"opts"}{"max_rows"}, "cache", _query_cache($st))
ABresult
1X1
011
000
730 );
731 }
732  
733 # THE CHUNK CACHE, BUILT ONCE PER WORKER AND SHARED BY EVERY TENANT'S STORE.
734 #
735 # A file store, because the point is that five workers rendering the same
736 # dashboard compute a chunk once between them - a per-worker memory cache
737 # would compute it five times and hold five copies. `compute` gives the
738 # single-flight on top of that.
739 #
740 # It lives beside the data it summarises rather than in a system temp
741 # directory: a cache of one store's history has no meaning next to another's,
742 # and an operator moving the store expects its derived files to go too.
743 #
744 # ON UNLESS TURNED OFF. A cache that has to be discovered is a cache nobody
745 # has, and every path through it falls back to the plain query - a store
746 # without Punk::Cache installed, an unwritable directory or a full disk all
747 # come out as a slower answer rather than a broken page.
748 3 ▶sub _query_cache {
Subroutine _query_cache: called CC=7 SCAR=20.9
749 3 my ($st) = @_;
750 3 ▶ return $st->{query_cache} if exists $st->{query_cache};
Branch: 1 branch
BranchTrueFalseTotal
if exists $st->{"query_cache"} 0 3 3
751  
752 3 my $opt = $st->{opts}{cache};
753 3 ▶ $opt = {} unless defined $opt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt 3 0 3
754 3 ▶ return $st->{query_cache} = undef unless $opt; # cache => 0
Branch: 1 branch
BranchTrueFalseTotal
unless $opt 0 3 3
755 3 ▶ $opt = {} unless ref $opt eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt eq "HASH" 0 3 3
756  
757 3 $st->{query_cache} = eval {
758 3 require Punk::Cache;
759 3 require File::Spec;
760 my $dir = $opt->{dir}
761 3 ▶ || File::Spec->catdir($st->{store}, 'cache');
Condition33%1/3
$opt->{'dir'} || 'File::Spec'->catdir($st->{'store'}, 'cache')
l!l&&r!l&&!r
030
MC/DC0%0/2
$opt->{'dir'} || 'File::Spec'->catdir($st->{'store'}, 'cache')
$opt->{'dir'} 'File::Spec'->catdir($st->{'store'}, 'cache')
Truth table33%1/3
A || B
A= $opt->{'dir'}B= 'File::Spec'->catdir($st->{'store'}, 'cache')
ABresult
1X1
011
000
762 Punk::Cache->new('file', dir => $dir,
763 3 ▶ max_bytes => $opt->{max_bytes} || '256M');
Condition50%1/2
$opt->{'max_bytes'} || '256M'
l!l
03
MC/DC0%0/1
$opt->{'max_bytes'} || '256M'
$opt->{'max_bytes'}
Truth table50%1/2
A
A= $opt->{'max_bytes'}
Aresult
11
00
764 };
765 3 return $st->{query_cache};
766 }
767  
768 17 ▶sub _request_tenant {
Subroutine _request_tenant: called CC=3 SCAR=12.0
769 17 my ($st, $c) = @_;
770 17 my $t = eval {
771 Punk::Observe::Tenant::resolve($st->{tenant}{fixed},
772 17 $st->{tenant}{resolver});
773 };
774 17 ▶ return ($t && $t->{ok}) ? $t->{tenant} : 'default';
Branch: 1 branch
BranchTrueFalseTotal
$t && $t->{'ok'} ? : 17 0 17
Condition33%1/3
$t and $t->{"ok"}
!ll&&!rl&&r
0017
MC/DC0%0/2
$t and $t->{"ok"}
$t $t->{"ok"}
Truth table33%1/3
A and B
A= $tB= $t->{"ok"}
ABresult
0X0
100
111
775 }
776  
777 17 ▶sub _params {
Subroutine _params: called CC=9 SCAR=22.3
778 17 my ($st, $c) = @_;
779 17 my %p;
780 17 for my $k (qw(q from to range errors min_ms service id trace slug
781 before after)) {
782 204 my $v = eval { $c->param($k) };
783 204 ▶ $p{$k} = $v if defined $v && length $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and length $v 12 192 204
Condition66%2/3
defined $v and length $v
!ll&&!rl&&r
192012
MC/DC50%1/2
defined $v and length $v
defined $v length $v
Truth table66%2/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
784 }
785 17 for my $k (qw(alerts dashboards)) {
786 34 my $seam = $st->{seam}{$k};
787 34 ▶ $p{$k} = $seam->{read} if $seam && $seam->{read};
Branch: 1 branch
BranchTrueFalseTotal
if $seam and $seam->{"read"} 7 27 34
Condition66%2/3
$seam and $seam->{"read"}
!ll&&!rl&&r
2707
MC/DC50%1/2
$seam and $seam->{"read"}
$seam $seam->{"read"}
Truth table66%2/3
A and B
A= $seamB= $seam->{"read"}
ABresult
0X0
100
111
788 34 ▶ $p{$k} = _reader($st, $k) if !$p{$k} && $st->{db};
Branch: 1 branch
BranchTrueFalseTotal
if not $p{$k} and $st->{"db"} 0 34 34
Condition66%2/3
not $p{$k} and $st->{"db"}
!ll&&!rl&&r
7270
MC/DC0%0/2
not $p{$k} and $st->{"db"}
not $p{$k} $st->{"db"}
Truth table66%2/3
not(A) and B
A= not $p{$k}B= $st->{"db"}
ABresult
0X0
100
111
789 }
790 17 $p{writable} = $st->{writable};
791 17 return \%p;
792 }
793  
794 55 ▶sub _range_qs {
Subroutine _range_qs: called CC=8 SCAR=20.9
795 55 my ($req, $lead) = @_;
796 55 ▶ $lead = '?' unless defined $lead;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lead 6 49 55
797 55 ▶ if (defined $req->{from} && length $req->{from}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->{"from"} and length $req->{"from"} and defined $req->{"to"} and length $req->{"to"}) 8 47 55
Condition77%7/9
defined $req->{"from"} and length $req->{"from"} and defined $req->{"to"} and length $req->{"to"}
!ll&&!rl&&r
4708
defined $req->{"from"} and length $req->{"from"} and defined $req->{"to"}
!ll&&!rl&&r
4708
defined $req->{"from"} and length $req->{"from"}
!ll&&!rl&&r
4618
MC/DC50%2/4
defined $req->{"from"} and length $req->{"from"} and defined $req->{"to"} and length $req->{"to"}
defined $req->{"from"} length $req->{"from"} defined $req->{"to"} length $req->{"to"}
Truth table60%3/5
A and B and C and D
A= defined $req->{"from"}B= length $req->{"from"}C= defined $req->{"to"}D= length $req->{"to"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
798 && defined $req->{to} && length $req->{to}) {
799 return $lead . 'from=' . _uri_esc($req->{from})
800 8 . '&to=' . _uri_esc($req->{to});
801 }
802 return $lead . 'range=' . _uri_esc($req->{range})
803 47 ▶ if defined $req->{range} && length $req->{range};
Branch: 1 branch
BranchTrueFalseTotal
if defined $req->{"range"} and length $req->{"range"} 8 39 47
Condition66%2/3
defined $req->{"range"} and length $req->{"range"}
!ll&&!rl&&r
3908
MC/DC50%1/2
defined $req->{"range"} and length $req->{"range"}
defined $req->{"range"} length $req->{"range"}
Truth table66%2/3
A and B
A= defined $req->{"range"}B= length $req->{"range"}
ABresult
0X0
100
111
804 39 return '';
805 }
806  
807 52 ▶sub _uri_esc {
Subroutine _uri_esc: called CC=2 SCAR=7.0
808 52 my ($v) = @_;
809 52 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 52 52
810 52 $v =~ s/([^A-Za-z0-9_.~-])/sprintf('%%%02X', ord $1)/ge;
811 52 return $v;
812 }
813  
814 23 ▶sub _discover {
Subroutine _discover: called CC=36 SCAR=36.3
815 23 my ($st, $c, $name, $vars, $store, $req) = @_;
816  
817 23 my %src = (logs => 'log', metrics => 'metric', trace => 'trace',
818 explore => '');
819 23 my $source = $src{$name};
820  
821 23 ▶ if (!length $source && defined $req->{q} && $req->{q} =~ /\A\s*(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $source and defined $req->{"q"} and $req->{"q"} =~ /\A\s*(\w+)/) 5 18 23
Condition83%5/6
not length $source and defined $req->{"q"} and $req->{"q"} =~ /\A\s*(\w+)/
!ll&&!rl&&r
1715
not length $source and defined $req->{"q"}
!ll&&!rl&&r
1706
MC/DC66%2/3
not length $source and defined $req->{"q"} and $req->{"q"} =~ /\A\s*(\w+)/
not length $source defined $req->{"q"} $req->{"q"} =~ /\A\s*(\w+)/
Truth table75%3/4
not(A) and B and C
A= not length $sourceB= defined $req->{"q"}C= $req->{"q"} =~ /\A\s*(\w+)/
ABCresult
0XX0
10X0
1100
1111
822 5 my %alias = (logs => 'log', traces => 'trace', span => 'spans');
823 5 my $w = lc $1;
824 5 ▶ $source = $alias{$w} || $w;
Condition66%2/3
$alias{$w} || $w
l!l&&r!l&&!r
230
MC/DC0%0/2
$alias{$w} || $w
$alias{$w} $w
Truth table66%2/3
A || B
A= $alias{$w}B= $w
ABresult
1X1
011
000
825 }
826 23 ▶ $source = 'log' unless length $source;
Branch: 1 branch
BranchTrueFalseTotal
unless length $source 1 22 23
827  
828 23 my $g = Punk::Observe::Query::grammar();
829 23 ▶ my $cols = $g->{columns}{$source} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cols = $g->{"columns"}{$source} or return 23 0 23
830  
831 23 $vars->{columns} = [ map { { name => $_, kind => 'column' } } @$cols ];
832 23 $vars->{columns_source} = $source;
833  
834 23 ▶ my $q = defined $req->{q} && $req->{q} =~ /\S/ ? $req->{q} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $req->{'q'} && $req->{'q'} =~ /\S/ ? : 7 16 23
Condition100%3/3
defined $req->{"q"} and $req->{"q"} =~ /\S/
!ll&&!rl&&r
1517
MC/DC100%2/2
defined $req->{"q"} and $req->{"q"} =~ /\S/
defined $req->{"q"} $req->{"q"} =~ /\S/
Truth table100%3/3
A and B
A= defined $req->{"q"}B= $req->{"q"} =~ /\S/
ABresult
0X0
100
111
835 23 $q =~ s/\A\s+|[\s|]+\z//g;
836 23 my $base = $q;
837 23 ▶ if (!length $base) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $base) 16 7 23
838 16 ▶ if ($source eq 'metric') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq 'metric') { } 4 12 16
839 4 my $first = eval { $vars->{names}[0]{name} };
840 4 ▶ $base = defined $first && length $first ? "metric $first" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $first && length $first ? : 2 2 4
Condition66%2/3
defined $first and length $first
!ll&&!rl&&r
202
MC/DC50%1/2
defined $first and length $first
defined $first length $first
Truth table66%2/3
A and B
A= defined $firstB= length $first
ABresult
0X0
100
111
841 }
842 12 else { $base = $source }
843 }
844 23 ▶ return unless length $base;
Branch: 1 branch
BranchTrueFalseTotal
unless length $base 2 21 23
845  
846 21 (my $stem = $base) =~ s/\s*\|.*\z//s;
847 21 my %page = (logs => 'logs', metrics => 'metrics', trace => 'traces',
848 explore => 'explore');
849 21 ▶ $vars->{discover_page} = $page{$name} || 'explore';
Condition50%1/2
$page{$name} || 'explore'
l!l
210
MC/DC0%0/1
$page{$name} || 'explore'
$page{$name}
Truth table50%1/2
A
A= $page{$name}
Aresult
11
00
850  
851 21 my $where_base = $base;
852  
853 21 ▶ return unless $store;
Branch: 1 branch
BranchTrueFalseTotal
unless $store 8 13 21
854 13 ▶ return if $vars->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $vars->{"error"} 1 12 13
855  
856 12 my %kind = (metric => 1, log => 2, trace => 3, spans => 3);
857  
858 12 my $LOOK = 2000;
859 12 my %seen;
860 my $sampled;
861 12 ▶ my $gen = eval { $store->can('generation') ? $store->generation : '' };
Branch: 1 branch
BranchTrueFalseTotal
$store->can('generation') ? : 5 7 12
862 12 ▶ $gen = '' unless defined $gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gen 0 12 12
863 my $ckey = join "\0", ($store->{tenant} // 'default'), $source,
864 12 ▶ map { $_ // '' } @{$req}{qw(range from to)};
Condition100%4/4
$store->{"tenant"} // "default"
l!l
57
$_ // ""
l!l
531
MC/DC100%1/1
$store->{"tenant"} // "default"
$store->{"tenant"}
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $store->{"tenant"}
Aresult
11
00
Truth table100%2/2
A
A= $_
Aresult
11
00
865 12 my $hit = $st->{discover}{$ckey};
866 12 ▶ if ($hit && $hit->{gen} eq $gen && time - $hit->{at} < 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hit and $hit->{'gen'} eq $gen and time - $hit->{'at'} < 60) { } 1 11 12
Condition100%6/6
$hit and $hit->{"gen"} eq $gen and time - $hit->{"at"} < 60
!ll&&!rl&&r
1011
$hit and $hit->{"gen"} eq $gen
!ll&&!rl&&r
912
MC/DC100%3/3
$hit and $hit->{"gen"} eq $gen and time - $hit->{"at"} < 60
$hit $hit->{"gen"} eq $gen time - $hit->{"at"} < 60
Truth table100%4/4
A and B and C
A= $hitB= $hit->{"gen"} eq $genC= time - $hit->{"at"} < 60
ABCresult
0XX0
10X0
1100
1111
867 1 %seen = %{ $hit->{seen} };
868 1 $sampled = $hit->{sampled};
869 }
870 else {
871 11 my $recs = eval {
872 my ($r) = $store->records(from => $vars->{from},
873 to => $vars->{to},
874 limit => $LOOK,
875 ($kind{$source}
876 11 ▶ ? (kind => $kind{$source}) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$kind{$source} ? : 11 0 11
877 11 $r;
878 };
879 11 ▶ return unless ref $recs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $recs eq "ARRAY" 0 11 11
880  
881 11 for my $rec (@$recs) {
882 11 ▶ my $a = $rec->{attrs} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $a = $rec->{"attrs"} or next 11 0 11
883 11 $seen{$_}++ for keys %$a;
884 }
885 11 $sampled = scalar @$recs;
886  
887 11 ▶ my $d = $st->{discover} ||= {};
Condition50%1/2
$st->{"discover"} ||= {}
l!l
110
MC/DC0%0/1
$st->{"discover"} ||= {}
$st->{"discover"}
Truth table50%1/2
A
A= $st->{"discover"}
Aresult
11
00
888 11 delete @$d{ grep { time - $d->{$_}{at} >= 60 } keys %$d };
889 11 $d->{$ckey} = { gen => $gen, at => time,
890 seen => {%seen}, sampled => $sampled };
891 }
892 12 ▶ return unless %seen;
Branch: 1 branch
BranchTrueFalseTotal
unless %seen 0 12 12
893  
894 12 my $allow = $st->{limits}{attributes};
895 12 ▶ my %indexed = map { $_ => 1 }
Branch: 1 branch
BranchTrueFalseTotal
ref $allow eq 'ARRAY' ? : 0 12 12
896 ref $allow eq 'ARRAY' ? @$allow
897 : qw(service.name severity host.name deployment.environment);
898  
899 12 ▶ my @keys = sort { $seen{$b} <=> $seen{$a} || $a cmp $b } keys %seen;
Condition33%1/3
$seen{$b} <=> $seen{$a} || $a cmp $b
l!l&&r!l&&!r
0190
MC/DC0%0/2
$seen{$b} <=> $seen{$a} || $a cmp $b
$seen{$b} <=> $seen{$a} $a cmp $b
Truth table33%1/3
A || B
A= $seen{$b} <=> $seen{$a}B= $a cmp $b
ABresult
1X1
011
000
900 12 my $cap = 40;
901 12 ▶ $vars->{attrs_truncated} = @keys > $cap ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@keys > $cap ? : 0 12 12
902 12 ▶ @keys = @keys[0 .. $cap - 1] if @keys > $cap;
Branch: 1 branch
BranchTrueFalseTotal
if @keys > $cap 0 12 12
903  
904 $vars->{attr_keys} = [ map {
905 12 ▶ { name => $_, count => $seen{$_}, indexed => ($indexed{$_} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$indexed{$_} ? : 12 16 28
906 query => _uri_esc("$stem | by $_ | count"),
907 where => "$where_base | where $_ = " }
908 } @keys ];
909 12 $vars->{attrs_sampled} = $sampled;
910 }
911  
912 4 ▶sub _dur_text {
Subroutine _dur_text: called CC=6 SCAR=18.8
913 4 my ($ns) = @_;
914 4 ▶ return '' unless defined $ns && $ns > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns and $ns > 0 0 4 4
Condition33%1/3
defined $ns and $ns > 0
!ll&&!rl&&r
004
MC/DC0%0/2
defined $ns and $ns > 0
defined $ns $ns > 0
Truth table33%1/3
A and B
A= defined $nsB= $ns > 0
ABresult
0X0
100
111
915 4 ▶ return Punk::Observe::View::fmt_dur($ns) if $ns % 1_000_000_000;
Branch: 1 branch
BranchTrueFalseTotal
if $ns % 1000000000 0 4 4
916 4 my $s = $ns / 1_000_000_000;
917 4 for my $u ([ 604_800 => 'w' ], [ 86_400 => 'd' ], [ 3_600 => 'h' ],
918 [ 60 => 'm' ]) {
919 16 ▶ return ($s / $u->[0]) . $u->[1] unless $s % $u->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $s % $u->[0] 1 15 16
920 }
921 3 return "${s}s";
922 }
923  
924 2 ▶sub _alert_form_vars {
Subroutine _alert_form_vars: called CC=6 SCAR=18.2
925 2 my ($rule) = @_;
926 2 ▶ my %r = %{ $rule || {} };
Condition50%1/2
$rule || {}
l!l
20
MC/DC0%0/1
$rule || {}
$rule
Truth table50%1/2
A
A= $rule
Aresult
11
00
927 2 for my $f (qw(for every)) {
928 4 ▶ next if defined $r{$f};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r{$f} 0 4 4
929 4 $r{$f} = _dur_text($r{"${f}_ns"});
930 }
931 2 ▶ my $cur = defined $r{op} ? $r{op} : '>';
Branch: 1 branch
BranchTrueFalseTotal
defined $r{'op'} ? : 2 0 2
932 return (rule => \%r,
933 2 ▶ op_options => [ map { { name => $_,
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $cur ? : 2 10 12
934 current => ($_ eq $cur ? 1 : 0) } }
935 qw(> >= < <= == !=) ]);
936 }
937  
938 0 ▶sub _write {
Subroutine _write: not called CC=69 SCAR=84.8
939 0 my ($st, $what, $c) = @_;
940  
941 0 ▶ return $c->status(503)->text("no configuration store\n") unless $st->{db};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"db"} 0 0 0
942  
943 0 my $tenant = _request_tenant($st, $c);
944 0 my $slug = eval { $c->param('slug') };
945 0 my $id = eval { $c->param('id') };
946 0 my %in = map { $_ => scalar eval { $c->param($_) } }
947 qw(slug title cols query viz span position);
948  
949 0 my $r;
950 0 ▶ if ($what eq 'dashboard') {
Branch: 13 branches
BranchTrueFalseTotal
if ($what eq 'dashboard') { } 0 0 0
elsif ($what eq 'dashboard_delete') { } 0 0 0
elsif ($what eq 'panel') { } 0 0 0
elsif ($what eq 'panel_delete') { } 0 0 0
elsif ($what eq 'panels_save') { } 0 0 0
elsif ($what eq 'view') { } 0 0 0
elsif ($what eq 'view_delete') { } 0 0 0
elsif ($what eq 'alert') { } 0 0 0
elsif ($what eq 'alert_delete') { } 0 0 0
elsif ($what eq 'silence') { } 0 0 0
elsif ($what eq 'silence_delete') { } 0 0 0
elsif ($what eq 'health_target') { } 0 0 0
elsif ($what eq 'health_target_delete') { } 0 0 0
951 0 ▶ $in{slug} = $slug if defined $slug && length $slug;
Branch: 1 branch
BranchTrueFalseTotal
if defined $slug and length $slug 0 0 0
Condition0%0/3
defined $slug and length $slug
!ll&&!rl&&r
000
MC/DC0%0/2
defined $slug and length $slug
defined $slug length $slug
Truth table0%0/3
A and B
A= defined $slugB= length $slug
ABresult
0X0
100
111
952 0 $r = Punk::Observe::Config::save_dashboard($st->{db}, $tenant, \%in);
953 }
954 elsif ($what eq 'dashboard_delete') {
955 0 $r = Punk::Observe::Config::delete_dashboard($st->{db}, $tenant, $slug);
956 }
957 elsif ($what eq 'panel') {
958 0 $in{id} = $id;
959 0 $r = Punk::Observe::Config::save_panel($st->{db}, $tenant, $slug, \%in);
960 }
961 elsif ($what eq 'panel_delete') {
962 0 $r = Punk::Observe::Config::delete_panel($st->{db}, $tenant, $slug, $id);
963 }
964 elsif ($what eq 'panels_save') {
965 0 my $d = Punk::Observe::Config::dashboards($st->{db}, $tenant, $slug);
966 0 my @rows;
967 0 ▶ for my $p (@{ $d->{panels} || [] }) {
Condition0%0/2
$d->{'panels'} || []
l!l
00
MC/DC0%0/1
$d->{'panels'} || []
$d->{'panels'}
Truth table0%0/2
A
A= $d->{'panels'}
Aresult
11
00
968 0 my %row = map { $_ => scalar eval { $c->param("p$p->{id}_$_") } }
969 qw(title query viz span position);
970 0 ▶ next unless grep { defined } values %row;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} values %row 0 0 0
971 0 $row{id} = $p->{id};
972 0 push @rows, \%row;
973 }
974 0 $r = Punk::Observe::Config::save_panels($st->{db}, $tenant, $slug,
975 \@rows);
976 }
977 elsif ($what eq 'view') {
978 0 my %v = map { $_ => scalar eval { $c->param($_) } }
979 qw(name page q from to range errors min_ms service);
980 0 $r = Punk::Observe::Config::save_view($st->{db}, $tenant, \%v);
981 0 ▶ if ($r->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"ok"}) 0 0 0
982 0 ▶ my $page = $v{page} || 'logs';
Condition0%0/2
$v{'page'} || 'logs'
l!l
00
MC/DC0%0/1
$v{'page'} || 'logs'
$v{'page'}
Truth table0%0/2
A
A= $v{'page'}
Aresult
11
00
983 0 ▶ $page = 'traces' if $page eq 'trace';
Branch: 1 branch
BranchTrueFalseTotal
if $page eq "trace" 0 0 0
984 0 my $qs = join '&', map { "$_=" . _uri_esc($v{$_}) }
985 0 ▶ grep { defined $v{$_} && length $v{$_} }
Condition0%0/3
defined $v{$_} && length $v{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v{$_} && length $v{$_}
defined $v{$_} length $v{$_}
Truth table0%0/3
A && B
A= defined $v{$_}B= length $v{$_}
ABresult
0X0
100
111
986 qw(q from to range errors min_ms service);
987 0 ▶ return $c->redirect($st->{prefix} . "/$page"
Branch: 1 branch
BranchTrueFalseTotal
length $qs ? : 0 0 0
988 . (length $qs ? "?$qs" : ''), 303);
989 }
990 }
991 elsif ($what eq 'view_delete') {
992 0 $r = Punk::Observe::Config::delete_view($st->{db}, $tenant, $id);
993 0 ▶ if ($r->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"ok"}) 0 0 0
994 0 ▶ my $page = eval { $c->param('page') } || 'logs';
Condition0%0/2
eval { do { $c->param('page') } } || 'logs'
l!l
00
MC/DC0%0/1
eval { do { $c->param('page') } } || 'logs'
eval { do { $c->param('page') } }
Truth table0%0/2
A
A= eval { do { $c->param('page') } }
Aresult
11
00
995 0 ▶ $page = 'traces' if $page eq 'trace';
Branch: 1 branch
BranchTrueFalseTotal
if $page eq "trace" 0 0 0
996 0 return $c->redirect($st->{prefix} . "/$page", 303);
997 }
998 }
999 elsif ($what eq 'alert') {
1000 0 my %a = map { $_ => scalar eval { $c->param($_) } }
1001 qw(name query op threshold for every enabled);
1002 0 ▶ $a{id} = $id if defined $id && length $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and length $id 0 0 0
Condition0%0/3
defined $id and length $id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table0%0/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
1003 0 ▶ $a{enabled} = 0 unless defined $a{enabled} && $a{enabled};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a{"enabled"} and $a{"enabled"} 0 0 0
Condition0%0/3
defined $a{"enabled"} and $a{"enabled"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $a{"enabled"} and $a{"enabled"}
defined $a{"enabled"} $a{"enabled"}
Truth table0%0/3
A and B
A= defined $a{"enabled"}B= $a{"enabled"}
ABresult
0X0
100
111
1004 0 $r = Punk::Observe::Config::save_alert($st->{db}, $tenant, \%a);
1005 0 ▶ return $c->redirect($st->{prefix} . '/alerts', 303) if $r->{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"ok"} 0 0 0
1006 return _page($st, 'alerts', $c, {
1007 template => 'alertedit', editing => 1,
1008 creating => (defined $id && length $id) ? 0 : 1,
1009 _alert_form_vars({ %a, id => $id }),
1010 error => $r->{error}, field => $r->{field} || '',
1011 hint => $r->{refused}
1012 ? 'Correct it and submit again.'
1013 : 'The configuration store could not be written to.',
1014 0 ▶ status => $r->{refused} ? 400 : 500,
Branch: 3 branches
BranchTrueFalseTotal
defined $id && length $id ? : 0 0 0
$r->{'refused'} ? : 0 0 0
$r->{'refused'} ? : 0 0 0
Condition0%0/5
defined $id and length $id
!ll&&!rl&&r
000
$r->{'field'} || ''
l!l
00
MC/DC0%0/2
defined $id and length $id
defined $id length $id
MC/DC0%0/1
$r->{'field'} || ''
$r->{'field'}
Truth table0%0/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
Truth table0%0/2
A
A= $r->{'field'}
Aresult
11
00
1015 });
1016 }
1017 elsif ($what eq 'alert_delete') {
1018 0 $r = Punk::Observe::Config::delete_alert($st->{db}, $tenant, $id);
1019 0 ▶ return $c->redirect($st->{prefix} . '/alerts', 303) if $r->{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"ok"} 0 0 0
1020 }
1021 elsif ($what eq 'silence') {
1022 0 my %s = map { $_ => scalar eval { $c->param($_) } }
1023 qw(pattern until reason);
1024 0 ▶ unless (defined $s{pattern} && length $s{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s{"pattern"} and length $s{"pattern"}) 0 0 0
Condition0%0/3
defined $s{"pattern"} and length $s{"pattern"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s{"pattern"} and length $s{"pattern"}
defined $s{"pattern"} length $s{"pattern"}
Truth table0%0/3
A and B
A= defined $s{"pattern"}B= length $s{"pattern"}
ABresult
0X0
100
111
1025 0 my $rule = Punk::Observe::Config::alert($st->{db}, $tenant, $id);
1026 0 ▶ $s{pattern} = $rule ? "$rule->{name}/" : '';
Branch: 1 branch
BranchTrueFalseTotal
$rule ? : 0 0 0
1027 }
1028 0 ▶ $s{by} = eval { $c->current_user } || eval { $c->auth_id } || undef;
Condition0%0/5
eval { do { $c->current_user } } || eval { do { $c->auth_id } } || undef
l!l
00
eval { do { $c->current_user } } or eval { do { $c->auth_id } }
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $c->current_user } } || eval { do { $c->auth_id } } || undef
eval { do { $c->current_user } } eval { do { $c->auth_id } }
Truth table0%0/3
A or B
A= eval { do { $c->current_user } }B= eval { do { $c->auth_id } }
ABresult
1X1
011
000
1029 0 $r = Punk::Observe::Config::save_silence($st->{db}, $tenant, \%s);
1030 0 ▶ return $c->redirect($st->{prefix} . '/alerts', 303) if $r->{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"ok"} 0 0 0
1031 }
1032 elsif ($what eq 'silence_delete') {
1033 0 $r = Punk::Observe::Config::delete_silence($st->{db}, $tenant, $id);
1034 0 ▶ return $c->redirect($st->{prefix} . '/alerts', 303) if $r->{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"ok"} 0 0 0
1035 }
1036 elsif ($what eq 'health_target') {
1037 0 my %t = map { $_ => scalar eval { $c->param($_) } }
1038 qw(name url every_s timeout_ms);
1039 $t{every_ns} = int($t{every_s} * 1_000_000_000)
1040 0 ▶ if defined $t{every_s} && length $t{every_s};
Branch: 1 branch
BranchTrueFalseTotal
if defined $t{"every_s"} and length $t{"every_s"} 0 0 0
Condition0%0/3
defined $t{"every_s"} and length $t{"every_s"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $t{"every_s"} and length $t{"every_s"}
defined $t{"every_s"} length $t{"every_s"}
Truth table0%0/3
A and B
A= defined $t{"every_s"}B= length $t{"every_s"}
ABresult
0X0
100
111
1041 0 delete $t{every_s};
1042 0 delete $t{$_} for grep { !defined $t{$_} } keys %t;
1043 $r = Punk::Observe::Config::save_health_target(
1044 0 $st->{db}, $tenant, \%t, $st->{opts}{health_allow});
1045 return $c->redirect($st->{prefix} . '/health-targets/edit', 303)
1046 0 ▶ if $r->{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"ok"} 0 0 0
1047 }
1048 elsif ($what eq 'health_target_delete') {
1049 0 my $name = eval { $c->param('name') };
1050 $r = Punk::Observe::Config::delete_health_target(
1051 0 $st->{db}, $tenant, $name);
1052 return $c->redirect($st->{prefix} . '/health-targets/edit', 303)
1053 0 ▶ if $r->{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"ok"} 0 0 0
1054 }
1055 0 else { $r = { ok => 0, error => "unknown write '$what'" } }
1056  
1057 0 ▶ if ($what =~ /\A(?:alert_delete|silence)/ && !$r->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /\A(?:alert_delete|silence)/ and not $r->{"ok"}) 0 0 0
Condition0%0/3
$what =~ /\A(?:alert_delete|silence)/ and not $r->{"ok"}
!ll&&!rl&&r
000
MC/DC0%0/2
$what =~ /\A(?:alert_delete|silence)/ and not $r->{"ok"}
$what =~ /\A(?:alert_delete|silence)/ not $r->{"ok"}
Truth table0%0/3
A and not(B)
A= $what =~ /\A(?:alert_delete|silence)/B= not $r->{"ok"}
ABresult
0X0
100
111
1058 return _page($st, 'alerts', $c, {
1059 error => $r->{error},
1060 hint => $r->{refused}
1061 ? 'Correct it and submit again.'
1062 : 'The configuration store could not be written to.',
1063 0 ▶ status => $r->{refused} ? 400 : 500,
Branch: 2 branches
BranchTrueFalseTotal
$r->{'refused'} ? : 0 0 0
$r->{'refused'} ? : 0 0 0
1064 });
1065 }
1066  
1067 0 ▶ if ($what =~ /\Ahealth_target/ && !$r->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /\Ahealth_target/ and not $r->{"ok"}) 0 0 0
Condition0%0/3
$what =~ /\Ahealth_target/ and not $r->{"ok"}
!ll&&!rl&&r
000
MC/DC0%0/2
$what =~ /\Ahealth_target/ and not $r->{"ok"}
$what =~ /\Ahealth_target/ not $r->{"ok"}
Truth table0%0/3
A and not(B)
A= $what =~ /\Ahealth_target/B= not $r->{"ok"}
ABresult
0X0
100
111
1068 return _page($st, 'status', $c, {
1069 template => 'healthedit',
1070 heading => 'Health targets',
1071 here_status => 0, here_home => 0, here_health => 1,
1072 error => $r->{error},
1073 hint => $r->{refused}
1074 ? 'Correct it and submit again.'
1075 : 'The configuration store could not be written to.',
1076 0 ▶ status => $r->{refused} ? 400 : 500,
Branch: 2 branches
BranchTrueFalseTotal
$r->{'refused'} ? : 0 0 0
$r->{'refused'} ? : 0 0 0
1077 });
1078 }
1079  
1080 0 my $back = $st->{prefix} . '/dashboards';
1081 0 ▶ if ($r->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"ok"}) 0 0 0
1082 0 ▶ $back .= '/' . _uri_esc($slug)
Branch: 1 branch
BranchTrueFalseTotal
if defined $slug and length $slug and $what ne "dashboard_delete" 0 0 0
Condition0%0/6
defined $slug and length $slug and $what ne "dashboard_delete"
!ll&&!rl&&r
000
defined $slug and length $slug
!ll&&!rl&&r
000
MC/DC0%0/3
defined $slug and length $slug and $what ne "dashboard_delete"
defined $slug length $slug $what ne "dashboard_delete"
Truth table0%0/4
A and B and C
A= defined $slugB= length $slugC= $what ne "dashboard_delete"
ABCresult
0XX0
10X0
1100
1111
1083 if defined $slug && length $slug && $what ne 'dashboard_delete';
1084 0 return $c->redirect($back, 303);
1085 }
1086  
1087 0 ▶ my $status = $r->{refused} ? 400 : 500;
Branch: 1 branch
BranchTrueFalseTotal
$r->{'refused'} ? : 0 0 0
1088 return _page($st, 'dashboard', $c, {
1089 template => 'dashedit',
1090 editing => 1,
1091 creating => ($what eq 'dashboard' && !(defined $slug && length $slug)) ? 1 : 0,
1092 error => $r->{error},
1093 field => $r->{field} || '',
1094 hint => $r->{refused}
1095 0 ▶ ? 'Correct it and submit again.'
Branch: 2 branches
BranchTrueFalseTotal
$what eq 'dashboard' && !(defined $slug && length $slug) ? : 0 0 0
$r->{'refused'} ? : 0 0 0
Condition0%0/8
$what eq "dashboard" and not defined $slug && length $slug
!ll&&!rl&&r
000
defined $slug && length $slug
!ll&&!rl&&r
000
$r->{'field'} || ''
l!l
00
MC/DC0%0/3
$what eq "dashboard" and not defined $slug && length $slug
$what eq "dashboard" defined $slug length $slug
MC/DC0%0/1
$r->{'field'} || ''
$r->{'field'}
Truth table0%0/4
A and not(B && C)
A= $what eq "dashboard"B= defined $slugC= length $slug
ABCresult
0XX0
1110
10X1
1101
Truth table0%0/2
A
A= $r->{'field'}
Aresult
11
00
1096 : 'The configuration store could not be written to.',
1097 status => $status,
1098 });
1099 }
1100  
1101 12 ▶sub _page {
Subroutine _page: called CC=94 SCAR=62.8
1102 12 my ($st, $name, $c, $over) = @_;
1103 return $c->status(501)->text("Template::Stencil is not installed\n")
1104 12 ▶ unless $st->{stencil};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"stencil"} 0 12 12
1105  
1106 12 my %vars = _empty($st, $c);
1107 12 my $req = _params($st, $c);
1108 12 my $store = store_for($st, _request_tenant($st, $c));
1109  
1110 12 my $dash_full = 0;
1111 12 ▶ if ($name eq 'dashboard'
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "dashboard" and ref $over ne "HASH" || ($over->{"template"} || "") ne "dashedit") 3 9 12
Condition87%7/8
$name eq "dashboard" and ref $over ne "HASH" || ($over->{"template"} || "") ne "dashedit"
!ll&&!rl&&r
813
ref $over ne "HASH" or ($over->{"template"} || "") ne "dashedit"
l!l&&r!l&&!r
031
$over->{'template'} || ''
l!l
13
MC/DC66%2/3
$name eq "dashboard" and ref $over ne "HASH" || ($over->{"template"} || "") ne "dashedit"
$name eq "dashboard" ref $over ne "HASH" $over->{'template'}
Truth table75%3/4
A and B or C
A= $name eq "dashboard"B= ref $over ne "HASH"C= $over->{'template'}
ABCresult
0XX0
1000
11X1
1011
1112 && (ref $over ne 'HASH' || ($over->{template} || '') ne 'dashedit')) {
1113 3 $dash_full = ($c && eval { $c->param('full') }) || !$st->{fragment}
1114 3 ▶ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c && eval { do { $c->param('full') } } || !$st->{'fragment'} ? : 1 2 3
Condition66%4/6
$c and eval { do { $c->param("full") } } or not $st->{"fragment"}
l!l&&r!l&&!r
102
$c and eval { do { $c->param("full") } }
!ll&&!rl&&r
021
MC/DC33%1/3
$c and eval { do { $c->param("full") } } or not $st->{"fragment"}
$c eval { do { $c->param("full") } } not $st->{"fragment"}
Truth table40%2/5
A and B or not(C)
A= $cB= eval { do { $c->param("full") } }C= not $st->{"fragment"}
ABCresult
11X1
0X11
1011
0X00
1000
1115 3 ▶ $req->{panels_inline} = 1 if $dash_full;
Branch: 1 branch
BranchTrueFalseTotal
if $dash_full 1 2 3
1116 }
1117  
1118 12 my $built = eval { Punk::Observe::View->page($store, $name, $req) };
1119 12 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 12 12
elsif (ref $built eq 'HASH') { } 12 0 12
1120 0 $vars{error} = 'That screen could not be built from the store.';
1121 0 $vars{hint} = "$@";
1122 }
1123 elsif (ref $built eq 'HASH') {
1124 12 %vars = (%vars, %$built);
1125 }
1126  
1127 12 ▶ if ($dash_full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dash_full) 1 11 12
1128 1 ▶ for my $p (@{ $vars{panels} || [] }) {
Condition50%1/2
$vars{'panels'} || []
l!l
10
MC/DC0%0/1
$vars{'panels'} || []
$vars{'panels'}
Truth table50%1/2
A
A= $vars{'panels'}
Aresult
11
00
1129 my $html = _fragment_html($st, q{panelslow.tmpl},
1130 { p => $p, prefix => $st->{prefix},
1131 from => $vars{from}, to => $vars{to},
1132 2 range_amp => _range_qs($req, q{&}) });
1133 2 ▶ $p->{body_html} = $html if defined $html;
Branch: 1 branch
BranchTrueFalseTotal
if defined $html 2 0 2
1134 }
1135 }
1136  
1137 12 ▶ $vars{heading} = ucfirst $name unless length($vars{heading} || '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($vars{"heading"} || "") 0 12 12
Condition50%1/2
$vars{'heading'} || ''
l!l
120
MC/DC0%0/1
$vars{'heading'} || ''
$vars{'heading'}
Truth table50%1/2
A
A= $vars{'heading'}
Aresult
11
00
1138 12 ▶ $vars{writable} = $st->{writable} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st->{'writable'} ? : 0 12 12
1139 $vars{csrf_field} = ($st->{writable} && $c && $c->can('csrf_field'))
1140 0 ▶ ? (eval { $c->csrf_field } || '') : '';
Branch: 1 branch
BranchTrueFalseTotal
$st->{'writable'} && $c && $c->can('csrf_field') ? : 0 12 12
Condition25%2/8
$st->{"writable"} and $c and $c->can("csrf_field")
!ll&&!rl&&r
1200
$st->{"writable"} and $c
!ll&&!rl&&r
1200
eval { do { $c->csrf_field } } or ""
l!l
00
MC/DC0%0/3
$st->{"writable"} and $c and $c->can("csrf_field")
$st->{"writable"} $c $c->can("csrf_field")
MC/DC0%0/1
eval { do { $c->csrf_field } } or ""
eval { do { $c->csrf_field } }
Truth table25%1/4
A and B and C
A= $st->{"writable"}B= $cC= $c->can("csrf_field")
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= eval { do { $c->csrf_field } }
Aresult
11
00
1141  
1142 12 ▶ if ($name eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "help") 0 12 12
1143 0 my $g = Punk::Observe::Query::grammar();
1144 0 $vars{$_} = $g->{$_} for qw(aggregates severities units operators);
1145 $vars{sources} = [ map {
1146 { %$_, alias => ($_->{alias} // ''),
1147 0 ▶ cols => join ', ', @{ $g->{columns}{ $_->{name} } || [] } }
Condition0%0/4
$_->{"alias"} // ""
l!l
00
$g->{'columns'}{$_->{'name'}} || []
l!l
00
MC/DC0%0/1
$_->{"alias"} // ""
$_->{"alias"}
MC/DC0%0/1
$g->{'columns'}{$_->{'name'}} || []
$g->{'columns'}{$_->{'name'}}
Truth table0%0/2
A
A= $_->{"alias"}
Aresult
11
00
Truth table0%0/2
A
A= $g->{'columns'}{$_->{'name'}}
Aresult
11
00
1148 0 } @{ $g->{sources} } ];
1149 0 $vars{heading} = 'The query language';
1150 0 $vars{title} = 'The query language';
1151 }
1152  
1153 12 ▶ if ($st->{db} || $store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"db"} or $store) 12 0 12
Condition33%1/3
$st->{"db"} or $store
l!l&&r!l&&!r
0120
MC/DC0%0/2
$st->{"db"} or $store
$st->{"db"} $store
Truth table33%1/3
A or B
A= $st->{"db"}B= $store
ABresult
1X1
011
000
1154 12 ▶ if ($name =~ /\A(?:explore|logs|metrics|trace)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\A(?:explore|logs|metrics|trace)\z/) 0 12 12
1155 0 _discover($st, $c, $name, \%vars, $store, $req);
1156 }
1157 }
1158  
1159 12 ▶ if ($st->{db} && $name =~ /\A(?:logs|metrics|trace|explore)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"db"} and $name =~ /\A(?:logs|metrics|trace|explore)\z/) 0 12 12
Condition33%1/3
$st->{"db"} and $name =~ /\A(?:logs|metrics|trace|explore)\z/
!ll&&!rl&&r
1200
MC/DC0%0/2
$st->{"db"} and $name =~ /\A(?:logs|metrics|trace|explore)\z/
$st->{"db"} $name =~ /\A(?:logs|metrics|trace|explore)\z/
Truth table33%1/3
A and B
A= $st->{"db"}B= $name =~ /\A(?:logs|metrics|trace|explore)\z/
ABresult
0X0
100
111
1160 0 my $vs = eval { Punk::Observe::Config::saved_views($st->{db},
1161 0 _request_tenant($st, $c),
1162 $name) };
1163 0 ▶ $vars{saved_views} = (ref $vs eq 'ARRAY') ? $vs : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $vs eq 'ARRAY' ? : 0 0 0
1164 0 $vars{save_page} = $name;
1165 0 ▶ $vars{save_path} = $name eq 'trace' ? 'traces' : $name;
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'trace' ? : 0 0 0
1166 }
1167  
1168 12 $vars{range_qs} = _range_qs($req, '?');
1169 12 $vars{range_amp} = _range_qs($req, '&');
1170  
1171 12 ▶ if ($name eq 'status' && $store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "status" and $store) 8 4 12
Condition66%2/3
$name eq "status" and $store
!ll&&!rl&&r
408
MC/DC50%1/2
$name eq "status" and $store
$name eq "status" $store
Truth table66%2/3
A and B
A= $name eq "status"B= $store
ABresult
0X0
100
111
1172 8 my $s = delete $vars{_stats};
1173 0 ▶ $s = eval { $store->stats } || {} unless ref $s eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $s eq "HASH" 0 8 8
Condition0%0/2
eval { do { $store->stats } } || {}
l!l
00
MC/DC0%0/1
eval { do { $store->stats } } || {}
eval { do { $store->stats } }
Truth table0%0/2
A
A= eval { do { $store->stats } }
Aresult
11
00
1174  
1175 my $fig = Punk::Observe::Plot::gauge(
1176 value => $s->{bytes} || 0,
1177 8 ▶ max => $st->{limits}{storage} || 0,
Condition75%3/4
$s->{'bytes'} || 0
l!l
53
$st->{'limits'}{'storage'} || 0
l!l
08
MC/DC100%1/1
$s->{'bytes'} || 0
$s->{'bytes'}
MC/DC0%0/1
$st->{'limits'}{'storage'} || 0
$st->{'limits'}{'storage'}
Truth table100%2/2
A
A= $s->{'bytes'}
Aresult
11
00
Truth table50%1/2
A
A= $st->{'limits'}{'storage'}
Aresult
11
00
1178 title => 'bytes on disk');
1179 8 ▶ $vars{storage_gauge_plot} = Punk::Observe::Plot::encode($fig) if $fig;
Branch: 1 branch
BranchTrueFalseTotal
if $fig 0 8 8
1180  
1181 my $tname = (ref $over eq 'HASH' && $over->{template})
1182 8 ▶ || $vars{template} || 'status';
Condition87%7/8
ref $over eq 'HASH' && $over->{'template'} || $vars{'template'} || 'status'
l!l
26
ref $over eq "HASH" and $over->{"template"} or $vars{"template"}
l!l&&r!l&&!r
206
ref $over eq "HASH" and $over->{"template"}
!ll&&!rl&&r
332
MC/DC66%2/3
ref $over eq 'HASH' && $over->{'template'} || $vars{'template'} || 'status'
ref $over eq "HASH" $over->{"template"} $vars{"template"}
Truth table60%3/5
A and B or C
A= ref $over eq "HASH"B= $over->{"template"}C= $vars{"template"}
ABCresult
11X1
0X11
1011
0X00
1000
1183 8 ▶ my $full = $c ? (eval { $c->param('full') } ? 1 : 0) : 0;
Branch: 2 branches
BranchTrueFalseTotal
$c ? : 8 0 8
eval { do { $c->param('full') } } ? : 2 6 8
1184 my %fvars = (prefix => $st->{prefix},
1185 writable => $st->{writable},
1186 8 range_qs => _range_qs($req, '?'),
1187 range_amp => _range_qs($req, '&'));
1188  
1189 8 ▶ if ($tname eq 'status') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tname eq 'status') { } 6 2 8
elsif ($tname eq 'health') { } 2 0 2
elsif ($tname eq 'healthedit') { } 0 0 0
1190 6 ▶ if ($full || !$st->{fragment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full or not $st->{"fragment"}) 4 2 6
Condition100%3/3
$full or not $st->{"fragment"}
l!l&&r!l&&!r
132
MC/DC100%2/2
$full or not $st->{"fragment"}
$full not $st->{"fragment"}
Truth table100%3/3
A or not(B)
A= $fullB= not $st->{"fragment"}
ABresult
1X1
011
000
1191 4 my ($from, $to) = _status_window($req);
1192 4 $vars{ingest_html} = _fragment_html($st, 'statusslow.tmpl',
1193 { %fvars, _ingest_vars($store, $from, $to) });
1194 }
1195 }
1196 elsif ($tname eq 'health') {
1197 2 ▶ if ($full || !$st->{fragment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full or not $st->{"fragment"}) 1 1 2
Condition66%2/3
$full or not $st->{"fragment"}
l!l&&r!l&&!r
101
MC/DC50%1/2
$full or not $st->{"fragment"}
$full not $st->{"fragment"}
Truth table66%2/3
A or not(B)
A= $fullB= not $st->{"fragment"}
ABresult
1X1
011
000
1198 1 my ($hf, $ht) = _status_window($req);
1199 1 $vars{health_html} = _fragment_html($st, "healthslow.tmpl",
1200 { %fvars, _health_vars($st, $store, $hf, $ht) });
1201 }
1202 }
1203 elsif ($tname eq 'healthedit') {
1204 0 my %h = _health_vars($st, $store);
1205 0 @vars{keys %h} = values %h;
1206 }
1207  
1208 my $c = $st->{arena}
1209 0 ▶ ? eval { Punk::Observe::Segment::shm_stats($st->{arena}{handle}) }
Branch: 1 branch
BranchTrueFalseTotal
$st->{'arena'} ? : 0 8 8
1210 : undef;
1211 8 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) 0 8 8
1212 0 $vars{accepted} = Punk::Observe::View::fmt_count($c->{records});
1213 0 $vars{accepted_bytes} = Punk::Observe::View::fmt_bytes($c->{bytes});
1214 0 $vars{rate_rejected} = Punk::Observe::View::fmt_count($c->{rate_rejected});
1215 0 ▶ $vars{counters_shared} = $c->{shared} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c->{'shared'} ? : 0 0 0
1216  
1217 0 $vars{series_used} = Punk::Observe::View::fmt_count($c->{series});
1218 $vars{series_cap} = $c->{series_cap}
1219 0 ▶ ? Punk::Observe::View::fmt_count($c->{series_cap}) : '';
Branch: 1 branch
BranchTrueFalseTotal
$c->{'series_cap'} ? : 0 0 0
1220 0 $vars{series_rejected} = Punk::Observe::View::fmt_count($c->{rejected});
1221 0 ▶ $vars{series_dropping} = ($c->{rejected} || 0) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($c->{'rejected'} || 0) > 0 ? : 0 0 0
Condition0%0/2
$c->{'rejected'} || 0
l!l
00
MC/DC0%0/1
$c->{'rejected'} || 0
$c->{'rejected'}
Truth table0%0/2
A
A= $c->{'rejected'}
Aresult
11
00
1222 0 $vars{overflow_records} = Punk::Observe::View::fmt_count($c->{overflow});
1223  
1224 0 $vars{live_gaps} = Punk::Observe::View::fmt_count($c->{live_gaps});
1225  
1226 0 ▶ if ($c->{series_cap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"series_cap"}) 0 0 0
1227 my $g = Punk::Observe::Plot::gauge(
1228 value => $c->{series} || 0,
1229 max => $c->{series_cap},
1230 0 ▶ title => 'active series');
Condition0%0/2
$c->{'series'} || 0
l!l
00
MC/DC0%0/1
$c->{'series'} || 0
$c->{'series'}
Truth table0%0/2
A
A= $c->{'series'}
Aresult
11
00
1231 0 ▶ $vars{series_gauge_plot} = Punk::Observe::Plot::encode($g) if $g;
Branch: 1 branch
BranchTrueFalseTotal
if $g 0 0 0
1232 }
1233 }
1234 }
1235  
1236 12 ▶ $vars{wants_plot} = (grep { /_plot\z/ && defined $vars{$_} && length $vars{$_} }
Branch: 1 branch
BranchTrueFalseTotal
grep({/_plot\z/ and defined $vars{$_} and length $vars{$_};} keys %vars) ? : 0 12 12
Condition33%2/6
/_plot\z/ && defined $vars{$_} && length $vars{$_}
!ll&&!rl&&r
142900
/_plot\z/ and defined $vars{$_}
!ll&&!rl&&r
142900
MC/DC0%0/3
/_plot\z/ && defined $vars{$_} && length $vars{$_}
/_plot\z/ defined $vars{$_} length $vars{$_}
Truth table25%1/4
A and B && C
A= /_plot\z/B= defined $vars{$_}C= length $vars{$_}
ABCresult
0XX0
10X0
1100
1111
1237 keys %vars) ? 1 : 0;
1238 $vars{wants_plot} ||= (grep {
1239 8 my $p = $_;
1240 ref $p eq 'HASH'
1241 8 ▶ && grep { /_plot\z/ && defined $p->{$_} && length $p->{$_} } keys %$p;
Condition66%6/9
ref $p eq 'HASH' && grep({/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p)
!ll&&!rl&&r
071
/_plot\z/ && defined $p->{$_} && length $p->{$_}
!ll&&!rl&&r
8801
/_plot\z/ and defined $p->{$_}
!ll&&!rl&&r
8801
MC/DC50%1/2
ref $p eq 'HASH' && grep({/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p)
ref $p eq 'HASH' grep({/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p)
MC/DC33%1/3
/_plot\z/ && defined $p->{$_} && length $p->{$_}
/_plot\z/ defined $p->{$_} length $p->{$_}
Truth table66%2/3
A && B
A= ref $p eq 'HASH'B= grep({/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p)
ABresult
0X0
100
111
Truth table50%2/4
A and B && C
A= /_plot\z/B= defined $p->{$_}C= length $p->{$_}
ABCresult
0XX0
10X0
1100
1111
1242 12 ▶ } @{ $vars{panels} || [] }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({my $p = $_; ref $p eq 'HASH' and grep {/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p;} @{$vars{'panels'} or [];}) ? : 1 11 12
Condition60%3/5
$vars{"wants_plot"} ||= grep({my $p = $_; ref $p eq "HASH" and grep {/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p;} @{$vars{"panels"} or [];}) ? 1 : 0
l!l&&r!l&&!r
0111
$vars{'panels'} || []
l!l
120
MC/DC50%1/2
$vars{"wants_plot"} ||= grep({my $p = $_; ref $p eq "HASH" and grep {/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p;} @{$vars{"panels"} or [];}) ? 1 : 0
$vars{"wants_plot"} grep({my $p = $_; ref $p eq "HASH" and grep {/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p;} @{$vars{"panels"} or [];}) ? 1 : 0
MC/DC0%0/1
$vars{'panels'} || []
$vars{'panels'}
Truth table66%2/3
A ||= B
A= $vars{"wants_plot"}B= grep({my $p = $_; ref $p eq "HASH" and grep {/_plot\z/ and defined $p->{$_} and length $p->{$_};} keys %$p;} @{$vars{"panels"} or [];}) ? 1 : 0
ABresult
1X1
011
000
Truth table50%1/2
A
A= $vars{'panels'}
Aresult
11
00
1243 $vars{wants_plot} ||= ($name eq 'dashboard' && !$dash_full
1244 && (ref $over ne 'HASH'
1245 || ($over->{template} || '') ne 'dashedit')
1246 12 ▶ && @{ $vars{panels} || [] }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'dashboard' && !$dash_full && (ref $over ne 'HASH' || ($over->{'template'} || '') ne 'dashedit') && @{$vars{'panels'} or [];} ? : 2 9 11
Condition78%15/19
$vars{"wants_plot"} ||= $name eq "dashboard" && !$dash_full && (ref $over ne "HASH" || ($over->{"template"} || "") ne "dashedit") && @{$vars{"panels"} or [];} ? 1 : 0
l!l&&r!l&&!r
129
$name eq "dashboard" and not $dash_full and ref $over ne "HASH" || ($over->{"template"} || "") ne "dashedit" and @{$vars{"panels"} or [];}
!ll&&!rl&&r
902
$name eq "dashboard" and not $dash_full and ref $over ne "HASH" || ($over->{"template"} || "") ne "dashedit"
!ll&&!rl&&r
812
$name eq "dashboard" and not $dash_full
!ll&&!rl&&r
803
ref $over ne "HASH" or ($over->{"template"} || "") ne "dashedit"
l!l&&r!l&&!r
021
$over->{'template'} || ''
l!l
12
$vars{'panels'} || []
l!l
20
MC/DC0%0/6
$vars{"wants_plot"} ||= $name eq "dashboard" && !$dash_full && (ref $over ne "HASH" || ($over->{"template"} || "") ne "dashedit") && @{$vars{"panels"} or [];} ? 1 : 0
$vars{"wants_plot"} $name eq "dashboard" not $dash_full ref $over ne "HASH" $over->{'template'} @{$vars{"panels"} or [];}
MC/DC0%0/1
$vars{'panels'} || []
$vars{'panels'}
Truth table0%0/8
A ||= B and not(C) and D or E and F
A= $vars{"wants_plot"}B= $name eq "dashboard"C= not $dash_fullD= ref $over ne "HASH"E= $over->{'template'}F= @{$vars{"panels"} or [];}
ABCDEFresult
1XXXXX1
0111X11
0110111
00XXXX0
010XXX0
01100X0
0111X00
0110100
Truth table50%1/2
A
A= $vars{'panels'}
Aresult
11
00
1247  
1248 12 ▶ if (my $cb = $st->{opts}{stats}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $st->{"opts"}{"stats"}) 1 11 12
1249 1 my $extra = eval { $cb->($c, $name, $store) };
1250 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
1251 1 my $why = $@; $why =~ s/\s+\z//;
1252 1 Carp::carp("Punk::Plugin::Observe: the stats callback died on "
1253 . "the $name screen - $why");
1254 }
1255 1 ▶ %vars = (%vars, %$extra) if ref $extra eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $extra eq "HASH" 0 1 1
1256 }
1257  
1258 12 my ($status, $use);
1259 12 ▶ if (ref $over eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $over eq "HASH") 9 3 12
1260 9 $status = delete $over->{status};
1261 9 $use = delete $over->{template};
1262 9 %vars = (%vars, %$over);
1263 }
1264  
1265 12 ▶ if ($vars{editing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"editing"}) 1 11 12
1266 1 my @viz = Punk::Observe::Dashboard::viz_names();
1267 1 $vars{viz_options} = [ map { { name => $_ } } @viz ];
1268 1 ▶ for my $p (@{ $vars{panels} || [] }) {
Condition50%1/2
$vars{'panels'} || []
l!l
10
MC/DC0%0/1
$vars{'panels'} || []
$vars{'panels'}
Truth table50%1/2
A
A= $vars{'panels'}
Aresult
11
00
1269 2 ▶ my $cur = $p->{viz} || 'line';
Condition50%1/2
$p->{'viz'} || 'line'
l!l
20
MC/DC0%0/1
$p->{'viz'} || 'line'
$p->{'viz'}
Truth table50%1/2
A
A= $p->{'viz'}
Aresult
11
00
1270 2 ▶ $p->{viz_options} = [ map { { name => $_,
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $cur ? : 2 8 10
1271 current => ($_ eq $cur ? 1 : 0) } } @viz ];
1272 }
1273 }
1274  
1275 0 ▶ if (my $f = $vars{field}) { $vars{"field_$f"} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $vars{"field"}) 0 12 12
1276  
1277 12 ▶ my $tmpl = ($use || $name) . '.tmpl';
Condition66%2/3
$use || $name
l!l&&r!l&&!r
390
MC/DC0%0/2
$use || $name
$use $name
Truth table66%2/3
A || B
A= $useB= $name
ABresult
1X1
011
000
1278 12 my $html = eval { $st->{stencil}->render($tmpl, \%vars) };
1279 12 ▶ return $c->status(500)->text("render failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
1280 # The name-or-source guess: a template name that transiently fails to
1281 # resolve renders AS SOURCE - a 200 whose whole body is the file name.
1282 # See _fragment_html; a page five words long is a 500, not an answer.
1283 12 ▶ return $c->status(500)->text("template '$tmpl' did not resolve\n")
Branch: 1 branch
BranchTrueFalseTotal
if defined $html and $html =~ /\A\s*\Q$tmpl\E\s*\z/ 1 11 12
Condition66%2/3
defined $html and $html =~ /\A\s*\Q$tmpl\E\s*\z/
!ll&&!rl&&r
0111
MC/DC50%1/2
defined $html and $html =~ /\A\s*\Q$tmpl\E\s*\z/
defined $html $html =~ /\A\s*\Q$tmpl\E\s*\z/
Truth table66%2/3
A and B
A= defined $htmlB= $html =~ /\A\s*\Q$tmpl\E\s*\z/
ABresult
0X0
100
111
1284 if defined $html && $html =~ /\A\s*\Q$tmpl\E\s*\z/;
1285 11 ▶ $c->status($status) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 11 11
1286 11 return $c->html($html);
1287 }
1288  
1289 0 ▶sub _stream {
Subroutine _stream: not called CC=13 SCAR=52.0
1290 0 my ($st, $c) = @_;
1291 0 my $store = store_for($st, _request_tenant($st, $c));
1292  
1293 0 $c->header('Content-Type' => 'text/event-stream');
1294 0 $c->header('Cache-Control' => 'no-cache');
1295 0 $c->header('X-Accel-Buffering' => 'no');
1296  
1297 0 my $req = _params($st, $c);
1298 0 my $q = $req->{q};
1299 0 ▶ $q = 'log' unless defined $q && $q =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q and $q =~ /\S/ 0 0 0
Condition0%0/3
defined $q and $q =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $q and $q =~ /\S/
defined $q $q =~ /\S/
Truth table0%0/3
A and B
A= defined $qB= $q =~ /\S/
ABresult
0X0
100
111
1300  
1301 0 my $since = eval { $c->param('since') };
1302 0 ▶ $since = eval { $c->req->header('Last-Event-ID') } unless defined $since;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $since 0 0 0
1303 0 ▶ $since = undef unless defined $since && $since =~ /\A\d+\z/ && $since > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $since and $since =~ /\A\d+\z/ and $since > 0 0 0 0
Condition0%0/6
defined $since and $since =~ /\A\d+\z/ and $since > 0
!ll&&!rl&&r
000
defined $since and $since =~ /\A\d+\z/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $since and $since =~ /\A\d+\z/ and $since > 0
defined $since $since =~ /\A\d+\z/ $since > 0
Truth table0%0/4
A and B and C
A= defined $sinceB= $since =~ /\A\d+\z/C= $since > 0
ABCresult
0XX0
10X0
1100
1111
1304  
1305 0 my $body = sprintf("retry: %d\n\n", 2000);
1306 0 ▶ if ($store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store) 0 0 0
1307 0 ▶ my $r = eval { $store->query($q, limit => 200) } || {};
Condition0%0/2
eval { do { $store->query($q, 'limit', 200) } } || {}
l!l
00
MC/DC0%0/1
eval { do { $store->query($q, 'limit', 200) } } || {}
eval { do { $store->query($q, 'limit', 200) } }
Truth table0%0/2
A
A= eval { do { $store->query($q, 'limit', 200) } }
Aresult
11
00
1308 0 ▶ my @rows = reverse @{ $r->{rows} || [] };
Condition0%0/2
$r->{'rows'} || []
l!l
00
MC/DC0%0/1
$r->{'rows'} || []
$r->{'rows'}
Truth table0%0/2
A
A= $r->{'rows'}
Aresult
11
00
1309 0 ▶ if (defined $since) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $since) { } 0 0 0
elsif (@rows > 50) { } 0 0 0
1310 0 @rows = grep { Punk::Observe::Store::ncmp($_->{t}, $since) > 0 } @rows;
1311 }
1312 elsif (@rows > 50) {
1313 0 splice @rows, 0, @rows - 50;
1314 }
1315 0 for my $row (@rows) {
1316 0 $body .= Punk::Observe::Live::sse($row->{t}, 'log',
1317 _tail_json($row));
1318 }
1319 }
1320 0 $body .= Punk::Observe::Live::heartbeat();
1321 0 return $c->text($body);
1322 }
1323  
1324 0 ▶sub _tail_json {
Subroutine _tail_json: not called CC=3 SCAR=24.8
1325 0 my ($row) = @_;
1326 0 ▶ return '{' . join(',', map { _json_pair(@$_) }
Branch: 2 branches
BranchTrueFalseTotal
defined $row->{'service'} ? : 0 0 0
defined $row->{'body'} ? : 0 0 0
1327 [ 'id', Punk::Observe::View::record_id($row) ],
1328 [ 'time', Punk::Observe::View::fmt_time($row->{t}) ],
1329 [ 'sev_name', Punk::Observe::View::severity_name($row->{severity}) ],
1330 [ 'service', (defined $row->{service} ? $row->{service} : '') ],
1331 0 [ 'body', (defined $row->{body} ? $row->{body} : '') ],
1332 ) . '}';
1333 }
1334  
1335 0 ▶sub _json_pair {
Subroutine _json_pair: not called CC=2 SCAR=17.9
1336 0 my ($k, $v) = @_;
1337 0 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
1338 0 $v =~ s/([\\"])/\\$1/g;
1339 0 $v =~ s/([\x00-\x1f])/sprintf('\\u%04x', ord $1)/ge;
1340 0 return "\"$k\":\"$v\"";
1341 }
1342  
1343 0 ▶sub _asset {
Subroutine _asset: not called CC=9 SCAR=45.0
1344 0 my ($st, $c, $name) = @_;
1345 0 ▶ $name = 'observe.css' unless defined $name && $ASSET_TYPE{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $ASSET_TYPE{$name} 0 0 0
Condition0%0/3
defined $name and $ASSET_TYPE{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and $ASSET_TYPE{$name}
defined $name $ASSET_TYPE{$name}
Truth table0%0/3
A and B
A= defined $nameB= $ASSET_TYPE{$name}
ABresult
0X0
100
111
1346 0 ▶ my $path = File::Spec->catfile($st->{root} || '', 'static', $name);
Condition0%0/2
$st->{'root'} || ''
l!l
00
MC/DC0%0/1
$st->{'root'} || ''
$st->{'root'}
Truth table0%0/2
A
A= $st->{'root'}
Aresult
11
00
1347 0 ▶ return $c->status(404)->text("no such asset\n") unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 0 0
1348  
1349 0 my $enc;
1350 0 my $ae = eval { $c->req->header('Accept-Encoding') };
1351 0 ▶ if (defined $ae && $ae =~ /\bgzip\b/ && -f "$path.gz") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ae and $ae =~ /\bgzip\b/ and -f "$path.gz") 0 0 0
Condition0%0/6
defined $ae and $ae =~ /\bgzip\b/ and -f "$path.gz"
!ll&&!rl&&r
000
defined $ae and $ae =~ /\bgzip\b/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $ae and $ae =~ /\bgzip\b/ and -f "$path.gz"
defined $ae $ae =~ /\bgzip\b/ -f "$path.gz"
Truth table0%0/4
A and B and C
A= defined $aeB= $ae =~ /\bgzip\b/C= -f "$path.gz"
ABCresult
0XX0
10X0
1100
1111
1352 0 $path .= '.gz';
1353 0 $enc = 'gzip';
1354 }
1355  
1356 0 $c->header('Vary' => 'Accept-Encoding');
1357 0 ▶ $c->header('Content-Encoding' => $enc) if $enc;
Branch: 1 branch
BranchTrueFalseTotal
if $enc 0 0 0
1358  
1359 return $c->send_file($path,
1360 0 type => $ASSET_TYPE{$name},
1361 cache_control => 'no-cache',
1362 missing => 'not_found');
1363 }
1364  
1365 16 my $FAVICON = <<'SVG';
1366 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
1367 <rect width="32" height="32" rx="7" fill="#151821"/>
1368 <path d="M4 21 L10 21 L13 11 L17 25 L20 17 L23 17"
1369 fill="none" stroke="#6ee7b7" stroke-width="2.5"
1370 stroke-linecap="round" stroke-linejoin="round"/>
1371 <circle cx="26" cy="9" r="3.5" fill="#f87171"/>
1372 </svg>
1373 SVG
1374  
1375 0 ▶sub _favicon {
Subroutine _favicon: not called CC=4 SCAR=30.0
1376 0 my ($st, $c) = @_;
1377 0 ▶ $st->{favicon_etag} //= sprintf('"%08x"', length($FAVICON) ^ 0x5f5f5f5f);
Condition0%0/2
$st->{"favicon_etag"} //= sprintf("\"%08x\"", length $FAVICON ^ 1600085855)
l!l
00
MC/DC0%0/1
$st->{"favicon_etag"} //= sprintf("\"%08x\"", length $FAVICON ^ 1600085855)
$st->{"favicon_etag"}
Truth table0%0/2
A
A= $st->{"favicon_etag"}
Aresult
11
00
1378 0 ▶ my $inm = eval { $c->req->header('If-None-Match') } || '';
Condition0%0/2
eval { do { $c->req->header('If-None-Match') } } || ''
l!l
00
MC/DC0%0/1
eval { do { $c->req->header('If-None-Match') } } || ''
eval { do { $c->req->header('If-None-Match') } }
Truth table0%0/2
A
A= eval { do { $c->req->header('If-None-Match') } }
Aresult
11
00
1379 0 ▶ return $c->status(304)->text('') if $inm eq $st->{favicon_etag};
Branch: 1 branch
BranchTrueFalseTotal
if $inm eq $st->{"favicon_etag"} 0 0 0
1380 0 $c->header('Content-Type' => 'image/svg+xml');
1381 0 $c->header('ETag' => $st->{favicon_etag});
1382 0 $c->header('Cache-Control' => 'public, max-age=86400');
1383 0 return $c->text($FAVICON);
1384 }
1385  
1386 14 ▶sub _register_ingest {
Subroutine _register_ingest: called CC=4 SCAR=17.3
1387 14 my ($st) = @_;
1388 14 ▶ my $i = $st->{ingest} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $i = $st->{"ingest"} or return 2 12 14
1389 2 my $app = $st->{app};
1390  
1391 2 my $keys = _keyring($st);
1392 2 my $store = $st->{store};
1393  
1394 my $recv = Punk::Observe::Ingest->new(
1395 max_body => $i->{max_body},
1396 0 ▶ auth => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1397 0 my ($env) = @_;
1398 0 ▶ return undef unless _key_ok($keys, $env);
Branch: 1 branch
BranchTrueFalseTotal
unless _key_ok($keys, $env) 0 0 0
1399 my $t = Punk::Observe::Tenant::resolve(
1400 0 $st->{tenant}{fixed}, $st->{tenant}{resolver});
1401 0 ▶ return $t->{ok} ? $t->{tenant} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$t->{'ok'} ? : 0 0 0
1402 },
1403 0 ▶ on_batch => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1404 0 my ($tenant, $signal, $body, $encoding, $out) = @_;
1405 0 return _persist($st, $tenant, $signal, $body, $encoding, $out);
1406 },
1407 2 );
1408  
1409 2 $st->{receiver} = $recv;
1410 2 my $inner = $recv->to_app;
1411 0 ▶ $st->{ingest_app} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1412 0 my ($env) = @_;
1413 0 ▶ local $env->{PATH_INFO} = '/v1' . ($env->{PATH_INFO} // '');
Condition0%0/2
$env->{"PATH_INFO"} // ""
l!l
00
MC/DC0%0/1
$env->{"PATH_INFO"} // ""
$env->{"PATH_INFO"}
Truth table0%0/2
A
A= $env->{"PATH_INFO"}
Aresult
11
00
1414 0 return $inner->($env);
1415 2 };
1416  
1417 2 ▶ return unless $app && $app->can('mount');
Branch: 1 branch
BranchTrueFalseTotal
unless $app and $app->can("mount") 1 1 2
Condition66%2/3
$app and $app->can("mount")
!ll&&!rl&&r
101
MC/DC50%1/2
$app and $app->can("mount")
$app $app->can("mount")
Truth table66%2/3
A and B
A= $appB= $app->can("mount")
ABresult
0X0
100
111
1418 1 $app->mount($i->{prefix} => $st->{ingest_app});
1419 1 return $st->{ingest_app};
1420 }
1421  
1422 2 ▶sub _keyring {
Subroutine _keyring: called CC=8 SCAR=37.9
1423 2 my ($st) = @_;
1424 2 my $path = $st->{ingest}{keys};
1425 2 ▶ return undef unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 2 0 2
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
200
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
1426  
1427 0 ▶ open my $fh, '<', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or Carp::croak("Punk::Plugin::Observe: cannot read key file ${path}: $!") 0 0 0
1428 or Carp::croak("Punk::Plugin::Observe: cannot read key file $path: $!");
1429 0 my @pairs;
1430 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
1431 0 chomp $line;
1432 0 ▶ next if $line =~ /^\s*(?:#|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*(?:#|$)/ 0 0 0
1433 0 my ($name, $token) = split /\s+/, $line, 2;
1434 0 ▶ next unless defined $token && length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 0 0 0
Condition0%0/3
defined $token and length $token
!ll&&!rl&&r
000
MC/DC0%0/2
defined $token and length $token
defined $token length $token
Truth table0%0/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
1435 0 push @pairs, $name, $token;
1436 }
1437 0 close $fh;
1438 0 return \@pairs;
1439 }
1440  
1441 0 ▶sub _key_ok {
Subroutine _key_ok: not called CC=4 SCAR=30.0
1442 0 my ($keys, $env) = @_;
1443 0 ▶ return 1 unless $keys && @$keys;
Branch: 1 branch
BranchTrueFalseTotal
unless $keys and @$keys 0 0 0
Condition0%0/3
$keys and @$keys
!ll&&!rl&&r
000
MC/DC0%0/2
$keys and @$keys
$keys @$keys
Truth table0%0/3
A and B
A= $keysB= @$keys
ABresult
0X0
100
111
1444 0 my $r = Punk::Observe::Key::check($keys, $env->{HTTP_AUTHORIZATION}, undef);
1445 0 ▶ return $r->{ok} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$r->{'ok'} ? : 0 0 0
1446 }
1447  
1448 0 ▶sub _persist {
Subroutine _persist: not called CC=22 SCAR=62.3
1449 0 my ($st, $tenant, $signal, $body, $encoding, $out) = @_;
1450  
1451 0 ▶ my $json = ($encoding || '') eq 'json';
Condition0%0/2
$encoding || ''
l!l
00
MC/DC0%0/1
$encoding || ''
$encoding
Truth table0%0/2
A
A= $encoding
Aresult
11
00
1452 0 my $doc = $body;
1453 0 ▶ if ($json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json) 0 0 0
1454 0 $doc = eval { File::Raw::JSON::file_json_decode($body) };
1455 0 ▶ return 0 unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 0 0
1456 }
1457  
1458 0 ▶ my $want = $st->{opts}{on_records} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st->{'opts'}{'on_records'} ? : 0 0 0
1459 0 my $store = store_for($st, $tenant);
1460 0 ▶ my $path = $store ? $store->wal_path : '';
Branch: 1 branch
BranchTrueFalseTotal
$store ? : 0 0 0
1461  
1462 0 my $r = eval {
1463 Punk::Observe::Ingest::decode_append(
1464 $path, $doc, $signal, $json ? 'json' : 'protobuf',
1465 1, '200000000', $want,
1466 $st->{arena} ? $st->{arena}{handle} : 0,
1467 $st->{limits}{rate_records} || 0,
1468 0 ▶ $st->{limits}{rate_bytes} || 0);
Branch: 2 branches
BranchTrueFalseTotal
$json ? : 0 0 0
$st->{'arena'} ? : 0 0 0
Condition0%0/4
$st->{'limits'}{'rate_records'} || 0
l!l
00
$st->{'limits'}{'rate_bytes'} || 0
l!l
00
MC/DC0%0/1
$st->{'limits'}{'rate_records'} || 0
$st->{'limits'}{'rate_records'}
MC/DC0%0/1
$st->{'limits'}{'rate_bytes'} || 0
$st->{'limits'}{'rate_bytes'}
Truth table0%0/2
A
A= $st->{'limits'}{'rate_records'}
Aresult
11
00
Truth table0%0/2
A
A= $st->{'limits'}{'rate_bytes'}
Aresult
11
00
1469 };
1470 0 ▶ if (!$r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 0 0
1471 0 ▶ warn "Punk::Plugin::Observe: ingest failed: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1472 0 return 0;
1473 }
1474  
1475 0 ▶ return 0 unless $r->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 0 0 0
1476  
1477 0 ▶ if ($want && $r->{records}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want and $r->{"records"}) 0 0 0
Condition0%0/3
$want and $r->{"records"}
!ll&&!rl&&r
000
MC/DC0%0/2
$want and $r->{"records"}
$want $r->{"records"}
Truth table0%0/3
A and B
A= $wantB= $r->{"records"}
ABresult
0X0
100
111
1478 0 eval { $st->{opts}{on_records}->($tenant, $signal, $r->{records}) };
1479 }
1480  
1481 0 ▶ if (!$r->{appended}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"appended"}) 0 0 0
1482 warn "Punk::Plugin::Observe: WAL append failed: $r->{errno}\n"
1483 0 ▶ if $r->{errno};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"errno"} 0 0 0
1484 0 return 0;
1485 }
1486  
1487 0 $st->{written} += $r->{n};
1488  
1489 $out->{rejected} = ($out->{rejected} || 0) + $r->{rejected}
1490 0 ▶ if ref $out eq 'HASH' && $r->{rejected};
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "HASH" and $r->{"rejected"} 0 0 0
Condition0%0/5
ref $out eq "HASH" and $r->{"rejected"}
!ll&&!rl&&r
000
$out->{'rejected'} || 0
l!l
00
MC/DC0%0/2
ref $out eq "HASH" and $r->{"rejected"}
ref $out eq "HASH" $r->{"rejected"}
MC/DC0%0/1
$out->{'rejected'} || 0
$out->{'rejected'}
Truth table0%0/3
A and B
A= ref $out eq "HASH"B= $r->{"rejected"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $out->{'rejected'}
Aresult
11
00
1491  
1492 0 ▶ $store->seal_if_full($r->{bytes} || 0) if $store;
Branch: 1 branch
BranchTrueFalseTotal
if $store 0 0 0
Condition0%0/2
$r->{'bytes'} || 0
l!l
00
MC/DC0%0/1
$r->{'bytes'} || 0
$r->{'bytes'}
Truth table0%0/2
A
A= $r->{'bytes'}
Aresult
11
00
1493 0 return 1;
1494 }
1495  
1496 16 1;
1497  
1498 __END__
↑ Summary blib/lib/Punk/Observe/Ingest.pm »