mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-02 14:22:19 +00:00
Update matrix info
This commit is contained in:
parent
ecfe777efa
commit
b3c829c381
7 changed files with 103 additions and 4 deletions
|
@ -10,6 +10,10 @@ var (
|
|||
|
||||
TIMEZONE string
|
||||
|
||||
MATRIX_WEBHOOK_URL string
|
||||
MATRIX_WEBHOOK_API_KEY string
|
||||
MATRIX_WEBHOOK_CHANNEL string
|
||||
|
||||
// UserCount metric name
|
||||
METRICNAME_USERCOUNT = "userCount"
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ func Run() {
|
|||
}
|
||||
|
||||
// Write to DB
|
||||
persistMetrics(metrics)
|
||||
updatedMetrics := persistMetrics(metrics)
|
||||
|
||||
// Output example
|
||||
sendToMatrix(updatedMetrics)
|
||||
}
|
||||
|
|
60
internal/gomastodonstats/matrix.go
Normal file
60
internal/gomastodonstats/matrix.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package gomastodonstats
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type MatrixWebhook struct {
|
||||
Body string `json:"body"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
func sendToMatrix(m []metric) {
|
||||
|
||||
startOfDay := getStartofDay()
|
||||
msg := fmt.Sprintf(
|
||||
"*User stats for %d:*\n\n",
|
||||
startOfDay,
|
||||
getPrintableString(m),
|
||||
)
|
||||
|
||||
err := sendMatrixWebhook(msg, MATRIX_WEBHOOK_CHANNEL)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
||||
|
||||
// sendMatrixWebhook - takes msg, sends to matrix
|
||||
func sendMatrixWebhook(msgText string, channel string) error {
|
||||
// log.Println(msgText)
|
||||
data := MatrixWebhook{
|
||||
Key: MATRIX_WEBHOOK_API_KEY,
|
||||
}
|
||||
data.Body = msgText
|
||||
b, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", MATRIX_WEBHOOK_URL+"/"+channel, bytes.NewBuffer(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package gomastodonstats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
@ -13,15 +14,22 @@ type metric struct {
|
|||
MetricValue int `json:"metric_value"`
|
||||
}
|
||||
|
||||
func persistMetrics(metrics []metric) {
|
||||
// persistMetrics - return any updated
|
||||
func persistMetrics(metrics []metric) []metric {
|
||||
var updatedMetrics []metric
|
||||
|
||||
startOfDay := getStartofDay()
|
||||
for _, v := range metrics {
|
||||
v.MetricTime = startOfDay
|
||||
err := insertValues(v)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
updatedMetrics = append(updatedMetrics, v)
|
||||
}
|
||||
}
|
||||
|
||||
return updatedMetrics
|
||||
}
|
||||
|
||||
func getUserCounts() ([]metric, error) {
|
||||
|
@ -104,3 +112,13 @@ 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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue