parse .log files delimited by space
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 27 Jun 2023 09:29:28 +0000 (11:29 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 27 Jun 2023 09:29:28 +0000 (11:29 +0200)
lib/MojoFacets/Import/Log.pm [new file with mode: 0644]
t/MojoFacets-Import-Log.t [new file with mode: 0755]

diff --git a/lib/MojoFacets/Import/Log.pm b/lib/MojoFacets/Import/Log.pm
new file mode 100644 (file)
index 0000000..49be8fd
--- /dev/null
@@ -0,0 +1,47 @@
+package MojoFacets::Import::Log;
+
+use warnings;
+use strict;
+
+use base 'Mojo::Base';
+
+use Data::Dump qw(dump);
+
+__PACKAGE__->attr('full_path');
+
+sub ext { '.log' }
+
+sub data {
+       my $self = shift;
+
+       my $path = $self->full_path;
+
+       my $data = { items => [] };
+       my $need_header = 1;
+
+       open(my $fh, $path) || die "$path: $!";
+       while(<$fh>) {
+               chomp;
+               warn "## $_\n";
+               my @header;
+
+               chomp;
+               my @v = split(/\s/, $_);
+
+               my $item;
+               foreach my $i ( 0 .. $#v ) {
+                       $item->{ $header[$i] || "f_$i" } = [ $v[$i] ];
+               }
+               push @{ $data->{items} }, $item;
+
+               if ( $need_header ) {
+                       $data->{header} = [ @header ];
+                       $need_header = 0;
+               }
+       }
+
+       return $data;
+
+}
+
+1
diff --git a/t/MojoFacets-Import-Log.t b/t/MojoFacets-Import-Log.t
new file mode 100755 (executable)
index 0000000..4950336
--- /dev/null
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+use Data::Dump qw(dump);
+
+use lib 'lib';
+
+use_ok('MojoFacets::Import::Log');
+
+my $path = $ARGV[0] || (glob 'data/log/*.log')[0];
+diag "using $path";
+
+ok( my $o = MojoFacets::Import::Log->new( full_path => $path ), 'new' );
+
+ok( my $data = $o->data, 'data' );
+diag dump($data);