| 1 | package Mojo::Netdata::Collector; | ||||||||||||||||||||
| 2 | 2 | ▶ | use Mojo::Base 'Mojo::EventEmitter', -signatures; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::Netdata::Chart; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::Netdata::Util qw(logf safe_id); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Mojo::Promise; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 6 | ▶ | has charts => sub ($self) { +{} }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 4 | ▶ | has module => sub ($self) { lc(ref $self) =~ s!\W+!_!gr }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | has type => sub ($self) { croak '"type" cannot be built' }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | has update_every => 1; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 23 | ▶ | sub chart ($self, $id) { | ||||||||||||||||||
|
Subroutine
chart: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 16 | 23 | my $key = safe_id $id; | |||||||||||||||||||
| 17 | 23 | ▶ | return $self->charts->{$key} //= Mojo::Netdata::Chart->new( | ||||||||||||||||||
|
Condition66%2/3
$self->charts->{$key} //= "Mojo::Netdata::Chart"->new("module", $self->module, "id", $id, "type", $self->type, "update_every", $self->update_every)
MC/DC0%0/2
$self->charts->{$key} //= "Mojo::Netdata::Chart"->new("module", $self->module, "id", $id, "type", $self->type, "update_every", $self->update_every)
$self->charts->{$key}
"Mojo::Netdata::Chart"->new("module", $self->module, "id", $id, "type", $self->type, "update_every", $self->update_every)
Truth table66%2/3
A //= B
A= $self->charts->{$key}B= "Mojo::Netdata::Chart"->new("module", $self->module, "id", $id, "type", $self->type, "update_every", $self->update_every)
| |||||||||||||||||||||
| 18 | module => $self->module, | ||||||||||||||||||||
| 19 | id => $id, | ||||||||||||||||||||
| 20 | type => $self->type, | ||||||||||||||||||||
| 21 | update_every => $self->update_every, | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 3 | ▶ | sub recurring_update_p ($self) { | ||||||||||||||||||
|
Subroutine
recurring_update_p: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 26 | 3 | my $next_time = time + $self->update_every; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2 | ▶ | return $self->{recurring_update_p} //= $self->update_p->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 2 | $self->emit_data; | |||||||||||||||||||
| 30 | 2 | logf(debug => 'Will update in %0.3fs...', $next_time - time); | |||||||||||||||||||
| 31 | 2 | return Mojo::Promise->timer($next_time - time); | |||||||||||||||||||
| 32 | 2 | ▶ | })->then(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 2 | delete $self->{recurring_update_p}; | |||||||||||||||||||
| 34 | 2 | return $self->recurring_update_p; | |||||||||||||||||||
| 35 | 3 | ▶ | }); | ||||||||||||||||||
|
Condition33%1/3
$self->{"recurring_update_p"} //= $self->update_p->then(sub {
$self->emit_data;
logf("debug", "Will update in %0.3fs...", $next_time - &Mojo::Netdata::Collector::time());
return "Mojo::Promise"->timer($next_time - &Mojo::Netdata::Collector::time());
}
)->then(sub {
delete $self->{"recurring_update_p"};
return $self->recurring_update_p;
}
)
MC/DC0%0/2
$self->{"recurring_update_p"} //= $self->update_p->then(sub {
$self->emit_data;
logf("debug", "Will update in %0.3fs...", $next_time - &Mojo::Netdata::Collector::time());
return "Mojo::Promise"->timer($next_time - &Mojo::Netdata::Collector::time());
}
)->then(sub {
delete $self->{"recurring_update_p"};
return $self->recurring_update_p;
}
)
$self->{"recurring_update_p"}
$self->update_p->then(sub {
$self->emit_data;
logf("debug", "Will update in %0.3fs...", $next_time - &Mojo::Netdata::Collector::time());
return "Mojo::Promise"->timer($next_time - &Mojo::Netdata::Collector::time());
}
)->then(sub {
delete $self->{"recurring_update_p"};
return $self->recurring_update_p;
}
)
Truth table33%1/3
A //= B
A= $self->{"recurring_update_p"}B= $self->update_p->then(sub {
$self->emit_data;
logf("debug", "Will update in %0.3fs...", $next_time - &Mojo::Netdata::Collector::time());
return "Mojo::Promise"->timer($next_time - &Mojo::Netdata::Collector::time());
}
)->then(sub {
delete $self->{"recurring_update_p"};
return $self->recurring_update_p;
}
)
| |||||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | sub register ($self, $config, $netdata) { } | ||||||||||||||||||||
| 39 | 1 | ▶ | sub update_p ($self) { Mojo::Promise->resolve } | ||||||||||||||||||
|
Subroutine
update_p: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 4 | ▶ | sub emit_data ($self) { | ||||||||||||||||||
|
Subroutine
emit_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 42 | 4 | my @stdout = map { $self->charts->{$_}->data_to_string } sort keys %{$self->charts}; | |||||||||||||||||||
| 43 | 4 | return $self->emit(stdout => join '', @stdout); | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 2 | ▶ | sub emit_charts ($self) { | ||||||||||||||||||
|
Subroutine
emit_charts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 47 | 2 | my @stdout = map { $self->charts->{$_}->to_string } sort keys %{$self->charts}; | |||||||||||||||||||
| 48 | 2 | return $self->emit(stdout => join '', @stdout); | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 2 | 1; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =encoding utf8 | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head1 NAME | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | Mojo::Netdata::Collector - Base class for Mojo::Netdata collectors | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head1 SYNOPSIS | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | package Mojo::Netdata::Collector::CoolBeans; | ||||||||||||||||||||
| 62 | use Mojo::Base 'Mojo::Netdata::Collector', -signatures; | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | has type => 'ice_cool'; | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub register ($self, $config, $netdata) { ... } | ||||||||||||||||||||
| 67 | sub update_p ($self) { ... } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 1; | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =head1 DESCRIPTION | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | L<Mojo::Netdata::Collector> has basic functionality which should be inherited | ||||||||||||||||||||
| 74 | by L<Mojo::Netdata> collectors. See L<Mojo::Netdata::Collector::HTTP> for an | ||||||||||||||||||||
| 75 | (example) implementation. | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =head2 charts | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | $hash_ref = $collector->charts; | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =head2 module | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | $str = $collector->module; | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | Defaults to a decamelized version of L<$collector>. This is used as default | ||||||||||||||||||||
| 88 | value for L<Mojo::Netdata::Chart/module>. | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =head2 type | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | $str = $collector->type; | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | This value must be set. This is used as default value for | ||||||||||||||||||||
| 95 | L<Mojo::Netdata::Chart/type>. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =head2 update_every | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | $num = $chart->update_every; | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Used by L</recurring_update_p> to figure out how often to update Netdata. | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =head1 METHODS | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head2 chart | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | $chart = $collector->chart($id); | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | Returns a L<Mojo::Netdata::Chart> object identified by L</id>. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head2 emit_charts | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | $collector = $collector->emit_charts; | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Emits all the L</charts> specifications as an "stdout" event. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head2 emit_data | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | $collector = $collector->emit_data; | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | Emits all the L</charts> data as an "stdout" event. | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =head2 recurring_update_p | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | $p = $collector->recurring_update_p; | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | Calls L</update_p> on L</update_every> interval, until the process is killed. | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head2 register | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | $collector = $collector->register(\%config, $netdata); | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Called by L<Mojo::Netdata> when initialized, and should return C<$collector> on | ||||||||||||||||||||
| 134 | success or C<undef> if the collector should I<not> be registered. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head2 update_p | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | $p = $collector->update_p; | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | Must be defined in the sub class. This method should update | ||||||||||||||||||||
| 141 | L<Mojo::Netdata::Chart/dimensions>. | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =head1 SEE ALSO | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | L<Mojo::Netdata>. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||