field selection by regex anchor
[barcat.git] / graph
1 #!/usr/bin/env perl
2 use 5.018;
3 use warnings;
4 use utf8;
5 use List::Util qw( min max sum );
6 use open qw( :std :utf8 );
7 use experimental qw( lexical_subs );
8
9 our $VERSION = '1.02';
10
11 use Getopt::Long '2.33', qw( :config gnu_getopt );
12 sub podexit {
13         require Pod::Usage;
14         Pod::Usage::pod2usage(-exitval => 0, -perldocopt => '-oman', @_);
15 }
16 my %opt;
17 GetOptions(\%opt,
18         'color|c!',
19         'C' => sub { $opt{color} = 0 },
20         'field|f=s',
21         'interval|t:i',
22         'trim|length|l=s' => sub {
23                 my ($optname, $optval) = @_;
24                 $optval =~ s/%$// and $opt{trimpct}++;
25                 $optval =~ m/^-?[0-9]+$/ or die(
26                         "Value \"$optval\" invalid for option $optname",
27                         " (number or percentage expected)\n"
28                 );
29                 $opt{trim} = $optval;
30         },
31         'markers|m=s',
32         'unmodified|u!',
33         'width|w=i',
34         'usage|h' => sub { podexit() },
35         'help'    => sub { podexit(-verbose => 2) },
36 ) or exit 64;  # EX_USAGE
37
38 $opt{width} ||= $ENV{COLUMNS} || 80;
39 $opt{color} //= -t *STDOUT;  # enable on tty
40 $opt{trim}   *= $opt{width} / 100 if $opt{trimpct};
41
42 if (defined $opt{interval}) {
43         $opt{interval} ||= 1;
44         $SIG{ALRM} = sub {
45                 show_lines();
46                 alarm $opt{interval};
47         };
48         alarm $opt{interval};
49 }
50
51 $SIG{INT} = 'IGNORE';  # continue after assumed eof
52
53 my (@lines, @values);
54 my $anchor = !defined $opt{field} ? qr/\A/ :
55         $opt{field} =~ /^[0-9]+$/ ? qr/(?:\S*\h+){$opt{field}}\K/ :
56         $opt{field};
57 while (readline) {
58         s/\r?\n\z//;
59         s/^\h*// unless $opt{unmodified};
60         push @values, s/$anchor ( \h* -? [0-9]* \.? [0-9]+ |)/\n/x && $1;
61         if (defined $opt{trim}) {
62                 my $trimpos = abs $opt{trim};
63                 if ($trimpos <= 1) {
64                         $_ = substr $_, 0, 1;
65                 }
66                 elsif (length > $trimpos) {
67                         substr($_, $trimpos - 1) = '…';
68                 }
69         }
70         push @lines, $_;
71 }
72
73 $SIG{INT} = 'DEFAULT';
74
75 sub show_lines {
76
77 state $nr = 0;
78 @lines and @lines > $nr or return;
79
80 my @order  = sort { $b <=> $a } grep { length } @values;
81 my $maxval = $order[0];
82 my $minval = min $order[-1], 0;
83 my $lenval = max map { length } @order;
84 my $len    = defined $opt{trim} && $opt{trim} <= 0 ? -$opt{trim} + 1 :
85         max map { length $values[$_] && length $lines[$_] } 0 .. $#lines;  # left padding
86 my $size   = ($maxval - $minval) &&
87         ($opt{width} - $lenval - $len) / ($maxval - $minval);  # bar multiplication
88
89 my @barmark;
90 if ($opt{markers} // 1 and $size > 0) {
91         my sub orderpos { (($order[$_[0]] + $order[$_[0] + .5]) / 2 - $minval) * $size }
92         $barmark[ (sum(@order) / @order - $minval) * $size ] = '=';  # average
93         $barmark[ orderpos($#order * .31731) ] = '>';
94         $barmark[ orderpos($#order * .68269) ] = '<';
95         $barmark[ orderpos($#order / 2) ] = '+';  # mean
96         $barmark[ -$minval * $size ] = '|' if $minval < 0;  # zero
97         defined and $opt{color} and $_ = "\e[36m$_\e[0m" for @barmark;
98
99         state $lastmax = $maxval;
100         if ($maxval > $lastmax) {
101                 print ' ' x ($lenval + $len);
102                 printf "\e[90m" if $opt{color};
103                 printf '%-*s',
104                         ($lastmax - $minval) * $size + .5,
105                         '-' x (($values[$nr - 1] - $minval) * $size);
106                 print "\e[92m" if $opt{color};
107                 say '+' x (($maxval - $lastmax - $minval) * $size + .5);
108                 print "\e[0m" if $opt{color};
109                 $lastmax = $maxval;
110         }
111 }
112
113 while ($nr <= $#lines) {
114         my $val = $values[$nr];
115         if (length $val) {
116                 my $color = !$opt{color} ? 0 :
117                         $val == $order[0] ? 32 : # max
118                         $val == $order[-1] ? 31 : # min
119                         90;
120                 $val = sprintf "%*s", $lenval, $val;
121                 $val = "\e[${color}m$val\e[0m" if $color;
122         }
123         my $line = $lines[$nr] =~ s/\n/$val/r;
124         printf '%-*s', $len + length($val), $line;
125         print $barmark[$_] // '-' for 1 .. $size && (($values[$nr] || 0) - $minval) * $size;
126         say '';
127         $nr++;
128 }
129
130 }
131 show_lines();
132
133 __END__
134
135 =head1 NAME
136
137 graph - append bar chart to input numbers
138
139 =head1 SYNOPSIS
140
141 B<graph> [<options>] [<input>]
142
143 =head1 DESCRIPTION
144
145 Visualizes relative sizes of values read from input (file(s) or STDIN).
146 Contents are concatenated similar to I<cat>,
147 but numbers are reformatted and a bar graph is appended to each line.
148
149 =head1 OPTIONS
150
151 =over
152
153 =item -c, --[no-]color
154
155 Force colored output of values and bar markers.
156 Defaults on if output is a tty,
157 disabled otherwise such as when piped or redirected.
158
159 =item -f, --field=(<number>|<regexp>)
160
161 Compare values after a given number of whitespace separators,
162 or matching a regular expression.
163
164 Unspecified or I<-f0> means values are at the start of each line.
165 With I<-f1> the second word is taken instead.
166 A string can indicate the starting position of a value
167 (such as I<-f:> if preceded by colons),
168 or capture the numbers itself,
169 for example I<-f'(\d+)'> for the first digits anywhere.
170
171 =item -t, --interval[=<seconds>]
172
173 Interval time to output partial progress.
174
175 =item -l, --length=[-]<size>[%]
176
177 Trim line contents (between number and bars)
178 to a maximum number of characters.
179 The exceeding part is replaced by an abbreviation sign,
180 unless C<--length=0>.
181
182 Prepend a dash (i.e. make negative) to enforce padding
183 regardless of encountered contents.
184
185 =item -m, --markers=
186
187 Statistical positions to indicate on bars.
188 Cannot be customized yet,
189 only disabled by providing an empty argument.
190
191 Any value enables all marker characters:
192
193 =over 2
194
195 =item B<=>
196
197 Average:
198 the sum of all values divided by the number of counted lines.
199
200 =item B<+>
201
202 Mean, median:
203 the middle value or average between middle values.
204
205 =item B<<>
206
207 Standard deviation left of the mean.
208 Only 16% of all values are lower.
209
210 =item B<< > >>
211
212 Standard deviation right of the mean.
213 The part between B<< <--> >> encompass all I<normal> results,
214 or 68% of all entries.
215
216 =back
217
218 =item -u, --unmodified
219
220 Do not strip leading whitespace.
221 Keep original value alignment, which may be significant in some programs.
222
223 =item -w, --width=<columns>
224
225 Override the maximum number of columns to use.
226 Appended graphics will extend to fill up the entire screen.
227
228 =back
229
230 =head1 EXAMPLES
231
232 Commonly used after counting, such as users on the current server:
233
234     users | sed 's/ /\n/g' | sort | uniq -c | graph
235
236 Letter frequencies in text files:
237
238     cat /usr/share/games/fortunes/*.u8 |
239     perl -CO -nE 'say for grep length, split /\PL*/, uc' |
240     sort | uniq -c | graph
241
242 Memory usage of user processes:
243
244     ps xo %mem,pid,cmd | graph -l40
245
246 Sizes (in megabytes) of all root files and directories:
247
248     du -d0 -m * | graph
249
250 Number of HTTP requests per day:
251
252     cat log/access.log | cut -d\  -f4 | cut -d: -f1 | uniq -c | graph
253
254 Any kind of database query with leading counts:
255
256     echo 'SELECT count(*),schemaname FROM pg_tables GROUP BY 2' |
257     psql -t | graph -u
258
259 Exchange rate USD/EUR history from CSV download provided by ECB:
260
261     curl https://sdw.ecb.europa.eu/export.do \
262          -Gd 'node=SEARCHRESULTS&q=EXR.D.USD.EUR.SP00.A&exportType=csv' |
263     grep '^[12]' | graph -f',\K'
264
265 Total population history from the World Bank dataset (XML):
266
267     curl http://api.worldbank.org/v2/country/1W/indicator/SP.POP.TOTL |
268     xmllint --xpath '//*[local-name()="date" or local-name()="value"]' - |
269     sed -r 's,</wb:value>,\n,g; s,(<[^>]+>)+, ,g' | graph -f1
270
271 Movies per year from prepared JSON data:
272
273     curl https://github.com/prust/wikipedia-movie-data/raw/master/movies.json |
274     jq '.[].year' | uniq -c | graph
275
276 Pokémon height comparison:
277
278         curl https://github.com/Biuni/PokemonGO-Pokedex/raw/master/pokedex.json |
279         jq -r '.pokemon[] | [.height,.num,.name] | join(" ")' | graph
280
281 Git statistics, such commit count by year:
282
283     git log --pretty=%ci | cut -b-4 | uniq -c | graph
284
285 Or the most frequent authors:
286
287     git shortlog -sn | graph | head -3
288
289 Latency history:
290
291     ping google.com | graph -f'time=\K' -t
292
293 =head1 AUTHOR
294
295 Mischa POSLAWSKY <perl@shiar.org>
296
297 =head1 LICENSE
298
299 GPL3+.