feat: incident updates with a status, public or internal
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m54s
ci / test (pull_request) Successful in 12m15s
ci / build (pull_request) Successful in 11m0s
ci / image (pull_request) Successful in 4m23s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m54s
ci / test (pull_request) Successful in 12m15s
ci / build (pull_request) Successful in 11m0s
ci / image (pull_request) Successful in 4m23s
Outages open and close with their monitor; now people can say what is going on. An incident gets an optional title and updates, each with a status (investigating, identified, monitoring, resolved) and a Markdown message, shown on status pages or kept as an internal note. The incident shows the latest update's status, resolved once its monitor recovered. The console lists title and status and has a page per incident with its timeline (outage, updates, recovery) and, for incidents.write in the monitor's group, forms to post updates and set the title. Status pages show title, status and public updates. API: GET/PATCH /org/incidents/:id, POST and DELETE /org/incidents/:id/updates; the incident list carries title and status; the organization export includes the updates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
6f0cf59406
commit
43a04e99b1
27 files changed
+2753
-908
No files matched your search
@@ -130,6 +130,16 @@ var endpoints = []endpoint{
|
||||
status: http.StatusNoContent},
|
||||
{method: "GET", path: "/api/v1/org/incidents", tag: "Monitors", summary: "List incidents", perm: "incidents.read",
|
||||
description: "Paged, newest first. `monitor_id` limits them to one monitor.", resp: paged(obj{"incidents": []incidentView{}})},
|
||||
{method: "GET", path: "/api/v1/org/incidents/:id", tag: "Monitors", summary: "An incident with its updates", perm: "incidents.read",
|
||||
description: "`status` is `resolved` once the monitor recovered, otherwise the status of the latest update (`investigating`, `identified`, `monitoring`, `resolved`), empty before the first. Updates newest first, internal notes included.",
|
||||
resp: incidentDetailView{}},
|
||||
{method: "PATCH", path: "/api/v1/org/incidents/:id", tag: "Monitors", summary: "Name an incident", perm: "incidents.write",
|
||||
description: "The title status pages show; empty shows the monitor's name.", req: incidentTitleRequest{}, status: http.StatusNoContent},
|
||||
{method: "POST", path: "/api/v1/org/incidents/:id/updates", tag: "Monitors", summary: "Post an update with a status", perm: "incidents.write",
|
||||
description: "`message` is Markdown. `public` (default true) shows it on status pages; false keeps it an internal note. The update's status becomes the incident's.",
|
||||
req: incidentUpdateRequest{}, resp: incidentUpdateView{}, status: http.StatusCreated},
|
||||
{method: "DELETE", path: "/api/v1/org/incidents/:id/updates/:update", tag: "Monitors", summary: "Delete an update", perm: "incidents.write",
|
||||
status: http.StatusNoContent},
|
||||
|
||||
// Status pages
|
||||
{method: "GET", path: "/api/v1/org/statuspages", tag: "Status pages", summary: "List status pages", resp: obj{"pages": []statusPageView{}}},
|
||||
|
||||
+26
-23
@@ -25,29 +25,32 @@ import (
|
||||
// empty name marks a route deliberately not audited. A test fails for
|
||||
// routes missing here, so new endpoints cannot slip past the log.
|
||||
var auditActions = map[string]string{
|
||||
"POST /api/v1/org/alert-groups": "alert_group.create",
|
||||
"PUT /api/v1/org/alert-groups/:id": "alert_group.update",
|
||||
"DELETE /api/v1/org/alert-groups/:id": "alert_group.delete",
|
||||
"POST /api/v1/locale": "",
|
||||
"POST /api/v1/markdown/preview": "",
|
||||
"POST /api/v1/auth/signup": "account.signup",
|
||||
"POST /api/v1/auth/verify": "account.verify",
|
||||
"POST /api/v1/auth/verify/resend": "",
|
||||
"POST /api/v1/orgs": "org.create",
|
||||
"POST /api/v1/auth/login": "auth.login",
|
||||
"POST /api/v1/auth/login/totp": "auth.login",
|
||||
"POST /api/v1/auth/logout": "auth.logout",
|
||||
"POST /api/v1/consent": "account.consent",
|
||||
"PUT /api/v1/org/members/:user/owner": "member.owner",
|
||||
"POST /api/v1/me/org": "",
|
||||
"POST /api/v1/me/totp/setup": "",
|
||||
"POST /api/v1/me/totp/enable": "account.totp_enable",
|
||||
"POST /api/v1/me/totp/disable": "account.totp_disable",
|
||||
"POST /api/v1/me/totp/recovery-codes": "account.recovery_codes",
|
||||
"POST /api/v1/me/delete": "account.delete",
|
||||
"POST /api/v1/org/delete": "org.delete",
|
||||
"POST /api/v1/invitations/:token/accept": "member.join",
|
||||
"POST /api/v1/invitations/:token/signup": "member.join",
|
||||
"POST /api/v1/org/alert-groups": "alert_group.create",
|
||||
"PUT /api/v1/org/alert-groups/:id": "alert_group.update",
|
||||
"DELETE /api/v1/org/alert-groups/:id": "alert_group.delete",
|
||||
"POST /api/v1/locale": "",
|
||||
"POST /api/v1/markdown/preview": "",
|
||||
"POST /api/v1/auth/signup": "account.signup",
|
||||
"POST /api/v1/auth/verify": "account.verify",
|
||||
"POST /api/v1/auth/verify/resend": "",
|
||||
"POST /api/v1/orgs": "org.create",
|
||||
"POST /api/v1/auth/login": "auth.login",
|
||||
"POST /api/v1/auth/login/totp": "auth.login",
|
||||
"POST /api/v1/auth/logout": "auth.logout",
|
||||
"POST /api/v1/consent": "account.consent",
|
||||
"PUT /api/v1/org/members/:user/owner": "member.owner",
|
||||
"PATCH /api/v1/org/incidents/:id": "incident.title",
|
||||
"POST /api/v1/org/incidents/:id/updates": "incident.update",
|
||||
"DELETE /api/v1/org/incidents/:id/updates/:update": "incident.update_delete",
|
||||
"POST /api/v1/me/org": "",
|
||||
"POST /api/v1/me/totp/setup": "",
|
||||
"POST /api/v1/me/totp/enable": "account.totp_enable",
|
||||
"POST /api/v1/me/totp/disable": "account.totp_disable",
|
||||
"POST /api/v1/me/totp/recovery-codes": "account.recovery_codes",
|
||||
"POST /api/v1/me/delete": "account.delete",
|
||||
"POST /api/v1/org/delete": "org.delete",
|
||||
"POST /api/v1/invitations/:token/accept": "member.join",
|
||||
"POST /api/v1/invitations/:token/signup": "member.join",
|
||||
|
||||
"PUT /api/v1/org/policy": "org.policy_update",
|
||||
"POST /api/v1/org/monitors": "monitor.create",
|
||||
|
||||
@@ -289,6 +289,10 @@ type incidentView struct {
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
ResolvedAt *time.Time `json:"resolved_at"`
|
||||
Cause map[string]string `json:"cause"`
|
||||
Title string `json:"title"`
|
||||
// Status is resolved once the monitor recovered, otherwise the status
|
||||
// of the latest update, empty before the first.
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// incidentPage reads a page of incidents, newest first, of one monitor
|
||||
@@ -336,7 +340,10 @@ func (s *Server) listIncidents(c *gin.Context) {
|
||||
}
|
||||
out := make([]incidentView, len(rows))
|
||||
for i, r := range rows {
|
||||
out[i] = incidentView{ID: r.ID, MonitorID: r.MonitorID, MonitorName: r.MonitorName, StartedAt: r.StartedAt, ResolvedAt: r.ResolvedAt}
|
||||
out[i] = incidentView{
|
||||
ID: r.ID, MonitorID: r.MonitorID, MonitorName: r.MonitorName, StartedAt: r.StartedAt, ResolvedAt: r.ResolvedAt,
|
||||
Title: r.Title, Status: incidentStatus(store.Incident{ResolvedAt: r.ResolvedAt, Status: r.Status}),
|
||||
}
|
||||
_ = json.Unmarshal(r.Cause, &out[i].Cause)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"incidents": out, "next_cursor": info.Next, "prev_cursor": info.Prev})
|
||||
|
||||
@@ -533,6 +533,7 @@ func (s *Server) monitorDetail(c *gin.Context, p *principal, page web.Page, me w
|
||||
}
|
||||
v.Incidents = append(v.Incidents, d.incidentRow(store.Incident{
|
||||
ID: i.ID, OrgID: i.OrgID, MonitorID: i.MonitorID, StartedAt: i.StartedAt, ResolvedAt: i.ResolvedAt, Cause: i.Cause,
|
||||
Title: i.Title, Status: i.Status,
|
||||
}, m.Name))
|
||||
}
|
||||
page.TitleID = "console.monitors.title"
|
||||
@@ -556,6 +557,7 @@ func (d monitorData) incidentRow(i store.Incident, monitor string) web.IncidentR
|
||||
row := web.IncidentRow{
|
||||
ID: i.ID.String(), MonitorID: i.MonitorID.String(), Monitor: monitor,
|
||||
Started: i.StartedAt.UTC().Format(timeLayout), Open: i.ResolvedAt == nil,
|
||||
Title: i.Title, Status: incidentStatus(i),
|
||||
}
|
||||
end := time.Now()
|
||||
if i.ResolvedAt != nil {
|
||||
@@ -906,6 +908,7 @@ func (s *Server) incidentsView(c *gin.Context, p *principal, page web.Page, me w
|
||||
for i, r := range rows {
|
||||
v.Rows[i] = d.incidentRow(store.Incident{
|
||||
ID: r.ID, OrgID: r.OrgID, MonitorID: r.MonitorID, StartedAt: r.StartedAt, ResolvedAt: r.ResolvedAt, Cause: r.Cause,
|
||||
Title: r.Title, Status: r.Status,
|
||||
}, r.MonitorName)
|
||||
}
|
||||
if monitor != nil {
|
||||
|
||||
+20
-1
@@ -337,6 +337,16 @@ type exportIncident struct {
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
ResolvedAt *time.Time `json:"resolved_at"`
|
||||
Cause json.RawMessage `json:"cause"`
|
||||
Title string `json:"title"`
|
||||
Status *string `json:"status"`
|
||||
Updates []exportUpdate `json:"updates"`
|
||||
}
|
||||
|
||||
type exportUpdate struct {
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Public bool `json:"public"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type exportAuditEntry struct {
|
||||
@@ -518,10 +528,19 @@ func (s *Server) buildOrgExport(ctx context.Context, p *principal) (orgExport, e
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updates, err := q.OrgIncidentUpdatesExport(ctx, org)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updatesOf := map[uuid.UUID][]exportUpdate{}
|
||||
for _, u := range updates {
|
||||
updatesOf[u.IncidentID] = append(updatesOf[u.IncidentID], exportUpdate{Status: u.Status, Message: u.Message, Public: u.Public, CreatedAt: u.CreatedAt})
|
||||
}
|
||||
x.Incidents = []exportIncident{}
|
||||
for _, i := range incidents {
|
||||
x.Incidents = append(x.Incidents, exportIncident{ID: i.ID, MonitorID: i.MonitorID, Monitor: i.Monitor,
|
||||
StartedAt: i.StartedAt, ResolvedAt: i.ResolvedAt, Cause: rawJSON(i.Cause)})
|
||||
StartedAt: i.StartedAt, ResolvedAt: i.ResolvedAt, Cause: rawJSON(i.Cause),
|
||||
Title: i.Title, Status: i.Status, Updates: orEmpty(updatesOf[i.ID])})
|
||||
}
|
||||
|
||||
audit, err := q.OrgAuditExport(ctx, org)
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/authz"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
||||
)
|
||||
|
||||
// Incidents open and close with their monitor. People explain them with a
|
||||
// title, a status and updates, public on status pages or internal notes.
|
||||
|
||||
// incidentStatus is what an incident shows: resolved once its monitor
|
||||
// recovered, otherwise the status of its latest update, empty before one.
|
||||
func incidentStatus(i store.Incident) string {
|
||||
if i.ResolvedAt != nil {
|
||||
return "resolved"
|
||||
}
|
||||
return deref(i.Status)
|
||||
}
|
||||
|
||||
type incidentUpdateView struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Public bool `json:"public"`
|
||||
Author string `json:"author"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type incidentDetailView struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
MonitorID uuid.UUID `json:"monitor_id"`
|
||||
Monitor string `json:"monitor"`
|
||||
Title string `json:"title"`
|
||||
Status string `json:"status"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
ResolvedAt *time.Time `json:"resolved_at"`
|
||||
Updates []incidentUpdateView `json:"updates"`
|
||||
}
|
||||
|
||||
// loadIncident returns the incident of the request with its updates.
|
||||
func (s *Server) loadIncident(c *gin.Context) (store.GetIncidentRow, []store.ListIncidentUpdatesRow, bool) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return store.GetIncidentRow{}, nil, false
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
var (
|
||||
inc store.GetIncidentRow
|
||||
updates []store.ListIncidentUpdatesRow
|
||||
)
|
||||
err = s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
||||
var err error
|
||||
if inc, err = q.GetIncident(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
updates, err = q.ListIncidentUpdates(ctx, id)
|
||||
return err
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return inc, nil, false
|
||||
}
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return inc, nil, false
|
||||
}
|
||||
return inc, updates, true
|
||||
}
|
||||
|
||||
func incidentOf(r store.GetIncidentRow) store.Incident {
|
||||
return store.Incident{
|
||||
ID: r.ID, OrgID: r.OrgID, MonitorID: r.MonitorID, StartedAt: r.StartedAt, ResolvedAt: r.ResolvedAt,
|
||||
Cause: r.Cause, Title: r.Title, Status: r.Status,
|
||||
}
|
||||
}
|
||||
|
||||
func toIncidentDetail(inc store.GetIncidentRow, updates []store.ListIncidentUpdatesRow) incidentDetailView {
|
||||
v := incidentDetailView{
|
||||
ID: inc.ID, MonitorID: inc.MonitorID, Monitor: inc.MonitorName, Title: inc.Title,
|
||||
Status: incidentStatus(incidentOf(inc)), StartedAt: inc.StartedAt, ResolvedAt: inc.ResolvedAt,
|
||||
Updates: []incidentUpdateView{},
|
||||
}
|
||||
for _, u := range updates {
|
||||
v.Updates = append(v.Updates, incidentUpdateView{
|
||||
ID: u.ID, Status: u.Status, Message: u.Message, Public: u.Public, Author: u.AuthorName, CreatedAt: u.CreatedAt,
|
||||
})
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (s *Server) getIncident(c *gin.Context) {
|
||||
inc, updates, ok := s.loadIncident(c)
|
||||
if ok {
|
||||
c.JSON(http.StatusOK, toIncidentDetail(inc, updates))
|
||||
}
|
||||
}
|
||||
|
||||
// mayWriteIncident checks incidents.write for the incident's monitor,
|
||||
// which roles limited to monitor groups grant too.
|
||||
func (s *Server) mayWriteIncident(c *gin.Context, inc store.GetIncidentRow) bool {
|
||||
p := current(c)
|
||||
t, err := s.loadGroupTree(c.Request.Context(), p.scope())
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return false
|
||||
}
|
||||
if !canInGroup(p, authz.IncidentsWrite, t, inc.MonitorGroupID) {
|
||||
fail(c, http.StatusForbidden, "forbidden")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type incidentTitleRequest struct {
|
||||
Title string `json:"title" binding:"max=200"`
|
||||
}
|
||||
|
||||
// setIncidentTitle names the incident, e.g. on status pages; empty shows
|
||||
// the monitor's name.
|
||||
func (s *Server) setIncidentTitle(c *gin.Context) {
|
||||
var req incidentTitleRequest
|
||||
if !bind(c, &req) {
|
||||
return
|
||||
}
|
||||
inc, _, ok := s.loadIncident(c)
|
||||
if !ok || !s.mayWriteIncident(c, inc) {
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
if err := s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
||||
_, err := q.SetIncidentTitle(ctx, store.SetIncidentTitleParams{ID: inc.ID, Title: strings.TrimSpace(req.Title)})
|
||||
return err
|
||||
}); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
auditDetail(c, "incident_id", inc.ID.String())
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
type incidentUpdateRequest struct {
|
||||
Status string `json:"status" binding:"required"`
|
||||
Message string `json:"message" binding:"required,max=5000"`
|
||||
// Public shows the update on status pages; otherwise it is an internal
|
||||
// note. Defaults to public.
|
||||
Public *bool `json:"public"`
|
||||
}
|
||||
|
||||
// addIncidentUpdate tells what is going on: a status and a message.
|
||||
func (s *Server) addIncidentUpdate(c *gin.Context) {
|
||||
var req incidentUpdateRequest
|
||||
if !bind(c, &req) {
|
||||
return
|
||||
}
|
||||
req.Message = strings.TrimSpace(req.Message)
|
||||
if !slices.Contains(web.IncidentStatuses, req.Status) || req.Message == "" {
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_input")
|
||||
return
|
||||
}
|
||||
inc, _, ok := s.loadIncident(c)
|
||||
if !ok || !s.mayWriteIncident(c, inc) {
|
||||
return
|
||||
}
|
||||
public := req.Public == nil || *req.Public
|
||||
p := current(c)
|
||||
var author *uuid.UUID
|
||||
if p.Session != nil {
|
||||
author = &p.UserID
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
var u store.IncidentUpdate
|
||||
if err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
var err error
|
||||
u, err = q.AddIncidentUpdate(ctx, store.AddIncidentUpdateParams{
|
||||
OrgID: p.Org.ID, IncidentID: inc.ID, Status: req.Status, Message: req.Message, Public: public, AuthorID: author,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return q.RefreshIncidentStatus(ctx, inc.ID)
|
||||
}); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
auditDetail(c, "incident_id", inc.ID.String())
|
||||
auditDetail(c, "status", req.Status)
|
||||
c.JSON(http.StatusCreated, incidentUpdateView{ID: u.ID, Status: u.Status, Message: u.Message, Public: u.Public, CreatedAt: u.CreatedAt})
|
||||
}
|
||||
|
||||
func (s *Server) deleteIncidentUpdate(c *gin.Context) {
|
||||
update, err := uuid.Parse(c.Param("update"))
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
inc, _, ok := s.loadIncident(c)
|
||||
if !ok || !s.mayWriteIncident(c, inc) {
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
var n int64
|
||||
if err := s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
||||
var err error
|
||||
if n, err = q.DeleteIncidentUpdate(ctx, store.DeleteIncidentUpdateParams{ID: update, IncidentID: inc.ID}); err != nil || n == 0 {
|
||||
return err
|
||||
}
|
||||
return q.RefreshIncidentStatus(ctx, inc.ID)
|
||||
}); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
auditDetail(c, "incident_id", inc.ID.String())
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// incidentDetailView is the console page of one incident.
|
||||
func (s *Server) incidentDetailView(c *gin.Context, p *principal, page web.Page, me web.Me) (templ.Component, error) {
|
||||
c.Set(ctxPrincipal, p)
|
||||
inc, updates, ok := s.loadIncident(c)
|
||||
if !ok {
|
||||
return nil, errAborted
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
d, err := s.loadMonitorData(ctx, p, web.RangeByKey(""))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, err := s.loadGroupTree(ctx, p.scope())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v := web.IncidentDetailView{
|
||||
Row: d.incidentRow(incidentOf(inc), inc.MonitorName),
|
||||
CanWrite: canInGroup(p, authz.IncidentsWrite, t, inc.MonitorGroupID),
|
||||
}
|
||||
// Newest first; the monitor's own events among the updates.
|
||||
type entry struct {
|
||||
at time.Time
|
||||
e web.IncidentTimelineEntry
|
||||
}
|
||||
entries := []entry{{inc.StartedAt, web.IncidentTimelineEntry{Kind: "started"}}}
|
||||
if inc.ResolvedAt != nil {
|
||||
entries = append(entries, entry{*inc.ResolvedAt, web.IncidentTimelineEntry{Kind: "resolved"}})
|
||||
}
|
||||
for _, u := range updates {
|
||||
html, err := web.RenderMarkdown(u.Message)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries = append(entries, entry{u.CreatedAt, web.IncidentTimelineEntry{
|
||||
Kind: "update", ID: u.ID.String(), Status: u.Status, HTML: html, Author: u.AuthorName, Public: u.Public,
|
||||
}})
|
||||
}
|
||||
slices.SortStableFunc(entries, func(a, b entry) int { return b.at.Compare(a.at) })
|
||||
for _, x := range entries {
|
||||
x.e.At = x.at.UTC().Format(timeLayout)
|
||||
v.Timeline = append(v.Timeline, x.e)
|
||||
}
|
||||
v.NextStatus = nextIncidentStatus(v.Row.Status)
|
||||
return web.IncidentDetailPage(page, me, v), nil
|
||||
}
|
||||
|
||||
// nextIncidentStatus suggests the status after current: usually one step
|
||||
// further, staying at resolved.
|
||||
func nextIncidentStatus(current string) string {
|
||||
i := slices.Index(web.IncidentStatuses, current)
|
||||
if i < 0 {
|
||||
return web.IncidentStatuses[0]
|
||||
}
|
||||
return web.IncidentStatuses[min(i+1, len(web.IncidentStatuses)-1)]
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
)
|
||||
|
||||
func TestIncidentUpdates(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
ctx := context.Background()
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("iu")+".test", uniq("iu-"))
|
||||
_, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Login", "type": "heartbeat", "interval_seconds": 3600})
|
||||
monitorID := m["id"].(string)
|
||||
slug := uniq("iup-")
|
||||
_, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Acme"})
|
||||
pageID := page["id"].(string)
|
||||
owner.do("PUT", "/api/v1/org/statuspages/"+pageID, map[string]any{"slug": slug, "title": "Acme", "published": true})
|
||||
if st, body := owner.do("PUT", "/api/v1/org/statuspages/"+pageID+"/monitors", map[string]any{"monitors": []map[string]any{{"monitor_id": monitorID}}}); st != http.StatusNoContent && st != http.StatusOK {
|
||||
t.Fatalf("list monitor on page = %d %v", st, body)
|
||||
}
|
||||
var incident uuid.UUID
|
||||
if err := e.asSystem(t, func(tx pgx.Tx) error {
|
||||
return tx.QueryRow(ctx, `INSERT INTO incidents (org_id, monitor_id, started_at, cause)
|
||||
SELECT org_id, id, now() - interval '10 minutes', '{}' FROM monitors WHERE id = $1 RETURNING id`, monitorID).Scan(&incident)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
base := "/api/v1/org/incidents/" + incident.String()
|
||||
status := func() string {
|
||||
_, body := owner.do("GET", base, nil)
|
||||
s, _ := body["status"].(string)
|
||||
return s
|
||||
}
|
||||
if s := status(); s != "" {
|
||||
t.Errorf("status before any update = %q", s)
|
||||
}
|
||||
|
||||
// Updates with a status, public or internal, and a title.
|
||||
for _, bad := range []map[string]any{
|
||||
{"status": "panicking", "message": "x"},
|
||||
{"status": "investigating", "message": " "},
|
||||
} {
|
||||
st, body := owner.do("POST", base+"/updates", bad)
|
||||
expect(t, "invalid update", st, body, http.StatusUnprocessableEntity, "invalid_input")
|
||||
}
|
||||
if st, body := owner.do("POST", base+"/updates", map[string]any{"status": "investigating", "message": "We are **looking** into it."}); st != http.StatusCreated {
|
||||
t.Fatalf("post update = %d %v", st, body)
|
||||
}
|
||||
if st, body := owner.do("PATCH", base, map[string]any{"title": "Sign-in is slow"}); st != http.StatusNoContent {
|
||||
t.Fatalf("set title = %d %v", st, body)
|
||||
}
|
||||
_, note := owner.do("POST", base+"/updates", map[string]any{"status": "identified", "message": "DB failover, ask Bob", "public": false})
|
||||
if s := status(); s != "identified" {
|
||||
t.Errorf("status = %q, want the latest update's", s)
|
||||
}
|
||||
_, list := owner.do("GET", "/api/v1/org/incidents", nil)
|
||||
if first := list["incidents"].([]any)[0].(map[string]any); first["title"] != "Sign-in is slow" || first["status"] != "identified" {
|
||||
t.Errorf("incident list = %v", first)
|
||||
}
|
||||
|
||||
// Status pages show the title, the status and only public updates.
|
||||
snap, err := e.statuspageClient(t, nil).Page(ctx, "", slug)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var shown agentapi.PageIncident
|
||||
for _, i := range snap.Incidents {
|
||||
if i.Component == "Login" {
|
||||
shown = i
|
||||
}
|
||||
}
|
||||
if shown.Title != "Sign-in is slow" || shown.Status != "identified" || len(shown.Updates) != 1 || !strings.Contains(shown.Updates[0].Message, "looking") {
|
||||
t.Errorf("status page incident = %+v", shown)
|
||||
}
|
||||
|
||||
// The console page tells the story, internal notes marked.
|
||||
_, _, html := owner.page("/incidents/" + incident.String())
|
||||
for _, want := range []string{"Sign-in is slow", "<strong>looking</strong>", "DB failover", ">internal<", "Post update", "Outage began"} {
|
||||
if !strings.Contains(html, want) {
|
||||
t.Errorf("incident page lacks %q", want)
|
||||
}
|
||||
}
|
||||
|
||||
// Readers read, writers write.
|
||||
viewer := e.joinVia(t, owner.invite("v@"+uniq("iu")+".test", owner.roleID("viewer")))
|
||||
if st, _ := viewer.do("GET", base, nil); st != http.StatusOK {
|
||||
t.Errorf("viewer reads incident = %d", st)
|
||||
}
|
||||
st, body := viewer.do("POST", base+"/updates", map[string]any{"status": "monitoring", "message": "x"})
|
||||
expect(t, "viewer posts update", st, body, http.StatusForbidden, "forbidden")
|
||||
if _, _, html := viewer.page("/incidents/" + incident.String()); strings.Contains(html, "Post update") {
|
||||
t.Error("the viewer is offered the update form")
|
||||
}
|
||||
|
||||
// Deleting an update falls back to the previous status; the monitor
|
||||
// recovering resolves the incident whatever was said.
|
||||
if st, _ := owner.do("DELETE", base+"/updates/"+note["id"].(string), nil); st != http.StatusNoContent || status() != "investigating" {
|
||||
t.Errorf("after deleting the latest update status = %q", status())
|
||||
}
|
||||
if err := e.asSystem(t, func(tx pgx.Tx) error {
|
||||
_, err := tx.Exec(ctx, `UPDATE incidents SET resolved_at = $2 WHERE id = $1`, incident, time.Now())
|
||||
return err
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s := status(); s != "resolved" {
|
||||
t.Errorf("status after recovery = %q", s)
|
||||
}
|
||||
if st, _ := owner.do("GET", "/api/v1/org/incidents/"+uuid.NewString(), nil); st != http.StatusNotFound {
|
||||
t.Errorf("unknown incident = %d", st)
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,7 @@ func (s *Server) registerPages(r gin.IRouter) {
|
||||
r.GET("/admin/orgs", s.adminOrgsPage)
|
||||
r.GET("/admin/legal", s.adminLegalPage)
|
||||
r.GET("/settings/legal", s.orgPage("legal.org_title", authz.OrgManage, s.orgLegalView))
|
||||
r.GET("/incidents/:id", s.orgPage("incidents.title", authz.IncidentsRead, s.incidentDetailView))
|
||||
r.GET("/legal/:kind", s.legalPage)
|
||||
r.GET("/consent", s.consentPage)
|
||||
r.GET("/account/suspended", s.suspendedPage)
|
||||
|
||||
@@ -223,6 +223,10 @@ func (s *Server) Mount(r gin.IRouter) {
|
||||
page.GET("/export", canPage(authz.StatusPagesRead), s.exportStatusPage)
|
||||
|
||||
org.GET("/incidents", can(authz.IncidentsRead), s.listIncidents)
|
||||
org.GET("/incidents/:id", can(authz.IncidentsRead), s.getIncident)
|
||||
org.PATCH("/incidents/:id", s.setIncidentTitle)
|
||||
org.POST("/incidents/:id/updates", s.addIncidentUpdate)
|
||||
org.DELETE("/incidents/:id/updates/:update", s.deleteIncidentUpdate)
|
||||
org.GET("/audit", can(authz.AuditRead), s.listOrgAudit)
|
||||
|
||||
org.GET("/alert-groups", can(authz.NotificationsRead), s.listAlertGroups)
|
||||
|
||||
@@ -62,6 +62,7 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
hourly []store.HourlyBucketsRow
|
||||
regions []store.Region
|
||||
incidents []store.Incident
|
||||
updates []store.PublicIncidentUpdatesRow
|
||||
planned map[uuid.UUID][]store.MaintenanceWindow
|
||||
)
|
||||
now := time.Now()
|
||||
@@ -101,6 +102,13 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
if incidents, err = q.IncidentsForMonitors(ctx, store.IncidentsForMonitorsParams{Ids: ids, Since: ptr(now.Add(-incidentLookback))}); err != nil {
|
||||
return err
|
||||
}
|
||||
incidentIDs := make([]uuid.UUID, len(incidents))
|
||||
for i, inc := range incidents {
|
||||
incidentIDs[i] = inc.ID
|
||||
}
|
||||
if updates, err = q.PublicIncidentUpdates(ctx, incidentIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
if planned, err = maintenanceFor(ctx, q, ids); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -191,8 +199,15 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
snap.Sections = append(snap.Sections, buildPageSections(newPageGroupTree(pageGroups), byGroup)...)
|
||||
snap.Status = agentapi.Summarize(states)
|
||||
snap.Maintenance = pageMaintenance(planned, names, now)
|
||||
updatesOf := map[uuid.UUID][]agentapi.PageIncidentUpdate{}
|
||||
for _, u := range updates {
|
||||
updatesOf[u.IncidentID] = append(updatesOf[u.IncidentID], agentapi.PageIncidentUpdate{Status: u.Status, Message: u.Message, At: u.CreatedAt})
|
||||
}
|
||||
for _, i := range incidents {
|
||||
snap.Incidents = append(snap.Incidents, agentapi.PageIncident{Component: names[i.MonitorID], StartedAt: i.StartedAt, ResolvedAt: i.ResolvedAt})
|
||||
snap.Incidents = append(snap.Incidents, agentapi.PageIncident{
|
||||
Component: names[i.MonitorID], StartedAt: i.StartedAt, ResolvedAt: i.ResolvedAt,
|
||||
Title: i.Title, Status: incidentStatus(i), Updates: updatesOf[i.ID],
|
||||
})
|
||||
}
|
||||
return snap, nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user