| 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
Condition0%0/2
$@ || 'the configuration store did not say why'
MC/DC0%0/1
$@ || 'the configuration store did not say why'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 87 | ▶ | return wantarray ? @r : $r[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
not defined $slug && length $slug and @$list
defined $slug && length $slug
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 24 | ▶ | return { list => $list, panels => [], cols => 2, can_edit => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $slug and length $slug
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
defined $p->{"viz"} and length $p->{"viz"} and not $known{$p->{"viz"}}
defined $p->{"viz"} and length $p->{"viz"}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 18 | ▶ | return refused('a dashboard needs a slug', 'slug') unless length $slug; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 17 | ▶ | return refused('a slug is letters, digits, dash and underscore', 'slug') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $in->{"title"} and length $in->{"title"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 13 | ▶ | my $cols = int($in->{cols} || 2); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$in->{'cols'} || 2
MC/DC100%1/1
$in->{'cols'} || 2
$in->{'cols'}
Truth table100%2/2
A
A= $in->{'cols'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 13 | ▶ | $cols = 1 if $cols < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 13 | ▶ | $cols = 6 if $cols > 6; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $slug and length $slug
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$n and $n != 0
MC/DC0%0/2
$n and $n != 0
$n
$n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition71%5/7
defined $in->{"viz"} and length $in->{"viz"}
$in->{'position'} || 0
$in->{'span'} || 1
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"}
Truth table50%1/2
A
A= $in->{'position'}
Truth table100%2/2
A
A= $in->{'span'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 18 | ▶ | unless ($chk->{ok}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | my $f = ($chk->{code} || 0) == 3 || ($chk->{code} || 0) == 4 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition57%4/7
($chk->{"code"} || 0) == 3 or ($chk->{"code"} || 0) == 4
$chk->{'code'} || 0
$chk->{'code'} || 0
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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$chk->{'error'} || 'that panel is not valid'
$chk->{'error'}
Truth table50%1/2
A
A= $chk->{'error'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 14 | ▶ | my $span = int($in->{span} || 1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$in->{'span'} || 1
MC/DC100%1/1
$in->{'span'} || 1
$in->{'span'}
Truth table100%2/2
A
A= $in->{'span'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 14 | ▶ | $span = 1 if $span < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 14 | ▶ | $span = 6 if $span > 6; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 14 | ▶ | my $pos = int($in->{position} || 0); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$in->{'position'} || 0
MC/DC0%0/1
$in->{'position'} || 0
$in->{'position'}
Truth table50%1/2
A
A= $in->{'position'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 14 | ▶ | $pos = 0 if $pos < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 14 | ▶ | if (my $id = $in->{id}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$n and $n != 0
MC/DC0%0/2
$n and $n != 0
$n
$n != 0
Truth table0%0/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition85%6/7
defined $in->{"viz"} and length $in->{"viz"}
$in->{'position'} || 0
$in->{'span'} || 1
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"}
Truth table100%2/2
A
A= $in->{'position'}
Truth table100%2/2
A
A= $in->{'span'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | ▶ | unless ($chk->{ok}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | ▶ | my $f = ($chk->{code} || 0) == 3 || ($chk->{code} || 0) == 4 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition42%3/7
($chk->{"code"} || 0) == 3 or ($chk->{"code"} || 0) == 4
$chk->{'code'} || 0
$chk->{'code'} || 0
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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | ? 'title' : 'query'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | my $name = (defined $in->{title} && length $in->{title}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | ▶ | ? "'$in->{title}'" : "panel $in->{id}"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $in->{"title"} and length $in->{"title"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | ▶ | return refused(($chk->{error} || 'that panel is not valid') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$chk->{'error'} || 'that panel is not valid'
MC/DC0%0/1
$chk->{'error'} || 'that panel is not valid'
$chk->{'error'}
Truth table50%1/2
A
A= $chk->{'error'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | ▶ | return ok(undef) unless @checked; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$in->{'span'} || 1
$in->{'span'}
Truth table100%2/2
A
A= $in->{'span'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 4 | ▶ | $span = 1 if $span < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 4 | ▶ | $span = 6 if $span > 6; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 4 | ▶ | my $pos = int($in->{position} || 0); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$in->{'position'} || 0
MC/DC100%1/1
$in->{'position'} || 0
$in->{'position'}
Truth table100%2/2
A
A= $in->{'position'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 4 | ▶ | $pos = 0 if $pos < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$n and $n != 0
MC/DC50%1/2
$n and $n != 0
$n
$n != 0
Truth table66%2/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | my $e = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | eval { $dbh->rollback }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 1 | ▶ | return $e ? failed($e) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$n and $n != 0
MC/DC50%1/2
$n and $n != 0
$n
$n != 0
Truth table66%2/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $name and length $name
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 6 | ▶ | return refused('a target needs a name') unless length $name; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 6 | ▶ | return refused('a name is letters, digits, dash and underscore') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | if $name =~ /[^A-Za-z0-9_-]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 6 | ▶ | my $url = defined $in->{url} ? $in->{url} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 6 | ▶ | return refused('a target needs a URL') unless length $url; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 6 | my $t = Punk::Observe::Target::check($url, $allow); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 6 | ▶ | unless ($t->{ok}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | my $why = $t->{reason} || 'that URL is refused'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$t->{'reason'} || 'that URL is refused'
MC/DC0%0/1
$t->{'reason'} || 'that URL is refused'
$t->{'reason'}
Truth table0%0/2
A
A= $t->{'reason'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | $why .= $allow && @$allow | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$allow and @$allow
MC/DC0%0/2
$allow and @$allow
$allow
@$allow
Truth table0%0/3
A and B
A= $allowB= @$allow
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$in->{'every_ns'} || 60000000000
$in->{'every_ns'}
Truth table100%2/2
A
A= $in->{'every_ns'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 6 | ▶ | $every = 10_000_000_000 if $every < 10_000_000_000; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 6 | ▶ | my $tmo = int($in->{timeout_ms} || 5000); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$in->{'timeout_ms'} || 5000
MC/DC0%0/1
$in->{'timeout_ms'} || 5000
$in->{'timeout_ms'}
Truth table50%1/2
A
A= $in->{'timeout_ms'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 6 | ▶ | $tmo = 100 if $tmo < 100; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 6 | ▶ | $tmo = 60000 if $tmo > 60000; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 6 | ▶ | my $enabled = exists $in->{enabled} ? ($in->{enabled} ? 1 : 0) : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $name and length $name
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$n and $n != 0
MC/DC0%0/2
$n and $n != 0
$n
$n != 0
Truth table0%0/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $v and length $v
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$qs // ""
$qs
Truth table50%1/2
A
A= $qs
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 16 | my ($k, $v) = split /=/, $pair, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 16 | ▶ | next unless defined $k && length $k; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $k and length $k
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 16 | ▶ | $p{$k} = _unesc(defined $v ? $v : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
defined $page and length $page
defined $page and length $page
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
Truth table66%2/3
A and B
A= defined $pageB= length $page
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$p->{"from"} and $p->{"to"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | $r->{window} = $r->{frozen} ? 'a fixed window' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 10 | ▶ | : ($p->{range} || 'the default window'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$p->{"range"} or "the default window"
MC/DC100%1/1
$p->{"range"} or "the default window"
$p->{"range"}
Truth table100%2/2
A
A= $p->{"range"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 11 | ▶ | return refused('a view needs a name', 'name') unless length $name; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 10 | ▶ | return refused('that name is too long', 'name') if length $name > 80; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 10 | ▶ | my $page = defined $in->{page} ? $in->{page} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 10 | ▶ | return refused('a view needs a page', 'page') unless length $page; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 10 | ▶ | return refused('there is no such screen', 'page') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | if $page =~ /[^a-z]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 10 | ▶ | if (defined $in->{q} && length $in->{q}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $in->{"q"} and length $in->{"q"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$chk->{'error'} || 'that query is not valid'
MC/DC0%0/1
$chk->{'error'} || 'that query is not valid'
$chk->{'error'}
Truth table50%1/2
A
A= $chk->{'error'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$n and $n != 0
MC/DC50%1/2
$n and $n != 0
$n
$n != 0
Truth table66%2/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 20 | ▶ | return refused('a rule name is at most 128 characters', 'name') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | if length($name) > 128; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 20 | ▶ | return refused('a rule name cannot contain control characters or "/"', | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 'name') if $name =~ m{[[:cntrl:]/]}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 20 | ▶ | my $q = defined $in->{query} ? $in->{query} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 20 | ▶ | return refused('a rule needs a query', 'query') unless $q =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'
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'
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 19 | ▶ | @{ $p->{stages} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$p->{'stages'} || []
MC/DC0%0/1
$p->{'stages'} || []
$p->{'stages'}
Truth table50%1/2
A
A= $p->{'stages'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 18 | ▶ | my $op = defined $in->{op} ? $in->{op} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | return refused('the operator is one of > >= < <= == !=', 'op') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 18 | ▶ | unless $ALERT_OP{$op}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 17 | ▶ | my $threshold = defined $in->{threshold} ? $in->{threshold} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 17 | ▶ | return refused('the threshold is a number', 'threshold') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $in->{"for"} and $in->{"for"} =~ /\S/
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 'for') unless defined $ns; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 15 | $for_ns = $ns; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 15 | ▶ | if (defined $in->{every} && $in->{every} =~ /\S/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $in->{"every"} and $in->{"every"} =~ /\S/
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 'every') unless defined $ns; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 15 | ▶ | $ns = 10_000_000_000 if $ns < 10_000_000_000; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 15 | $every_ns = $ns; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 15 | ▶ | my $enabled = exists $in->{enabled} ? ($in->{enabled} ? 1 : 0) : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$n and $n != 0
MC/DC0%0/2
$n and $n != 0
$n
$n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$n and $n != 0
MC/DC0%0/2
$n and $n != 0
$n
$n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $pattern and defined $key
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 4 | ▶ | return $is_prefix | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | . 'rule, `<rule>/<series>` one series', 'pattern') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | unless length $pattern; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | 3 | ▶ | my $until = defined $in->{until} ? $in->{until} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | 3 | ▶ | return refused('a silence needs a duration - try 1h, 2d', 'until') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $ns and $ns > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | unless defined $ns && $ns > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 2 | ▶ | my $is_prefix = $in->{is_prefix} ? 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
defined $in->{"reason"} and length $in->{"reason"}
defined $in->{"by"} and length $in->{"by"}
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"}
Truth table66%2/3
A and B
A= defined $in->{"by"}B= length $in->{"by"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$n and $n != 0
MC/DC0%0/2
$n and $n != 0
$n
$n != 0
Truth table33%1/3
A and B
A= $nB= $n != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 2 | ▶ | $r->{state} = 'ok' unless defined $r->{state}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 2 | $r->{value} = $r->{last_value}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 2 | ▶ | $r->{held} = $r->{fired_at} ? $now - $r->{fired_at} : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 2 | my $key = "$r->{name}/$r->{series}"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 2 | ▶ | $r->{silenced} = (grep { silence_match(@{$_}{qw(pattern is_prefix)}, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $req->{"from"} and length $req->{"from"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | my $k = "$r->{name} / $r->{series}"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | ▶ | next if $seen{$k}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||