« blib/lib/App/RecordStream/Operation/toptable.pm
↑ Summary
blib/lib/App/RecordStream/DomainLanguage/Registry.pm »
| 1 | package App::RecordStream::Operation::togdgraph; | ||||||||||
| 2 | |||||||||||
| 3 | our $VERSION = "4.0.23"; | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use App::RecordStream::OptionalRequire qw(GD::Graph::lines); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use App::RecordStream::OptionalRequire qw(GD::Graph::bars); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use App::RecordStream::OptionalRequire qw(GD::Graph::points); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | App::RecordStream::OptionalRequire::require_done(); | ||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use base qw(App::RecordStream::Operation); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | my $GD_TYPES = { | ||||||||||
| 16 | 'line' => 'lines', | ||||||||||
| 17 | 'scatter' => 'points', | ||||||||||
| 18 | 'bar' => 'bars' | ||||||||||
| 19 | }; | ||||||||||
| 20 | |||||||||||
| 21 | sub init { | ||||||||||
| 22 | 0 | ▶ | my $this = shift; | ||||||||
|
Subroutine
init: not called CC=7 SCAR=40.3
| |||||||||||
| 23 | 0 | my $args = shift; | |||||||||
| 24 | |||||||||||
| 25 | 0 | my $png_file = 'togdgraph.png'; | |||||||||
| 26 | 0 | my $title; | |||||||||
| 27 | my $label_x; | ||||||||||
| 28 | 0 | my $label_y; | |||||||||
| 29 | 0 | my @additional_options; | |||||||||
| 30 | 0 | my $graph_type = 'scatter'; | |||||||||
| 31 | 0 | my $width = 600; | |||||||||
| 32 | 0 | my $height = 300; | |||||||||
| 33 | |||||||||||
| 34 | 0 | my $dump_use_spec; | |||||||||
| 35 | |||||||||||
| 36 | 0 | my $key_groups = App::RecordStream::KeyGroups->new(); | |||||||||
| 37 | |||||||||||
| 38 | my $cmdspec = { | ||||||||||
| 39 | 0 | ▶ | 'key|k|fields|f=s' => sub { $key_groups->add_groups($_[1]); }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 40 | 0 | ▶ | 'option|o=s' => sub { push @additional_options, [split(/=/, $_[1])]; }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 41 | 0 | 'label-x=s' => \$label_x, | |||||||||
| 42 | 'label-y=s' => \$label_y, | ||||||||||
| 43 | 'graph-title=s' => \$title, | ||||||||||
| 44 | 'png-file=s' => \$png_file, | ||||||||||
| 45 | 'type=s' => \$graph_type, | ||||||||||
| 46 | 'width=i' => \$width, | ||||||||||
| 47 | 'height=i' => \$height, | ||||||||||
| 48 | 'dump-use-spec' => \$dump_use_spec | ||||||||||
| 49 | }; | ||||||||||
| 50 | 0 | $this->parse_options($args, $cmdspec); | |||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | if ( ! $GD_TYPES->{$graph_type} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | die "Unsupported graph type: $graph_type\n"; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | $this->{'DUMP_USE_SPEC'} = $dump_use_spec; | |||||||||
| 57 | |||||||||||
| 58 | 0 | $this->{'LABEL_X'} = $label_x; | |||||||||
| 59 | 0 | $this->{'LABEL_Y'} = $label_y; | |||||||||
| 60 | 0 | ▶ | $this->{'TITLE'} = $title unless !$this->{'TITLE'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 0 | $this->{'GDGRAPH_OPTIONS'} = \@additional_options; | |||||||||
| 63 | 0 | $this->{'KEYGROUPS'} = $key_groups; | |||||||||
| 64 | 0 | $this->{'FIRST_RECORD'} = 1; | |||||||||
| 65 | |||||||||||
| 66 | 0 | $this->{'GRAPH_TYPE'} = $graph_type; | |||||||||
| 67 | 0 | $this->{'WIDTH'} = $width; | |||||||||
| 68 | 0 | $this->{'HEIGHT'} = $height; | |||||||||
| 69 | 0 | $this->{'PNG_FILE'} = $png_file; | |||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | if ( $dump_use_spec ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | ▶ | $this->push_line('x label: '.$title) unless !$this->{'LABEL_X'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | ▶ | $this->push_line('y label: '.$title) unless !$this->{'LABEL_Y'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | ▶ | $this->push_line('title: '.$title) unless !$this->{'TITLE'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | $this->push_line('type: '.$graph_type); | |||||||||
| 76 | 0 | $this->push_line('width: '.$width); | |||||||||
| 77 | 0 | $this->push_line('height: '.$height); | |||||||||
| 78 | 0 | $this->push_line('output file: '.$png_file); | |||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | sub init_fields { | ||||||||||
| 83 | 0 | ▶ | my ($this, $record) = @_; | ||||||||
|
Subroutine
init_fields: not called CC=4 SCAR=30.0
| |||||||||||
| 84 | |||||||||||
| 85 | 0 | my $specs = $this->{'KEYGROUPS'}->get_keyspecs($record); | |||||||||
| 86 | 0 | ▶ | if ( $this->{'DUMP_USE_SPEC'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | foreach my $sfield (@{$specs}) { | |||||||||
| 88 | 0 | $this->push_line('field: '.$sfield); | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | 0 | $this->{'FIELDS'} = $specs; | |||||||||
| 92 | |||||||||||
| 93 | 0 | $this->{'PLOTDATA'} = (); | |||||||||
| 94 | 0 | foreach my $fkey (@{$this->{'FIELDS'}}) { | |||||||||
| 95 | 0 | $this->{'PLOTDATA'}->{$fkey} = []; | |||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | sub accept_record { | ||||||||||
| 100 | 0 | ▶ | my $this = shift; | ||||||||
|
Subroutine
accept_record: not called CC=4 SCAR=30.0
| |||||||||||
| 101 | 0 | my $record = shift; | |||||||||
| 102 | |||||||||||
| 103 | 0 | ▶ | if ( $this->{'FIRST_RECORD'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | $this->{'FIRST_RECORD'} = 0; | |||||||||
| 105 | 0 | $this->init_fields($record); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 0 | my @record_spec; | |||||||||
| 109 | 0 | foreach my $key (@{$this->{'FIELDS'}}) { | |||||||||
| 110 | 0 | push @{$this->{'PLOTDATA'}->{$key}}, $record->{$key}; | |||||||||
| 111 | 0 | push @record_spec, $record->{$key}; | |||||||||
| 112 | } | ||||||||||
| 113 | 0 | ▶ | if ( $this->{'DUMP_USE_SPEC'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | $this->push_line(join(' ',@record_spec)); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 0 | return 1; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | sub stream_done { | ||||||||||
| 121 | 0 | ▶ | my $this = shift; | ||||||||
|
Subroutine
stream_done: not called CC=7 SCAR=40.3
| |||||||||||
| 122 | |||||||||||
| 123 | 0 | my $gdhnd; | |||||||||
| 124 | 0 | my $w = $this->{'WIDTH'}; | |||||||||
| 125 | 0 | my $h = $this->{'HEIGHT'}; | |||||||||
| 126 | |||||||||||
| 127 | 0 | my $gtype = 'GD::Graph::'.$GD_TYPES->{$this->{'GRAPH_TYPE'}}; | |||||||||
| 128 | 0 | $gdhnd = $gtype->new($w,$h); | |||||||||
| 129 | |||||||||||
| 130 | $gdhnd->set( | ||||||||||
| 131 | x_label => $this->{'LABEL_X'}, | ||||||||||
| 132 | 0 | y_label => $this->{'LABEL_Y'} | |||||||||
| 133 | ); | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | if ( $this->{'TITLE'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | $gdhnd->set( title => $this->{'TITLE'} ); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 0 | foreach my $kv (@{$this->{'GDGRAPH_OPTIONS'}}) { | |||||||||
| 140 | 0 | $gdhnd->set( $kv->[0] => $kv->[1] ); | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | my @data; | |||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | if ( scalar(keys %{$this->{'PLOTDATA'}}) == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 0 | my @hkey = keys(%{$this->{'PLOTDATA'}}); | |||||||||
| 147 | 0 | my $arrsize = scalar @{$this->{'PLOTDATA'}->{$hkey[0]}}; | |||||||||
| 148 | 0 | push @data, [ 1 .. $arrsize ]; | |||||||||
| 149 | 0 | push @data, $this->{'PLOTDATA'}->{$hkey[0]}; | |||||||||
| 150 | } else { | ||||||||||
| 151 | 0 | for my $field (@{$this->{'FIELDS'}}) { | |||||||||
| 152 | 0 | push @data, $this->{'PLOTDATA'}->{$field}; | |||||||||
| 153 | } | ||||||||||
| 154 | } | ||||||||||
| 155 | 0 | my $gd = $gdhnd->plot(\@data); | |||||||||
| 156 | 0 | ▶ | if ( !$gd ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | print "could not plot data\n"; | |||||||||
| 158 | 0 | exit; | |||||||||
| 159 | } | ||||||||||
| 160 | 0 | ▶ | open(IMG, '>', $this->{'PNG_FILE'}) or die "Could not open file for writing $this->{PNG_FILE}: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 0 | binmode IMG; | |||||||||
| 162 | 0 | print IMG $gd->png; | |||||||||
| 163 | 0 | close IMG; | |||||||||
| 164 | |||||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | sub add_help_types { | ||||||||||
| 168 | 0 | ▶ | my $this = shift; | ||||||||
|
Subroutine
add_help_types: not called CC=1 SCAR=6.9
| |||||||||||
| 169 | 0 | $this->use_help_type('keyspecs'); | |||||||||
| 170 | 0 | $this->use_help_type('keygroups'); | |||||||||
| 171 | 0 | $this->use_help_type('keys'); | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | sub usage { | ||||||||||
| 175 | 0 | ▶ | my $this = shift; | ||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||
| 176 | |||||||||||
| 177 | 0 | my $options = [ | |||||||||
| 178 | ['key|-k|--key <keyspec>', 'Specify keys that correlate to keys in JSON data'], | ||||||||||
| 179 | ['option|-o option=val', 'Specify custom command for GD::Graph'], | ||||||||||
| 180 | ['label-x <val>', 'Specify X-axis label'], | ||||||||||
| 181 | ['label-y <val>', 'Specify Y-axis label'], | ||||||||||
| 182 | ['width <val>', 'Specify width'], | ||||||||||
| 183 | ['height <val>', 'Specify height'], | ||||||||||
| 184 | ['graph-title <val>', 'Specify graph title'], | ||||||||||
| 185 | ['type <val>', 'Specify different graph type other than scatter (supported: line, bar)'], | ||||||||||
| 186 | ['png-file <val>', 'Specify output PNG filename'], | ||||||||||
| 187 | ['dump-use-spec <val>', 'Dump GD usage (used mainly for testing)'] | ||||||||||
| 188 | ]; | ||||||||||
| 189 | |||||||||||
| 190 | 0 | my $args_string = $this->options_string($options); | |||||||||
| 191 | |||||||||||
| 192 | 0 | return <<USAGE; | |||||||||
| 193 | Usage: recs-togdgraph <args> [<files>] | ||||||||||
| 194 | __FORMAT_TEXT__ | ||||||||||
| 195 | Create a bar, scatter, or line graph using GD::Graph. | ||||||||||
| 196 | __FORMAT_TEXT__ | ||||||||||
| 197 | |||||||||||
| 198 | Args: | ||||||||||
| 199 | $args_string | ||||||||||
| 200 | |||||||||||
| 201 | Examples: | ||||||||||
| 202 | for a plain point graph: | ||||||||||
| 203 | |||||||||||
| 204 | recs-togdgraph --keys uid,ct --png-file login-graph.png --graph-title '# of logins' --label-x user --label-y logins | ||||||||||
| 205 | |||||||||||
| 206 | togdgraph also accepts any GD::Graph options with the --option command... | ||||||||||
| 207 | for a pink background with yellow label text if that really is your thing: | ||||||||||
| 208 | |||||||||||
| 209 | recs-togdgraph --keys uid,ct --option boxclr=pink --label-y 'logins' --label-x 'user' --option labelclr=yellow | ||||||||||
| 210 | |||||||||||
| 211 | however, for a different graph type such as line or bar, specify with --type: | ||||||||||
| 212 | |||||||||||
| 213 | recs-togdgraph --keys uid,ct --type line | ||||||||||
| 214 | USAGE | ||||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | 1; | ||||||||||