#!/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 id, time_commit AS t, 1
		FROM Confuga.TransferJob
		WHERE state = 'COMPLETED'
UNION ALL
	SELECT id, time_complete+1 /* prevent time_commit == time_complete */ AS t, -1
		FROM Confuga.TransferJob
		WHERE state = 'COMPLETED'
ORDER BY t;
EOF
cat "$data"

gnuplot <<EOF
set terminal postscript eps mono
set output 'concurrent-transfers.eps'

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

set rmargin 4

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

set ylabel "Concurrent Transfer Jobs"
set yrange [0:]
set mytics

plot "${data}" using (\$2-tj_min):3 title "Concurrent Transfer Jobs" smooth cumulative
EOF

# vim: set noexpandtab tabstop=4:
