From 9bc4bc3466e4a96ff6c9a695b18f921f4458c981 Mon Sep 17 00:00:00 2001 From: idanoo Date: Mon, 30 Jan 2023 11:44:03 +1300 Subject: [PATCH] Send separate messages --- internal/gomastodonstats/matrix.go | 15 ++++----------- internal/gomastodonstats/metrics.go | 10 ++-------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/internal/gomastodonstats/matrix.go b/internal/gomastodonstats/matrix.go index 8965fd2..022e316 100644 --- a/internal/gomastodonstats/matrix.go +++ b/internal/gomastodonstats/matrix.go @@ -3,7 +3,6 @@ package gomastodonstats import ( "bytes" "encoding/json" - "fmt" "log" "net/http" ) @@ -14,21 +13,15 @@ type MatrixWebhook struct { } func sendToMatrix(m []metric) { - if MATRIX_WEBHOOK_URL == "" { + if MATRIX_WEBHOOK_URL == "" || len(m) == 0 { log.Println("Skipping posting to Matrix as missing env vars") return } startOfDay := getStartofDay() - msg := fmt.Sprintf( - "*User stats for %d:*\n%s", - startOfDay, - getPrintableString(m), - ) - - err := sendMatrixWebhook(msg, MATRIX_WEBHOOK_CHANNEL) - if err != nil { - log.Print(err) + sendMatrixWebhook(startOfDay.String(), MATRIX_WEBHOOK_CHANNEL) + for _, m := range m { + sendMatrixWebhook(getPrintableString(m), MATRIX_WEBHOOK_CHANNEL) } } diff --git a/internal/gomastodonstats/metrics.go b/internal/gomastodonstats/metrics.go index 8a4ad92..63e6126 100644 --- a/internal/gomastodonstats/metrics.go +++ b/internal/gomastodonstats/metrics.go @@ -113,12 +113,6 @@ func getUserCounts() ([]metric, error) { return metrics, nil } -func getPrintableString(m []metric) string { - output := "" - - for _, v := range m { - output = fmt.Sprintf("%s\n%s: %d", output, v.Service, v.MetricValue) - } - - return output +func getPrintableString(m metric) string { + return fmt.Sprintf("%s: %d", m.Service, m.MetricValue) }