blib/lib/Punk/Observe/Config.pm

filter: Statementstmt 92.9% 301 / 324 Branchbran 65.6% 155 / 236 Conditioncond 51.8% 83 / 160 MC/DCmcdc 27.9% 26 / 93 Subroutinesub 94.1% 48 / 51 Podpod 100% 22 / 22 totaltotal 71.6% 635 / 886 CC 189 SCAR 25.7
CC
189
Cov
75%
save_health_target
33.3
save_alert
30.4
alerts
16.6
CRAP 13.0
« blib/lib/Punk/Observe/Warm.pm ↑ Summary blib/lib/Punk/Observe/Backend.pm »
1 package Punk::Observe::Config;
2  
3 27 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use Punk::Observe ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use Punk::Observe::Dashboard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use Punk::Observe::Query ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Punk::Observe::Store ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Punk::Observe::View ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Punk::Observe::Target ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 27 our $VERSION = $Punk::Observe::VERSION;
15  
16 65 ▶sub ok { return { ok => 1, id => $_[0] } }
Subroutine ok: called CC=1 SCAR=0.0
Pod documented
17  
18 23 ▶sub refused { return { ok => 0, refused => 1, error => $_[0], field => $_[1] } }
Subroutine refused: called CC=1 SCAR=0.0
Pod documented
19 0 ▶sub failed { my $e = $_[0]; $e =~ s/\s+\z//; return { ok => 0, error => $e } }
Subroutine failed: not called CC=1 SCAR=6.9
Pod documented
20  
21 87 ▶sub _guard {
Subroutine _guard: called CC=4 SCAR=15.6
22 87 my ($code) = @_;
23 87 my @r = eval { $code->() };
24 87 ▶ return failed($@ || 'the configuration store did not say why') if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 87 87
Condition0%0/2
$@ || 'the configuration store did not say why'
l!l
00
MC/DC0%0/1
$@ || 'the configuration store did not say why'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
25 87 ▶ return wantarray ? @r : $r[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 87 87
26 }
27  
28 24 ▶sub dashboards {
Subroutine dashboards: called CC=11 SCAR=25.1
Pod documented
29 24 my ($db, $tenant, $slug) = @_;
30 24 my $dbh = $db->dbh;
31  
32 24 my $list = $dbh->selectall_arrayref(
33 'SELECT slug, title FROM dashboards WHERE tenant = ? ORDER BY title',
34 { Slice => {} }, $tenant);
35  
36 24 ▶ $slug = $list->[0]{slug} if (!defined $slug || !length $slug) && @$list;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $slug && length $slug and @$list 5 19 24
Condition66%4/6
not defined $slug && length $slug and @$list
!ll&&!rl&&r
1905
defined $slug && length $slug
!ll&&!rl&&r
5019
MC/DC33%1/3
not defined $slug && length $slug and @$list
defined $slug length $slug @$list
Truth table40%2/5
not(A && B) and C
A= defined $slugB= length $slugC= @$list
ABCresult
11X0
0X00
1000
0X11
1011
37 24 ▶ return { list => $list, panels => [], cols => 2, can_edit => 1,
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slug and length $slug 0 24 24
Condition33%1/3
defined $slug and length $slug
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $slug and length $slug
defined $slug length $slug
Truth table33%1/3
A and B
A= defined $slugB= length $slug
ABresult
0X0
100
111
38 title => 'Dashboards' }
39 unless defined $slug && length $slug;
40  
41 24 my $d = $dbh->selectrow_hashref(
42 'SELECT id, slug, title, cols FROM dashboards
43 WHERE tenant = ? AND slug = ?', undef, $tenant, $slug);
44 24 ▶ return { list => $list, panels => [], cols => 2, can_edit => 1,
Branch: 1 branch
BranchTrueFalseTotal
unless $d 1 23 24
45 title => $slug, missing => 1 }
46 unless $d;
47  
48 my $panels = $dbh->selectall_arrayref(
49 'SELECT id, position, title, query, viz, span
50 FROM dashboard_panels WHERE dashboard_id = ? ORDER BY position, id',
51 23 { Slice => {} }, $d->{id});
52  
53 23 my %known = map { $_ => 1 } Punk::Observe::Dashboard::viz_names();
54 23 for my $p (@$panels) {
55 $p->{viz_unknown} = 1
56 43 ▶ if defined $p->{viz} && length $p->{viz} && !$known{ $p->{viz} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{"viz"} and length $p->{"viz"} and not $known{$p->{"viz"}} 1 42 43
Condition50%3/6
defined $p->{"viz"} and length $p->{"viz"} and not $known{$p->{"viz"}}
!ll&&!rl&&r
0421
defined $p->{"viz"} and length $p->{"viz"}
!ll&&!rl&&r
0043
MC/DC33%1/3
defined $p->{"viz"} and length $p->{"viz"} and not $known{$p->{"viz"}}
defined $p->{"viz"} length $p->{"viz"} not $known{$p->{"viz"}}
Truth table50%2/4
A and B and not(C)
A= defined $p->{"viz"}B= length $p->{"viz"}C= not $known{$p->{"viz"}}
ABCresult
0XX0
10X0
1100
1111
57 }
58  
59 return {
60 id => $d->{id}, slug => $d->{slug}, title => $d->{title},
61 23 cols => $d->{cols}, list => $list, panels => $panels, can_edit => 1,
62 };
63 }
64  
65 18 ▶sub save_dashboard {
Subroutine save_dashboard: called CC=9 SCAR=22.1
Pod documented
66 18 my ($db, $tenant, $in) = @_;
67 18 ▶ my $slug = defined $in->{slug} ? $in->{slug} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'slug'} ? : 17 1 18
68 18 ▶ return refused('a dashboard needs a slug', 'slug') unless length $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless length $slug 1 17 18
69 17 ▶ return refused('a slug is letters, digits, dash and underscore', 'slug')
Branch: 1 branch
BranchTrueFalseTotal
if $slug =~ /[^A-Za-z0-9_-]/ 2 15 17
70 if $slug =~ /[^A-Za-z0-9_-]/;
71 return refused('a dashboard needs a title', 'title')
72 15 ▶ unless defined $in->{title} && length $in->{title};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in->{"title"} and length $in->{"title"} 2 13 15
Condition66%2/3
defined $in->{"title"} and length $in->{"title"}
!ll&&!rl&&r
2013
MC/DC50%1/2
defined $in->{"title"} and length $in->{"title"}
defined $in->{"title"} length $in->{"title"}
Truth table66%2/3
A and B
A= defined $in->{"title"}B= length $in->{"title"}
ABresult
0X0
100
111
73  
74 13 ▶ my $cols = int($in->{cols} || 2);
Condition100%2/2
$in->{'cols'} || 2
l!l
310
MC/DC100%1/1
$in->{'cols'} || 2
$in->{'cols'}
Truth table100%2/2
A
A= $in->{'cols'}
Aresult
11
00
75 13 ▶ $cols = 1 if $cols < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $cols < 1 0 13 13
76 13 ▶ $cols = 6 if $cols > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $cols > 6 1 12 13
77  
78 13 ▶ return _guard(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
79 13 my $dbh = $db->dbh;
80 13 my $now = time;
81 13 my ($id) = $dbh->selectrow_array(
82 'SELECT id FROM dashboards WHERE tenant = ? AND slug = ?',
83 undef, $tenant, $slug);
84 13 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 13 13
85 $dbh->do('UPDATE dashboards SET title = ?, cols = ?, updated_at = ?
86 0 WHERE id = ?', undef, $in->{title}, $cols, $now, $id);
87 }
88 else {
89 $dbh->do('INSERT INTO dashboards
90 (tenant, slug, title, cols, created_at, updated_at)
91 VALUES (?, ?, ?, ?, ?, ?)',
92 13 undef, $tenant, $slug, $in->{title}, $cols, $now, $now);
93 13 ($id) = $dbh->selectrow_array(
94 'SELECT id FROM dashboards WHERE tenant = ? AND slug = ?',
95 undef, $tenant, $slug);
96 }
97 13 return ok($id);
98 13 });
99 }
100  
101 1 ▶sub delete_dashboard {
Subroutine delete_dashboard: called CC=3 SCAR=12.7
Pod documented
102 1 my ($db, $tenant, $slug) = @_;
103 1 ▶ return refused('a dashboard needs a slug')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slug and length $slug 0 1 1
Condition33%1/3
defined $slug and length $slug
!ll&&!rl&&r
001
MC/DC0%0/2
defined $slug and length $slug
defined $slug length $slug
Truth table33%1/3
A and B
A= defined $slugB= length $slug
ABresult
0X0
100
111
104 unless defined $slug && length $slug;
105 1 ▶ return _guard(sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
106 1 my $n = $db->dbh->do('DELETE FROM dashboards WHERE tenant = ? AND slug = ?',
107 undef, $tenant, $slug);
108 1 ▶ return $n && $n != 0 ? ok(undef) : refused('no such dashboard');
Branch: 1 branch
BranchTrueFalseTotal
$n && $n != 0 ? : 1 0 1
Condition33%1/3
$n and $n != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$n and $n != 0
$n $n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
109 1 });
110 }
111  
112 18 ▶sub save_panel {
Subroutine save_panel: called CC=11 SCAR=26.8
Pod documented
113 18 my ($db, $tenant, $slug, $in) = @_;
114  
115 my $chk = Punk::Observe::Dashboard::check_panel({
116 title => $in->{title},
117 query => $in->{query},
118 viz => (defined $in->{viz} && length $in->{viz})
119 ? $in->{viz} : 'line',
120 position => $in->{position} || 0,
121 18 ▶ span => $in->{span} || 1,
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'viz'} && length $in->{'viz'} ? : 5 13 18
Condition71%5/7
defined $in->{"viz"} and length $in->{"viz"}
!ll&&!rl&&r
1305
$in->{'position'} || 0
l!l
018
$in->{'span'} || 1
l!l
216
MC/DC50%1/2
defined $in->{"viz"} and length $in->{"viz"}
defined $in->{"viz"} length $in->{"viz"}
MC/DC0%0/1
$in->{'position'} || 0
$in->{'position'}
MC/DC100%1/1
$in->{'span'} || 1
$in->{'span'}
Truth table66%2/3
A and B
A= defined $in->{"viz"}B= length $in->{"viz"}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $in->{'position'}
Aresult
11
00
Truth table100%2/2
A
A= $in->{'span'}
Aresult
11
00
122 });
123 18 ▶ unless ($chk->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chk->{"ok"}) 3 15 18
124 3 ▶ my $f = ($chk->{code} || 0) == 3 || ($chk->{code} || 0) == 4
Branch: 1 branch
BranchTrueFalseTotal
($chk->{'code'} || 0) == 3 || ($chk->{'code'} || 0) == 4 ? : 1 2 3
Condition57%4/7
($chk->{"code"} || 0) == 3 or ($chk->{"code"} || 0) == 4
l!l&&r!l&&!r
102
$chk->{'code'} || 0
l!l
30
$chk->{'code'} || 0
l!l
20
MC/DC0%0/2
($chk->{"code"} || 0) == 3 or ($chk->{"code"} || 0) == 4
$chk->{'code'} $chk->{'code'}
Truth table33%1/3
A or B
A= $chk->{'code'}B= $chk->{'code'}
ABresult
1X1
011
000
125 ? 'title' : 'query';
126 3 ▶ return refused($chk->{error} || 'that panel is not valid', $f);
Condition50%1/2
$chk->{'error'} || 'that panel is not valid'
l!l
30
MC/DC0%0/1
$chk->{'error'} || 'that panel is not valid'
$chk->{'error'}
Truth table50%1/2
A
A= $chk->{'error'}
Aresult
11
00
127 }
128  
129 15 ▶ return _guard(sub {
Subroutine __ANON__: called CC=10 SCAR=27.8
130 15 my $dbh = $db->dbh;
131 15 my ($did) = $dbh->selectrow_array(
132 'SELECT id FROM dashboards WHERE tenant = ? AND slug = ?',
133 undef, $tenant, $slug);
134 15 ▶ return refused('no such dashboard') unless $did;
Branch: 1 branch
BranchTrueFalseTotal
unless $did 1 14 15
135  
136 14 ▶ my $span = int($in->{span} || 1);
Condition100%2/2
$in->{'span'} || 1
l!l
212
MC/DC100%1/1
$in->{'span'} || 1
$in->{'span'}
Truth table100%2/2
A
A= $in->{'span'}
Aresult
11
00
137 14 ▶ $span = 1 if $span < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $span < 1 0 14 14
138 14 ▶ $span = 6 if $span > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $span > 6 0 14 14
139 14 ▶ my $pos = int($in->{position} || 0);
Condition50%1/2
$in->{'position'} || 0
l!l
014
MC/DC0%0/1
$in->{'position'} || 0
$in->{'position'}
Truth table50%1/2
A
A= $in->{'position'}
Aresult
11
00
140 14 ▶ $pos = 0 if $pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 14 14
141  
142 14 ▶ if (my $id = $in->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $in->{"id"}) 0 14 14
143 my $n = $dbh->do(
144 'UPDATE dashboard_panels
145 SET position = ?, title = ?, query = ?, viz = ?, span = ?
146 WHERE id = ? AND dashboard_id = ?',
147 0 undef, $pos, $in->{title}, $in->{query}, $chk->{viz}, $span,
148 $id, $did);
149 0 ▶ return refused('no such panel') unless $n && $n != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $n and $n != 0 0 0 0
Condition0%0/3
$n and $n != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$n and $n != 0
$n $n != 0
Truth table0%0/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
150 0 return ok($id);
151 }
152 $dbh->do('INSERT INTO dashboard_panels
153 (dashboard_id, position, title, query, viz, span)
154 VALUES (?, ?, ?, ?, ?, ?)',
155 undef, $did, $pos, $in->{title}, $in->{query}, $chk->{viz},
156 14 $span);
157 14 return ok(undef);
158 15 });
159 }
160  
161 3 ▶sub save_panels {
Subroutine save_panels: called CC=14 SCAR=27.7
Pod documented
162 3 my ($db, $tenant, $slug, $rows) = @_;
163  
164 3 my @checked;
165 3 for my $in (@$rows) {
166 my $chk = Punk::Observe::Dashboard::check_panel({
167 title => $in->{title},
168 query => $in->{query},
169 viz => (defined $in->{viz} && length $in->{viz})
170 ? $in->{viz} : 'line',
171 position => $in->{position} || 0,
172 6 ▶ span => $in->{span} || 1,
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'viz'} && length $in->{'viz'} ? : 2 4 6
Condition85%6/7
defined $in->{"viz"} and length $in->{"viz"}
!ll&&!rl&&r
402
$in->{'position'} || 0
l!l
15
$in->{'span'} || 1
l!l
24
MC/DC50%1/2
defined $in->{"viz"} and length $in->{"viz"}
defined $in->{"viz"} length $in->{"viz"}
MC/DC100%1/1
$in->{'position'} || 0
$in->{'position'}
MC/DC100%1/1
$in->{'span'} || 1
$in->{'span'}
Truth table66%2/3
A and B
A= defined $in->{"viz"}B= length $in->{"viz"}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $in->{'position'}
Aresult
11
00
Truth table100%2/2
A
A= $in->{'span'}
Aresult
11
00
173 });
174 6 ▶ unless ($chk->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chk->{"ok"}) 1 5 6
175 1 ▶ my $f = ($chk->{code} || 0) == 3 || ($chk->{code} || 0) == 4
Branch: 1 branch
BranchTrueFalseTotal
($chk->{'code'} || 0) == 3 || ($chk->{'code'} || 0) == 4 ? : 0 1 1
Condition42%3/7
($chk->{"code"} || 0) == 3 or ($chk->{"code"} || 0) == 4
l!l&&r!l&&!r
001
$chk->{'code'} || 0
l!l
10
$chk->{'code'} || 0
l!l
10
MC/DC0%0/2
($chk->{"code"} || 0) == 3 or ($chk->{"code"} || 0) == 4
$chk->{'code'} $chk->{'code'}
Truth table0%0/3
A or B
A= $chk->{'code'}B= $chk->{'code'}
ABresult
1X1
011
000
176 ? 'title' : 'query';
177 my $name = (defined $in->{title} && length $in->{title})
178 1 ▶ ? "'$in->{title}'" : "panel $in->{id}";
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'title'} && length $in->{'title'} ? : 1 0 1
Condition33%1/3
defined $in->{"title"} and length $in->{"title"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $in->{"title"} and length $in->{"title"}
defined $in->{"title"} length $in->{"title"}
Truth table33%1/3
A and B
A= defined $in->{"title"}B= length $in->{"title"}
ABresult
0X0
100
111
179 1 ▶ return refused(($chk->{error} || 'that panel is not valid')
Condition50%1/2
$chk->{'error'} || 'that panel is not valid'
l!l
10
MC/DC0%0/1
$chk->{'error'} || 'that panel is not valid'
$chk->{'error'}
Truth table50%1/2
A
A= $chk->{'error'}
Aresult
11
00
180 . " - in $name", $f);
181 }
182 5 push @checked, [ $in, $chk ];
183 }
184  
185 2 ▶ return _guard(sub {
Subroutine __ANON__: called CC=12 SCAR=25.4
186 2 my $dbh = $db->dbh;
187 2 my ($did) = $dbh->selectrow_array(
188 'SELECT id FROM dashboards WHERE tenant = ? AND slug = ?',
189 undef, $tenant, $slug);
190 2 ▶ return refused('no such dashboard') unless $did;
Branch: 1 branch
BranchTrueFalseTotal
unless $did 0 2 2
191 2 ▶ return ok(undef) unless @checked;
Branch: 1 branch
BranchTrueFalseTotal
unless @checked 0 2 2
192  
193 2 $dbh->begin_work;
194 2 for my $c2 (@checked) {
195 4 my ($in, $chk) = @$c2;
196 4 ▶ my $span = int($in->{span} || 1);
Condition100%2/2
$in->{'span'} || 1
l!l
22
MC/DC100%1/1
$in->{'span'} || 1
$in->{'span'}
Truth table100%2/2
A
A= $in->{'span'}
Aresult
11
00
197 4 ▶ $span = 1 if $span < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $span < 1 0 4 4
198 4 ▶ $span = 6 if $span > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $span > 6 0 4 4
199 4 ▶ my $pos = int($in->{position} || 0);
Condition100%2/2
$in->{'position'} || 0
l!l
13
MC/DC100%1/1
$in->{'position'} || 0
$in->{'position'}
Truth table100%2/2
A
A= $in->{'position'}
Aresult
11
00
200 4 ▶ $pos = 0 if $pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 4 4
201 4 my $n = eval { $dbh->do(
202 'UPDATE dashboard_panels
203 SET position = ?, title = ?, query = ?, viz = ?, span = ?
204 WHERE id = ? AND dashboard_id = ?',
205 undef, $pos, $in->{title}, $in->{query}, $chk->{viz}, $span,
206 4 $in->{id}, $did) };
207 4 ▶ unless ($n && $n != 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n and $n != 0) 1 3 4
Condition66%2/3
$n and $n != 0
!ll&&!rl&&r
013
MC/DC50%1/2
$n and $n != 0
$n $n != 0
Truth table66%2/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
208 1 my $e = $@;
209 1 eval { $dbh->rollback };
210 1 ▶ return $e ? failed($e)
Branch: 1 branch
BranchTrueFalseTotal
$e ? : 0 1 1
211 : refused("panel $in->{id} no longer exists");
212 }
213 }
214 1 $dbh->commit;
215 1 return ok(undef);
216 2 });
217 }
218  
219 3 ▶sub delete_panel {
Subroutine delete_panel: called CC=2 SCAR=7.0
Pod documented
220 3 my ($db, $tenant, $slug, $id) = @_;
221 3 ▶ return refused('a panel needs an id') unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
222 3 ▶ return _guard(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
223 3 my $n = $db->dbh->do(
224 'DELETE FROM dashboard_panels
225 WHERE id = ? AND dashboard_id IN
226 (SELECT id FROM dashboards WHERE tenant = ? AND slug = ?)',
227 undef, $id, $tenant, $slug);
228 3 ▶ return $n && $n != 0 ? ok(undef) : refused('no such panel');
Branch: 1 branch
BranchTrueFalseTotal
$n && $n != 0 ? : 2 1 3
Condition66%2/3
$n and $n != 0
!ll&&!rl&&r
012
MC/DC50%1/2
$n and $n != 0
$n $n != 0
Truth table66%2/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
229 3 });
230 }
231  
232 9 ▶sub health_targets {
Subroutine health_targets: called CC=1 SCAR=0.4
Pod documented
233 9 my ($db, $tenant, $name) = @_;
234 9 ▶ return _guard(sub {
Subroutine __ANON__: called CC=3 SCAR=13.0
235 9 my $sql = 'SELECT * FROM health_targets WHERE tenant = ?';
236 9 my @bind = ($tenant);
237 0 ▶ if (defined $name && length $name) { $sql .= ' AND name = ?'; push @bind, $name }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and length $name) 0 9 9
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
900
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
238 9 $sql .= ' ORDER BY name';
239 9 return $db->dbh->selectall_arrayref($sql, { Slice => {} }, @bind);
240 9 });
241 }
242  
243 6 ▶sub save_health_target {
Subroutine save_health_target: called CC=17 SCAR=35.0
Pod documented
244 6 my ($db, $tenant, $in, $allow) = @_;
245 6 ▶ my $name = defined $in->{name} ? $in->{name} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'name'} ? : 6 0 6
246 6 ▶ return refused('a target needs a name') unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 6 6
247 6 ▶ return refused('a name is letters, digits, dash and underscore')
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[^A-Za-z0-9_-]/ 0 6 6
248 if $name =~ /[^A-Za-z0-9_-]/;
249  
250 6 ▶ my $url = defined $in->{url} ? $in->{url} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'url'} ? : 6 0 6
251 6 ▶ return refused('a target needs a URL') unless length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless length $url 0 6 6
252  
253 6 my $t = Punk::Observe::Target::check($url, $allow);
254 6 ▶ unless ($t->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"ok"}) 0 6 6
255 0 ▶ my $why = $t->{reason} || 'that URL is refused';
Condition0%0/2
$t->{'reason'} || 'that URL is refused'
l!l
00
MC/DC0%0/1
$t->{'reason'} || 'that URL is refused'
$t->{'reason'}
Truth table0%0/2
A
A= $t->{'reason'}
Aresult
11
00
256 0 ▶ $why .= $allow && @$allow
Branch: 1 branch
BranchTrueFalseTotal
$allow && @$allow ? : 0 0 0
Condition0%0/3
$allow and @$allow
!ll&&!rl&&r
000
MC/DC0%0/2
$allow and @$allow
$allow @$allow
Truth table0%0/3
A and B
A= $allowB= @$allow
ABresult
0X0
100
111
257 ? '. It is not in the allowlist this installation was given.'
258 : '. Set an allowlist if this host is meant to be polled.';
259 0 return refused($why);
260 }
261  
262 6 ▶ my $every = int($in->{every_ns} || 60_000_000_000);
Condition100%2/2
$in->{'every_ns'} || 60000000000
l!l
24
MC/DC100%1/1
$in->{'every_ns'} || 60000000000
$in->{'every_ns'}
Truth table100%2/2
A
A= $in->{'every_ns'}
Aresult
11
00
263 6 ▶ $every = 10_000_000_000 if $every < 10_000_000_000;
Branch: 1 branch
BranchTrueFalseTotal
if $every < '10000000000' 0 6 6
264 6 ▶ my $tmo = int($in->{timeout_ms} || 5000);
Condition50%1/2
$in->{'timeout_ms'} || 5000
l!l
06
MC/DC0%0/1
$in->{'timeout_ms'} || 5000
$in->{'timeout_ms'}
Truth table50%1/2
A
A= $in->{'timeout_ms'}
Aresult
11
00
265 6 ▶ $tmo = 100 if $tmo < 100;
Branch: 1 branch
BranchTrueFalseTotal
if $tmo < 100 0 6 6
266 6 ▶ $tmo = 60000 if $tmo > 60000;
Branch: 1 branch
BranchTrueFalseTotal
if $tmo > 60000 0 6 6
267  
268 6 ▶ my $enabled = exists $in->{enabled} ? ($in->{enabled} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $in->{'enabled'} ? : 1 5 6
$in->{'enabled'} ? : 0 1 1
269  
270 6 ▶ return _guard(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
271 6 my $dbh = $db->dbh;
272 6 my ($id) = $dbh->selectrow_array(
273 'SELECT id FROM health_targets WHERE tenant = ? AND name = ?',
274 undef, $tenant, $name);
275 6 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 6 6
276 0 $dbh->do('UPDATE health_targets
277 SET url = ?, every_ns = ?, timeout_ms = ?, enabled = ?
278 WHERE id = ?',
279 undef, $url, $every, $tmo, $enabled, $id);
280 }
281 else {
282 6 $dbh->do('INSERT INTO health_targets
283 (tenant, name, url, every_ns, timeout_ms, enabled, created_at)
284 VALUES (?, ?, ?, ?, ?, ?, ?)',
285 undef, $tenant, $name, $url, $every, $tmo, $enabled, time);
286 6 ($id) = $dbh->selectrow_array(
287 'SELECT id FROM health_targets WHERE tenant = ? AND name = ?',
288 undef, $tenant, $name);
289 }
290 6 return ok($id);
291 6 });
292 }
293  
294 0 ▶sub delete_health_target {
Subroutine delete_health_target: not called CC=3 SCAR=24.8
Pod documented
295 0 my ($db, $tenant, $name) = @_;
296 0 ▶ return refused('a target needs a name') unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
297 0 ▶ return _guard(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
298 0 my $n = $db->dbh->do(
299 'DELETE FROM health_targets WHERE tenant = ? AND name = ?',
300 undef, $tenant, $name);
301 0 ▶ return $n && $n != 0 ? ok(undef) : refused('no such target');
Branch: 1 branch
BranchTrueFalseTotal
$n && $n != 0 ? : 0 0 0
Condition0%0/3
$n and $n != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$n and $n != 0
$n $n != 0
Truth table0%0/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
302 0 });
303 }
304  
305 27 my @VIEW_PARAMS = qw(q from to range errors min_ms service);
306  
307 9 ▶sub _view_qs {
Subroutine _view_qs: called CC=4 SCAR=13.9
308 9 my ($in) = @_;
309 9 my @pair;
310 9 for my $k (@VIEW_PARAMS) {
311 63 my $v = $in->{$k};
312 63 ▶ next unless defined $v && length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and length $v 49 14 63
Condition66%2/3
defined $v and length $v
!ll&&!rl&&r
49014
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
313 14 push @pair, join '=', $k, _esc($v);
314 }
315 9 return join '&', @pair;
316 }
317  
318 14 ▶sub _esc {
Subroutine _esc: called CC=1 SCAR=0.0
319 14 my ($v) = @_;
320 14 $v =~ s/([^A-Za-z0-9_.~-])/sprintf('%%%02X', ord $1)/ge;
321 14 return $v;
322 }
323  
324 16 ▶sub _unesc {
Subroutine _unesc: called CC=1 SCAR=0.0
325 16 my ($v) = @_;
326 16 $v =~ tr/+/ /;
327 16 $v =~ s/%([0-9A-Fa-f]{2})/chr hex $1/ge;
328 16 return $v;
329 }
330  
331 10 ▶sub _view_parts {
Subroutine _view_parts: called CC=6 SCAR=19.6
332 10 my ($qs) = @_;
333 10 my %p;
334 10 ▶ for my $pair (split /&/, $qs // '') {
Condition50%1/2
$qs // ""
l!l
100
MC/DC0%0/1
$qs // ""
$qs
Truth table50%1/2
A
A= $qs
Aresult
11
00
335 16 my ($k, $v) = split /=/, $pair, 2;
336 16 ▶ next unless defined $k && length $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k and length $k 0 16 16
Condition33%1/3
defined $k and length $k
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $k and length $k
defined $k length $k
Truth table33%1/3
A and B
A= defined $kB= length $k
ABresult
0X0
100
111
337 16 ▶ $p{$k} = _unesc(defined $v ? $v : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 16 0 16
338 }
339 10 return \%p;
340 }
341  
342 9 ▶sub saved_views {
Subroutine saved_views: called CC=1 SCAR=0.0
Pod documented
343 9 my ($db, $tenant, $page) = @_;
344 9 ▶ return _guard(sub {
Subroutine __ANON__: called CC=10 SCAR=23.2
345 9 ▶ my $rows = $db->dbh->selectall_arrayref(
Branch: 2 branches
BranchTrueFalseTotal
defined $page && length $page ? : 8 1 9
defined $page && length $page ? : 8 1 9
Condition66%4/6
defined $page and length $page
!ll&&!rl&&r
108
defined $page and length $page
!ll&&!rl&&r
108
MC/DC50%1/2
defined $page and length $page
defined $page length $page
MC/DC50%1/2
defined $page and length $page
defined $page length $page
Truth table66%2/3
A and B
A= defined $pageB= length $page
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $pageB= length $page
ABresult
0X0
100
111
346 'SELECT id, page, name, params FROM saved_views
347 WHERE tenant = ?' . (defined $page && length $page
348 ? ' AND page = ?' : '') . ' ORDER BY name',
349 { Slice => {} }, $tenant,
350 (defined $page && length $page ? ($page) : ()));
351  
352 9 for my $r (@$rows) {
353 10 my $p = _view_parts($r->{params});
354 10 $r->{query} = $p->{q};
355 10 ▶ $r->{frozen} = ($p->{from} && $p->{to}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$p->{'from'} && $p->{'to'} ? : 1 9 10
Condition66%2/3
$p->{"from"} and $p->{"to"}
!ll&&!rl&&r
901
MC/DC50%1/2
$p->{"from"} and $p->{"to"}
$p->{"from"} $p->{"to"}
Truth table66%2/3
A and B
A= $p->{"from"}B= $p->{"to"}
ABresult
0X0
100
111
356 $r->{window} = $r->{frozen} ? 'a fixed window'
357 10 ▶ : ($p->{range} || 'the default window');
Branch: 1 branch
BranchTrueFalseTotal
$r->{'frozen'} ? : 1 9 10
Condition100%2/2
$p->{"range"} or "the default window"
l!l
36
MC/DC100%1/1
$p->{"range"} or "the default window"
$p->{"range"}
Truth table100%2/2
A
A= $p->{"range"}
Aresult
11
00
358 }
359 9 return $rows;
360 9 });
361 }
362  
363 11 ▶sub save_view {
Subroutine save_view: called CC=12 SCAR=25.1
Pod documented
364 11 my ($db, $tenant, $in) = @_;
365 11 ▶ my $name = defined $in->{name} ? $in->{name} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'name'} ? : 10 1 11
366 11 ▶ return refused('a view needs a name', 'name') unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 1 10 11
367 10 ▶ return refused('that name is too long', 'name') if length $name > 80;
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 80 0 10 10
368  
369 10 ▶ my $page = defined $in->{page} ? $in->{page} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'page'} ? : 10 0 10
370 10 ▶ return refused('a view needs a page', 'page') unless length $page;
Branch: 1 branch
BranchTrueFalseTotal
unless length $page 0 10 10
371 10 ▶ return refused('there is no such screen', 'page')
Branch: 1 branch
BranchTrueFalseTotal
if $page =~ /[^a-z]/ 0 10 10
372 if $page =~ /[^a-z]/;
373  
374 10 ▶ if (defined $in->{q} && length $in->{q}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in->{"q"} and length $in->{"q"}) 9 1 10
Condition66%2/3
defined $in->{"q"} and length $in->{"q"}
!ll&&!rl&&r
109
MC/DC50%1/2
defined $in->{"q"} and length $in->{"q"}
defined $in->{"q"} length $in->{"q"}
Truth table66%2/3
A and B
A= defined $in->{"q"}B= length $in->{"q"}
ABresult
0X0
100
111
375 my $chk = Punk::Observe::Dashboard::check_panel({
376 9 title => $name, query => $in->{q}, viz => 'table',
377 position => 0, span => 1 });
378 return refused($chk->{error} || 'that query is not valid', 'q')
379 9 ▶ unless $chk->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $chk->{"ok"} 1 8 9
Condition50%1/2
$chk->{'error'} || 'that query is not valid'
l!l
10
MC/DC0%0/1
$chk->{'error'} || 'that query is not valid'
$chk->{'error'}
Truth table50%1/2
A
A= $chk->{'error'}
Aresult
11
00
380 }
381  
382 9 my $qs = _view_qs($in);
383 9 ▶ return refused('there is nothing to save on this view', 'q')
Branch: 1 branch
BranchTrueFalseTotal
unless length $qs 1 8 9
384 unless length $qs;
385  
386 8 ▶ return _guard(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
387 8 my $dbh = $db->dbh;
388 8 my ($id) = $dbh->selectrow_array(
389 'SELECT id FROM saved_views WHERE tenant = ? AND page = ? AND name = ?',
390 undef, $tenant, $page, $name);
391 8 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 1 7 8
392 1 $dbh->do('UPDATE saved_views SET params = ? WHERE id = ?',
393 undef, $qs, $id);
394 }
395 else {
396 7 $dbh->do('INSERT INTO saved_views (tenant, page, name, params, created_at)
397 VALUES (?, ?, ?, ?, ?)',
398 undef, $tenant, $page, $name, $qs, time);
399 7 ($id) = $dbh->selectrow_array(
400 'SELECT id FROM saved_views WHERE tenant = ? AND page = ? AND name = ?',
401 undef, $tenant, $page, $name);
402 }
403 8 return ok($id);
404 8 });
405 }
406  
407 2 ▶sub delete_view {
Subroutine delete_view: called CC=2 SCAR=7.0
Pod documented
408 2 my ($db, $tenant, $id) = @_;
409 2 ▶ return refused('a view needs an id') unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 2 2
410 2 ▶ return _guard(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
411 2 my $n = $db->dbh->do('DELETE FROM saved_views WHERE id = ? AND tenant = ?',
412 undef, $id, $tenant);
413 2 ▶ return $n && $n != 0 ? ok(undef) : refused('no such view');
Branch: 1 branch
BranchTrueFalseTotal
$n && $n != 0 ? : 1 1 2
Condition66%2/3
$n and $n != 0
!ll&&!rl&&r
011
MC/DC50%1/2
$n and $n != 0
$n $n != 0
Truth table66%2/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
414 2 });
415 }
416  
417 27 my %ALERT_OP = map { $_ => 1 } qw(> >= < <= == !=);
418  
419 3 ▶sub alert {
Subroutine alert: called CC=2 SCAR=7.1
Pod documented
420 3 my ($db, $tenant, $id) = @_;
421 3 ▶ return undef unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
422 3 return $db->dbh->selectrow_hashref(
423 'SELECT id, name, query, op, threshold, for_ns, every_ns, enabled
424 FROM alert_rules WHERE tenant = ? AND id = ?',
425 undef, $tenant, $id);
426 }
427  
428 21 ▶sub save_alert {
Subroutine save_alert: called CC=24 SCAR=34.2
Pod documented
429 21 my ($db, $tenant, $in) = @_;
430  
431 21 ▶ my $name = defined $in->{name} ? $in->{name} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'name'} ? : 20 1 21
432 21 $name =~ s/\A\s+|\s+\z//g;
433 21 ▶ return refused('a rule needs a name', 'name') unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 1 20 21
434 20 ▶ return refused('a rule name is at most 128 characters', 'name')
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 128 0 20 20
435 if length($name) > 128;
436 20 ▶ return refused('a rule name cannot contain control characters or "/"',
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[[[:cntrl:]/]] 0 20 20
437 'name') if $name =~ m{[[:cntrl:]/]};
438  
439 20 ▶ my $q = defined $in->{query} ? $in->{query} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'query'} ? : 20 0 20
440 20 ▶ return refused('a rule needs a query', 'query') unless $q =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $q =~ /\S/ 0 20 20
441 20 my $p = Punk::Observe::Query::parse($q);
442 return refused("that query does not parse: $p->{error}", 'query')
443 20 ▶ unless $p->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"ok"} 1 19 20
444 return refused('an alert rule needs a bucketed query - '
445 . 'add `| bucket(30s) <aggregate>` or `| rate(30s)`', 'query')
446 20 ▶ unless grep { $_->{kind} eq 'bucket' || $_->{kind} eq 'rate' }
Condition66%2/3
$_->{'kind'} eq 'bucket' || $_->{'kind'} eq 'rate'
l!l&&r!l&&!r
1802
MC/DC50%1/2
$_->{'kind'} eq 'bucket' || $_->{'kind'} eq 'rate'
$_->{'kind'} eq 'bucket' $_->{'kind'} eq 'rate'
Truth table66%2/3
A || B
A= $_->{'kind'} eq 'bucket'B= $_->{'kind'} eq 'rate'
ABresult
1X1
011
000
447 19 ▶ @{ $p->{stages} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->{"kind"} eq "bucket" or $_->{"kind"} eq "rate";} @{$p->{"stages"} or [];} 1 18 19
Condition50%1/2
$p->{'stages'} || []
l!l
190
MC/DC0%0/1
$p->{'stages'} || []
$p->{'stages'}
Truth table50%1/2
A
A= $p->{'stages'}
Aresult
11
00
448  
449 18 ▶ my $op = defined $in->{op} ? $in->{op} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'op'} ? : 18 0 18
450 return refused('the operator is one of > >= < <= == !=', 'op')
451 18 ▶ unless $ALERT_OP{$op};
Branch: 1 branch
BranchTrueFalseTotal
unless $ALERT_OP{$op} 1 17 18
452  
453 17 ▶ my $threshold = defined $in->{threshold} ? $in->{threshold} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'threshold'} ? : 17 0 17
454 17 ▶ return refused('the threshold is a number', 'threshold')
Branch: 1 branch
BranchTrueFalseTotal
unless $threshold =~ /\A-?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?\z/ 1 16 17
455 unless $threshold =~ /\A-?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?\z/;
456  
457 16 my ($for_ns, $every_ns) = (0, 60_000_000_000);
458 16 ▶ if (defined $in->{for} && $in->{for} =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in->{"for"} and $in->{"for"} =~ /\S/) 16 0 16
Condition33%1/3
defined $in->{"for"} and $in->{"for"} =~ /\S/
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $in->{"for"} and $in->{"for"} =~ /\S/
defined $in->{"for"} $in->{"for"} =~ /\S/
Truth table33%1/3
A and B
A= defined $in->{"for"}B= $in->{"for"} =~ /\S/
ABresult
0X0
100
111
459 16 my ($ns) = Punk::Observe::View::min_duration($in->{for});
460 16 ▶ return refused("'$in->{for}' is not a duration - try 30s, 5m",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 1 15 16
461 'for') unless defined $ns;
462 15 $for_ns = $ns;
463 }
464 15 ▶ if (defined $in->{every} && $in->{every} =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in->{"every"} and $in->{"every"} =~ /\S/) 15 0 15
Condition33%1/3
defined $in->{"every"} and $in->{"every"} =~ /\S/
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $in->{"every"} and $in->{"every"} =~ /\S/
defined $in->{"every"} $in->{"every"} =~ /\S/
Truth table33%1/3
A and B
A= defined $in->{"every"}B= $in->{"every"} =~ /\S/
ABresult
0X0
100
111
465 15 my ($ns) = Punk::Observe::View::min_duration($in->{every});
466 15 ▶ return refused("'$in->{every}' is not a duration - try 30s, 5m",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 0 15 15
467 'every') unless defined $ns;
468 15 ▶ $ns = 10_000_000_000 if $ns < 10_000_000_000;
Branch: 1 branch
BranchTrueFalseTotal
if $ns < '10000000000' 1 14 15
469 15 $every_ns = $ns;
470 }
471  
472 15 ▶ my $enabled = exists $in->{enabled} ? ($in->{enabled} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $in->{'enabled'} ? : 0 15 15
$in->{'enabled'} ? : 0 0 0
473  
474 15 ▶ return _guard(sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
475 15 my $dbh = $db->dbh;
476 15 my $id = $in->{id};
477 15 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 1 14 15
478 1 my $n = $dbh->do(
479 'UPDATE alert_rules
480 SET name = ?, query = ?, op = ?, threshold = ?,
481 for_ns = ?, every_ns = ?, enabled = ?, next_eval_at = 0
482 WHERE id = ? AND tenant = ?',
483 undef, $name, $q, $op, $threshold, $for_ns, $every_ns,
484 $enabled, $id, $tenant);
485 1 ▶ return refused('no such rule') unless $n && $n != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $n and $n != 0 0 1 1
Condition33%1/3
$n and $n != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$n and $n != 0
$n $n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
486 1 return ok($id);
487 }
488 $dbh->do(
489 14 'INSERT INTO alert_rules
490 (tenant, name, query, op, threshold, for_ns, every_ns,
491 enabled, next_eval_at, created_at)
492 VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?)',
493 undef, $tenant, $name, $q, $op, $threshold, $for_ns, $every_ns,
494 $enabled, time);
495 14 ($id) = $dbh->selectrow_array(
496 'SELECT id FROM alert_rules WHERE tenant = ? AND name = ?',
497 undef, $tenant, $name);
498 14 return ok($id);
499 15 });
500 }
501  
502 1 ▶sub delete_alert {
Subroutine delete_alert: called CC=2 SCAR=7.6
Pod documented
503 1 my ($db, $tenant, $id) = @_;
504 1 ▶ return refused('a rule needs an id') unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
505 1 ▶ return _guard(sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
506 1 my $n = $db->dbh->do(
507 'DELETE FROM alert_rules WHERE id = ? AND tenant = ?',
508 undef, $id, $tenant);
509 1 ▶ return $n && $n != 0 ? ok(undef) : refused('no such rule');
Branch: 1 branch
BranchTrueFalseTotal
$n && $n != 0 ? : 1 0 1
Condition33%1/3
$n and $n != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$n and $n != 0
$n $n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
510 1 });
511 }
512  
513 4 ▶sub silence_match {
Subroutine silence_match: called CC=6 SCAR=20.3
Pod documented
514 4 my ($pattern, $is_prefix, $key) = @_;
515 4 ▶ return 0 unless defined $pattern && defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern and defined $key 0 4 4
Condition33%1/3
defined $pattern and defined $key
!ll&&!rl&&r
004
MC/DC0%0/2
defined $pattern and defined $key
defined $pattern defined $key
Truth table33%1/3
A and B
A= defined $patternB= defined $key
ABresult
0X0
100
111
516 4 ▶ return $is_prefix
Branch: 3 branches
BranchTrueFalseTotal
$is_prefix ? : 3 1 4
rindex($key, $pattern, 0) == 0 ? : 3 0 3
$key eq $pattern ? : 0 1 1
517 ? (rindex($key, $pattern, 0) == 0 ? 1 : 0)
518 : ($key eq $pattern ? 1 : 0);
519 }
520  
521 3 ▶sub save_silence {
Subroutine save_silence: called CC=9 SCAR=22.8
Pod documented
522 3 my ($db, $tenant, $in) = @_;
523  
524 3 ▶ my $pattern = defined $in->{pattern} ? $in->{pattern} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'pattern'} ? : 3 0 3
525 3 $pattern =~ s/\A\s+|\s+\z//g;
526 3 ▶ return refused('a silence needs a pattern - `<rule>/` silences a whole '
Branch: 1 branch
BranchTrueFalseTotal
unless length $pattern 0 3 3
527 . 'rule, `<rule>/<series>` one series', 'pattern')
528 unless length $pattern;
529  
530 3 ▶ my $until = defined $in->{until} ? $in->{until} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $in->{'until'} ? : 3 0 3
531 3 ▶ return refused('a silence needs a duration - try 1h, 2d', 'until')
Branch: 1 branch
BranchTrueFalseTotal
unless $until =~ /\S/ 0 3 3
532 unless $until =~ /\S/;
533 3 my ($ns) = Punk::Observe::View::min_duration($until);
534 3 ▶ return refused("'$until' is not a duration - try 1h, 2d", 'until')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns and $ns > 0 1 2 3
Condition66%2/3
defined $ns and $ns > 0
!ll&&!rl&&r
102
MC/DC50%1/2
defined $ns and $ns > 0
defined $ns $ns > 0
Truth table66%2/3
A and B
A= defined $nsB= $ns > 0
ABresult
0X0
100
111
535 unless defined $ns && $ns > 0;
536  
537 2 ▶ my $is_prefix = $in->{is_prefix} ? 1
Branch: 2 branches
BranchTrueFalseTotal
$in->{'is_prefix'} ? : 0 2 2
$pattern =~ m[/\z] ? : 2 0 2
538 : ($pattern =~ m{/\z} ? 1 : 0);
539  
540 2 ▶ return _guard(sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
541 2 my $dbh = $db->dbh;
542 2 my $now = Punk::Observe::now_ns();
543 $dbh->do(
544 'INSERT INTO silences
545 (tenant, pattern, is_prefix, reason, created_by, until,
546 created_at)
547 VALUES (?, ?, ?, ?, ?, ?, ?)',
548 undef, $tenant, $pattern, $is_prefix,
549 (defined $in->{reason} && length $in->{reason} ? $in->{reason} : undef),
550 2 ▶ (defined $in->{by} && length $in->{by} ? $in->{by} : undef),
Branch: 2 branches
BranchTrueFalseTotal
defined $in->{'reason'} && length $in->{'reason'} ? : 1 1 2
defined $in->{'by'} && length $in->{'by'} ? : 1 1 2
Condition66%4/6
defined $in->{"reason"} and length $in->{"reason"}
!ll&&!rl&&r
101
defined $in->{"by"} and length $in->{"by"}
!ll&&!rl&&r
101
MC/DC50%1/2
defined $in->{"reason"} and length $in->{"reason"}
defined $in->{"reason"} length $in->{"reason"}
MC/DC50%1/2
defined $in->{"by"} and length $in->{"by"}
defined $in->{"by"} length $in->{"by"}
Truth table66%2/3
A and B
A= defined $in->{"reason"}B= length $in->{"reason"}
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $in->{"by"}B= length $in->{"by"}
ABresult
0X0
100
111
551 Punk::Observe::Store::nadd($now, $ns), time);
552 2 my ($id) = $dbh->selectrow_array('SELECT MAX(id) FROM silences
553 WHERE tenant = ?', undef, $tenant);
554 2 return ok($id);
555 2 });
556 }
557  
558 1 ▶sub delete_silence {
Subroutine delete_silence: called CC=2 SCAR=7.6
Pod documented
559 1 my ($db, $tenant, $id) = @_;
560 1 ▶ return refused('a silence needs an id') unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
561 1 ▶ return _guard(sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
562 1 my $n = $db->dbh->do(
563 'UPDATE silences SET until = ? WHERE id = ? AND tenant = ?',
564 undef, Punk::Observe::now_ns(), $id, $tenant);
565 1 ▶ return $n && $n != 0 ? ok(undef) : refused('no such silence');
Branch: 1 branch
BranchTrueFalseTotal
$n && $n != 0 ? : 1 0 1
Condition33%1/3
$n and $n != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$n and $n != 0
$n $n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
ABresult
0X0
100
111
566 1 });
567 }
568  
569 2 ▶sub alerts {
Subroutine alerts: called CC=12 SCAR=28.1
Pod documented
570 2 my ($db, $tenant, $req) = @_;
571 2 my $dbh = $db->dbh;
572  
573 2 my $rules = $dbh->selectall_arrayref(
574 'SELECT r.id, r.name, r.query, s.series, s.state, s.last_value,
575 s.since, s.fired_at, s.reason
576 FROM alert_rules r
577 LEFT JOIN alert_state s ON s.rule_id = r.id
578 WHERE r.tenant = ?
579 ORDER BY r.name, s.series', { Slice => {} }, $tenant);
580  
581 2 my $now = Punk::Observe::now_ns();
582  
583 2 my $sil = $dbh->selectall_arrayref(
584 'SELECT id, pattern, is_prefix, reason, created_by, until
585 FROM silences WHERE tenant = ? AND until > ? ORDER BY until',
586 { Slice => {} }, $tenant, $now);
587  
588 2 for my $r (@$rules) {
589 2 ▶ $r->{series} = '' unless defined $r->{series};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"series"} 2 0 2
590 2 ▶ $r->{state} = 'ok' unless defined $r->{state};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"state"} 2 0 2
591 2 $r->{value} = $r->{last_value};
592 2 ▶ $r->{held} = $r->{fired_at} ? $now - $r->{fired_at} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$r->{'fired_at'} ? : 0 2 2
593 2 my $key = "$r->{name}/$r->{series}";
594 2 ▶ $r->{silenced} = (grep { silence_match(@{$_}{qw(pattern is_prefix)},
Branch: 1 branch
BranchTrueFalseTotal
grep({silence_match(@{$_;}{'pattern', 'is_prefix'}, $key);} @$sil) ? : 1 1 2
595 $key) } @$sil) ? 1 : 0;
596 }
597  
598 my $from = defined $req->{from} && length $req->{from}
599 2 ▶ ? $req->{from} : $now - 86_400 * 1_000_000_000;
Branch: 1 branch
BranchTrueFalseTotal
defined $req->{'from'} && length $req->{'from'} ? : 0 2 2
Condition33%1/3
defined $req->{"from"} and length $req->{"from"}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $req->{"from"} and length $req->{"from"}
defined $req->{"from"} length $req->{"from"}
Truth table33%1/3
A and B
A= defined $req->{"from"}B= length $req->{"from"}
ABresult
0X0
100
111
600 2 my $events = $dbh->selectall_arrayref(
601 'SELECT r.name AS rule_name, e.series, e.to_state, e.at
602 FROM alert_events e
603 JOIN alert_rules r ON r.id = e.rule_id
604 WHERE r.tenant = ? AND e.at >= ?
605 ORDER BY e.at', { Slice => {} }, $tenant, $from);
606  
607 2 my %seen;
608 my @ev = map {
609 0 $seen{"$_->{rule_name} / $_->{series}"} = 1;
610 { series => "$_->{rule_name} / $_->{series}",
611 to => $_->{to_state}, at => $_->{at} }
612 0 } @$events;
613 2 for my $r (@$rules) {
614 2 ▶ next unless length $r->{series};
Branch: 1 branch
BranchTrueFalseTotal
unless length $r->{"series"} 2 0 2
615 0 my $k = "$r->{name} / $r->{series}";
616 0 ▶ next if $seen{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$k} 0 0 0
617 0 push @ev, { series => $k, to => $r->{state}, at => $from };
618 }
619  
620 2 $_->{by} = $_->{created_by} for @$sil;
621  
622 2 return { rules => $rules, silences => $sil, events => \@ev,
623 to => $now, can_edit => 1 };
624 }
625  
626 27 1;
627  
628 __END__
« blib/lib/Punk/Observe/Warm.pm ↑ Summary blib/lib/Punk/Observe/Backend.pm »