| 1 | package Net::Async::Graphite::Draw; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '0.1_1'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =encoding utf8 | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Net::Async::Graphite::Draw - Interpret data obtained from graphite. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | with 'Net::Async::Graphite::Draw'; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Don't use this module directly, use L<Net::Async::Graphite> and create | ||||||||||||||||||||||||||||
| 18 | objects using its C<new> method in the normal way. Those objects will | ||||||||||||||||||||||||||||
| 19 | include the functionality documented here. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | This role brings the capacity to interpret and encode the data | ||||||||||||||||||||||||||||
| 22 | obtained from graphite in various ways. Currently those various ways | ||||||||||||||||||||||||||||
| 23 | are gnuplot, and probably using a blocking module at that (although | ||||||||||||||||||||||||||||
| 24 | it's pretty fast). | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 BUGS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Gnuplut may be called synchronously. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | plot() is ridiculously naïve. Format and scale in particular need to | ||||||||||||||||||||||||||||
| 31 | be dealt with, and the timestamps and step returned by graphite. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | use Gnuplot::Builder::Dataset; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | use Gnuplot::Builder::Script; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use List::Util qw(min max); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 ROLE | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =over | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =item last_value ($target, [%extra]) | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Calls C<render()> with the same arguments but prepended by the format | ||||||||||||||||||||||||||||
| 56 | C<raw>, and returns (a L<Future> which completes to) a list of the final | ||||||||||||||||||||||||||||
| 57 | datum on each line in the response. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub last_value { | ||||||||||||||||||||||||||
|
Subroutine
last_value: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | $self->render(raw => @_)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $response = shift or return Future->fail('no data'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | Future->done(map { | ||||||||||||||||||||||||||||
| 66 | 0 | my $result = (split /\|/)[-1]; | |||||||||||||||||||||||||||
| 67 | # Blessed object which scalar's into this but has attributes for other data? | ||||||||||||||||||||||||||||
| 68 | 0 | (split /,/, $result)[-1]; | |||||||||||||||||||||||||||
| 69 | } split /\r?\n/, $response); | ||||||||||||||||||||||||||||
| 70 | 0 | }); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item plot ($target, [%extra]) | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Return a (L<Future> which completes to) a scalar containing the result | ||||||||||||||||||||||||||||
| 76 | of running a plot inside a child gnuplot process. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Mostly this exists for fun so that I can get data displayed on the | ||||||||||||||||||||||||||||
| 79 | terminal. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | my %plots = ( | ||||||||||||||||||||||||||||
| 84 | ascii => { | ||||||||||||||||||||||||||||
| 85 | terminal => 'dumb', | ||||||||||||||||||||||||||||
| 86 | # Use a proper module instead of tput. | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | default_size => sub { [ map int, `tput cols`, `tput lines` ] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||
| 89 | 3 | png => { | |||||||||||||||||||||||||||
| 90 | terminal => 'png', | ||||||||||||||||||||||||||||
| 91 | default_size => [ 640, 480 ], | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub plot { | ||||||||||||||||||||||||||
|
Subroutine
plot: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 97 | 0 | my (undef, %extra) = @_; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $format = delete $extra{format} || 'ascii'; # To not confuse render() | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $extra{'format'} || 'ascii'
MC/DC0%0/1
delete $extra{'format'} || 'ascii'
delete $extra{'format'}
Truth table0%0/2
A
A= delete $extra{'format'}
| |||||||||||||||||||||||||||||
| 99 | return Future->fail("Invalid plot format: $format") | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | unless exists $plots{$format}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | $self->_plot_gather_datasets(@_)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my @req_size = ( $extra{width} || 0, $extra{height} || 0 ); | ||||||||||||||||||||||||||
|
Condition0%0/4
$extra{'width'} || 0
$extra{'height'} || 0
MC/DC0%0/1
$extra{'width'} || 0
$extra{'width'}
MC/DC0%0/1
$extra{'height'} || 0
$extra{'height'}
Truth table0%0/2
A
A= $extra{'width'}
Truth table0%0/2
A
A= $extra{'height'}
| |||||||||||||||||||||||||||||
| 103 | 0 | my @default_size = @{ $self->_default_plot_size($format) }; | |||||||||||||||||||||||||||
| 104 | my @real_size = map { | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $req_size[$_] < 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$req_size[$_] or $default_size[$_]
MC/DC0%0/2
$req_size[$_] or $default_size[$_]
$req_size[$_]
$default_size[$_]
Truth table0%0/3
A or B
A= $req_size[$_]B= $default_size[$_]
| |||||||||||||||||||||||||||||
| 106 | ? $default_size[$_] + $req_size[$_] | ||||||||||||||||||||||||||||
| 107 | : $req_size[$_] || $default_size[$_] | ||||||||||||||||||||||||||||
| 108 | } 0..1; | ||||||||||||||||||||||||||||
| 109 | 0 | my @sets = @_; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $min = min (map { $_->{min} } @sets) || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
&min(map({$_->{'min'};} @sets)) || 0
MC/DC0%0/1
&min(map({$_->{'min'};} @sets)) || 0
&min(map({$_->{'min'};} @sets))
Truth table0%0/2
A
A= &min(map({$_->{'min'};} @sets))
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $max = max (map { $_->{max} } @sets) || 1; # Nonsensical but better than die (maybe) | ||||||||||||||||||||||||||
|
Condition0%0/2
&max(map({$_->{'max'};} @sets)) || 1
MC/DC0%0/1
&max(map({$_->{'max'};} @sets)) || 1
&max(map({$_->{'max'};} @sets))
Truth table0%0/2
A
A= &max(map({$_->{'max'};} @sets))
| |||||||||||||||||||||||||||||
| 112 | my $plot = Gnuplot::Builder::Script->new( | ||||||||||||||||||||||||||||
| 113 | terminal => "$plots{$format}{terminal} size $real_size[0],$real_size[1] enhanced", | ||||||||||||||||||||||||||||
| 114 | yrange => "[$min:$max]", | ||||||||||||||||||||||||||||
| 115 | 0 | )->plot_with(dataset => [ map { $_->{dataset} } @_ ], | |||||||||||||||||||||||||||
| 116 | no_stderr => 1); # Would be nice to put it somewhere | ||||||||||||||||||||||||||||
| 117 | # but it's all or nothing. | ||||||||||||||||||||||||||||
| 118 | 0 | Future->done($plot); | |||||||||||||||||||||||||||
| 119 | 0 | }); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item _plot_gather_datasets | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | I can't remember. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub _plot_gather_datasets { | ||||||||||||||||||||||||||
|
Subroutine
_plot_gather_datasets: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 129 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | $self->render_asperl(@_)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 131 | Future->done(map { | ||||||||||||||||||||||||||||
| 132 | 0 | my ($min, $max) = (0,0); | |||||||||||||||||||||||||||
| 133 | my $dataset = Gnuplot::Builder::Dataset->new_data(join "\n", | ||||||||||||||||||||||||||||
| 134 | map { | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $datum = looks_like_number $_ ? $_ : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | $min = min($min, $datum); | |||||||||||||||||||||||||||
| 137 | 0 | $max = max($max, $datum); | |||||||||||||||||||||||||||
| 138 | 0 | $datum; | |||||||||||||||||||||||||||
| 139 | 0 | } @{ $_->{data} }); | |||||||||||||||||||||||||||
| 140 | 0 | $dataset->set(title => "\"$_->{target}\""); | |||||||||||||||||||||||||||
| 141 | 0 | +{ %$_, min => $min, max => $max, dataset => $dataset }; | |||||||||||||||||||||||||||
| 142 | } @_); | ||||||||||||||||||||||||||||
| 143 | 0 | }); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =item _default_plot_size | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | I can't remember. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub _default_plot_size { | ||||||||||||||||||||||||||
|
Subroutine
_default_plot_size: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 153 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | 0 | my ($format) = @_; | |||||||||||||||||||||||||||
| 155 | 0 | my $default = $plots{$format}{default_size}; | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | ref $default eq 'ARRAY' ? $default : $default->($self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 3 | 1; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =back | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Gnuplot L<http://gnuplot.info/> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | L<Net::Async::Graphite> | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | L<Gnuplot::Builder> | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | L<Future> | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | L<Moo> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Matthew King <matthew.king@cloudbeds.com> | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||