#!/usr/bin/env perl

use lib './lib';
use strict;

use Dist;
use Storable;
use Time::HiRes qw/time/;
use DateTime;

my $pass = $ARGV[0];
my $start_time = time();
my $start_dt = DateTime->now();
my $cache = retrieve 'everything.stor';

my $first_count =()= grep {
     $cache->{jobstatus}{$_}{status} ne 'success' &&
     $cache->{jobstatus}{$_}{tested} == $pass
   } keys $cache->{jobstatus}->%*;

my $dists =()= grep {
     $cache->{jobstatus}{$_}{status} ne 'success'
   } keys $cache->{jobstatus}->%*;

while (1) {
  sleep(2);
  my $cache = eval {retrieve 'everything.stor'};
  next unless $cache;
  my $count =()= grep {
     $cache->{jobstatus}{$_}{status} ne 'success' &&
     $cache->{jobstatus}{$_}{tested} == $pass
   } keys $cache->{jobstatus}->%*;

  my $rate = ($count-$first_count)/(time()-$start_time);
  my $tocomplete = ($rate == 0? sub{-1} : sub{($dists-$count)/$rate})->();
  my $final_dt = DateTime->now(time_zone => 'America/Los_Angeles')->add(seconds => $tocomplete);
  printf "%d/%d [%02.2f%%] %0.2f/min %s %0.2f\n", $count, $dists, 100*$count/$dists, $rate*60, $final_dt->iso8601, $tocomplete;
 
};
