#!/bin/perl -w
use DBI;
use Data::Dumper;
use warnings;
use strict;

# In order to run this script you need to install:
#   - cpan install DBI
#   - cpan install DBD::SQLite

my $dbfile = shift || die("No File");
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");

my $sth = $dbh->prepare("select t.ID, t.address, p.PhaseName, p.PhaseBegin, p.PhaseEnd
                      from Transactions t, Phases p
                      where t.ID = p.Transact
                      and (PhaseName='REQ' or PhaseName='RESP');");

$sth->execute();

my @score;
my @timelog;
my $counter = 0;
my $start = 0;

print "address, delay\n";

while(my @row = $sth->fetchrow_array)
{
    my $id      = $row[0];
    my $phase   = $row[2];
    my $addr    = $row[1];

    if($phase eq "REQ")
    {
        $start = $row[4];
        $timelog[$counter] = $start;
    }
    elsif($phase eq "RESP")
    {
        my $end = $row[3];
        my $delay = $end - $start;

        #if($counter > 1)
        #{
            #print relative time:
            #print ($timelog[$counter] - $timelog[$counter-1]);
            #print ", ";
            #print address
            print $addr;
            print ", ";
            #print delay
            print $delay;
            print "\n";
        #}

        #$counter++;
    }
}



