aboutsummaryrefslogtreecommitdiff
path: root/plot-log-plotly.pl
blob: 10f32d5402dfacf58f1eaca2e9e61fe81a7e8c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env perl 
use strict;
use warnings;
use 5.026;
use Path::Tiny;
 
use Chart::Plotly;
use Chart::Plotly::Plot;
use Chart::Plotly::Trace::Scatter;
 
my @rows = map { path($_)->lines_utf8({chomp=>1}) } @ARGV;
 
my @column_names = qw(humidity temperature battery);
my %data_for_src;
for my $row (@rows) {
    my %data;my $src;
    ($data{dt},$src,@data{@column_names}) =
        $row =~ m{
                     \A \s*
                     ( \d{4}-\d{2}-\d{2} \s\d{2}:\d{2}:\d{2} )
                     \s+
                     (?: [0-9a-f:]+ ):([0-9a-f]+) # only take the last byte of the MAC, it's enough
                     \s+
                     ( [0-9.]+ )
                     \s+
                     ( [0-9.]+ )
                     \s+
                     ( [0-9.]+ )
                     \s\z
             }or next;
    for my $column ('dt',@column_names) {
        push $data_for_src{$src}->{$column}->@*, $data{$column}
    }
}
 
my %plots;
for my $column (@column_names) {
    my $plot = Chart::Plotly::Plot->new(
        layout => { title => $column },
    );
    for my $src (sort keys %data_for_src) {
        my $trace = Chart::Plotly::Trace::Scatter->new(
            => $data_for_src{$src}->{dt},
            => $data_for_src{$src}->{$column},
            name => $src,
        );
        $plot->add_trace($trace);
    }
    $plots{$column} = $plot;
}
 
Chart::Plotly::show_plot(@plots{@column_names});