smon/notification_log.go

95 lines
2.0 KiB
Go
Raw Normal View History

2024-05-05 20:16:28 +02:00
package main
import (
2024-06-29 15:20:31 +02:00
// External
werr "git.gibonuddevalla.se/go/wrappederror"
"github.com/jmoiron/sqlx"
2024-05-05 20:16:28 +02:00
// Internal
"smon/notification"
2024-06-29 15:20:31 +02:00
2024-05-05 20:16:28 +02:00
// Standard
2024-06-29 18:09:56 +02:00
"database/sql"
"encoding/json"
2024-06-29 15:20:31 +02:00
"time"
2024-05-05 20:16:28 +02:00
)
2024-06-29 15:20:31 +02:00
type NotificationSend struct {
2024-06-29 18:09:56 +02:00
Prio int
Service string
ID int
UUID string
Sent time.Time `db:"send"`
OK bool
Error sql.NullString
ErrorIndented string
Acknowledged bool
TriggerName string `db:"trigger_name"`
2024-06-29 15:20:31 +02:00
}
2024-05-05 20:16:28 +02:00
func notificationLog(notificationService *notification.Service, problemID int, err error) {
if err == nil {
logger.Info("notification", "service", (*notificationService).GetType(), "problemID", problemID, "prio", (*notificationService).GetPrio(), "ok", true)
service.Db.Conn.Query(
`
INSERT INTO notification_send()
`,
)
} else {
logger.Error("notification", "service", (*notificationService).GetType(), "problemID", problemID, "prio", (*notificationService).GetPrio(), "ok", false, "error", err)
}
}
2024-06-29 15:20:31 +02:00
func notificationsSent() (nss []NotificationSend, err error) {
var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(
`
SELECT
n.prio,
n.service,
t.name AS trigger_name,
ns.id,
ns.uuid,
ns.send,
ns.ok,
2024-06-29 18:09:56 +02:00
ns.error::varchar,
2024-06-29 15:20:31 +02:00
ns.acknowledged
FROM
public.notification_send ns
INNER JOIN notification n ON ns.notification_id = n.id
INNER JOIN problem p ON ns.problem_id = p.id
INNER JOIN "trigger" t ON p.trigger_id = t.id
ORDER BY
send DESC
`)
if err != nil {
err = werr.Wrap(err)
return
}
defer rows.Close()
for rows.Next() {
ns := NotificationSend{}
err = rows.StructScan(&ns)
if err != nil {
err = werr.Wrap(err)
return
}
2024-06-29 18:09:56 +02:00
// Error contains json (can be NULL),
// and can at least be presented indented.
foo := make(map[string]any)
json.Unmarshal([]byte(ns.Error.String), &foo)
var j []byte
j, err = json.MarshalIndent(foo, "", " ")
ns.ErrorIndented = string(j)
2024-06-29 15:20:31 +02:00
nss = append(nss, ns)
}
return
}