smon/notification_log.go

100 lines
2.1 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
2024-06-29 19:30:26 +02:00
func notificationsSent(from, to time.Time) (nss []NotificationSend, err error) {
2024-06-29 15:20:31 +02:00
var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(
`
SELECT
n.prio,
n.service,
2024-06-29 20:50:57 +02:00
COALESCE(t.name, '[NODATA]') AS trigger_name,
2024-06-29 15:20:31 +02:00
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
2024-06-29 19:30:26 +02:00
FROM public.notification_send ns
2024-06-29 15:20:31 +02:00
INNER JOIN notification n ON ns.notification_id = n.id
2024-06-29 19:41:38 +02:00
LEFT JOIN problem p ON ns.problem_id = p.id
LEFT JOIN "trigger" t ON p.trigger_id = t.id
2024-06-29 19:30:26 +02:00
WHERE
ns.send >= $1 AND
ns.send <= $2
2024-06-29 15:20:31 +02:00
ORDER BY
send DESC
2024-06-29 19:30:26 +02:00
`,
from,
to,
)
2024-06-29 15:20:31 +02:00
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
}