feat: own incident statuses with a color; incidents before maintenance
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m38s
ci / test (pull_request) Successful in 14m21s
ci / build (pull_request) Successful in 16m36s
ci / image (pull_request) Successful in 3m37s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m38s
ci / test (pull_request) Successful in 14m21s
ci / build (pull_request) Successful in 16m36s
ci / image (pull_request) Successful in 3m37s
Organizations add statuses of their own, with a name and a color, next to the built-in ones (Settings, incident statuses; API under /org/incident-statuses, managed with org.manage). Updates pick them by ID; the console and status pages show them in their color. Renaming or recoloring carries into the history; a status in use cannot be deleted (409 status_in_use). API responses give status "custom" with status_id, status_name and status_color. Status pages now list recent incidents before planned maintenance. 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
43a04e99b1
commit
2409a919c3
26 files changed
+1257
-238
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"slices"
|
||||
@@ -19,19 +20,63 @@ import (
|
||||
|
||||
// Incidents open and close with their monitor. People explain them with a
|
||||
// title, a status and updates, public on status pages or internal notes.
|
||||
// Statuses are the built-in ones or the organization's own, with a color.
|
||||
|
||||
// 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"
|
||||
// customStatuses are an organization's own statuses by ID.
|
||||
type customStatuses map[uuid.UUID]store.IncidentStatus
|
||||
|
||||
func loadCustomStatuses(ctx context.Context, q *store.Queries) (customStatuses, error) {
|
||||
rows, err := q.ListIncidentStatuses(ctx)
|
||||
out := customStatuses{}
|
||||
for _, r := range rows {
|
||||
out[r.ID] = r
|
||||
}
|
||||
return deref(i.Status)
|
||||
return out, err
|
||||
}
|
||||
|
||||
// info describes a stored status: a built-in key, or custom with the
|
||||
// status named by id.
|
||||
func (cs customStatuses) info(key *string, id *uuid.UUID) web.IncidentStatusInfo {
|
||||
if key == nil {
|
||||
return web.IncidentStatusInfo{}
|
||||
}
|
||||
if *key == "custom" && id != nil {
|
||||
st := cs[*id]
|
||||
return web.IncidentStatusInfo{Key: "custom", Name: st.Name, Color: st.Color}
|
||||
}
|
||||
return web.IncidentStatusInfo{Key: *key}
|
||||
}
|
||||
|
||||
// of is what an incident shows: resolved once its monitor recovered,
|
||||
// otherwise the status of its latest update, empty before one.
|
||||
func (cs customStatuses) of(i store.Incident) web.IncidentStatusInfo {
|
||||
if i.ResolvedAt != nil {
|
||||
return web.IncidentStatusInfo{Key: "resolved"}
|
||||
}
|
||||
return cs.info(i.Status, i.StatusID)
|
||||
}
|
||||
|
||||
// statusFields are a status in API responses.
|
||||
type statusFields struct {
|
||||
// Status is investigating, identified, monitoring, resolved or custom.
|
||||
Status string `json:"status"`
|
||||
// StatusID, StatusName and StatusColor describe a custom status.
|
||||
StatusID *uuid.UUID `json:"status_id,omitempty"`
|
||||
StatusName string `json:"status_name,omitempty"`
|
||||
StatusColor string `json:"status_color,omitempty"`
|
||||
}
|
||||
|
||||
func toStatusFields(info web.IncidentStatusInfo, id *uuid.UUID) statusFields {
|
||||
f := statusFields{Status: info.Key}
|
||||
if info.Key == "custom" {
|
||||
f.StatusID, f.StatusName, f.StatusColor = id, info.Name, info.Color
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
type incidentUpdateView struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Status string `json:"status"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
statusFields
|
||||
Message string `json:"message"`
|
||||
Public bool `json:"public"`
|
||||
Author string `json:"author"`
|
||||
@@ -39,72 +84,81 @@ type incidentUpdateView struct {
|
||||
}
|
||||
|
||||
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"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
MonitorID uuid.UUID `json:"monitor_id"`
|
||||
Monitor string `json:"monitor"`
|
||||
Title string `json:"title"`
|
||||
statusFields
|
||||
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) {
|
||||
// incidentData is an incident with its updates and the organization's
|
||||
// statuses.
|
||||
type incidentData struct {
|
||||
inc store.GetIncidentRow
|
||||
updates []store.ListIncidentUpdatesRow
|
||||
statuses customStatuses
|
||||
}
|
||||
|
||||
// loadIncident reads the incident of the request.
|
||||
func (s *Server) loadIncident(c *gin.Context) (incidentData, bool) {
|
||||
var d incidentData
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return store.GetIncidentRow{}, nil, false
|
||||
return d, 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 {
|
||||
if d.inc, err = q.GetIncident(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
updates, err = q.ListIncidentUpdates(ctx, id)
|
||||
if d.updates, err = q.ListIncidentUpdates(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
d.statuses, err = loadCustomStatuses(ctx, q)
|
||||
return err
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return inc, nil, false
|
||||
return d, false
|
||||
}
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return inc, nil, false
|
||||
return d, false
|
||||
}
|
||||
return inc, updates, true
|
||||
return d, 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,
|
||||
Cause: r.Cause, Title: r.Title, Status: r.Status, StatusID: r.StatusID,
|
||||
}
|
||||
}
|
||||
|
||||
func toIncidentDetail(inc store.GetIncidentRow, updates []store.ListIncidentUpdatesRow) incidentDetailView {
|
||||
func (d incidentData) view() incidentDetailView {
|
||||
inc := incidentOf(d.inc)
|
||||
v := incidentDetailView{
|
||||
ID: inc.ID, MonitorID: inc.MonitorID, Monitor: inc.MonitorName, Title: inc.Title,
|
||||
Status: incidentStatus(incidentOf(inc)), StartedAt: inc.StartedAt, ResolvedAt: inc.ResolvedAt,
|
||||
ID: inc.ID, MonitorID: inc.MonitorID, Monitor: d.inc.MonitorName, Title: inc.Title,
|
||||
statusFields: toStatusFields(d.statuses.of(inc), inc.StatusID), StartedAt: inc.StartedAt, ResolvedAt: inc.ResolvedAt,
|
||||
Updates: []incidentUpdateView{},
|
||||
}
|
||||
for _, u := range updates {
|
||||
for _, u := range d.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,
|
||||
ID: u.ID, statusFields: toStatusFields(d.statuses.info(&u.Status, u.StatusID), u.StatusID),
|
||||
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))
|
||||
if d, ok := s.loadIncident(c); ok {
|
||||
c.JSON(http.StatusOK, d.view())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,23 +189,24 @@ func (s *Server) setIncidentTitle(c *gin.Context) {
|
||||
if !bind(c, &req) {
|
||||
return
|
||||
}
|
||||
inc, _, ok := s.loadIncident(c)
|
||||
if !ok || !s.mayWriteIncident(c, inc) {
|
||||
d, ok := s.loadIncident(c)
|
||||
if !ok || !s.mayWriteIncident(c, d.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)})
|
||||
_, err := q.SetIncidentTitle(ctx, store.SetIncidentTitleParams{ID: d.inc.ID, Title: strings.TrimSpace(req.Title)})
|
||||
return err
|
||||
}); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
auditDetail(c, "incident_id", inc.ID.String())
|
||||
auditDetail(c, "incident_id", d.inc.ID.String())
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
type incidentUpdateRequest struct {
|
||||
// Status is a built-in status or the ID of a custom one.
|
||||
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
|
||||
@@ -166,12 +221,23 @@ func (s *Server) addIncidentUpdate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
req.Message = strings.TrimSpace(req.Message)
|
||||
if !slices.Contains(web.IncidentStatuses, req.Status) || req.Message == "" {
|
||||
d, ok := s.loadIncident(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
key, statusID := req.Status, (*uuid.UUID)(nil)
|
||||
if id, err := uuid.Parse(req.Status); err == nil {
|
||||
if _, ok := d.statuses[id]; !ok {
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_input")
|
||||
return
|
||||
}
|
||||
key, statusID = "custom", &id
|
||||
}
|
||||
if (statusID == nil && !slices.Contains(web.IncidentStatuses, key)) || req.Message == "" {
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_input")
|
||||
return
|
||||
}
|
||||
inc, _, ok := s.loadIncident(c)
|
||||
if !ok || !s.mayWriteIncident(c, inc) {
|
||||
if !s.mayWriteIncident(c, d.inc) {
|
||||
return
|
||||
}
|
||||
public := req.Public == nil || *req.Public
|
||||
@@ -185,19 +251,22 @@ func (s *Server) addIncidentUpdate(c *gin.Context) {
|
||||
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,
|
||||
OrgID: p.Org.ID, IncidentID: d.inc.ID, Status: key, StatusID: statusID, Message: req.Message, Public: public, AuthorID: author,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return q.RefreshIncidentStatus(ctx, inc.ID)
|
||||
return q.RefreshIncidentStatus(ctx, d.inc.ID)
|
||||
}); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
auditDetail(c, "incident_id", inc.ID.String())
|
||||
auditDetail(c, "incident_id", d.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})
|
||||
c.JSON(http.StatusCreated, incidentUpdateView{
|
||||
ID: u.ID, statusFields: toStatusFields(d.statuses.info(&u.Status, u.StatusID), u.StatusID),
|
||||
Message: u.Message, Public: u.Public, CreatedAt: u.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) deleteIncidentUpdate(c *gin.Context) {
|
||||
@@ -206,18 +275,18 @@ func (s *Server) deleteIncidentUpdate(c *gin.Context) {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
inc, _, ok := s.loadIncident(c)
|
||||
if !ok || !s.mayWriteIncident(c, inc) {
|
||||
d, ok := s.loadIncident(c)
|
||||
if !ok || !s.mayWriteIncident(c, d.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 {
|
||||
if n, err = q.DeleteIncidentUpdate(ctx, store.DeleteIncidentUpdateParams{ID: update, IncidentID: d.inc.ID}); err != nil || n == 0 {
|
||||
return err
|
||||
}
|
||||
return q.RefreshIncidentStatus(ctx, inc.ID)
|
||||
return q.RefreshIncidentStatus(ctx, d.inc.ID)
|
||||
}); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
@@ -226,19 +295,19 @@ func (s *Server) deleteIncidentUpdate(c *gin.Context) {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
auditDetail(c, "incident_id", inc.ID.String())
|
||||
auditDetail(c, "incident_id", d.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)
|
||||
d, ok := s.loadIncident(c)
|
||||
if !ok {
|
||||
return nil, errAborted
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
d, err := s.loadMonitorData(ctx, p, web.RangeByKey(""))
|
||||
md, err := s.loadMonitorData(ctx, p, web.RangeByKey(""))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -246,9 +315,10 @@ func (s *Server) incidentDetailView(c *gin.Context, p *principal, page web.Page,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inc := incidentOf(d.inc)
|
||||
v := web.IncidentDetailView{
|
||||
Row: d.incidentRow(incidentOf(inc), inc.MonitorName),
|
||||
CanWrite: canInGroup(p, authz.IncidentsWrite, t, inc.MonitorGroupID),
|
||||
Row: md.incidentRow(inc, d.inc.MonitorName),
|
||||
CanWrite: canInGroup(p, authz.IncidentsWrite, t, d.inc.MonitorGroupID),
|
||||
}
|
||||
// Newest first; the monitor's own events among the updates.
|
||||
type entry struct {
|
||||
@@ -259,13 +329,14 @@ func (s *Server) incidentDetailView(c *gin.Context, p *principal, page web.Page,
|
||||
if inc.ResolvedAt != nil {
|
||||
entries = append(entries, entry{*inc.ResolvedAt, web.IncidentTimelineEntry{Kind: "resolved"}})
|
||||
}
|
||||
for _, u := range updates {
|
||||
for _, u := range d.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,
|
||||
Kind: "update", ID: u.ID.String(), Status: d.statuses.info(&u.Status, u.StatusID), HTML: html,
|
||||
Author: u.AuthorName, Public: u.Public,
|
||||
}})
|
||||
}
|
||||
slices.SortStableFunc(entries, func(a, b entry) int { return b.at.Compare(a.at) })
|
||||
@@ -273,16 +344,23 @@ func (s *Server) incidentDetailView(c *gin.Context, p *principal, page web.Page,
|
||||
x.e.At = x.at.UTC().Format(timeLayout)
|
||||
v.Timeline = append(v.Timeline, x.e)
|
||||
}
|
||||
v.NextStatus = nextIncidentStatus(v.Row.Status)
|
||||
v.NextStatus = nextIncidentStatus(inc)
|
||||
for _, st := range d.statuses {
|
||||
v.Custom = append(v.Custom, web.CustomIncidentStatus{ID: st.ID.String(), Name: st.Name, Color: st.Color})
|
||||
}
|
||||
slices.SortFunc(v.Custom, func(a, b web.CustomIncidentStatus) int { return strings.Compare(a.Name, b.Name) })
|
||||
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 {
|
||||
// nextIncidentStatus suggests the status of the next update: one built-in
|
||||
// step further, the same custom status, resolved at the end.
|
||||
func nextIncidentStatus(inc store.Incident) string {
|
||||
switch {
|
||||
case inc.Status == nil:
|
||||
return web.IncidentStatuses[0]
|
||||
case *inc.Status == "custom" && inc.StatusID != nil:
|
||||
return inc.StatusID.String()
|
||||
}
|
||||
i := slices.Index(web.IncidentStatuses, *inc.Status)
|
||||
return web.IncidentStatuses[min(i+1, len(web.IncidentStatuses)-1)]
|
||||
}
|
||||
Reference in new issue
Block a user