refactor(monitors): extract applyTransition and add passive monitor model fields
This commit is contained in:
@@ -17,6 +17,7 @@ const TypePatch = "patch"
|
||||
|
||||
type Event struct {
|
||||
MonitorName string
|
||||
ServerName string
|
||||
Type string
|
||||
OldStatus string
|
||||
NewStatus string
|
||||
@@ -42,7 +43,13 @@ func (e Event) title() string {
|
||||
}
|
||||
s = fmt.Sprintf("[Vantage] Server %s %s", e.MonitorName, verb)
|
||||
} else {
|
||||
s = fmt.Sprintf("[Vantage] %s (%s) %s", e.MonitorName, e.Type, verb)
|
||||
name := e.MonitorName
|
||||
if e.ServerName != "" {
|
||||
name = fmt.Sprintf("%s (%s) on %s", e.MonitorName, e.Type, e.ServerName)
|
||||
} else {
|
||||
name = fmt.Sprintf("%s (%s)", e.MonitorName, e.Type)
|
||||
}
|
||||
s = fmt.Sprintf("[Vantage] %s %s", name, verb)
|
||||
}
|
||||
if e.Message != "" {
|
||||
s += ": " + e.Message
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.hostxtra.co.uk/mrhid6/vantage/server/internal/models"
|
||||
)
|
||||
|
||||
func TestTitleIncludesServerName(t *testing.T) {
|
||||
ev := Event{MonitorName: "Disk full", Type: models.MonitorMetric, NewStatus: models.StatusDown, ServerName: "web-01", Message: "/var 94.2% used"}
|
||||
got := ev.title()
|
||||
want := "[Vantage] Disk full (metric) on web-01 is DOWN: /var 94.2% used"
|
||||
if got != want {
|
||||
t.Fatalf("title = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTitleWithoutServerNameUnchanged(t *testing.T) {
|
||||
ev := Event{MonitorName: "Site", Type: models.MonitorHTTP, NewStatus: models.StatusUp}
|
||||
if got := ev.title(); strings.Contains(got, " on ") || got != "[Vantage] Site (http) recovered" {
|
||||
t.Fatalf("title = %q", got)
|
||||
}
|
||||
}
|
||||
@@ -33,14 +33,18 @@ func dispatchWebhook(ch models.NotificationChannel, ev Event) error {
|
||||
if target == "" {
|
||||
return fmt.Errorf("webhook: missing url")
|
||||
}
|
||||
return postJSON(target, map[string]any{
|
||||
payload := map[string]any{
|
||||
"monitor": ev.MonitorName,
|
||||
"type": ev.Type,
|
||||
"old_status": ev.OldStatus,
|
||||
"new_status": ev.NewStatus,
|
||||
"message": ev.Message,
|
||||
"time": ev.Time.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
if ev.ServerName != "" {
|
||||
payload["server_name"] = ev.ServerName
|
||||
}
|
||||
return postJSON(target, payload)
|
||||
}
|
||||
|
||||
func dispatchDiscord(ch models.NotificationChannel, ev Event) error {
|
||||
|
||||
Reference in New Issue
Block a user