#!/bin/bash

LINSCALELIMIT=`/usr/libexec/chrony-graph/linscalelimit 1 5 tracking.log | /usr/libexec/chrony-graph/mul 1000000`

# echo $LINSCALELIMIT

YTICS=`/usr/libexec/chrony-graph/linlogytics $LINSCALELIMIT`

lc() {
	local img_format=$1
	local img_name=$2
	local img_format_options=$3
gnuplot <<EOF
set terminal ${img_format} ${img_format_options}
set xdata time
set timefmt "%s"
set output "${img_name}.${img_format}"
set grid
set xlabel "Time"
set format x "%d-%H:%M"
flogy(y) = ((log(abs(y))-log($LINSCALELIMIT))/log(10)+2)*$LINSCALELIMIT*0.5
glogy(y) = exp((log(10)*(abs(y)/($LINSCALELIMIT)-1)/0.5)+log($LINSCALELIMIT))
f(y) = (abs(y) < $LINSCALELIMIT) ? y : sgn(y)*flogy(y)
g(y) = (abs(y) < $LINSCALELIMIT) ? y : sgn(y)*glogy(y)
set nonlinear y via f(y) inverse g(y)
set xtic rotate by -45 scale 0
set ytics format "%1.2f us" nomirror ($YTICS)
set y2tics format "%2.3f ppm" nomirror
set title "local clock"
set key bottom right box
set lmargin 12
set rmargin 12
plot \
 "tracking.log" using 1:(\$6*1000000) title "offset" with linespoints, \
 "tracking.log" using 1:4 title "local clock error" with linespoints axis x1y2
EOF
}
lc 'png' 'local-clock' 'size 900,600'
lc 'svg' 'l.local-clock' 'size 22222,800 dynamic name "local_clock"'

NINETYFIVE=`/usr/libexec/chrony-graph/percentile 8 95 tracking.log | /usr/libexec/chrony-graph/mul 1000000`
NINETYNINE=`/usr/libexec/chrony-graph/percentile 8 99 tracking.log | /usr/libexec/chrony-graph/mul 1000000`
FIVE=`/usr/libexec/chrony-graph/percentile 8 5 tracking.log | /usr/libexec/chrony-graph/mul 1000000`
ONE=`/usr/libexec/chrony-graph/percentile 8 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 "local-clock-stddev.png"
set grid
set xlabel "Time"
set format x "%d-%H:%M"
set logscale y
set xtic rotate by -45 scale 0
set ytics format "%1.2f us" nomirror
set title "local clock stddev"
set key top right box
set label 1 gprintf("99%% = $NINETYNINE us",99) at graph 0.01,0.95 left front
set label 2 gprintf("95%% = $NINETYFIVE us",95) at graph 0.01,0.9 left front
set label 3 gprintf(" 5%% = $FIVE us",5) at graph 0.01,0.85 left front
set label 4 gprintf(" 1%% = $ONE us",1) at graph 0.01,0.8 left front
set label 5 gprintf("95%% - 5%% = $NF_M_F us",90) at graph 0.01,0.75 left front
set label 6 gprintf("99%% - 1%% = $NN_M_O us",98) at graph 0.01,0.7 left front
set lmargin 12
set rmargin 12
plot \
 "tracking.log" using 1:(\$9*1000000) title "stddev" with linespoints, \
 $NINETYNINE title "99th percentile", \
 $NINETYFIVE title "95th percentile", \
 $FIVE title "5th percentile", \
 $ONE title "1st percentile"
EOF

lc_skew() {
	local img_format=$1
	local img_name=$2
	local img_format_options=$3
NINETYFIVE=`/usr/libexec/chrony-graph/percentile 4 95 tracking.log`
NINETYNINE=`/usr/libexec/chrony-graph/percentile 4 99 tracking.log`
FIVE=`/usr/libexec/chrony-graph/percentile 4 5 tracking.log`
ONE=`/usr/libexec/chrony-graph/percentile 4 1 tracking.log`
NF_M_F=`echo $NINETYFIVE - $FIVE | bc`
NN_M_O=`echo $NINETYNINE - $ONE | bc`
gnuplot <<EOF
set terminal ${img_format} ${img_format_options}
set xdata time
set timefmt "%s"
set output "${img_name}.${img_format}"
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 skew"
set key top right box
set label 1 gprintf("99%% = $NINETYNINE ppm",99) at graph 0.01,0.95 left front
set label 2 gprintf("95%% = $NINETYFIVE ppm",95) at graph 0.01,0.9 left front
set label 3 gprintf(" 5%% = $FIVE ppm",5) at graph 0.01,0.85 left front
set label 4 gprintf(" 1%% = $ONE ppm",1) at graph 0.01,0.8 left front
set label 5 gprintf("95%% - 5%% = $NF_M_F ppm",90) at graph 0.01,0.75 left front
set label 6 gprintf("99%% - 1%% = $NN_M_O ppm",98) at graph 0.01,0.7 left front
set lmargin 12
set rmargin 12
plot \
 "tracking.log" using 1:5 title "skew" with linespoints, \
 $NINETYNINE title "99th percentile", \
 $NINETYFIVE title "95th percentile", \
 $FIVE title "5th percentile", \
 $ONE title "1st percentile"
EOF
}
lc_skew 'png' 'local-clock-skew' 'size 900,600'
lc_skew 'svg' 'l.local-clock-skew' 'size 22222,800 dynamic name "local_clock_skew"'
