generator-status/stats.sh

110 lines
4.3 KiB
Bash

#!/bin/bash
rm stats.csv days.csv &> /dev/null
first_line=true
lines=$(cat light_cuts.csv | grep -c ,)
line=0
while read in
do
line=$(( $line + 1 ))
if [[ $in != 'status,date' ]]
then
state=$(echo $in | cut -d, -f1)
date_var=$(echo $in | cut -d, -f2)
if [[ $first_line = true ]]
then
prev_state=$state
prev_date_var=$date_var
first_line=false
else
D1=$(date -d "$prev_date_var" '+%s'); D2=$(date -d "$date_var" '+%s')
stats_hours=$(echo "$(date -u -d@$((D2-D1)) +%H:%M)")
echo "the generator was $prev_state for $stats_hours"
day_date=$(echo $prev_date_var | cut --delimiter=' ' -f1 | cut -d, -f2)
echo "$prev_state, $day_date, $stats_hours" >> stats.csv
prev_state=$state
prev_date_var=$date_var
fi
fi
if [[ $line = $lines ]]
then
D1=$(date -d "$prev_date_var" '+%s'); D2=$(date '+%s')
stats_hours=$(echo "$(date -u -d@$((D2-D1)) +%H:%M)")
day_date=$(echo $prev_date_var | cut --delimiter=' ' -f1 | cut -d, -f2)
echo "$prev_state, $day_date, $stats_hours" >> stats.csv
fi
done < light_cuts.csv
first_line=true
lines=$(cat stats.csv | grep -c OFF)
line=0
while read in
do
state=$(echo $in | cut -d, -f1)
if [[ $state = 'OFF' ]]
then
line=$(( $line + 1 ))
date_var=$(echo $in | cut -d, -f2)
time=$(echo $in | cut -d, -f3)
if [[ $first_line = true ]]
then
current_date=$date_var
total_time=$time
first_line=false
else
if [[ $current_date = $date_var ]]
then
total_time=$(date -d "$total_time $(date -d "$time" +'%H hours %M minutes')" +'%H:%M')
else
time_decimal=$(echo $(date -d "$total_time" +'%H').$(($(date -d "$total_time" +'%M') * 100 / 60)))
echo "$current_date,$time_decimal" >> days.csv
current_date=$date_var
total_time=$time
fi
fi
if [[ $line = $lines ]]
then
time_decimal=$(echo $(date -d "$total_time" +'%H' | bc).$(($(date -d "$total_time" +'%M' | bc) * 100 / 60)))
echo "$current_date,$time_decimal" >> days.csv
fi
fi
done < stats.csv
lines=$(cat days.csv | grep -c ,)
line=0
line_start=$(( $lines - 7 ))
day_num=0
#echo "$lines - lines, $line_start - line_start"
while read in
do
hours=10#$(echo $in | cut -d, -f2)
line=10#$(( $line + 1 ))
date_var=$(echo $in | cut -d, -f1)
# echo "$line - line, $date_var - date, $hours - hours, $day_num - day_num"
if [[ $line -gt $line_start ]]
then
# echo "LINE IS GREATER"
day_num=$(( $day_num + 1 ))
if [[ $day_num = 1 ]]; then day_1=$(echo $hours); day_date_1=$(echo $date_var | sed 's/\//\\\//g'); fi
if [[ $day_num = 2 ]]; then day_2=$(echo $hours); day_date_2=$(echo $date_var | sed 's/\//\\\//g'); fi
if [[ $day_num = 3 ]]; then day_3=$(echo $hours); day_date_3=$(echo $date_var | sed 's/\//\\\//g'); fi
if [[ $day_num = 4 ]]; then day_4=$(echo $hours); day_date_4=$(echo $date_var | sed 's/\//\\\//g'); fi
if [[ $day_num = 5 ]]; then day_5=$(echo $hours); day_date_5=$(echo $date_var | sed 's/\//\\\//g'); fi
if [[ $day_num = 6 ]]; then day_6=$(echo $hours); day_date_6=$(echo $date_var | sed 's/\//\\\//g'); fi
if [[ $day_num = 7 ]]; then day_7=$(echo $hours); day_date_7=$(echo $date_var | sed 's/\//\\\//g'); fi
fi
done < days.csv
hours_so_far=10#$((10#$(TZ='Asia/Beirut' date +'%H') - $(echo $day_7. | cut -d. -f1)))
cat stats.html | sed "s/offtime_1/$day_1/" | sed "s/offtime_2/$day_2/"| sed "s/offtime_3/$day_3/" | sed "s/offtime_4/$day_4/" | sed "s/offtime_5/$day_5/" | sed "s/offtime_6/$day_6/" | sed "s/offtime_7/$day_7/" | sed "s/hours_so_far_day/$hours_so_far/" | sed "s/day_date_1/$day_date_1/" | sed "s/day_date_2/$day_date_2/" | sed "s/day_date_3/$day_date_3/" | sed "s/day_date_4/$day_date_4/" | sed "s/day_date_5/$day_date_5/" | sed "s/day_date_6/$day_date_6/" | sed "s/day_date_7/$day_date_7/" | sed "s/date-n-time-placeholder/$(TZ='Asia/Beirut' date +'%b %d, %Y at %H:%M' | sed 's/\//\\\//g')/" | sed 's/10#//g' > stats/index.html
exit
sudo chown -R daemon:daemon .
sudo chown daemon:daemon ../htdocs/stats/index.html