-
Notifications
You must be signed in to change notification settings - Fork 5
/
pph
executable file
·267 lines (237 loc) · 7.2 KB
/
pph
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env bash
set -e
## Description: shows Chia Blockchain proofs found per hour, over the past 'n' hours
# ANSI color codes
bold=$(tput bold)
underline=$(tput smul)
reset=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
purple=$(tput setaf 5)
function usage() {
local script_name=$(basename "$0")
# Usage text
echo "${bold}${green}Usage:${reset} ${script_name} [options] [hours]"
echo
echo "${bold}${blue}Options:${reset}"
echo " ${bold}-g${reset} Display graph view."
echo " ${bold}-h${reset} Display this help message."
echo " ${bold}-v${reset} Enable verbose output."
echo
echo "${bold}${purple}Description:${reset}"
echo " This script shows Chia Blockchain proofs found per hour. By default, it"
echo " displays data for the past 24 hours. You can specify a different duration"
echo " by providing the number of hours as an argument."
echo
echo "${underline}Examples:${reset}"
echo " ${script_name} Display proofs for the past 24 hours in table format."
echo " ${script_name} -g 48 Display proofs for the past 48 hours in graph format."
echo
echo "${red}Note:${reset}"
echo " The graph view requires 'uplot' to be installed and in your PATH."
echo
echo "${bold}Author: wallentx${reset}"
}
while getopts "ghv" opt; do
case "$opt" in
g)
GRAPH=true
;;
h)
usage
exit 0
;;
v)
DEBUG=true
set -x
;;
*)
usage
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
HOURS=${1:-24}
TMPOUT=$(mktemp /tmp/tmpXXXXXXXXXX)
function shutdown() {
tput cnorm
rm $TMPOUT
}
trap shutdown EXIT
PRE_PROMPT=$(
cat <<- EOF
${yellow}Calculating Proofs Per Hour${reset}
for the past ${red}$HOURS${reset} hours...
EOF
)
POST_PROMPT=$(
cat <<- EOF
Proofs found per hour over the past ${red}$HOURS${reset} hours:
EOF
)
function spinner() {
local LC_CTYPE=en_US.UTF-8
tput civis
local spin="⣷⣯⣟⡿⢿⣻⣽⣾"
local pid=$(jobs -p)
local charwidth=1
while kill -0 "$pid" 2> /dev/null; do
local i=$(((i + $charwidth) % ${#spin}))
printf "%s" "$(tput setaf 2)${spin:$i:$charwidth}$(tput sgr0)"
tput cub1
sleep .1
done
tput cr
tput el
tput cuu1
tput el
echo "$POST_PROMPT"
tput cnorm
wait $(jobs -p)
}
echo -n "$PRE_PROMPT"
# I like to use 'ripgrep' instead of 'grep' whenever possible, because speed!
# https://github.com/BurntSushi/ripgrep
function findProofs() {
local search_command
if command -v rg >/dev/null; then
search_command="rg --only-matching --regexp"
else
search_command="grep -o"
fi
local log_dir="$HOME/.chia/mainnet/log"
local harvester_log="$log_dir/harvester-debug.log"
local debug_log="$log_dir/debug.log"
local log_files=()
# Check if harvester-debug.log files exist
if [[ -f "$harvester_log" ]] || [[ -f "${harvester_log}.1" ]]; then
log_files+=("$harvester_log"{,.1,.2,.3,.4})
else
log_files+=("$debug_log"{,.1,.2,.3,.4,.5,.6})
fi
for H in $(seq 1 "$HOURS" | xargs -I {} date -d '{} hour ago' "+%FT%H"); do
PROOFS=0
for file in "${log_files[@]}"; do
if [ -f "$file" ]; then
PROOFS=$((PROOFS + $(${search_command} "$H:[0-5][0-9]:[0-5][0-9].*Found [1-9] proofs" "$file" | wc -l)))
fi
done
paste <(echo "$H") <(printf "%6s\n" "$PROOFS")
done | tac
}
# I like to use 'choose' as an alternative to 'cut' and (sometimes) 'awk'
# https://github.com/theryangeary/choose
function processLine() {
if command -v choose >/dev/null; then
choose $0
else
awk '{print $1}'
fi
}
function proofGraph() {
# Check for uplot
if ! [ -x "$(command -v uplot)" ]; then
echo 'Error: uplot is not installed, or installed gem is not added to PATH' >&2
echo 'Get it at: https://github.com/red-data-tools/YouPlot' >&2
echo 'Get it with: gem install youplot' >&2
exit 1
fi
findProofs > "$TMPOUT" &
spinner
echo ""
LOW=$(cat "$TMPOUT" | choose 1 | sort -n -r | tail -n1)
MAX=$(cat "$TMPOUT" | choose 1 | sort -n | tail -n1)
SUM=$(cat "$TMPOUT" | choose 1 | paste -sd+ | bc)
AVG=$(echo "scale=0; $SUM / $HOURS" | bc -l)
cat "$TMPOUT" |
choose 1 |
tac |
nl -n ln |
tac |
column -t |
tr -s ' ' ',' |
sed 's/^/-/' |
uplot line \
--canvas braille \
-d , \
-b barplot \
-w $(($(tput cols) - 30)) \
-h 15 \
--xlabel "Hour" \
--ylabel "Proofs" \
-t "Proofs Per Hour" \
--xlim -$HOURS,0 \
--ylim $LOW,$MAX
echo "${purple}Average${reset}: ${blue}$AVG${reset}"
echo "${red}Total${reset}: ${blue}$SUM${reset}"
}
function printProofs() {
findProofs > "$TMPOUT" &
spinner
SUM=$(cat "$TMPOUT" | choose 1 | paste -sd+ | bc)
AVG=$(echo "scale=0; $SUM / $HOURS" | bc -l)
# Line drawing characters
local hl="─" vl="│" tlc="╭" trc="╮" blc="╰" brc="╯" ml="├" mr="┤" mc="┼" tc="┬" bc="┴"
# Column widths
local c1w=16 c2w=7 h1w=15 h2w=7
# Helper function to create horizontal lines
draw_line() {
for ((i = 0; i < $1; i++)); do
printf "%s" "$2"
done
}
# Top border
printf "%s" "$tlc"
draw_line $c1w $hl
printf "%s" "$tc"
draw_line $c2w $hl
printf "%s\n" "$trc"
# Header
printf "%s%-*s%14s%*s%s\n" "$vl" "$c1w" "${blue}Hour${reset}" "$vl" "$c2w" "${blue}Proofs${reset} " "$vl"
# Middle border
printf "%s" "$ml"
draw_line $c1w $hl
printf "%s" "$mc"
draw_line $c2w $hl
printf "%s\n" "$mr"
# Content
while IFS= read -r line; do
local hour="${line% *}"
local proofs="${line##* }"
local color="$reset" # Default color
# Calculate percentage difference from the average (modify the threshold as needed)
local threshold=35 # Percentage threshold for color coding
if [[ "$proofs" -le 9 ]]; then
color="$red"
hour="${hour% }"
proofs=" ${proofs% *}"
else
local percent_diff=$(awk -v proofs="$proofs" -v AVG="$AVG" 'BEGIN{print (proofs - AVG) / AVG * 100}')
if [[ $DEBUG == true ]]; then
echo "Hour: $hour, Proofs: $proofs, AVG: $AVG, Percent diff: $percent_diff"
fi
if (($(echo "$percent_diff < -$threshold" | bc -l))); then
color="$yellow"
elif (($(echo "$percent_diff > $threshold" | bc -l))); then
color="$green"
fi
fi
printf "%s%-*s%s %*s%s\n" "$vl" $(($h1w - 4)) "$hour" "$vl" "$h2w" "${color}$proofs${reset}" "$vl"
done < "$TMPOUT"
# Bottom border
printf "%s" "$blc"
draw_line $c1w $hl
printf "%s" "$bc"
draw_line $c2w $hl
printf "%s\n" "$brc"
}
if [[ "$GRAPH" == true ]]; then
proofGraph
else
printProofs
echo "${purple}Average${reset}: ${blue}$AVG${reset}"
echo "${red}Total${reset}: ${blue}$SUM${reset}"
fi