Skip to content

Commit 28c5660

Browse files
feat: change notifications to be a daily summary (#2164)
Co-authored-by: JuArce <[email protected]>
1 parent 11d1801 commit 28c5660

File tree

3 files changed

+177
-9
lines changed

3 files changed

+177
-9
lines changed

alerts/generate_send_summary.sh

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/bin/bash
2+
3+
# ENV VARIABLES:
4+
# - REPETITIONS
5+
# - EXPLORER_URL
6+
# - SENDER_ADDRESS
7+
# - RPC_URL
8+
# - EXPLORER_URL
9+
# - NETWORK
10+
# - PRIVATE_KEY
11+
# - VERIFICATION_WAIT_TIME
12+
# - LOGS_BLOCK_RANGE
13+
# - PAGER_DUTY_KEY
14+
# - PAGER_DUTY_EMAIL
15+
# - PAGER_DUTY_SERVICE_ID
16+
# - SLACK_WEBHOOK_URL
17+
18+
# Load env file from $1 path
19+
source "$1"
20+
21+
DATE=$(date -d "yesterday" +"%Y_%m_%d")
22+
23+
# Determine log file name based on current date
24+
LOG_FILE="./alerts/notification_logs/log_$DATE.txt"
25+
26+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
27+
cd "$SCRIPT_DIR"
28+
cd ..
29+
30+
batches=0
31+
submitted_total=0
32+
submitted_by_aligned=0
33+
verified_total=0
34+
unverified_total=0
35+
eth_by_aligned="0"
36+
usd_by_aligned="0"
37+
eth_total="0"
38+
usd_total="0"
39+
40+
# Read the log file entries and generate a summary
41+
if [[ -f "$LOG_FILE" ]]; then
42+
while IFS= read -r line; do
43+
case "$line" in
44+
*"SUCCESS:"*)
45+
batches=$((batches + 1))
46+
47+
proofs_submitted=$(printf '%s\n' "$line" \
48+
| grep -oE '[0-9]+ proofs submitted' \
49+
| head -1 \
50+
| cut -d' ' -f1)
51+
if [[ -n "$proofs_submitted" ]]; then
52+
submitted_total=$((submitted_total + proofs_submitted))
53+
verified_total=$((verified_total + proofs_submitted))
54+
fi
55+
56+
proofs_submitted_by_aligned=$(printf '%s\n' "$line" \
57+
| grep -oE '\([0-9]+ sent\)' \
58+
| grep -oE '[0-9]+' \
59+
| head -1)
60+
if [[ -n "$proofs_submitted_by_aligned" ]]; then
61+
submitted_by_aligned=$((submitted_by_aligned + proofs_submitted_by_aligned))
62+
fi
63+
64+
eth_spent=$(printf '%s\n' "$line" \
65+
| sed -n 's/.*Spent \([0-9.]*\) ETH.*/\1/p')
66+
if [[ -n "$eth_spent" ]]; then
67+
eth_total=$(echo "$eth_total + $eth_spent" | bc -l)
68+
eth_by_aligned=$(echo "$eth_by_aligned + $eth_spent / $proofs_submitted * $proofs_submitted_by_aligned" | bc -l)
69+
fi
70+
71+
usd_spent=$(printf '%s\n' "$line" \
72+
| sed -n 's/.*(\$ *\([0-9.]*\)).*/\1/p')
73+
if [[ -n "$usd_spent" ]]; then
74+
usd_total=$(echo "$usd_total + $usd_spent" | bc -l)
75+
usd_by_aligned=$(echo "$usd_by_aligned + $usd_spent / $proofs_submitted * $proofs_submitted_by_aligned" | bc -l)
76+
fi
77+
;;
78+
*"FAILURE:"*)
79+
batches=$((batches + 1))
80+
81+
proofs_submitted=$(printf '%s\n' "$line" \
82+
| grep -oE '[0-9]+ proofs submitted' \
83+
| head -1 \
84+
| cut -d' ' -f1)
85+
if [[ -n "$proofs_submitted" ]]; then
86+
submitted_total=$((submitted_total + proofs_submitted))
87+
unverified_total=$((unverified_total + proofs_submitted))
88+
fi
89+
90+
proofs_submitted_by_aligned=$(printf '%s\n' "$line" \
91+
| grep -oE '\([0-9]+ sent\)' \
92+
| grep -oE '[0-9]+' \
93+
| head -1)
94+
if [[ -n "$proofs_submitted_by_aligned" ]]; then
95+
submitted_by_aligned=$((submitted_by_aligned + proofs_submitted_by_aligned))
96+
fi
97+
98+
eth_spent=$(printf '%s\n' "$line" \
99+
| sed -n 's/.*Spent \([0-9.]*\) ETH.*/\1/p')
100+
if [[ -n "$eth_spent" ]]; then
101+
eth_total=$(echo "$eth_total + $eth_spent" | bc -l)
102+
eth_by_aligned=$(echo "$eth_by_aligned + $eth_spent / $proofs_submitted * $proofs_submitted_by_aligned" | bc -l)
103+
fi
104+
105+
usd_spent=$(printf '%s\n' "$line" \
106+
| sed -n 's/.*(\$ *\([0-9.]*\)).*/\1/p')
107+
if [[ -n "$usd_spent" ]]; then
108+
usd_total=$(echo "$usd_total + $usd_spent" | bc -l)
109+
usd_by_aligned=$(echo "$usd_by_aligned + $usd_spent / $proofs_submitted * $proofs_submitted_by_aligned" | bc -l)
110+
fi
111+
esac
112+
done < "$LOG_FILE"
113+
114+
summary=$(
115+
printf "Daily Proof Submission Summary\n"
116+
printf "From %s 00:00 to %s 23:59\n" "$DATE" "$DATE"
117+
echo "----------------------------------------------------"
118+
printf "Processed batches: %d\n" "$batches"
119+
printf "Total Proofs submitted: %d\n" "$submitted_total"
120+
printf "Total Proofs verified: %d\n" "$verified_total"
121+
printf "Total Proofs not verified: %d\n" "$unverified_total"
122+
printf "Submitted by Aligned: %d\n" "$submitted_by_aligned"
123+
printf "Submitted by 3rd parties: %d\n" "$((submitted_total - submitted_by_aligned))"
124+
echo "----------------------------------------------------"
125+
printf "Spent by Aligned (ETH): %.12f ETH\n" "$eth_by_aligned"
126+
printf "Spent by Aligned (USD): $ %.2f\n" "$usd_by_aligned"
127+
printf "Spent by 3rd parties (ETH): %.12f ETH\n" "$(echo "$eth_total - $eth_by_aligned" | bc -l)"
128+
printf "Spent by 3rd parties (USD): $ %.2f\n" "$(echo "$usd_total - $usd_by_aligned" | bc -l)"
129+
printf "Total spent (ETH): %.12f ETH\n" "$eth_total"
130+
printf "Total spent (USD): $ %.2f\n" "$usd_total"
131+
echo "----------------------------------------------------"
132+
)
133+
134+
echo "$summary"
135+
136+
# Send the summary to Slack
137+
if [[ -n "$SLACK_WEBHOOK_URL" ]]; then
138+
safe_summary=$(printf '%s\n' "$summary" | sed 's/"/\\"/g')
139+
curl -s -X POST -H 'Content-type: application/json' \
140+
--data "{\"text\":\"\`\`\`$safe_summary\`\`\`\"}" \
141+
"$SLACK_WEBHOOK_URL" >/dev/null 2>&1
142+
fi
143+
else
144+
echo "Proof Submission Summary - $DATE"
145+
echo "----------------------------------------"
146+
echo "No log file found for today: $LOG_FILE"
147+
echo "----------------------------------------"
148+
fi

