Apply accumulated changes from tsplotv to tsplotvsql

This commit is contained in:
Peter J. Holzer 2019-12-17 11:15:39 +01:00 committed by Peter J. Holzer
parent 1f4b6f1a91
commit 4a529d40d3
1 changed files with 40 additions and 10 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/perl -w #!/usr/bin/perl
=head1 NAME =head1 NAME
@ -14,6 +14,10 @@ tsplotv
[--output-format format ] [--output-format format ]
[--stacked] [--stacked]
[--style style] [--style style]
[--time_t]
[--colors rgb-list]
[--configfile yaml]
[--yrange min:max]
[query ...] [query ...]
=head1 DESCRIPTION =head1 DESCRIPTION
@ -37,11 +41,15 @@ other.
=cut =cut
use strict; use v5.24;
use TimeSeries; use warnings;
use Getopt::Long; use Getopt::Long;
use Pod::Usage; use Pod::Usage;
use YAML qw(LoadFile);
use DBIx::SimpleConnect; use DBIx::SimpleConnect;
use TimeSeries;
my $help; my $help;
my $legend_position = 'top right'; my $legend_position = 'top right';
@ -53,6 +61,8 @@ my $finalresolution;
my $time_t = 0; my $time_t = 0;
my $colors; my $colors;
my $dbname; my $dbname;
my $configfile;
my $yrange;
GetOptions('help|?' => \$help, GetOptions('help|?' => \$help,
'legend_position|legend-position=s' => \$legend_position, 'legend_position|legend-position=s' => \$legend_position,
@ -64,36 +74,56 @@ GetOptions('help|?' => \$help,
'time_t' => \$time_t, 'time_t' => \$time_t,
'colors=s' => \$colors, 'colors=s' => \$colors,
'dbname=s' => \$dbname, 'dbname=s' => \$dbname,
'configfile=s' => \$configfile,
'yrange=s' => \$yrange,
) or pod2usage(2); ) or pod2usage(2);
pod2usage(1) if $help; pod2usage(1) if $help;
my $config = LoadFile($configfile) if $configfile;
binmode STDOUT, ':raw'; binmode STDOUT, ':raw';
my %series; my $ns = 0;
my $ns;
my %data; my %data;
my $ts = TimeSeries->new(output_format => $output_format); $config->{timeseries} //= {};
for (keys $config->{timeseries}->%*) {
$ns = $config->{timeseries}{$_}{order} if ($config->{timeseries}{$_}{order} // 0) > $ns;
}
my $dbh = DBIx::SimpleConnect->connect($dbname); my $dbh = DBIx::SimpleConnect->connect($dbname);
for my $q (@ARGV) { for my $q (@ARGV) {
my $qdata = $dbh->selectall_arrayref($q); my $qdata = $dbh->selectall_arrayref($q);
for my $r (@$qdata) { for my $r (@$qdata) {
my ($timestamp, $series, $value) = @$r; my ($timestamp, $series, $value) = @$r;
$series{$series} = ++$ns unless ($series{$series}); $config->{timeseries}{$series}{order} = ++$ns unless ($config->{timeseries}{$series}{order});
$data{$timestamp}{$series} = $value; $data{$timestamp}{$series} = $value;
} }
} }
my @series = sort { $series{$a} <=> $series{$b} } keys %series;
my @series = sort { $config->{timeseries}{$a}{order} <=> $config->{timeseries}{$b}{order} }
keys $config->{timeseries}->%*;
if ($colors) {
my @colors = split(/,/, $colors);
while (my ($i, $c) = each(@colors)) {
$config->{timeseries}{$series[$i]}{color} = $c;
}
}
my $ts = TimeSeries->new(output_format => $output_format);
$ts->legend(@series); $ts->legend(@series);
$ts->legend_position($legend_position); $ts->legend_position($legend_position);
$ts->stacked($stacked); $ts->stacked($stacked);
$ts->style($style); $ts->style($style);
$ts->log_y($log_y); $ts->log_y($log_y);
$ts->finalresolution($finalresolution) if $finalresolution; $ts->finalresolution($finalresolution) if $finalresolution;
if ($colors) { $ts->colors(map $config->{timeseries}{$_}{color}, @series);
$ts->colors(split(/,/, $colors));
if ($yrange) {
$yrange =~s /^\[(.*)\]$/$1/; # remove optional brackets
my ($min, $max) = $yrange =~ /^(\*|[-+0-9E.]+):(\*|[-+0-9E.]+)$/;
$ts->yrange($min, $max);
} }
for my $timestamp (sort keys %data) { for my $timestamp (sort keys %data) {