blib/lib/App/RecordStream/Operation/tognuplot.pm

filter: Statementstmt 79.3% 123 / 155 Branchbran 46.0% 23 / 50 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 76.4% 13 / 17 Podpod 0.0% 0 / 7 totaltotal 65.0% 162 / 249 CC 34 SCAR 25.8
CC
34
Cov
69%
init_fields
28.8
stream_done
18.4
init
8.5
CRAP 13.2
« blib/lib/App/RecordStream/Test/OperationHelper.pm ↑ Summary blib/lib/App/RecordStream/Site.pm »
1 package App::RecordStream::Operation::tognuplot;
2  
3 2 our $VERSION = "4.0.23";
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub init {
13 2 ▶ my $this = shift;
Subroutine init: called CC=7 SCAR=21.5
Pod undocumented
14 2 my $args = shift;
15  
16 2 my $bar_graph;
17 2 my $dump_to_screen = 0;
18 2 my $gnuplot_command = 'gnuplot';
19 2 my $lines;
20 2 my $png_file = 'tognuplot.png';
21 2 my $title;
22 my @labels;
23 2 my @plots;
24 2 my @precommands;
25 2 my @using;
26  
27 2 my $key_groups = App::RecordStream::KeyGroups->new();
28  
29 my $spec = {
30 "file=s" => \$png_file,
31 1 ▶ "key|k|fields|f=s" => sub { $key_groups->add_groups($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 0 ▶ "label=s" => sub { push @labels, split(/,/, $_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 ▶ "plot=s" => sub { push @plots, split(/,/, $_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 ▶ "precommand=s" => sub { push @precommands, split(/,/, $_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 "title=s" => \$title,
36 0 ▶ "using=s" => sub { push @using, $_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 2 'bargraph' => \$bar_graph,
38 'dump-to-screen' => \$dump_to_screen,
39 'gnuplot-command=s' => \$gnuplot_command,
40 'lines' => \$lines,
41 $this->site_args(),
42 };
43  
44 2 $this->parse_options($args, $spec);
45  
46 1 ▶ die 'Must specify at least one field' unless ( $key_groups->has_any_group() );
Branch: 1 branch
BranchTrueFalseTotal
unless $key_groups->has_any_group 0 1 1
47  
48 1 ▶ if ( $bar_graph && $lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bar_graph and $lines) 0 1 1
Condition33%1/3
$bar_graph and $lines
!ll&&!rl&&r
100
MC/DC0%0/2
$bar_graph and $lines
$bar_graph $lines
Truth table33%1/3
A and B
A= $bar_graphB= $lines
ABresult
0X0
100
111
49 0 die 'Must specify one of --bargraph or --lines';
50 }
51  
52 1 ▶ $png_file .= '.png' unless ( $png_file =~ m/\.png$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $png_file =~ /\.png$/ 0 1 1
53  
54 1 ▶ if ( ! $dump_to_screen ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dump_to_screen) 0 1 1
55 0 ▶ if ( open(my $fh, '|-', $gnuplot_command) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '|-', $gnuplot_command) { } 0 0 0
56 0 close $fh;
57 }
58 else {
59 0 warn "Could not run gnuplot command: $gnuplot_command: $!\n";
60 0 warn "May want to specify a binary with --gnuplot-command\n";
61 0 exit 0;
62 }
63 }
64  
65 1 my ($tempfh, $tempfile) = tempfile();
66  
67 1 $this->{'BAR_GRAPH'} = $bar_graph;
68 1 $this->{'DUMP_TO_SCREEN'} = $dump_to_screen;
69 1 $this->{'FIRST_RECORD'} = 1;
70 1 $this->{'GNUPLOT_COMMAND'} = $gnuplot_command;
71 1 $this->{'KEY_GROUPS'} = $key_groups;
72 1 $this->{'LABELS'} = \@labels;
73 1 $this->{'LINES'} = $lines;
74 1 $this->{'PLOTS'} = \@plots;
75 1 $this->{'PNG_FILE'} = $png_file;
76 1 $this->{'PRECOMMANDS'} = \@precommands;
77 1 $this->{'TEMPFH'} = $tempfh;
78 1 $this->{'TEMPFILE'} = $tempfile;
79 1 $this->{'TITLE'} = $title;
80 1 $this->{'USING'} = \@using;
81 }
82  
83 sub init_fields {
84 1 ▶ my $this = shift;
Subroutine init_fields: called CC=12 SCAR=33.6
Pod undocumented
85 1 my $record = shift;
86  
87 1 my $specs = $this->{'KEY_GROUPS'}->get_keyspecs($record);
88 1 my $using = $this->{'USING'};
89 1 my $bar_graph = $this->{'BAR_GRAPH'};
90 1 my $lines = $this->{'LINES'};
91 1 my $title = $this->{'TITLE'};
92  
93 1 ▶ if ( ! $bar_graph && !$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bar_graph or $lines) 0 1 1
Condition33%1/3
$bar_graph or $lines
l!l&&r!l&&!r
010
MC/DC0%0/2
$bar_graph or $lines
$bar_graph $lines
Truth table33%1/3
A or B
A= $bar_graphB= $lines
ABresult
1X1
011
000
94 0 ▶ die 'Must specify using if more than 2 fields' if ( scalar @$specs > 2 ) && (! scalar @$using > 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$specs > 2 and !scalar(@$using) > 0 0 0 0
Condition0%0/3
scalar @$specs > 2 and !scalar(@$using) > 0
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @$specs > 2 and !scalar(@$using) > 0
scalar @$specs > 2 !scalar(@$using) > 0
Truth table0%0/3
A and not(B)
A= scalar @$specs > 2B= !scalar(@$using) > 0
ABresult
0X0
100
111
95 }
96  
97 1 ▶ if ( ! $title ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($title) 1 0 1
98 1 $title = join(', ', @$specs);
99 }
100  
101 1 ▶ if ( scalar @$using == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$using == 0) 1 0 1
102 1 ▶ if ( $bar_graph || $lines ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($bar_graph or $lines) { } 1 0 1
elsif (scalar @$specs == 1) { } 0 0 0
elsif (scalar @$specs == 2) { } 0 0 0
Condition33%1/3
$bar_graph or $lines
l!l&&r!l&&!r
010
MC/DC0%0/2
$bar_graph or $lines
$bar_graph $lines
Truth table33%1/3
A or B
A= $bar_graphB= $lines
ABresult
1X1
011
000
103 1 my $using_spec = "1 title \"$specs->[0]\"";
104  
105 1 foreach my $idx (2..@$specs) {
106 0 my $title = $specs->[$idx-1];
107 0 $using_spec .= ", '' using $idx title \"$title\"";
108 }
109  
110 1 push @$using, $using_spec;
111 }
112 elsif ( scalar @$specs == 1 ) {
113 0 push @$using, "1";
114 }
115 elsif ( scalar @$specs == 2 ) {
116 0 push @$using, "1:2";
117 }
118 }
119  
120 1 $this->{'FIELDS'} = $specs;
121 1 $this->{'TITLE'} = $title;
122 }
123  
124 # hook for additional args
125 ▶sub site_args {
Subroutine site_args: called CC=1 SCAR=0.0
Pod undocumented
126 }
127  
128 sub accept_record {
129 9 ▶ my ($this, $record) = @_;
Subroutine accept_record: called CC=5 SCAR=16.4
Pod undocumented
130  
131 9 ▶ if ( $this->{'FIRST_RECORD'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"FIRST_RECORD"}) 1 8 9
132 1 $this->{'FIRST_RECORD'} = 0;
133 1 $this->init_fields($record);
134 }
135  
136 9 my $line = '';
137 9 foreach my $key (@{$this->{'FIELDS'}}) {
138 9 my $value = ${$record->guess_key_from_spec($key)};
139 9 ▶ $value = 0 if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 9 9
140 9 $line .= "$value ";
141 }
142  
143 9 chop $line;
144 9 ▶ if ( $this->{'DUMP_TO_SCREEN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'DUMP_TO_SCREEN'}) { } 9 0 9
145 9 $this->push_line($line);
146 }
147 else {
148 0 my $tempfh = $this->{'TEMPFH'};
149 0 print $tempfh $line . "\n";
150 }
151  
152 9 return 1;
153 }
154  
155 sub stream_done {
156 1 ▶ my ($this) = @_;
Subroutine stream_done: called CC=11 SCAR=29.1
Pod undocumented
157  
158 1 close $this->{'TEMPFH'};
159  
160 1 my $plot_script = '';
161 1 $plot_script .= "set terminal png\n";
162 1 $plot_script .= "set output '" . $this->{'PNG_FILE'} . "'\n";
163 1 $plot_script .= "set title '" . $this->{'TITLE'} . "'\n";
164  
165 1 ▶ if ( $this->{'BAR_GRAPH'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'BAR_GRAPH'}) { } 0 1 1
elsif ($this->{'LINES'}) { } 1 0 1
166 0 $plot_script .= <<CMDS;
167 set style data histogram
168 set style histogram cluster gap 1
169 set style fill solid border -1
170 CMDS
171 }
172 elsif ( $this->{'LINES'} ) {
173 1 $plot_script .= "set style data linespoints\n";
174 }
175  
176 1 foreach my $command (@{$this->{'PRECOMMANDS'}}) {
177 0 $plot_script .= $command . "\n";
178 }
179  
180 1 my $plot_cmd = 'plot ';
181  
182 1 my $index = 0;
183 1 my $default_label = join(', ', @{$this->{'FIELDS'}});
184  
185 1 foreach my $use_spec (@{$this->{'USING'}}) {
186 1 ▶ if ( $this->{'DUMP_TO_SCREEN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'DUMP_TO_SCREEN'}) { } 1 0 1
187 1 $plot_cmd .= "'screen' using $use_spec ";
188 }
189 else {
190 0 $plot_cmd .= "'" . $this->{'TEMPFILE'} . "' using $use_spec ";
191 }
192  
193 1 ▶ if ( not ($use_spec =~ m/title/) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_spec =~ /title/) 0 1 1
194 0 my $label = $default_label;
195  
196 0 ▶ if ( $this->{'LABELS'}->[$index] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"LABELS"}[$index]) 0 0 0
197 0 $label = $this->{'LABELS'}->[$index];
198 }
199  
200 0 $plot_cmd .= "title '$label'";
201 }
202  
203 1 $plot_cmd .= ', ';
204 1 $index++;
205 }
206  
207 1 chop $plot_cmd;
208 1 chop $plot_cmd;
209  
210 1 ▶ if ( @{$this->{'PLOTS'}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$this->{"PLOTS"};}) 0 1 1
211 0 $plot_cmd .= ', ' . join(', ', @{$this->{'PLOTS'}});
212 }
213  
214 1 $plot_script .= $plot_cmd;
215  
216 1 ▶ if ( $this->{'DUMP_TO_SCREEN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'DUMP_TO_SCREEN'}) { } 1 0 1
217 1 $this->push_line($plot_script);
218 }
219 else {
220 0 open(my $plot, '|-', $this->{'GNUPLOT_COMMAND'});
221 0 print $plot $plot_script;
222 0 close $plot;
223 }
224  
225 1 ▶ if ( $? ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 1 1
226 0 warn "Gnuplot failed, bailing!\n";
227 0 $this->_set_exit_value($?);
228 0 return;
229 }
230  
231 1 $this->push_line("Wrote graph file: " . $this->{'PNG_FILE'});
232 }
233  
234 sub DESTROY {
235 2 ▶ my ($this) = @_;
Subroutine DESTROY: called CC=3 SCAR=11.0
236  
237 2 ▶ if ( $this->{'TEMPFH'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"TEMPFH"}) 1 1 2
238 1 close $this->{'TEMPFH'};
239 }
240  
241 2 ▶ if ( $this->{'TEMPFILE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"TEMPFILE"}) 1 1 2
242 1 unlink $this->{'TEMPFILE'};
243 }
244 }
245  
246 sub add_help_types {
247 2 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
248 2 $this->use_help_type('keyspecs');
249 2 $this->use_help_type('keygroups');
250 2 $this->use_help_type('keys');
251 }
252  
253 sub usage {
254 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
255  
256 1 my $options = [
257 ['key|-k <keys>', 'May be specified multiple times, may be comma separated. These are the keys to graph. If you have more than 2 keys, you must specify a --using statement or use --bargraph or --lines May be a keyspec or keygroup, see \'--help-keys\' for more information'],
258 ['using <using spec>', 'A \'using\' string passed directly to gnuplot, you can use keys specified with --key in the order specified. For instance --key count,date,avg with --using \'3:2\' would plot avg vs. date. May be specified multiple times'],
259 ['plot <plot spec>', 'May be specified multiple times, may be comma separated. A directive passed directly to plot, e.g. --plot \'5 title "threshold"\''],
260 ['precommand <gnuplot spec>', 'May be specified multiple times, may be comma separated. A command executed by gnuplot before executing plot, e.g. --precommand \'set xlabel "foo"\''],
261 ['title <title>', 'Specify a title for the entire graph'],
262 ['label <label>', 'Labels each --using line with the indicated label'],
263 ['file <filename>', 'Name of output png file. Will append .png if not present Defaults to tognuplot.png'],
264 ['lines', 'Draw lines between points, may specify more than 2 key, each field is a line'],
265 ['bargraph', 'Draw a bar graph, each field is a bar, may specify than 2 key, each field is a bar'],
266 ['gnuplot-command', 'Location of gnuplot binary if not on path'],
267 ['dump-to-screen', 'Instead of making a graph, dump the generated gnuplot script to STDOUT'],
268 ];
269  
270 1 my $args_string = $this->options_string($options);
271  
272 1 return <<USAGE;
273 Usage: recs-tognuplot <args> [<files>]
274 __FORMAT_TEXT__
275 Create a graph of points from a record stream using GNU Plot. Defaults to
276 creatinga scatterplot of points, can also create a bar or line graph
277  
278 For the --using and --plot arguments, you may want to reference a GNU Plot
279 tutorial, though it can get quite complex, here is one example:
280  
281 http://www.gnuplot.info/docs/node100.html
282 __FORMAT_TEXT__
283  
284 Arguments:
285 $args_string
286  
287 Graph the count field
288 recs-tognuplot --field count
289 Graph count vs. date with a threshold line
290 recs-tognuplot --field count,date --plot "5 title 'threshold'"
291 Graph a complicated expression, with a label
292 recs-tognuplot --field count,date,adjust --using '(\$1-\$3):2' --label "counts"
293 Graph count vs. date, with a title
294 recs-tognuplot --field count,date --title 'counts over time'
295 Graph count1, count2, count3 as 3 different bars in a bar graph
296 recs-tognuplot --field count1,count2,count3
297 USAGE
298 }
299  
300 2 1;
« blib/lib/App/RecordStream/Test/OperationHelper.pm ↑ Summary blib/lib/App/RecordStream/Site.pm »