#!/bin/bash

export GDFONTPATH=/usr/share/fonts/truetype
export GNUPLOT_DEFAULT_GDFONT=LiberationSans-Regular

/usr/libexec/chrony-graph/local-clock-graph
/usr/libexec/chrony-graph/offset-graph
/usr/libexec/chrony-graph/diff-freq-graph
/usr/libexec/chrony-graph/skew-graph

NINETYFIVE=`/usr/libexec/chrony-graph/percentile 3 95 tracking.log`
NINETYNINE=`/usr/libexec/chrony-graph/percentile 3 99 tracking.log`
FIVE=`/usr/libexec/chrony-graph/percentile 3 5 tracking.log`
ONE=`/usr/libexec/chrony-graph/percentile 3 1 tracking.log`
NF_M_F=`echo $NINETYFIVE - $FIVE | bc`
NN_M_O=`echo $NINETYNINE - $ONE | bc`
gnuplot <<EOF
set terminal png size 900,600
set xdata time
set timefmt "%s"
set output "percentiles.png"
set grid
set xlabel "Time"
set format x "%d-%H:%M"
set xtic rotate by -45 scale 0
set ytics format "%1.3f ppm" nomirror
set title "Local Clock range"
set key bottom right box
set label 1 gprintf("99%% = $NINETYNINE ppm",99) at graph 0.01,0.3 left front
set label 2 gprintf("95%% = $NINETYFIVE ppm",95) at graph 0.01,0.25 left front
set label 3 gprintf(" 5%% = $FIVE ppm",5) at graph 0.01,0.2 left front
set label 4 gprintf(" 1%% = $ONE ppm",1) at graph 0.01,0.15 left front
set label 5 gprintf("95%% - 5%% = $NF_M_F ppm",90) at graph 0.01,0.1 left front
set label 6 gprintf("99%% - 1%% = $NN_M_O ppm",98) at graph 0.01,0.05 left front
set lmargin 12
set rmargin 12
plot \
 "tracking.log" using 1:4 title "local clock error" with linespoints, \
 $NINETYNINE title "99th percentile", \
 $NINETYFIVE title "95th percentile", \
 $FIVE title "5th percentile", \
 $ONE title "1st percentile"
EOF

NINETYFIVE=`/usr/libexec/chrony-graph/percentile 5 95 tracking.log | /usr/libexec/chrony-graph/mul 1000000`
NINETYNINE=`/usr/libexec/chrony-graph/percentile 5 99 tracking.log | /usr/libexec/chrony-graph/mul 1000000`
FIVE=`/usr/libexec/chrony-graph/percentile 5 5 tracking.log | /usr/libexec/chrony-graph/mul 1000000`
ONE=`/usr/libexec/chrony-graph/percentile 5 1 tracking.log | /usr/libexec/chrony-graph/mul 1000000`
NF_M_F=`echo $NINETYFIVE - $FIVE | bc`
NN_M_O=`echo $NINETYNINE - $ONE | bc`
gnuplot <<EOF
set terminal png size 900,600
set xdata time
set timefmt "%s"
set output "percentiles-offset.png"
set grid
set xlabel "Time"
set format x "%d-%H:%M"
set xtic rotate by -45 scale 0
set ytics format "%1.2f us" nomirror
set title "Local Clock Offsets over time"
set key bottom right box
set label 1 gprintf("99%% = %1.2f us",$NINETYNINE) at graph 0.01,0.3 left front
set label 2 gprintf("95%% = %1.2f us",$NINETYFIVE) at graph 0.01,0.25 left front
set label 3 gprintf(" 5%% = %1.2f us",$FIVE) at graph 0.01,0.2 left front
set label 4 gprintf(" 1%% = %1.2f us",$ONE) at graph 0.01,0.15 left front
set label 5 gprintf("95%% - 5%% = %1.2f us", $NF_M_F) at graph 0.01,0.1 left front
set label 6 gprintf("99%% - 1%% = %1.2f us", $NN_M_O) at graph 0.01,0.05 left front
set lmargin 12
set rmargin 12
plot \
 "tracking.log" using 1:(\$6 * 1000000) title "local clock offset" with linespoints, \
 $NINETYNINE title "99th percentile", \
 $NINETYFIVE title "95th percentile", \
 $FIVE title "5th percentile", \
 $ONE title "1st percentile"
