line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Netdata::Chart; |
2
|
3
|
|
|
3
|
|
228317
|
use Mojo::Base -base, -signatures; |
|
3
|
|
|
|
|
164966
|
|
|
3
|
|
|
|
|
35
|
|
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
4907
|
use Carp qw(croak); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
150
|
|
5
|
3
|
|
|
3
|
|
966
|
use Mojo::Netdata::Util qw(safe_id); |
|
3
|
|
|
|
|
26
|
|
|
3
|
|
|
|
|
3206
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has chart_type => 'line'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has context => sub ($self) { |
10
|
|
|
|
|
|
|
return join '.', map { safe_id $self->$_ } qw(module type); |
11
|
|
|
|
|
|
|
}; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has dimensions => sub ($self) { +{} }; |
14
|
|
|
|
|
|
|
has family => sub ($self) { $self->id }; |
15
|
|
|
|
|
|
|
has id => sub ($self) { croak '"id" cannot be built' }; |
16
|
|
|
|
|
|
|
has module => 'mojo'; |
17
|
|
|
|
|
|
|
has name => ''; |
18
|
|
|
|
|
|
|
has options => ''; # "detail hidden obsolete" |
19
|
|
|
|
|
|
|
has plugin => 'mojo'; |
20
|
|
|
|
|
|
|
has priority => 10000; |
21
|
|
|
|
|
|
|
has title => sub ($self) { $self->name || $self->id }; |
22
|
|
|
|
|
|
|
has type => sub ($self) { croak '"type" cannot be built' }; |
23
|
|
|
|
|
|
|
has units => '#'; |
24
|
|
|
|
|
|
|
has update_every => 1; |
25
|
|
|
|
|
|
|
|
26
|
4
|
|
|
4
|
1
|
735
|
sub data_to_string ($self, $microseconds = undef) { |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
7
|
|
27
|
4
|
|
|
|
|
12
|
my $dimensions = $self->dimensions; |
28
|
|
|
|
|
|
|
my $set = join "\n", |
29
|
4
|
|
100
|
|
|
30
|
map { sprintf "SET %s = %s", $_, $dimensions->{$_}{value} // '' } sort keys %$dimensions; |
|
7
|
|
|
|
|
45
|
|
30
|
|
|
|
|
|
|
|
31
|
4
|
100
|
|
|
|
22
|
return !$set ? '' : sprintf "BEGIN %s.%s%s\n%s\nEND\n", safe_id($self->type), safe_id($self->id), |
|
|
50
|
|
|
|
|
|
32
|
|
|
|
|
|
|
($microseconds ? " $microseconds" : ""), $set; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
44
|
|
|
44
|
1
|
2365
|
sub dimension ($self, $name, $attrs = undef) { |
|
44
|
|
|
|
|
59
|
|
|
44
|
|
|
|
|
63
|
|
|
44
|
|
|
|
|
58
|
|
|
44
|
|
|
|
|
59
|
|
36
|
44
|
|
|
|
|
96
|
my $id = safe_id $name; |
37
|
44
|
100
|
|
|
|
116
|
return $self->dimensions->{$id} unless $attrs; |
38
|
29
|
|
100
|
|
|
63
|
my $dimension = $self->dimensions->{$id} //= {name => $name}; |
39
|
29
|
|
|
|
|
215
|
@$dimension{keys(%$attrs)} = values %$attrs; |
40
|
29
|
|
|
|
|
66
|
return $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
3
|
|
|
3
|
1
|
4762
|
sub to_string ($self) { |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
5
|
|
44
|
3
|
|
|
|
|
8
|
my $dimensions = $self->dimensions; |
45
|
3
|
100
|
|
|
|
20
|
return '' unless %$dimensions; |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
10
|
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
|
|
|
|
|
133
|
=~ s!([a-z_]+)!{$self->$1}!ger; |
|
22
|
|
|
|
|
71
|
|
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
35
|
for my $id (sort keys %$dimensions) { |
52
|
3
|
|
|
|
|
8
|
my $dimension = $dimensions->{$id}; |
53
|
3
|
|
100
|
|
|
20
|
$dimension->{algorithm} ||= 'absolute'; |
54
|
3
|
|
100
|
|
|
14
|
$dimension->{divisor} ||= 1; |
55
|
3
|
|
100
|
|
|
12
|
$dimension->{multiplier} ||= 1; |
56
|
3
|
|
66
|
|
|
16
|
$dimension->{name} ||= $id; |
57
|
3
|
|
100
|
|
|
17
|
$dimension->{options} ||= ''; |
58
|
3
|
|
|
|
|
14
|
$str .= sprintf "DIMENSION %s %s\n", $id, |
59
|
15
|
|
|
|
|
23
|
q('name' algorithm multiplier divisor 'options') =~ s!([a-z_]+)!{$dimension->{$1}}!ger; |
|
15
|
|
|
|
|
52
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
13
|
return $str; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
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 is a class that represents a Netdata chart and |
82
|
|
|
|
|
|
|
dimensions. See L |
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. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 family |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
$str = $chart->family; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Defaults to L. |
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 or L. |
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 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. Possible keys are "algorithm", |
187
|
|
|
|
|
|
|
"divisor", "multiplier", "name" and "options". |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
See L |
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. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=cut |