line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Netdata::Chart; |
2
|
3
|
|
|
3
|
|
197686
|
use Mojo::Base -base, -signatures; |
|
3
|
|
|
|
|
165118
|
|
|
3
|
|
|
|
|
18
|
|
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
4770
|
use Carp qw(croak); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
123
|
|
5
|
3
|
|
|
3
|
|
775
|
use Mojo::Netdata::Util qw(safe_id); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
2604
|
|
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
|
8
|
|
|
8
|
1
|
616
|
sub data_to_string ($self, $microseconds = undef) { |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
10
|
|
27
|
8
|
|
|
|
|
40
|
my $dimensions = $self->dimensions; |
28
|
|
|
|
|
|
|
my $set = join "\n", |
29
|
8
|
|
100
|
|
|
42
|
map { sprintf "SET %s = %s", $_, $dimensions->{$_}{value} // '' } sort keys %$dimensions; |
|
13
|
|
|
|
|
79
|
|
30
|
|
|
|
|
|
|
|
31
|
8
|
100
|
|
|
|
35
|
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
|
59
|
|
|
59
|
1
|
2059
|
sub dimension ($self, $name, $attrs = undef) { |
|
59
|
|
|
|
|
67
|
|
|
59
|
|
|
|
|
74
|
|
|
59
|
|
|
|
|
71
|
|
|
59
|
|
|
|
|
68
|
|
36
|
59
|
|
|
|
|
104
|
my $id = safe_id $name; |
37
|
59
|
100
|
|
|
|
151
|
return $self->dimensions->{$id} unless $attrs; |
38
|
41
|
|
100
|
|
|
101
|
my $dimension = $self->dimensions->{$id} //= {name => $name}; |
39
|
41
|
|
|
|
|
269
|
@$dimension{keys(%$attrs)} = values %$attrs; |
40
|
41
|
|
|
|
|
83
|
return $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
7
|
|
|
7
|
1
|
3837
|
sub to_string ($self) { |
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
9
|
|
44
|
7
|
|
|
|
|
19
|
my $dimensions = $self->dimensions; |
45
|
7
|
100
|
|
|
|
39
|
return '' unless %$dimensions; |
46
|
|
|
|
|
|
|
|
47
|
6
|
|
|
|
|
16
|
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
|
66
|
|
|
|
|
295
|
=~ s!([a-z_]+)!{$self->$1}!ger; |
|
66
|
|
|
|
|
179
|
|
50
|
|
|
|
|
|
|
|
51
|
6
|
|
|
|
|
77
|
for my $id (sort keys %$dimensions) { |
52
|
9
|
|
|
|
|
16
|
my $dimension = $dimensions->{$id}; |
53
|
9
|
|
100
|
|
|
46
|
$dimension->{algorithm} ||= 'absolute'; |
54
|
9
|
|
100
|
|
|
29
|
$dimension->{divisor} ||= 1; |
55
|
9
|
|
100
|
|
|
29
|
$dimension->{multiplier} ||= 1; |
56
|
9
|
|
66
|
|
|
24
|
$dimension->{name} ||= $id; |
57
|
9
|
|
100
|
|
|
33
|
$dimension->{options} ||= ''; |
58
|
9
|
|
|
|
|
31
|
$str .= sprintf "DIMENSION %s %s\n", $id, |
59
|
45
|
|
|
|
|
54
|
q('name' algorithm multiplier divisor 'options') =~ s!([a-z_]+)!{$dimension->{$1}}!ger; |
|
45
|
|
|
|
|
123
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
6
|
|
|
|
|
25
|
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 |