Netdata/Chart.pm

filter: Statementstmt 100% 58 / 58 Branchbran 80.0% 8 / 10 Conditioncond 93.3% 14 / 15 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 95.2% 101 / 106 CC 16 SCAR 14.7
CC
16
Cov
96%
to_string
8.0
data_to_string
5.0
dimension
4.0
CRAP 4.3
« Netdata.pm ↑ Summary Netdata/Collector.pm »
1 package Mojo::Netdata::Chart;
2 3 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Mojo::Netdata::Util qw(safe_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 has chart_type => 'line';
8  
9 4 ▶has context => sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
10 4 return join '.', map { safe_id $self->$_ } qw(module type);
11 3 };
12  
13 18 ▶has dimensions => sub ($self) { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 4 ▶has family => sub ($self) { $self->id };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 3 ▶has id => sub ($self) { croak '"id" cannot be built' };
Subroutine __ANON__: called CC=1 SCAR=0.0
16 3 has module => 'mojo';
17 3 has name => '';
18 3 has options => ''; # "detail hidden obsolete"
19 3 has plugin => 'mojo';
20 3 has priority => 10000;
21 4 ▶has title => sub ($self) { $self->name || $self->id };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self->name or $self->id 0 4 4
22 2 ▶has type => sub ($self) { croak '"type" cannot be built' };
Subroutine __ANON__: called CC=1 SCAR=0.0
23 3 has units => '#';
24 3 has update_every => 1;
25  
26 4 ▶sub data_to_string ($self, $microseconds = undef) {
Subroutine data_to_string: called CC=5 SCAR=16.1
Pod documented
27 4 my $dimensions = $self->dimensions;
28 my $set = join "\n",
29 4 ▶ map { sprintf "SET %s = %s", $_, $dimensions->{$_}{value} // '' } sort keys %$dimensions;
Condition100%2/2
$dimensions->{$_}{"value"} // ""
l!l
61
MC/DC100%1/1
$dimensions->{$_}{"value"} // ""
$dimensions->{$_}{"value"}
Truth table100%2/2
A
A= $dimensions->{$_}{"value"}
Aresult
11
00
30  
31 4 ▶ return !$set ? '' : sprintf "BEGIN %s.%s%s\n%s\nEND\n", safe_id($self->type), safe_id($self->id),
Branch: 2 branches
BranchTrueFalseTotal
!$set ? : 0 4 4
$microseconds ? : 1 3 4
32 ($microseconds ? " $microseconds" : ""), $set;
33 }
34  
35 44 ▶sub dimension ($self, $name, $attrs = undef) {
Subroutine dimension: called CC=4 SCAR=13.9
Pod documented
36 44 my $id = safe_id $name;
37 44 ▶ return $self->dimensions->{$id} unless $attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs 15 29 44
38 29 ▶ my $dimension = $self->dimensions->{$id} //= {name => $name};
Condition100%2/2
$self->dimensions->{$id} //= {"name", $name}
l!l
1217
MC/DC100%1/1
$self->dimensions->{$id} //= {"name", $name}
$self->dimensions->{$id}
Truth table100%2/2
A
A= $self->dimensions->{$id}
Aresult
11
00
39 29 @$dimension{keys(%$attrs)} = values %$attrs;
40 29 return $self;
41 }
42  
43 3 ▶sub to_string ($self) {
Subroutine to_string: called CC=8 SCAR=20.8
Pod documented
44 3 my $dimensions = $self->dimensions;
45 3 ▶ return '' unless %$dimensions;
Branch: 1 branch
BranchTrueFalseTotal
unless %$dimensions 1 2 3
46  
47 2 my $str = sprintf "CHART %s.%s %s\n", safe_id($self->type), safe_id($self->id),
48 q('name' 'title' 'units' 'family' context chart_type priority update_every 'options' 'plugin' 'module')
49 22 =~ s!([a-z_]+)!{$self->$1}!ger;
50  
51 2 for my $id (sort keys %$dimensions) {
52 3 my $dimension = $dimensions->{$id};
53 3 ▶ $dimension->{algorithm} ||= 'absolute';
Condition100%2/2
$dimension->{"algorithm"} ||= "absolute"
l!l
12
MC/DC100%1/1
$dimension->{"algorithm"} ||= "absolute"
$dimension->{"algorithm"}
Truth table100%2/2
A
A= $dimension->{"algorithm"}
Aresult
11
00
54 3 ▶ $dimension->{divisor} ||= 1;
Condition100%2/2
$dimension->{"divisor"} ||= 1
l!l
12
MC/DC100%1/1
$dimension->{"divisor"} ||= 1
$dimension->{"divisor"}
Truth table100%2/2
A
A= $dimension->{"divisor"}
Aresult
11
00
55 3 ▶ $dimension->{multiplier} ||= 1;
Condition100%2/2
$dimension->{"multiplier"} ||= 1
l!l
12
MC/DC100%1/1
$dimension->{"multiplier"} ||= 1
$dimension->{"multiplier"}
Truth table100%2/2
A
A= $dimension->{"multiplier"}
Aresult
11
00
56 3 ▶ $dimension->{name} ||= $id;
Condition66%2/3
$dimension->{"name"} ||= $id
l!l&&r!l&&!r
120
MC/DC0%0/2
$dimension->{"name"} ||= $id
$dimension->{"name"} $id
Truth table66%2/3
A ||= B
A= $dimension->{"name"}B= $id
ABresult
1X1
011
000
57 3 ▶ $dimension->{options} ||= '';
Condition100%2/2
$dimension->{"options"} ||= ""
l!l
12
MC/DC100%1/1
$dimension->{"options"} ||= ""
$dimension->{"options"}
Truth table100%2/2
A
A= $dimension->{"options"}
Aresult
11
00
58 3 $str .= sprintf "DIMENSION %s %s\n", $id,
59 15 q('name' algorithm multiplier divisor 'options') =~ s!([a-z_]+)!{$dimension->{$1}}!ger;
60 }
61  
62 2 return $str;
63 }
64  
65 3 1;
66  
67 =encoding utf8
68  
69 =head1 NAME
70  
71 Mojo::Netdata::Chart - Represents a Netdata chart and dimensions
72  
73 =head1 SYNOPSIS
74  
75 my $chart = Mojo::Netdata::Chart->new;
76 $chart->data_to_string;
77 $chart->to_string;
78  
79 =head1 DESCRIPTION
80  
81 L<Mojo::Netdata::Chart> is a class that represents a Netdata chart and
82 dimensions. See L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#chart>
83 for more details.
84  
85 =head1 ATTRIBUTES
86  
87 =head2 chart_type
88  
89 $str = $chart->chart_type;
90  
91 Either "area", "line" or "stacked". Defaults to "line".
92  
93 =head2 context
94  
95 $str = $chart->context;
96  
97 Defaults to "default".
98  
99 =head2 dimensions
100  
101 $hash_ref = $chart->dimensions;
102  
103 See L</dimension>.
104  
105 =head2 family
106  
107 $str = $chart->family;
108  
109 Defaults to L</id>.
110  
111 =head2 id
112  
113 $str = $chart->id;
114  
115 Required to be set.
116  
117 =head2 module
118  
119 $str = $chart->module;
120  
121 Defaults to empty string.
122  
123 =head2 name
124  
125 $str = $chart->name;
126  
127 Defaults to empty string.
128  
129 =head2 options
130  
131 $str = $chart->options;
132  
133 Defaults to empty string.
134  
135 =head2 plugin
136  
137 $str = $chart->options;
138  
139 Defaults to "mojo". The default is subject to change!
140  
141 =head2 priority
142  
143 $int = $chart->priority;
144  
145 Defaults to 10000.
146  
147 =head2 title
148  
149 $str = $chart->title;
150  
151 Defaults to L</name> or L</id>.
152  
153 =head2 type
154  
155 $str = $chart->type;
156  
157 Required to be set.
158  
159 =head2 units
160  
161 $str = $chart->units;
162  
163 Defaults to "#".
164  
165 =head2 update_every
166  
167 $num = $chart->update_every;
168  
169 How often to update Netdata.
170  
171 =head1 METHODS
172  
173 =head2 data_to_string
174  
175 $str = $chart->data_to_string;
176  
177 Takes the values in L</dimensions> and creates a string with SET, suitable to
178 be sent to Netdata.
179  
180 =head2 dimension
181  
182 $dimension = $chart->dimension($id);
183 $chart = $chart->dimension($id => {name => 'cool'});
184 $chart = $chart->dimension($id => {value => 42});
185  
186 Used to get or set an item in L</dimensions>. Possible keys are "algorithm",
187 "divisor", "multiplier", "name" and "options".
188  
189 See L<https://learn.netdata.cloud/docs/agent/collectors/plugins.d#dimension>
190 for more details.
191  
192 =head2 to_string
193  
194 $str = $chart->to_string;
195  
196 Creates a string with CHART and DIMENSION, suitable to be sent to Netdata.
197  
198 =head1 SEE ALSO
199  
200 L<Mojo::Netdata>.
201  
202 =cut
« Netdata.pm ↑ Summary Netdata/Collector.pm »