feat(statuspage): add nested status page groups and optional component details
This commit is contained in:
1 parent
1c31003baf
commit
9b26b4150e
26 files changed
+4566
-760
No files matched your search
+217
-34
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/authz"
|
||||
@@ -42,17 +44,25 @@ var (
|
||||
// buildSnapshot computes what the public sees of page.
|
||||
func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agentapi.PageSnapshot, error) {
|
||||
snap := agentapi.PageSnapshot{
|
||||
Slug: page.Slug, Title: page.Title, Description: page.Description,
|
||||
Sections: []agentapi.Section{}, Incidents: []agentapi.PageIncident{}, GeneratedAt: time.Now(),
|
||||
Slug: page.Slug,
|
||||
Title: page.Title,
|
||||
Description: page.Description,
|
||||
ShowDetails: page.ShowComponentDetails,
|
||||
Sections: []agentapi.Section{},
|
||||
Incidents: []agentapi.PageIncident{},
|
||||
GeneratedAt: time.Now(),
|
||||
}
|
||||
_ = json.Unmarshal(page.Theme, &snap.Theme)
|
||||
var (
|
||||
listed []store.StatusPageMonitorsRow
|
||||
latest []store.LatestResultsRow
|
||||
open []store.Incident
|
||||
daily []store.DailyBucketsForMonitorsRow
|
||||
incidents []store.Incident
|
||||
planned map[uuid.UUID][]store.MaintenanceWindow
|
||||
listed []store.StatusPageMonitorsRow
|
||||
pageGroups []store.StatusPageGroup
|
||||
latest []store.LatestResultsRow
|
||||
open []store.Incident
|
||||
daily []store.DailyBucketsForMonitorsRow
|
||||
hourly []store.HourlyBucketsRow
|
||||
regions []store.Region
|
||||
incidents []store.Incident
|
||||
planned map[uuid.UUID][]store.MaintenanceWindow
|
||||
)
|
||||
now := time.Now()
|
||||
err := s.db.Tx(ctx, tenant.Org(page.OrgID, uuid.Nil), func(q *store.Queries) error {
|
||||
@@ -60,6 +70,9 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
if listed, err = q.StatusPageMonitors(ctx, page.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
if pageGroups, err = q.ListStatusPageGroups(ctx, page.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
ids := make([]uuid.UUID, len(listed))
|
||||
for i, m := range listed {
|
||||
ids[i] = m.MonitorID
|
||||
@@ -75,6 +88,16 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if page.ShowComponentDetails {
|
||||
if hourly, err = q.HourlyBuckets(ctx, store.HourlyBucketsParams{
|
||||
OrgID: page.OrgID, Bucket: now.UTC().Add(-24 * time.Hour),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if regions, err = q.ListVisibleRegions(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if incidents, err = q.IncidentsForMonitors(ctx, store.IncidentsForMonitorsParams{Ids: ids, Since: ptr(now.Add(-incidentLookback))}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -90,11 +113,13 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
}
|
||||
failing := map[uuid.UUID]int{}
|
||||
reported := map[uuid.UUID]int{}
|
||||
latestByMonitor := map[uuid.UUID][]store.LatestResultsRow{}
|
||||
for _, l := range latest {
|
||||
reported[l.MonitorID]++
|
||||
if !l.Ok {
|
||||
failing[l.MonitorID]++
|
||||
}
|
||||
latestByMonitor[l.MonitorID] = append(latestByMonitor[l.MonitorID], l)
|
||||
}
|
||||
down := map[uuid.UUID]bool{}
|
||||
for _, i := range open {
|
||||
@@ -104,15 +129,34 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
for _, d := range daily {
|
||||
days[d.MonitorID] = append(days[d.MonitorID], web.Bucket{Start: d.Bucket, Total: d.Total, OK: d.OkCount})
|
||||
}
|
||||
hourlyByMonitor := map[uuid.UUID][]web.Bucket{}
|
||||
for _, h := range hourly {
|
||||
hourlyByMonitor[h.MonitorID] = append(hourlyByMonitor[h.MonitorID], web.Bucket{
|
||||
Start: h.Bucket, Total: h.Total, OK: h.OkCount, Latency: h.AvgLatencyMs,
|
||||
})
|
||||
}
|
||||
regionNames := make(map[string]string, len(regions))
|
||||
for _, r := range regions {
|
||||
regionNames[r.ID.String()] = r.Name
|
||||
}
|
||||
|
||||
names := map[uuid.UUID]string{}
|
||||
var states []string
|
||||
var ungrouped []agentapi.Component
|
||||
byGroup := map[uuid.UUID][]agentapi.Component{}
|
||||
for _, m := range listed {
|
||||
name := m.Label
|
||||
if name == "" {
|
||||
name = m.MonitorName
|
||||
}
|
||||
names[m.MonitorID] = name
|
||||
comp := agentapi.Component{Name: name, Uptime: web.Uptime(days[m.MonitorID]), Days: dayHistory(days[m.MonitorID], now)}
|
||||
uptime90 := web.Uptime(days[m.MonitorID])
|
||||
comp := agentapi.Component{
|
||||
ID: m.MonitorID.String(),
|
||||
Name: name,
|
||||
Uptime: uptime90,
|
||||
Days: dayHistory(days[m.MonitorID], now),
|
||||
}
|
||||
switch {
|
||||
case down[m.MonitorID]:
|
||||
comp.Status = agentapi.PageOutage
|
||||
@@ -123,13 +167,27 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
default:
|
||||
comp.Status = agentapi.PageOperational
|
||||
}
|
||||
if page.ShowComponentDetails {
|
||||
comp.Details = buildComponentDetails(
|
||||
days[m.MonitorID],
|
||||
hourlyByMonitor[m.MonitorID],
|
||||
latestByMonitor[m.MonitorID],
|
||||
regionNames,
|
||||
uptime90,
|
||||
now,
|
||||
)
|
||||
}
|
||||
states = append(states, comp.Status)
|
||||
if n := len(snap.Sections); n == 0 || snap.Sections[n-1].Name != m.Section {
|
||||
snap.Sections = append(snap.Sections, agentapi.Section{Name: m.Section})
|
||||
if m.GroupID != nil {
|
||||
byGroup[*m.GroupID] = append(byGroup[*m.GroupID], comp)
|
||||
} else {
|
||||
ungrouped = append(ungrouped, comp)
|
||||
}
|
||||
last := &snap.Sections[len(snap.Sections)-1]
|
||||
last.Components = append(last.Components, comp)
|
||||
}
|
||||
if len(ungrouped) > 0 {
|
||||
snap.Sections = append(snap.Sections, agentapi.Section{Name: "", Components: ungrouped})
|
||||
}
|
||||
snap.Sections = append(snap.Sections, buildPageSections(newPageGroupTree(pageGroups), byGroup)...)
|
||||
snap.Status = agentapi.Summarize(states)
|
||||
snap.Maintenance = pageMaintenance(planned, names, now)
|
||||
for _, i := range incidents {
|
||||
@@ -138,6 +196,102 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
return snap, nil
|
||||
}
|
||||
|
||||
// buildPageSections turns a status page's group tree and grouped components
|
||||
// into recursive public sections, omitting subtrees with no components.
|
||||
func buildPageSections(tree pageGroupTree, byGroup map[uuid.UUID][]agentapi.Component) []agentapi.Section {
|
||||
var build func(id uuid.UUID) (agentapi.Section, []string)
|
||||
build = func(id uuid.UUID) (agentapi.Section, []string) {
|
||||
g := tree.byID[id]
|
||||
sec := agentapi.Section{Name: g.Name, Components: byGroup[id]}
|
||||
var states []string
|
||||
for _, c := range sec.Components {
|
||||
states = append(states, c.Status)
|
||||
}
|
||||
sec.Total = len(sec.Components)
|
||||
for _, childID := range tree.children[id] {
|
||||
childSec, childStates := build(childID)
|
||||
if childSec.Total == 0 {
|
||||
continue
|
||||
}
|
||||
sec.Children = append(sec.Children, childSec)
|
||||
sec.Total += childSec.Total
|
||||
states = append(states, childStates...)
|
||||
}
|
||||
if sec.Total > 0 {
|
||||
sec.Status = agentapi.Summarize(states)
|
||||
}
|
||||
return sec, states
|
||||
}
|
||||
var out []agentapi.Section
|
||||
for _, rootID := range tree.roots {
|
||||
sec, _ := build(rootID)
|
||||
if sec.Total > 0 {
|
||||
out = append(out, sec)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func buildComponentDetails(
|
||||
daily []web.Bucket,
|
||||
hourly []web.Bucket,
|
||||
latest []store.LatestResultsRow,
|
||||
regionNames map[string]string,
|
||||
uptime90 float64,
|
||||
now time.Time,
|
||||
) *agentapi.ComponentDetails {
|
||||
since7d := now.UTC().AddDate(0, 0, -7)
|
||||
since30d := now.UTC().AddDate(0, 0, -30)
|
||||
var d7, d30 []web.Bucket
|
||||
for _, b := range daily {
|
||||
if !b.Start.Before(since30d) {
|
||||
d30 = append(d30, b)
|
||||
}
|
||||
if !b.Start.Before(since7d) {
|
||||
d7 = append(d7, b)
|
||||
}
|
||||
}
|
||||
byHour := make(map[int64]web.Bucket, len(hourly))
|
||||
for _, b := range hourly {
|
||||
byHour[web.AlignBucket(b.Start.UTC(), time.Hour).Unix()] = b
|
||||
}
|
||||
endHour := web.AlignBucket(now.UTC(), time.Hour)
|
||||
hours := make([]agentapi.HourBucket, 24)
|
||||
for i := range hours {
|
||||
ts := endHour.Add(-time.Duration(23-i) * time.Hour)
|
||||
hours[i] = agentapi.HourBucket{Time: ts, Uptime: -1}
|
||||
if b, ok := byHour[ts.Unix()]; ok && b.Total > 0 {
|
||||
hours[i].Uptime = float64(b.OK) / float64(b.Total)
|
||||
hours[i].AvgLatencyMS = int(math.Round(float64(b.Latency)))
|
||||
}
|
||||
}
|
||||
regs := make([]agentapi.ComponentRegion, 0, len(latest))
|
||||
for _, l := range latest {
|
||||
name := regionNames[l.Region]
|
||||
if name == "" {
|
||||
name = l.Region
|
||||
}
|
||||
status := agentapi.PageOperational
|
||||
if !l.Ok {
|
||||
status = agentapi.PageOutage
|
||||
}
|
||||
var latency int
|
||||
if l.LatencyMs != nil && *l.LatencyMs > 0 {
|
||||
latency = int(*l.LatencyMs)
|
||||
}
|
||||
regs = append(regs, agentapi.ComponentRegion{Name: name, Status: status, LatencyMS: latency})
|
||||
}
|
||||
slices.SortFunc(regs, func(a, b agentapi.ComponentRegion) int { return strings.Compare(a.Name, b.Name) })
|
||||
return &agentapi.ComponentDetails{
|
||||
SLA24h: web.Uptime(hourly),
|
||||
SLA7d: web.Uptime(d7),
|
||||
SLA30d: web.Uptime(d30),
|
||||
SLA90d: uptime90,
|
||||
Hours: hours,
|
||||
Regions: regs,
|
||||
}
|
||||
}
|
||||
|
||||
func dayHistory(buckets []web.Bucket, now time.Time) []agentapi.Day {
|
||||
byDay := map[string]web.Bucket{}
|
||||
for _, b := range buckets {
|
||||
@@ -192,11 +346,12 @@ func (s *Server) agentPage(c *gin.Context) {
|
||||
}
|
||||
|
||||
type statusPageRequest struct {
|
||||
Slug string `json:"slug" binding:"required,max=63"`
|
||||
Title string `json:"title" binding:"required,max=200"`
|
||||
Description string `json:"description" binding:"max=10000"`
|
||||
Published bool `json:"published"`
|
||||
Theme agentapi.Theme `json:"theme"`
|
||||
Slug string `json:"slug" binding:"required,max=63"`
|
||||
Title string `json:"title" binding:"required,max=200"`
|
||||
Description string `json:"description" binding:"max=10000"`
|
||||
Published bool `json:"published"`
|
||||
ShowComponentDetails bool `json:"show_component_details"`
|
||||
Theme agentapi.Theme `json:"theme"`
|
||||
}
|
||||
|
||||
func (r *statusPageRequest) normalize() string {
|
||||
@@ -226,14 +381,15 @@ func (r *statusPageRequest) normalize() string {
|
||||
}
|
||||
|
||||
type statusPageView struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Published bool `json:"published"`
|
||||
Theme agentapi.Theme `json:"theme"`
|
||||
CustomDomain *string `json:"custom_domain"`
|
||||
DomainVerified bool `json:"domain_verified"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Published bool `json:"published"`
|
||||
ShowComponentDetails bool `json:"show_component_details"`
|
||||
Theme agentapi.Theme `json:"theme"`
|
||||
CustomDomain *string `json:"custom_domain"`
|
||||
DomainVerified bool `json:"domain_verified"`
|
||||
// DomainRecord is the TXT record that proves control of CustomDomain.
|
||||
DomainRecord *dnsRecord `json:"domain_record,omitempty"`
|
||||
}
|
||||
@@ -246,7 +402,8 @@ type dnsRecord struct {
|
||||
|
||||
func viewStatusPage(p store.StatusPage) statusPageView {
|
||||
v := statusPageView{
|
||||
ID: p.ID, Slug: p.Slug, Title: p.Title, Description: p.Description, Published: p.Published,
|
||||
ID: p.ID, Slug: p.Slug, Title: p.Title, Description: p.Description,
|
||||
Published: p.Published, ShowComponentDetails: p.ShowComponentDetails,
|
||||
CustomDomain: p.CustomDomain, DomainVerified: p.DomainVerifiedAt != nil,
|
||||
}
|
||||
_ = json.Unmarshal(p.Theme, &v.Theme)
|
||||
@@ -313,7 +470,8 @@ func (s *Server) createStatusPage(c *gin.Context) {
|
||||
var err error
|
||||
row, err = q.CreateStatusPage(ctx, store.CreateStatusPageParams{
|
||||
OrgID: p.Org.ID, Slug: req.Slug, Title: req.Title, Description: req.Description,
|
||||
Published: req.Published, DomainToken: newDomainToken(), Theme: theme,
|
||||
Published: req.Published, ShowComponentDetails: req.ShowComponentDetails,
|
||||
DomainToken: newDomainToken(), Theme: theme,
|
||||
})
|
||||
return err
|
||||
})
|
||||
@@ -351,7 +509,8 @@ func (s *Server) updateStatusPage(c *gin.Context) {
|
||||
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
var err error
|
||||
row, err = q.UpdateStatusPage(ctx, store.UpdateStatusPageParams{
|
||||
ID: id, Slug: req.Slug, Title: req.Title, Description: req.Description, Published: req.Published, Theme: theme,
|
||||
ID: id, Slug: req.Slug, Title: req.Title, Description: req.Description,
|
||||
Published: req.Published, ShowComponentDetails: req.ShowComponentDetails, Theme: theme,
|
||||
})
|
||||
return err
|
||||
})
|
||||
@@ -389,9 +548,10 @@ func (s *Server) deleteStatusPage(c *gin.Context) {
|
||||
}
|
||||
|
||||
type pageMonitor struct {
|
||||
MonitorID uuid.UUID `json:"monitor_id" binding:"required"`
|
||||
Section string `json:"section" binding:"max=100"`
|
||||
Label string `json:"label" binding:"max=200"`
|
||||
MonitorID uuid.UUID `json:"monitor_id" binding:"required"`
|
||||
GroupID *uuid.UUID `json:"group_id"`
|
||||
Section string `json:"section" binding:"max=100"`
|
||||
Label string `json:"label" binding:"max=200"`
|
||||
}
|
||||
|
||||
type pageMonitorsRequest struct {
|
||||
@@ -412,13 +572,33 @@ func (s *Server) setPageMonitors(c *gin.Context) {
|
||||
if _, err := q.GetStatusPage(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
existingGroups, err := q.ListStatusPageGroups(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
groupsByID := make(map[uuid.UUID]store.StatusPageGroup, len(existingGroups))
|
||||
topByName := map[string]store.StatusPageGroup{}
|
||||
var nextPos int32
|
||||
for _, g := range existingGroups {
|
||||
groupsByID[g.ID] = g
|
||||
if g.ParentID == nil {
|
||||
topByName[g.Name] = g
|
||||
if g.Position >= nextPos {
|
||||
nextPos = g.Position + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := q.ClearStatusPageMonitors(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
for i, m := range req.Monitors {
|
||||
err := q.AddStatusPageMonitor(ctx, store.AddStatusPageMonitorParams{
|
||||
groupID, section, err := resolvePageMonitorGroup(ctx, q, id, p.Org.ID, m, groupsByID, topByName, &nextPos)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = q.AddStatusPageMonitor(ctx, store.AddStatusPageMonitorParams{
|
||||
PageID: id, MonitorID: m.MonitorID, OrgID: p.Org.ID,
|
||||
Section: strings.TrimSpace(m.Section), Label: strings.TrimSpace(m.Label), Position: int32(i),
|
||||
Section: section, GroupID: groupID, Label: strings.TrimSpace(m.Label), Position: int32(i),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -426,9 +606,12 @@ func (s *Server) setPageMonitors(c *gin.Context) {
|
||||
}
|
||||
return nil
|
||||
})
|
||||
var pgErr *pgconn.PgError
|
||||
switch {
|
||||
case errors.Is(err, pgx.ErrNoRows):
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
case errors.As(err, &pgErr) && pgErr.Code == "23503" && strings.Contains(pgErr.ConstraintName, "group_id"):
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_group")
|
||||
case isForeignKeyViolation(err), isUniqueViolation(err, "status_page_monitors_pkey"):
|
||||
// Unknown monitors, monitors of other orgs and duplicates.
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_monitor")
|
||||
|
||||
Reference in new issue
Block a user