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

my %probes;
my $series;
my $out;
open (F,"country2p2id.txt") or die;
while (<F>) {
   chomp();
   my ($co,@rest) = split(/,/,$_);
   $out->{groups}{$co} = [ @rest ];
   for (@rest) {
      $probes{$_}=1;
   }
}
close F;

open (G,"../case-bgp92/case-bgp92-data-rel.txt") or die;
while (<G>) {
   chomp();
   my ($src,$dst,$ts,$min,$avg,$max,$loss,$dup,$relmin,$relavg,$relmax) = split(/\t/,$_);
   next if (! $probes{$src} );
   #next unless ($dst == 1001);
   push @{ $series->{$ts}{$src} }, $relavg;
}
foreach my $ts (sort keys %{$series} ) {
   foreach my $src (sort keys %{$series->{$ts} } ) {
      if (ref($series->{$ts}{$src}) eq 'ARRAY' && scalar @{$series->{$ts}{$src}} > 0) {
         my $avg = 0;
         foreach my $n (@{$series->{$ts}{$src}}) {
            $avg+=$n;
            if (! $out->{min} || $out->{min} > $n) { $out->{min} = $n };
            if (! $out->{max} || $out->{max} < $n) { $out->{max} = $n };
         }
         $avg /= @{$series->{$ts}{$src}};
         $out->{data}{$ts}{$src} = $avg;
      }
   }
}

print "var data = ";
print to_json($out,{ ascii => 1, pretty => 1,canonical => 1});
print "\n";
__END__
281   1006  1292599800  253.22   270.70   274.11   1.000 0.000 0.981 0.612 0.435
281   1006  1292600700  218.34   253.19   274.24   1.000 0.000 0.846 0.573 0.435
