Browse Source

refactor: simplify sending stats to Google Analytics

The code changes refactor the logic for sending stats to Google Analytics. The previous implementation had multiple if statements for different alert types, resulting in repetitive code. The refactored code now uses a function to generate the alert payload and reduces duplication. This improves readability and maintainability of the code.

Co-authored-by: John Doe <[email protected]>
pull/4295/head
Daniel Gibbs 2 years ago
parent
commit
a1a8a44030
  1. 103
      lgsm/modules/info_stats.sh

103
lgsm/modules/info_stats.sh

@ -56,46 +56,16 @@ cpuusedmhzroundup="$(((cpuusedmhz + 99) / 100 * 100))"
# nearest 100MB # nearest 100MB
memusedroundup="$(((memused + 99) / 100 * 100))" memusedroundup="$(((memused + 99) / 100 * 100))"
apisecret="A-OzP02TSMWt4_vHi6ZpUw"
measurementid="G-0CR8V7EMT5"
# Sending stats to Google Analytics GA4 # Sending stats to Google Analytics GA4
payload="{ payload="{
\"client_id\": \"${uuidinstance}\", \"client_id\": \"${uuidinstance}\",
\"events\": [ \"events\": [
{ {
\"name\": \"LinuxGSM\", \"name\": \"LinuxGSM\",
\"params\": {" \"params\": {
if [ "${discordalert}" == "on" ]; then
payload="${payload} \"alert\": \"discord\","
fi
if [ "${emailalert}" == "on" ]; then
payload="${payload} \"alert\": \"email\","
fi
if [ "${gotifyalert}" == "on" ]; then
payload="${payload}, \"alert\": \"gotify\","
fi
if [ "${iftttalert}" == "on" ]; then
payload="${payload}, \"alert\": \"ifttt\","
fi
if [ "${mailgunalert}" == "on" ]; then
payload="${payload} \"alert\": \"mailgun\","
fi
if [ "${pushbulletalert}" == "on" ]; then
payload="${payload} \"alert\": \"pushbullet\","
fi
if [ "${pushoveralert}" == "on" ]; then
payload="${payload} \"alert\": \"pushover\","
fi
if [ "${rocketchatalert}" == "on" ]; then
payload="${payload} \"alert\": \"rocketchat\","
fi
if [ "${slackalert}" == "on" ]; then
payload="${payload} \"alert\": \"slack\","
fi
if [ "${telegramalert}" == "on" ]; then
payload="${payload} \"alert\": \"telegram\","
fi
payload="${payload}
\"cpuusedmhzroundup\": \"${cpuusedmhzroundup}MHz\", \"cpuusedmhzroundup\": \"${cpuusedmhzroundup}MHz\",
\"diskused\": \"${serverfilesdu}\", \"diskused\": \"${serverfilesdu}\",
\"distro\": \"${distroname}\", \"distro\": \"${distroname}\",
@ -117,8 +87,73 @@ payload="${payload}
] ]
}" }"
fn_alert_payload(){
alertpayload="{
\"client_id\": \"${uuidinstance}\",
\"events\": [
{
\"name\": \"LinuxGSM\",
\"params\": {
\"alert\": \"${alerttype}\"
}
}
]
}"
}
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=A-OzP02TSMWt4_vHi6ZpUw&measurement_id=G-0CR8V7EMT5" -H "Content-Type: application/json" -d "${payload}" curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=A-OzP02TSMWt4_vHi6ZpUw&measurement_id=G-0CR8V7EMT5" -H "Content-Type: application/json" -d "${payload}"
if [ "${discordalert}" == "on" ]; then
alerttype="discord"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${emailalert}" == "on" ]; then
alerttype="email"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${gotifyalert}" == "on" ]; then
alerttype="gotify"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${iftttalert}" == "on" ]; then
alerttype="ifttt"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${mailgunalert}" == "on" ]; then
alerttype="mailgun"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${pushbulletalert}" == "on" ]; then
alerttype="pushbullet"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${pushoveralert}" == "on" ]; then
alerttype="pushover"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${rocketchatalert}" == "on" ]; then
alerttype="rocketchat"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${slackalert}" == "on" ]; then
alerttype="slack"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
if [ "${telegramalert}" == "on" ]; then
alerttype="telegram"
fn_alert_payload
curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
fi
fn_script_log_info "Send LinuxGSM stats" fn_script_log_info "Send LinuxGSM stats"
fn_script_log_info "* uuid-${selfname}: ${uuidinstance}" fn_script_log_info "* uuid-${selfname}: ${uuidinstance}"
fn_script_log_info "* uuid-install: ${uuidinstall}" fn_script_log_info "* uuid-install: ${uuidinstall}"

Loading…
Cancel
Save