#!/bin/bash

MBIT="0.6Mbit stream"
T="pngcairo size 1000,600 enhanced font \"LiberationSerif,20\""
EXT=png
#T="pdfcairo"
#EXT=pdf

function runplot {
  for R in `seq -w 0 100`; do
    FLDS=( $1 )
    NAMS=( $2 )

    FN=`ls ${FLDS[0]}/run${R}_*.csv 2> /dev/null`
    if [ -z $FN ] ; then
      return
    fi
    FILE=`basename $FN`

    FN=`ls ${FLDS[0]}/run${R}_*.times 2> /dev/null`
    TIMES=`basename $FN`
    VIEWERS=`echo $FILE | cut -d '_' -f 2`
    TECH=`echo $FILE | cut -d '_' -f 3`
    SECS=`echo $FILE | cut -d '.' -f 1 | cut -d '_' -f 4`
    TIME=$(( $SECS / 60 ))m

    FILES=()
    TIME_FN=()

    for i in `seq $(( ${#FLDS[@]} - 1 )) -1 0`; do
      FN=`ls ${FLDS[$i]}/run${R}_*.csv 2> /dev/null`
      if [ -z $FN ] ; then
        return
      fi
      FILES[$i]="${FLDS[$i]}/`basename $FN`"
      FN=`ls ${FLDS[$i]}/run${R}_*.times 2> /dev/null`
      TIME_FN[$i]="${FLDS[$i]}/`basename $FN`"
    done

    if [ -n "$3" ] ; then
      COMMON_DATA="${TIME}, ${MBIT}, ${VIEWERS} $3"
      CFN="$3_${VIEWERS}_${TIME}"
      COMMON_FILENAME=${CFN,,}
    else
      COMMON_DATA="${TIME}, ${MBIT}, ${VIEWERS} ${TECH}"
      COMMON_FILENAME="${TECH}_${VIEWERS}_${TIME}"
    fi

    PLOT1=""
    PLOT2=""
    PLOT3=""
    PLOT4=""
    PLOT5=""
    PLOT6=""
    PLOT7=""
    for i in `seq $(( ${#FLDS[@]} - 1 )) -1 0`; do
      if [ -n "${PLOT1}" ] ; then
        PLOT1="${PLOT1}, "
        PLOT2="${PLOT2}, "
        PLOT3="${PLOT3}, "
        PLOT4="${PLOT4}, "
        PLOT5="${PLOT5}, "
        PLOT6="${PLOT6}, "
        PLOT7="${PLOT7}, "
      fi
      sort --field-separator=',' --key=4g < "${TIME_FN[$i]}" > "${TIME_FN[$i]}_sorted"
      VC=`cut -f 4 -d ',' < "${TIME_FN[$i]}" | awk "(int(\\\$1) >= ${SECS}000-5000) {sum++} END{print sum}"`
      SC=`cut -f 4 -d ',' < "${TIME_FN[$i]}" | awk "(int(\\\$1) >= ${SECS}000-30000) {sum++} END{print sum}"`
  
      #smooth CPU and network
      PLOT6="${PLOT6}'${FILES[$i]}' using 1:4:(1) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})' smooth acsplines"
      PLOT7="${PLOT7}'${FILES[$i]}' using (\$1-dx):(d(\$1,\$5/131072.0)):(1) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'  smooth acsplines"
  
      #actual CPU and network
      PLOT1="${PLOT1}'${FILES[$i]}' using 1:4 with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'"
      PLOT4="${PLOT4}'${FILES[$i]}' using (\$1-dx):(d(\$1,\$5/131072.0)) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'"

      #memory - no need for smoothing, it's already pretty smooth
      PLOT2="${PLOT2}'${FILES[$i]}' using 1:(\$3/1024.0) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'"
      #total upload - same here, no need to smooth anything over
      PLOT3="${PLOT3}'${FILES[$i]}' using 1:(\$5/134217728.0) with lines lw 2 title '${NAMS[$i]} (${VC}, ${SC})'"
      #and buffer times. Smoothing makes no sense here. Don't.
      PLOT5="${PLOT5}'${TIME_FN[$i]}_sorted' using (\$4-${SECS}000)/1000 with linespoints lw 1 title '${NAMS[$i]} (${VC}, ${SC})'"
    done

    gnuplot << EOF
set terminal ${T}
set datafile separator ","
set key on under center

#set style fill solid 0.25 border -1
#set style boxplot outliers pointtype 7
#set style data boxplot
#set boxwidth  0.5
set pointsize 0.2
set arrow from 0,-5 to ${VIEWERS}-1,-5 nohead

set title "Available buffer time, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_buffers.${EXT}'
set format y "%gs"
set ylabel "Buffer size (above line = success)"
set xlabel "Each point represents a single connection"
unset xtics
plot ${PLOT5}
unset style
unset xlabel
unset arrow

set timefmt "%s"
set xtics
set xdata time
#set xlabel "Time (mm:ss)"
set format x "%M:%S"
set grid

set title "CPU usage, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_cpu.${EXT}'
set yrange [0:*<100]
set format y "%.0f %%"
set ylabel "CPU use"
plot ${PLOT1}

set title "Memory usage, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_mem.${EXT}'
set yrange [0<*<0:0<*]
set ytics auto
set format y "%g MiB"
set ylabel "Memory use"
plot ${PLOT2}

set title "Network upload, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_upload.${EXT}'
set yrange [0<*<0:0<*]
set ytics auto
set format y "%g Gbit"
set ylabel "Total uploaded data"
plot ${PLOT3}

# derivative function.  Return 1/0 for first point, otherwise delta y or (delta y)/(delta x)
d(x,y) = (\$0 == 1) ? (x1 = x, y1 = y, 1/0) : (x2 = x1, x1 = x, y2 = y1, y1 = y, (y1-y2)/(x1-x2))

set title "Network speed, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_netspeed.${EXT}'
set yrange [0<*<0:0<*]
set ytics auto
set format y "%g Mbps"
set ylabel "Upload speed"
dx=0.5
plot ${PLOT4}

set title "Smoothed CPU usage, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_cpu_smooth.${EXT}'
set yrange [0:*<100]
set format y "%.0f %%"
set ylabel "CPU use"
plot ${PLOT6}

set title "Smoothed network speed, ${COMMON_DATA} clients"
set output '${COMMON_FILENAME}_netspeed_smooth.${EXT}'
set yrange [0<*<0:0<*]
set ytics auto
set format y "%g Mbps"
set ylabel "Upload speed"
dx=0.5
plot ${PLOT7}

EOF

    for i in `seq $(( ${#FLDS[@]} - 1 )) -1 0`; do
      rm -f "${FLDS[$i]}/${TIMES}_sorted"
    done
  done
}

#runplot "adobe_rtmp nginx_rtmp wowza_rtmp mist_rtmp flus_rtmp" "Adobe Nginx Wowza MistServer Flussonic"
#runplot "wowza_hls flus_hls mist_hls" "Wowza Flussonic MistServer"
#runplot "wowza_dash mist_dash" "Wowza MistServer"

#runplot "wowza_rtmp wowza_hls wowza_dash" "RTMP HLS DASH" "Wowza"
#runplot "mist_rtmp mist_hls mist_dash" "RTMP HLS DASH" "MistServer"
runplot "mistserver*" "test" "RTMP"

#runplot "wowza_dash mist_dash wowza_dash_new mist_dash_new mist_dash3" "Wowza Mist WowNew MistNew MistNewer"
#runplot "mist_dash mist_dash_new mist_dash3" "Old New Newer" "MistDash"
#runplot "wowza_hls_new flus_hls_new mist_hls_new mist_hls5" "Wowza Flus Mist MistNew"
#runplot "adobe_rtmp nginx_rtmp wowza_rtmp mist_rtmp flus_rtmp mist_rtmp5" "Adobe Nginx Wowza MistServer Flussonic MistNew"