#!/usr/bin/perl
use strict;
use warnings;

use POSIX qw(strftime);
use File::Slurp;
use DBD::Pg;
use Data::Dump qw(dump);
use autodie;

my $path = '/home/nodelogs/c405f97667784094bca5cfa52af0bcf1/';

# select _id,json->'received',time,to_timestamp((json->>'received')::int8/1000) - interval '1 hour' from nodelog ;

my $dbh = DBI->connect("dbi:Pg:dbname=eg5120", "dpavlin", "", { RaiseError => 1 });
warn "# truncate table nodelog";
$dbh->do( qq{ truncate table nodelog } ); # FIXME
my $sth = $dbh->prepare(qq{insert into nodelog (time,json) values (?,?)});

foreach my $filename (
		sort(glob("$path/node-red-out__2023-07-*")),
		"$path/node-red-out.log"
	) {
	warn "# $filename";
	open(my $log, '<', $filename);
	my $in_json = 0;
	my $json;
	my $year = $1 if $filename =~ m/__(20\d\d)-/;
	$year ||= (localtime)[5] + 1900;
	my $time;
	while(<$log>) {
		chomp;
		#warn "## $in_json -->$_<--\n";
		## XXX debug 1 in node-red with logging to system console
		if ( m/(\d+ \w+)\s+(\d\d:\d\d:\d\d) - \Q[info] [debug:debug 1]\E/ ) {
			$time = $1 . ' ' . $year . ' ' . $2;
			#warn "# time $time\n";
		} elsif ( $_ eq '{' ) {
			$json .= $_;
			$in_json = 1;
			#warn "+ in_json";
		} elsif ( $in_json ) {

			s/\s(\S+):\s/"$1":/g;	# fix names quoting
			s/'/"/g;		# replace ' with "
			$json .= $_;

			if ( $_ eq '}' ) {
				$in_json = 0;
				#warn "- in_json";
				warn ">>> $time [[[[ $json ]]]]";
				write_file '/dev/shm/json', $json;
				$sth->execute( $time, $json );
				$json = '';
			}
		}

	}
}

