feat(api): open incidents and notify channels when monitors go down

Results are evaluated as they arrive. Transitions open or resolve the
monitor's incident and queue a notification per enabled channel in the
same transaction; every API replica delivers the outbox with leases and
exponential retries. Organizations manage webhook and email channels,
can send test notifications and list incidents. Email requires
UPTIME_SMTP_URL.

Closes #14
Closes #15
Closes #16

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 01:23:37 +01:00
1 parent 01ef543e62
commit 5f42e3e650
12 files changed
+822 -13

No files matched your search

+13 -9
View File
@@ -187,18 +187,20 @@ func (s *Server) agentResults(c *gin.Context) {
ids[i] = r.MonitorID
}
now := time.Now()
monitors := map[uuid.UUID]store.MonitorsForResultsRow{}
var accepted []agentapi.Result
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
monitors, err := q.MonitorsForJobs(ctx, store.MonitorsForJobsParams{Region: region, Ids: ids})
rows, err := q.MonitorsForResults(ctx, store.MonitorsForResultsParams{Region: region, Ids: ids})
if err != nil {
return err
}
orgOf := make(map[uuid.UUID]uuid.UUID, len(monitors))
for _, m := range monitors {
orgOf[m.ID] = m.OrgID
for _, m := range rows {
monitors[m.ID] = m
}
rows := make([]store.InsertCheckResultsParams, 0, len(req.Results))
params := make([]store.InsertCheckResultsParams, 0, len(req.Results))
for _, r := range req.Results {
org, ok := orgOf[r.MonitorID]
m, ok := monitors[r.MonitorID]
org := m.OrgID
// A checker may only report monitors of its own region, and
// timestamps far from now are rejected rather than skewing
// history.
@@ -215,18 +217,20 @@ func (s *Server) agentResults(c *gin.Context) {
if r.Error != "" {
row.Error = ptr(truncateValid(r.Error, 500))
}
rows = append(rows, row)
params = append(params, row)
accepted = append(accepted, r)
}
if len(rows) == 0 {
if len(params) == 0 {
return nil
}
_, err = q.InsertCheckResults(ctx, rows)
_, err = q.InsertCheckResults(ctx, params)
return err
})
if err != nil {
internalError(c, err)
return
}
s.evaluateResults(ctx, region, accepted, monitors)
c.Status(http.StatusNoContent)
}