EOF

# for nanosecond level:
#/usr/libexec/chrony-graph/histogram 5 1000000000 <tracking.log >tracking.log.history
/usr/libexec/chrony-graph/histogram 5 1000000 <tracking.log >tracking.log.history
SEVENTYFIVE=`/usr/libexec/chrony-graph/percentile 5 75 tracking.log | /usr/libexec/chrony-graph/mul 1000000`
TWENTYFIVE=`/usr/libexec/chrony-graph/percentile 5 25 tracking.log | /usr/libexec/chrony-graph/mul 1000000`

gnuplot <<EOF
set terminal png size 900,600
set output "offset-histogram.png"
set grid
set xtic rotate by -45 scale 0
set title "Local Clock Offsets - Histogram"
set xtics format "%1.1f us" nomirror
set label 1 gprintf("99%% = %1.2f us",$NINETYNINE) at $NINETYNINE, graph 0.91 left front offset 1,-1
set style arrow 1 nohead 
set arrow from $NINETYNINE,0 to $NINETYNINE,graph 0.91 as 1
set label 2 gprintf(" 1%% = %1.2f us",$ONE) at $ONE, graph 0.91 right front offset -1,-1
set style arrow 2 nohead
set arrow from $ONE,0 to $ONE,graph 0.91 as 2
set label 3 gprintf("25%% = %1.2f us",$TWENTYFIVE) at $TWENTYFIVE, graph 0.7 right front offset -1,-1
set style arrow 3 nohead
set arrow from $TWENTYFIVE,0 to $TWENTYFIVE,graph 0.7 as 3
set label 4 gprintf("75%% = %1.2f us",$SEVENTYFIVE) at $SEVENTYFIVE, graph 0.7 left front offset 1,-1
set style arrow 4 nohead
set arrow from $SEVENTYFIVE,0 to $SEVENTYFIVE,graph 0.7 as 4
set xrange [-500:500]
set key off
set lmargin 12
set rmargin 12
plot \
 "tracking.log.history" using 1:2 title "histogram" with boxes
EOF

# optional GPS tracking
#gnuplot <<EOF
#set terminal png size 900,600
#set xdata time
#set timefmt "%s"
#set output "gps-lock.png"
#set grid
#set xlabel "Time"
#set format x "%d-%H:%M"
#set xtic rotate by -45 scale 0
#set ytics format "%1.0f seconds/hr with lock" nomirror
#set title "Seconds per hour spent in GPS lock"
#set key bottom right box
#plot \
# "gps-lock" using 1:2 title "second" with lines
#EOF
#
#/usr/libexec/chrony-graph/histogram-snr <snr-history >snr-history.history
#gnuplot <<EOF
#set terminal png size 900,600
#set output "gps-snr-histogram.png"
#set grid
#set xtic rotate by -45 scale 0
#set title "GPS SNR histogram"
#set xtics format "%1.1fdB snr" nomirror
#set key off
#plot \
# "snr-history.history" using 1:2 title "histogram" with boxes
#EOF
#
#/usr/libexec/chrony-graph/snr-hourly snr-history >snr-history.hourly
#gnuplot <<EOF
#set terminal png size 900,600
#set xdata time
#set timefmt "%s"
#set output "gps-snr-hourly.png"
#set grid
#set xlabel "Time"
#set format x "%d-%H:%M"
#set xtic rotate by -45 scale 0
#set ytics format "%1.0f %% of time" nomirror
#set title "Percent of time satellites spent at 0 snr"
#set key bottom right box
#plot \
# "snr-history.hourly" using 1:(\$2 / (\$2 + \$3) * 100) title "signal" with lines
#EOF

if [ -x custom-plot ]; then
  ./custom-plot
fi
