#!/usr/local/bin/perl -w
use strict;
use POSIX;

$ARGV[0] =~ /(\d+)-(\d+)-(\d+)/;
my $daystart = POSIX::mktime(0, 0, 0, int($3), int($2) - 1, int($1) - 1900);
my $dayend = $daystart + 24 * 60 * 60;

my %hosts = ();
foreach my $host ("pat", "greendale", "jess") { #discovery
	$hosts{$host} = {};
}

open LOG, "<log" or die "can't open log";
while (<LOG>) {
	chomp;
	/^([^ ]+) (\d+) (\d+)$/ or next;
	my ($host, $sent, $received) = ($1, int $2, int $3);
	my $delay = $received - $sent;

	if (exists $hosts{$host} and $sent >= $daystart and $sent <= $dayend) {
		$hosts{$host}->{$sent} = $delay;
	}
}
close LOG;

foreach my $host (keys %hosts) {
	open OUT, ">data.$host" or die "can't open output file";
	foreach my $sent (sort keys %{$hosts{$host}}) {
		my $senth = (1.0 * ($sent - $daystart)) / (60 * 60);
		my $delayh = (1.0 * $hosts{$host}->{$sent}) / (60 * 60);
		printf OUT "%.4f %.4f\n", $senth, $delayh;
	}
	close OUT;
}

