#!/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').$(($(date -d "$total_time" +'%M') * 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 while read in do hours=$(echo $in | cut -d, -f2) line=$(( $line + 1 )) if [[ $line > $line_start ]] then day_num=$(( $day_num + 1 )) if [[ $day_num = 1 ]]; then day_1=$(echo $hours | sed "s/0//"); fi if [[ $day_num = 2 ]]; then day_2=$(echo $hours | sed "s/0//"); fi if [[ $day_num = 3 ]]; then day_3=$(echo $hours | sed "s/0//"); fi if [[ $day_num = 4 ]]; then day_4=$(echo $hours | sed "s/0//"); fi if [[ $day_num = 5 ]]; then day_5=$(echo $hours | sed "s/0//"); fi if [[ $day_num = 6 ]]; then day_6=$(echo $hours | sed "s/0//"); fi if [[ $day_num = 7 ]]; then day_7=$(echo $hours | sed "s/0//"); fi fi done < days.csv 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/" > stats/index.html