| 1 | package Net::Async::Graphite::Draw; | ||
| 2 | |||
| 3 | our $VERSION = '0.0_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 | use v5.14; | ||
| 36 | use strictures 2; | ||
| 37 | use Moo::Role; | ||
| 38 | use Carp; | ||
| 39 | |||
| 40 | use Gnuplot::Builder::Dataset; | ||
| 41 | use Gnuplot::Builder::Script; | ||
| 42 | use Future; | ||
| 43 | use List::Util qw(min max); | ||
| 44 | use Scalar::Util qw(looks_like_number); | ||
| 45 | use namespace::clean; | ||
| 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 | sub last_value { | ||
| 62 | my $self = shift; | ||
| 63 | $self->render(raw => @_)->then(sub { | ||
| 64 | my $response = shift or return Future->fail('no data'); | ||
| 65 | Future->done(map { | ||
| 66 | my $result = (split /\|/)[-1]; | ||
| 67 | # Blessed object which scalar's into this but has attributes for other data? | ||
| 68 | (split /,/, $result)[-1]; | ||
| 69 | } split /\r?\n/, $response); | ||
| 70 | }); | ||
| 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 | default_size => sub { [ map int, `tput cols`, `tput lines` ] }, | ||
| 88 | }, | ||
| 89 | png => { | ||
| 90 | terminal => 'png', | ||
| 91 | default_size => [ 640, 480 ], | ||
| 92 | } | ||
| 93 | ); | ||
| 94 | |||
| 95 | sub plot { | ||
| 96 | my $self = shift; | ||
| 97 | my (undef, %extra) = @_; | ||
| 98 | my $format = delete $extra{format} || 'ascii'; # To not confuse render() | ||
| 99 | return Future->fail("Invalid plot format: $format") | ||
| 100 | unless exists $plots{$format}; | ||
| 101 | $self->_plot_gather_datasets(@_)->then(sub { | ||
| 102 | my @req_size = ( $extra{width} || 0, $extra{height} || 0 ); | ||
| 103 | my @default_size = @{ $self->_default_plot_size($format) }; | ||
| 104 | my @real_size = map { | ||
| 105 | $req_size[$_] < 0 | ||
| 106 | ? $default_size[$_] + $req_size[$_] | ||
| 107 | : $req_size[$_] || $default_size[$_] | ||
| 108 | } 0..1; | ||
| 109 | my @sets = @_; | ||
| 110 | my $min = min (map { $_->{min} } @sets) || 0; | ||
| 111 | my $max = max (map { $_->{max} } @sets) || 1; # Nonsensical but better than die (maybe) | ||
| 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 | )->plot_with(dataset => [ map { $_->{dataset} } @_ ], | ||
| 116 | no_stderr => 1); # Would be nice to put it somewhere | ||
| 117 | # but it's all or nothing. | ||
| 118 | Future->done($plot); | ||
| 119 | }); | ||
| 120 | } | ||
| 121 | |||
| 122 | =item _plot_gather_datasets | ||
| 123 | |||
| 124 | I can't remember. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub _plot_gather_datasets { | ||
| 129 | my $self = shift; | ||
| 130 | $self->render_asperl(@_)->then(sub { | ||
| 131 | Future->done(map { | ||
| 132 | my ($min, $max) = (0,0); | ||
| 133 | my $dataset = Gnuplot::Builder::Dataset->new_data(join "\n", | ||
| 134 | map { | ||
| 135 | my $datum = looks_like_number $_ ? $_ : 0; | ||
| 136 | $min = min($min, $datum); | ||
| 137 | $max = max($max, $datum); | ||
| 138 | $datum; | ||
| 139 | } @{ $_->{data} }); | ||
| 140 | $dataset->set(title => "\"$_->{target}\""); | ||
| 141 | +{ %$_, min => $min, max => $max, dataset => $dataset }; | ||
| 142 | } @_); | ||
| 143 | }); | ||
| 144 | } | ||
| 145 | |||
| 146 | =item _default_plot_size | ||
| 147 | |||
| 148 | I can't remember. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub _default_plot_size { | ||
| 153 | my $self = shift; | ||
| 154 | my ($format) = @_; | ||
| 155 | my $default = $plots{$format}{default_size}; | ||
| 156 | ref $default eq 'ARRAY' ? $default : $default->($self); | ||
| 157 | } | ||
| 158 | |||
| 159 | 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 |