mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-01 13:52:20 +00:00
Only report to mastodon Mondays
This commit is contained in:
parent
003efe910e
commit
533cde2eaf
4 changed files with 74 additions and 29 deletions
|
@ -29,7 +29,7 @@ func postToMastodon(metrics []metric) {
|
|||
startOfDay := getStartofDay()
|
||||
msg := fmt.Sprintf("Weekly user counts @ %d-%d-%d :laserkiwi:\n\n", startOfDay.Year(), startOfDay.Month(), startOfDay.Day())
|
||||
for _, m := range metrics {
|
||||
msg = msg + getPrintableString(m) + "\n"
|
||||
msg = msg + getPrintableString(m, true) + "\n"
|
||||
}
|
||||
msg = msg + "\n\n" + "#WeeklyStats"
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ func sendToMatrix(m []metric) {
|
|||
startOfDay := getStartofDay()
|
||||
sendMatrixWebhook(fmt.Sprintf("*User counts for %s*", startOfDay.String()), MATRIX_WEBHOOK_CHANNEL)
|
||||
for _, m := range m {
|
||||
sendMatrixWebhook(getPrintableString(m), MATRIX_WEBHOOK_CHANNEL)
|
||||
sendMatrixWebhook(getPrintableString(m, false), MATRIX_WEBHOOK_CHANNEL)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,11 +8,12 @@ import (
|
|||
|
||||
// Stores out metric/row data
|
||||
type metric struct {
|
||||
Service string `json:"service"`
|
||||
MetricName string `json:"metric_name"`
|
||||
MetricTime time.Time `json:"metric_time"`
|
||||
MetricValue int `json:"metric_value"`
|
||||
PreviousMetricValue int `json:"-"`
|
||||
Service string `json:"service"`
|
||||
MetricName string `json:"metric_name"`
|
||||
MetricTime time.Time `json:"metric_time"`
|
||||
MetricValue int `json:"metric_value"`
|
||||
PreviousDayMetricValue int `json:"-"`
|
||||
PreviousWeekMetricValue int `json:"-"`
|
||||
}
|
||||
|
||||
// persistMetrics - return any updated
|
||||
|
@ -42,10 +43,11 @@ func getUserCounts() ([]metric, error) {
|
|||
log.Println(err)
|
||||
} else {
|
||||
m := metric{
|
||||
Service: PIXELFED_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousMetricValue: getLastMetric(PIXELFED_IDENTIFIER),
|
||||
Service: PIXELFED_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousDayMetricValue: getLastMetric(PIXELFED_IDENTIFIER),
|
||||
PreviousWeekMetricValue: getLastWeekMetric(PIXELFED_IDENTIFIER),
|
||||
}
|
||||
log.Printf("%s user count: %d", PIXELFED_IDENTIFIER, userCount)
|
||||
metrics = append(metrics, m)
|
||||
|
@ -58,10 +60,11 @@ func getUserCounts() ([]metric, error) {
|
|||
log.Println(err)
|
||||
} else {
|
||||
m := metric{
|
||||
Service: MATRIX_IDENTIFIDER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousMetricValue: getLastMetric(MATRIX_IDENTIFIDER),
|
||||
Service: MATRIX_IDENTIFIDER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousDayMetricValue: getLastMetric(MATRIX_IDENTIFIDER),
|
||||
PreviousWeekMetricValue: getLastWeekMetric(MATRIX_IDENTIFIDER),
|
||||
}
|
||||
log.Printf("%s user count: %d", MATRIX_IDENTIFIDER, userCount)
|
||||
metrics = append(metrics, m)
|
||||
|
@ -74,10 +77,11 @@ func getUserCounts() ([]metric, error) {
|
|||
log.Println(err)
|
||||
} else {
|
||||
m := metric{
|
||||
Service: MASTODON_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousMetricValue: getLastMetric(MASTODON_IDENTIFIER),
|
||||
Service: MASTODON_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousDayMetricValue: getLastMetric(MASTODON_IDENTIFIER),
|
||||
PreviousWeekMetricValue: getLastWeekMetric(MASTODON_IDENTIFIER),
|
||||
}
|
||||
log.Printf("%s user count: %d", MASTODON_IDENTIFIER, userCount)
|
||||
metrics = append(metrics, m)
|
||||
|
@ -90,10 +94,11 @@ func getUserCounts() ([]metric, error) {
|
|||
log.Println(err)
|
||||
} else {
|
||||
m := metric{
|
||||
Service: MOBILIZON_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousMetricValue: getLastMetric(MOBILIZON_IDENTIFIER),
|
||||
Service: MOBILIZON_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousDayMetricValue: getLastMetric(MOBILIZON_IDENTIFIER),
|
||||
PreviousWeekMetricValue: getLastWeekMetric(MOBILIZON_IDENTIFIER),
|
||||
}
|
||||
log.Printf("%s user count: %d", MOBILIZON_IDENTIFIER, userCount)
|
||||
metrics = append(metrics, m)
|
||||
|
@ -106,10 +111,11 @@ func getUserCounts() ([]metric, error) {
|
|||
log.Println(err)
|
||||
} else {
|
||||
m := metric{
|
||||
Service: PEERTUBE_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousMetricValue: getLastMetric(PEERTUBE_IDENTIFIER),
|
||||
Service: PEERTUBE_IDENTIFIER,
|
||||
MetricName: METRICNAME_USERCOUNT,
|
||||
MetricValue: userCount,
|
||||
PreviousDayMetricValue: getLastMetric(PEERTUBE_IDENTIFIER),
|
||||
PreviousWeekMetricValue: getLastWeekMetric(PEERTUBE_IDENTIFIER),
|
||||
}
|
||||
log.Printf("%s user count: %d", PEERTUBE_IDENTIFIER, userCount)
|
||||
metrics = append(metrics, m)
|
||||
|
@ -119,9 +125,13 @@ func getUserCounts() ([]metric, error) {
|
|||
return metrics, nil
|
||||
}
|
||||
|
||||
func getPrintableString(m metric) string {
|
||||
func getPrintableString(m metric, useWeek bool) string {
|
||||
output := fmt.Sprintf("%s: %d", SERVICE_LINKS[m.Service], m.MetricValue)
|
||||
diff := m.MetricValue - m.PreviousMetricValue
|
||||
diff := m.MetricValue - m.PreviousDayMetricValue
|
||||
if useWeek {
|
||||
diff = m.MetricValue - m.PreviousWeekMetricValue
|
||||
}
|
||||
|
||||
if diff < 0 {
|
||||
output = fmt.Sprintf("%s (%d)", output, diff)
|
||||
} else if diff > 0 {
|
||||
|
@ -149,3 +159,24 @@ func getLastMetric(serviceName string) int {
|
|||
|
||||
return val
|
||||
}
|
||||
|
||||
func getLastWeekMetric(serviceName string) int {
|
||||
monday := getStartofDayMonday()
|
||||
val, err := runIntQuery(
|
||||
POSTGRESQL_STATS_DB,
|
||||
fmt.Sprintf(
|
||||
"SELECT metric_value FROM %s WHERE metric_name = '%s' AND service = '%s' AND metric_time = '%d' DESC LIMIT 1",
|
||||
POSTGRESQL_STATS_TABLE,
|
||||
METRICNAME_USERCOUNT,
|
||||
serviceName,
|
||||
monday,
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return 0
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
|
|
@ -14,3 +14,17 @@ func getStartofDay() time.Time {
|
|||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
func getStartofDayMonday() time.Time {
|
||||
localTime, err := time.LoadLocation(TIMEZONE)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Iterate until Monday!
|
||||
t := time.Now().In(localTime)
|
||||
for t.Weekday() != time.Monday {
|
||||
t = t.AddDate(0, 0, -1)
|
||||
}
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue