parse node-red logs to backfill data
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 4 Jul 2023 07:22:07 +0000 (09:22 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 4 Jul 2023 07:22:07 +0000 (09:22 +0200)
parse-node-logs.pl [new file with mode: 0755]

diff --git a/parse-node-logs.pl b/parse-node-logs.pl
new file mode 100755 (executable)
index 0000000..1e1b6e9
--- /dev/null
@@ -0,0 +1,50 @@
+#!/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 $dbh = DBI->connect("dbi:Pg:dbname=eg5120", "dpavlin", "", { RaiseError => 1 });
+$dbh->do( qq{ truncate table nodelog } ); # FIXME
+my $sth = $dbh->prepare(qq{insert into nodelog (time,json) values (?,?)});
+
+foreach my $filename ( sort(glob('node-logs/node-red-out__2023-07-*')), 'node-logs/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";
+               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 = '';
+                       }
+               }
+
+       }
+}