insert missing sensor readings
[eg5120] / parse-node-logs.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use POSIX qw(strftime);
6 use File::Slurp;
7 use DBD::Pg;
8 use Data::Dump qw(dump);
9 use autodie;
10
11 my $path = '/home/nodelogs/c405f97667784094bca5cfa52af0bcf1/';
12
13 # select _id,json->'received',time,to_timestamp((json->>'received')::int8/1000) - interval '1 hour' from nodelog ;
14
15 my $dbh = DBI->connect("dbi:Pg:dbname=eg5120", "dpavlin", "", { RaiseError => 1 });
16 warn "# truncate table nodelog";
17 $dbh->do( qq{ truncate table nodelog } ); # FIXME
18 my $sth = $dbh->prepare(qq{insert into nodelog (time,json) values (?,?)});
19
20 my $last_files = $ENV{LAST} || 3; # process last 3 logs by mtime
21
22 foreach my $filename (
23                 sort { -M $a <=> -M $b } (glob("$path/node-red-out*.log")),
24         ) {
25         last if $last_files-- == 0;
26         warn "# $filename";
27         open(my $log, '<', $filename);
28         my $in_json = 0;
29         my $json;
30         my $year = $1 if $filename =~ m/__(20\d\d)-/;
31         $year ||= (localtime)[5] + 1900;
32         my $time;
33         while(<$log>) {
34                 chomp;
35                 #warn "## $in_json -->$_<--\n";
36                 ## XXX debug 1 in node-red with logging to system console
37                 if ( m/(\d+ \w+)\s+(\d\d:\d\d:\d\d) - \Q[info] [debug:debug 1]\E/ ) {
38                         $time = $1 . ' ' . $year . ' ' . $2;
39                         #warn "# time $time\n";
40                 } elsif ( $_ eq '{' ) {
41                         $json .= $_;
42                         $in_json = 1;
43                         #warn "+ in_json";
44                 } elsif ( $in_json ) {
45
46                         s/\s(\S+):\s/"$1":/g;   # fix names quoting
47                         s/'/"/g;                # replace ' with "
48                         $json .= $_;
49
50                         if ( $_ eq '}' ) {
51                                 $in_json = 0;
52                                 #warn "- in_json";
53                                 warn ">>> $time [[[[ $json ]]]]";
54                                 write_file '/dev/shm/json', $json;
55                                 $sth->execute( $time, $json );
56                                 $json = '';
57                         }
58                 }
59
60         }
61 }
62
63 # insert missing sensor readings
64 $dbh->do( qq{
65
66 create temporary table nl_received as select _id,json->'received' as received from nodelog where json->'received' is not null;
67
68 create temporary table eg_received as select _id,json->'received' as received from eg5120 where json->'received' is not null;
69
70 create temporary table nl_new as select * from nl_received where received not in (select received from eg_received) ;
71
72 select to_timestamp((json->>'received')::int8/1000), time,mac,addr,"nodeId",json,_id,sensor_type from nodelog where _id in (select _id from nl_new);
73
74 insert into eg5120 select to_timestamp((json->>'received')::int8/1000) as time,mac,addr,"nodeId",json,sensor_type from nodelog where _id in (select _id from nl_new);
75
76 } );