#!/bin/bash

export GNUPLOT_DEFAULT_GDFONT=inconsolata

job=$(realpath "$1")
confuga=$(realpath "$2")

data=$(mktemp)

echo $0
sqlite3 -separator $'\t' > "$data"  <<EOF
ATTACH 'file://${job}?immutable=1' as Job;
ATTACH 'file://${confuga}?immutable=1' as Confuga;

SELECT UPPER(HEX(TransferJob.fid)), TransferJob.time_new, TransferJob.time_complete, File.size
	FROM Confuga.TransferJob JOIN Confuga.File ON TransferJob.fid = File.id
	WHERE state = 'COMPLETED'
	ORDER BY TransferJob.time_complete;
EOF
cat "$data"

gnuplot <<EOF
set terminal postscript eps mono
set output 'cumulative.eps'

stats "$data" using 2 prefix "tj" nooutput

set xdata time
set timefmt "%s"
set format x "%H:%M"
set xlabel "Time (hour:minute)"
set xrange ["0":]
set xtics rotate by -45 offset -.8,0

set format y "%0.1e"
set ylabel "Transferred (MB)"
set yrange [0:]

plot "${data}" using (\$3-tj_min):(\$4/(2**20)) title "Cumulative Transfer" smooth cumulative
EOF

# vim: set noexpandtab tabstop=4:
