Debian Codegraph @

src/battery-stats-graph-flow

raw
1 #!/bin/sh
2 #
3 # This file is part of the battery-stats package.
4 # Copyright (C) 2016 Petter Reinholdtsen <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18
19 input=/var/log/battery-stats.csv
20 filename="${1}"
21 flowfilename=$(mktemp)
22
23 perl -MText::CSV <<'EOF' > "$flowfilename"
24 use strict;
25 use warnings;
26
27 my $csv = Text::CSV->new();
28 my $fh;
29 open $fh, "<:encoding(utf8)", '/var/log/battery-stats.csv'
30 or die "open failed: $!";
31
32 my $last2 = undef;
33 my $lasttime2 = undef;
34 my $last = undef;
35 my $lasttime = undef;
36 my @fieldsrow = $csv->getline($fh);
37 my $levelrow = 8; # energy_now
38 while (my $row = $csv->getline($fh) ) {
39 if (defined $last2) {
40 my $diff = $row->[$levelrow] - $last2;
41 my $timediff = $row->[0] - $lasttime2;
42 if (0 < $timediff) {
43 printf "%s,%.2f\n", $row->[0], $diff / $timediff;
44 }
45 }
46 $last2 = $last;
47 $lasttime2 = $lasttime;
48 $last = $row->[$levelrow];
49 $lasttime = $row->[0];
50 }
51 $csv->eof or $csv->error_diag();
52 close($fh);
53
54 EOF
55
56 # manufacturer,model_name,technology
57 type=$(head -2 "$input" | tail -1 | cut -d, -f2-4 | tr , " ")
58
59 (
60 echo set xdata time
61 echo set timefmt \"%s\"
62 echo set format x \"%Y\"
63 echo set datafile separator \",\"
64 echo set title \'Battery statistics $type\'
65 echo set ylabel \'Flow per second\'
66 echo set xlabel \'Year\'
67 echo set grid
68 if [ "$filename" ]; then
69 echo set term png
70 echo set output \"$filename\"
71 fi
72 echo plot "\"$flowfilename\" using 1:2 smooth unique axis x1y1 title \"Energy flow\" with lines"
73
74 ) | gnuplot -p
75
76 if [ "$filename" ] ; then
77 echo "PNG graph $filename created."
78 fi
79
80 rm $flowfilename