#!/bin/bash

export GNUPLOT_DEFAULT_GDFONT=inconsolata

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

data_run=$(mktemp)
data_rec=$(mktemp)
data_sen=$(mktemp)

echo $0 "$data_run" "$data_rec" "$data_sen"

T=$(mktemp)
sqlite3 -separator $'\t' > "$T" <<EOF
ATTACH 'file://${job}?immutable=1' as Job;
ATTACH 'file://${confuga}?immutable=1' as Confuga;

SELECT COUNT(*) FROM Confuga.StorageNode;
EOF
N=$(cat "$T")
rm "$T"

{
	sqlite3 -separator $'\t' | awk '$1 != prev {printf "\n"; prev = $1}; {print}' > "$data_run"
} <<EOF
ATTACH 'file://${job}?immutable=1' as Job;
ATTACH 'file://${confuga}?immutable=1' as Confuga;

CREATE TEMPORARY TABLE IDS AS
	SELECT DISTINCT tag FROM ConfugaJob ORDER BY tag;

SELECT ConfugaJob.sid, ConfugaJob.time_committed, ConfugaJob.time_reaped, ConfugaJob.tag, IDS.rowid
	FROM
		Job
		JOIN ConfugaJob ON Job.id = ConfugaJob.id
		JOIN IDS ON ConfugaJob.tag = IDS.tag;
EOF

{
	sqlite3 -separator $'\t' | awk '$1 != prev {printf "\n"; prev = $1}; {print}' > "$data_rec"
} <<EOF
ATTACH 'file://${job}?immutable=1' as Job;
ATTACH 'file://${confuga}?immutable=1' as Confuga;

CREATE TEMPORARY TABLE IDS AS
	SELECT DISTINCT tag FROM ConfugaJob ORDER BY tag;

SELECT TransferJob.tsid, TransferJob.time_new, TransferJob.time_complete, Confuga.TransferJob.tag, IDS.rowid
	FROM
		Confuga.TransferJob
		JOIN IDS ON Confuga.TransferJob.tag = IDS.tag;
EOF

{
	sqlite3 -separator $'\t' | awk '$1 != prev {printf "\n"; prev = $1}; {print}' > "$data_sen"
} <<EOF
ATTACH 'file://${job}?immutable=1' as Job;
ATTACH 'file://${confuga}?immutable=1' as Confuga;

CREATE TEMPORARY TABLE IDS AS
	SELECT DISTINCT tag FROM ConfugaJob ORDER BY tag;

SELECT TransferJob.fsid, TransferJob.time_new, TransferJob.time_complete, Confuga.TransferJob.tag, IDS.rowid
	FROM
		Confuga.TransferJob
		JOIN IDS ON Confuga.TransferJob.tag = IDS.tag;
EOF

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

stats "$data_run" using 2 prefix "run" nooutput
stats "$data_sen" using 2 prefix "sen" nooutput
stats "$data_rec" using 2 prefix "rec" nooutput

set palette defined (1 "gray30", 2 "gray70", 3 "gray50")
set style line 1 lt 1 lw 16 lc palette z
set style line 2 lt 1 lw 04 lc rgb "black"
set style line 3 lt 1 lw 01 lc rgb "white"
set style arrow 1 nohead ls 1
set style arrow 2 nohead ls 2

set auto fix
set format x "%H:%M"
set notitle
set offset graph 0.05, graph 0.05, 1, 1
set timefmt "%s"
set xdata time
set xlabel "Time (HH:MM)"
set ylabel "Storage Node"
set yrange [1:]
set ytics 1,1 offset 0,0.5 rangelimited

unset colorbox

min(a,b) = a < b ? a : b
max(a,b) = a > b ? a : b

tmin = min(run_min, min(sen_min, rec_min))

# Use 2D vectors to indicate activity in time.
# <tstart, sid, max((tfinish-tstart), 30), 0, workflow_tag>
#  - The 3rd column is the duration, which is artifially set to at least 30 seconds in the graph for visibility.
#  - The 5th column is used for styling the vector according to the workflow source of the activity.
plot  "$data_run" u (\$2-tmin):(\$1+.30):(max(\$3-\$2, 30)):(0):5 with vectors arrowstyle 1 notitle \\
	 ,"$data_sen" u (\$2-tmin):(\$1+.75):(max(\$3-\$2, 30)):(0):5 with vectors arrowstyle 1 notitle \\
	 ,"$data_rec" u (\$2-tmin):(\$1+.75):(max(\$3-\$2, 30)):(0):5 with vectors arrowstyle 1 notitle \\
	 ,"$data_run" u (\$3-tmin):(\$1+.05):(5):(0):(0)              with vectors arrowstyle 2 notitle \\
	 ,for [i=1:${N}] i notitle ls 3 # Force gnuplot to show 1 & N xtic marks
EOF

# vim: set noexpandtab tabstop=4:
