Snippets
Created by
Ruslan Osmanov
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #!/bin/bash -
# Creates visual representation of data files generated by report.sh
# Author: Ruslan Osmanov <rrosmanov@gmail.com> 2016
TIME_FORMAT="%Y/%m/%d/%H:%M"
# Report files location $REPORT_DIR/param/device
DIR=$(cd $(dirname "$0"); pwd)
REPORT_DIR="${DIR}/r"
OUTPUT_DIR="${DIR}/o"
XRANGE=
PLOT_TERMINAL="png"
#####################################################################
# Functions
source $DIR/util.sh
function getPlots
{
param_path="$1"
find $param_path/ -maxdepth 1 -mindepth 1 -type f | while read -r device_path; do
# $device_path = /*/*/.../param/device
device=$(basename $device_path)
printf "'%s' using 1:2 t '%s' with lp pt 5, " \
"$device_path" $device
printf "'%s' using 1:2 t '%s' with impulses, " \
"$device_path" $device
done
}
# Returns default value for GNU plot xrange
function getXRange
{
y=$(date +%Y)
m=$(date +%m)
(( m -= 1 ))
if [ $m -lt 1 ]; then
m=1
(( y -= 1 ))
fi
d=$(date "+$TIME_FORMAT")
echo "[ '$y/$m/1/00:00' : '$d' ]"
}
# $1 - exit code
function usage
{
echo "
Creates charts from files generated by report.sh
Usage: $0 OPTIONS
OPTIONS:
-h, --help Display help message
-x, --xrange GNU Plot xrange value. Default: $(getXRange).
-t, --time-format Time format. Default: ${TIME_FORMAT}.
-i, --report-dir Directory with reports. Default: ${REPORT_DIR}.
-o, --output-dir Output directory. Default: ${OUTPUT_DIR}.
-f, --term GNU Plot terminal. Default: ${PLOT_TERMINAL}. See 'gnuplot set terminal'.
"
exit $1
}
#####################################################################
# Parsing CLI options
OPTS=$(getopt -o x:t:i:o:h -l xrange:,time-format:,report-dir:,output-dir:,help -- "$@")
[ $? -eq 0 ] || usage 1
eval set -- "$OPTS"
while true
do
case "$1" in
-x|--xrange)
XRANGE="$2"
shift 2;;
-t|--time-format)
TIME_FORMAT="$2"
shift 2;;
-i|--report-dir)
REPORT_DIR="$2"
shift 2;;
-o|--output-dir)
OUTPUT_DIR="$f"
shift 2;;
-h|--help)
usage 0
shift;;
--)
shift
break;;
?)
usage 1;;
*)
die "Internal error: failed to parse CLI args"
esac
done
echo XRANGE: $XRANGE TIME_FORMAT: $TIME_FORMAT OUTPUT_DIR: $OUTPUT_DIR REPORT_DIR: $REPORT_DIR
#####################################################################
[ -d $REPORT_DIR ] || die "$REPORT_DIR doesn't exist"
find $REPORT_DIR/ -maxdepth 1 -mindepth 1 -type d | while read -r param_path
do
# $param_path = /*/*/.../param
param=$(basename $param_path)
plots=$(getPlots "$param_path")
plots=${plots/%, /}
mkdir -p $OUTPUT_DIR
[ $? -eq 0 ] || die "Output directory '$OUTPUT_DIR' is inaccessible"
outfile="${OUTPUT_DIR}/${param}.${PLOT_TERMINAL}"
[[ -z $XRANGE ]] && XRANGE="$(getXRange)"
gnuplot <<EOS
TIME_FORMAT = "$TIME_FORMAT"
set terminal $PLOT_TERMINAL
set grid nopolar
set xtics rotate
set xlabel "Date/Time"
set ylabel "Value"
set yrange [ 0 : ]
set format x TIME_FORMAT timedate
set timefmt TIME_FORMAT
set xdata time
set xrange $XRANGE
set output "$outfile"
plot $plots
EOS
done
echo 'Done'
#vim: ts=2 sts=2 sw=2 et
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.