package Gruntmaster::Page::Log::Entry;

use 5.014000;
use strict;
use warnings;
use parent qw/Exporter/;
our @EXPORT_OK = qw/generate/;
our $VERSION = '0.001';

use constant TITLE => 'Job <tmpl_var id>';

use Fcntl qw/:flock/;
use HTML::Template::Compiled;
use IO::File;
use POSIX qw/strftime/;
use YAML::Any qw/LoadFile/;
use Gruntmaster::Page qw/header footer/;

my %templates = (
  en => <<'HTML',
<table border>
<thead>
<tr><th>Test number<th>Result<th>Time
<tbody>
<tmpl_loop tests><tr><td><tmpl_var id><td class="r<tmpl_var result>"><tmpl_var result_text><td><tmpl_var time>
</tmpl_loop>
</table>
HTML
);

$templates{$_}  = header($_, TITLE) . $templates{$_} for keys %templates;
$templates{$_} .= footer $_ for keys %templates;

sub generate{
  my ($path, $lang) = @_;
  $id = ($path =~ m,log/(.*)/index,)[0];
  $path =~ s,/index\.html,,;
  my $template = $templates{$lang};

  my $htc = HTML::Template::Compiled->new(scalarref => \$template);
  flock my $metafh = IO::File->new("<$path/meta.yml"), LOCK_SH;
  my $meta = LoadFile "$path/meta.yml";

  my @tests = map {
	$_->{time} = sprintf "%.4fs", $_->{time};
	$_
  } @{$meta->{results}};

  $htc->param(id => $id);
  $htc->param(tests => \@tests);
  $htc->output
}

1