alerts/notification_logs/.gitkeep

Whitespace-only changes.

alerts/sender_with_alert.sh

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ function send_slack_message() {
7979
. alerts/slack.sh "$1"
8080
}
8181

82+
# Creates a log entry in the daily log file
83+
function create_log_entry() {
84+
status="$1"
85+
reason="${2:-}"
86+
87+
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
88+
89+
log_file_path="./alerts/notification_logs/log_$(date +"%Y_%m_%d").txt"
90+
91+
# Check if file exists, if not create it
92+
if [ ! -f "$log_file_path" ]; then
93+
touch "$log_file_path"
94+
fi
95+
96+
echo "[$timestamp] $status: - $reason" >> "$log_file_path"
97+
}
98+
8299
################# SEND LOGIC #################
83100

84101
## Remove Proof Data
@@ -91,8 +108,7 @@ mkdir -p ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proof
91108
nonce=$(aligned get-user-nonce --network $NETWORK --user_addr $SENDER_ADDRESS 2>&1 | awk '{print $9}')
92109
echo $nonce
93110
if ! [[ "$nonce" =~ ^[0-9]+$ ]]; then
94-
echo "Failed getting user nonce, retrying in 10 seconds"
95-
sleep 10
111+
echo "Failed getting user nonce, exiting"
96112
exit 0
97113
fi
98114

@@ -129,6 +145,7 @@ while IFS= read -r error; do
129145
if [[ -n "$error" ]]; then
130146
slack_error_message="Error submitting proof to $NETWORK: $error"
131147
send_slack_message "$slack_error_message"
148+
create_log_entry "ERROR" "Error submitting proof to $NETWORK: $error"
132149
is_error=1
133150
fi
134151
done <<< "$submit_errors"
@@ -193,7 +210,7 @@ verified=1
193210

194211
## Verify Proofs
195212
echo "Verifying $REPETITIONS proofs $x != 0"
196-
for proof in ./aligned_verification_data/*.cbor; do
213+
for proof in ./aligned_verification_data_$x/*.cbor; do
197214
## Validate Proof on Chain
198215
verification=$(aligned verify-proof-onchain \
199216
--aligned-verification-data $proof \
@@ -213,15 +230,18 @@ for proof in ./aligned_verification_data/*.cbor; do
213230
fi
214231
done
215232

216-
if [ $verified -eq 1 ]; then
217-
slack_message="$total_number_proofs proofs submitted and verified. We sent $REPETITIONS proofs. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
218-
else
233+
if [ $verified -ne 1 ]; then
234+
## Send Update to Slack only in case verification failed
219235
slack_message="$total_number_proofs proofs submitted but not verified. We sent $REPETITIONS proofs. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
236+
echo "$slack_message"
237+
send_slack_message "$slack_message"
220238
fi
221239

222-
## Send Update to Slack
223-
echo "$slack_message"
224-
send_slack_message "$slack_message"
240+
if [ $verified -eq 1 ]; then
241+
create_log_entry "SUCCESS" "$total_number_proofs proofs submitted and verified ($REPETITIONS sent). Spent $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
242+
else
243+
create_log_entry "FAILURE" "$total_number_proofs proofs submitted but not verified ($REPETITIONS sent). Spent $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
244+
fi
225245

226246
## Remove Proof Data
227247
rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/*

0 commit comments

Comments
 (0)