5 use POSIX qw(strftime);
8 use Data::Dump qw(dump);
11 my $path = '/home/nodelogs/c405f97667784094bca5cfa52af0bcf1/';
13 # select _id,json->'received',time,to_timestamp((json->>'received')::int8/1000) - interval '1 hour' from nodelog ;
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 (?,?)});
20 my $last_files = $ENV{LAST} || 3; # process last 3 logs by mtime
22 foreach my $filename (
23 sort { -M $a <=> -M $b } (glob("$path/node-red-out*.log")),
25 last if $last_files-- == 0;
27 open(my $log, '<', $filename);
30 my $year = $1 if $filename =~ m/__(20\d\d)-/;
31 $year ||= (localtime)[5] + 1900;
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 '{' ) {
44 } elsif ( $in_json ) {
46 s/\s(\S+):\s/"$1":/g; # fix names quoting
47 s/'/"/g; # replace ' with "
53 warn ">>> $time [[[[ $json ]]]]";
54 write_file '/dev/shm/json', $json;
55 $sth->execute( $time, $json );
63 # insert missing sensor readings
66 create temporary table nl_received as select _id,json->'received' as received from nodelog where json->'received' is not null;
68 create temporary table eg_received as select _id,json->'received' as received from eg5120 where json->'received' is not null;
70 create temporary table nl_new as select * from nl_received where received not in (select received from eg_received) ;
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);
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);