5 use POSIX qw(strftime);
8 use Data::Dump qw(dump);
11 my $path = '/home/nodelogs/c405f97667784094bca5cfa52af0bcf1/';
13 my $last_files = $ENV{LAST} || 3; # process last 3 logs by mtime
15 my $debug = $ENV{DEBUG} || 0;
17 # select _id,json->'received',time,to_timestamp((json->>'received')::int8/1000) - interval '1 hour' from nodelog ;
19 my $dbh = DBI->connect("dbi:Pg:dbname=eg5120", "dpavlin", "", { RaiseError => 1 });
20 warn "# truncate table nodelog" if $debug;
21 $dbh->do( qq{ truncate table nodelog } ); # FIXME
22 my $sth = $dbh->prepare(qq{insert into nodelog (time,json) values (?,?)});
25 foreach my $filename (
26 sort { -M $a <=> -M $b } (glob("$path/node-red-out*.log")),
28 last if $last_files-- == 0;
29 warn "# $filename" if $debug;
30 open(my $log, '<', $filename);
33 my $year = $1 if $filename =~ m/__(20\d\d)-/;
34 $year ||= (localtime)[5] + 1900;
38 #warn "## $in_json -->$_<--\n";
39 ## XXX debug 1 in node-red with logging to system console
40 if ( m/(\d+ \w+)\s+(\d\d:\d\d:\d\d) - \Q[info] [debug:debug 1]\E/ ) {
41 $time = $1 . ' ' . $year . ' ' . $2;
42 #warn "# time $time\n";
43 } elsif ( $_ eq '{' ) {
47 } elsif ( $in_json ) {
49 s/\s(\S+):\s/"$1":/g; # fix names quoting
50 s/'/"/g; # replace ' with "
56 warn ">>> $time [[[[ $json ]]]]" if $debug;
57 write_file '/dev/shm/json', $json;
58 $sth->execute( $time, $json );
66 # insert missing sensor readings
69 create temporary table nl_received as select _id,json->'received' as received from nodelog where json->'received' is not null;
71 create temporary table eg_received as select _id,json->'received' as received from eg5120 where json->'received' is not null;
73 create temporary table nl_new as select * from nl_received where received not in (select received from eg_received) ;
75 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);
77 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);