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
+148
-20
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -33,19 +34,53 @@ type Day struct {
|
||||
Uptime float64 `json:"uptime"`
|
||||
}
|
||||
|
||||
// HourBucket is one hourly summary of a component's recent 24-hour history.
|
||||
// Uptime is -1 when no checks were recorded during that hour.
|
||||
type HourBucket struct {
|
||||
Time time.Time `json:"time"`
|
||||
Uptime float64 `json:"uptime"`
|
||||
AvgLatencyMS int `json:"avg_latency_ms"`
|
||||
}
|
||||
|
||||
// ComponentRegion is the public status of one monitoring region for a
|
||||
// component. Error details and checker IDs stay private.
|
||||
type ComponentRegion struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
LatencyMS int `json:"latency_ms,omitempty"`
|
||||
}
|
||||
|
||||
// ComponentDetails holds the public metrics shown on a component's detail page
|
||||
// when the status page opts in via ShowDetails.
|
||||
type ComponentDetails struct {
|
||||
SLA24h float64 `json:"sla_24h"`
|
||||
SLA7d float64 `json:"sla_7d"`
|
||||
SLA30d float64 `json:"sla_30d"`
|
||||
SLA90d float64 `json:"sla_90d"`
|
||||
Hours []HourBucket `json:"hours,omitempty"`
|
||||
Regions []ComponentRegion `json:"regions,omitempty"`
|
||||
}
|
||||
|
||||
// Component is one monitor as the public sees it. Targets, check details
|
||||
// and error messages stay private.
|
||||
type Component struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Uptime float64 `json:"uptime"`
|
||||
Days []Day `json:"days"`
|
||||
// ID identifies the component on detail URLs when ShowDetails is enabled.
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Uptime float64 `json:"uptime"`
|
||||
Days []Day `json:"days"`
|
||||
Details *ComponentDetails `json:"details,omitempty"`
|
||||
}
|
||||
|
||||
// Section groups components under a heading; the first may be unnamed.
|
||||
// Section groups components and nested child sections under a heading; a
|
||||
// top-level section with an empty Name holds ungrouped components.
|
||||
type Section struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status,omitempty"`
|
||||
Total int `json:"total,omitempty"`
|
||||
Components []Component `json:"components"`
|
||||
Children []Section `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
// PageIncident is an incident of a listed component.
|
||||
@@ -62,6 +97,7 @@ type PageSnapshot struct {
|
||||
Description string `json:"description"`
|
||||
Theme Theme `json:"theme"`
|
||||
Status string `json:"status"`
|
||||
ShowDetails bool `json:"show_details,omitempty"`
|
||||
Sections []Section `json:"sections"`
|
||||
Incidents []PageIncident `json:"incidents"`
|
||||
GeneratedAt time.Time `json:"generated_at"`
|
||||
@@ -87,9 +123,9 @@ type PageMaintenance struct {
|
||||
Components []string `json:"components"`
|
||||
}
|
||||
|
||||
// Summarize derives a page state from component states. Unknown components
|
||||
// do not make a page look broken, and a single failed component is a partial
|
||||
// problem: only when all fail is it an outage.
|
||||
// Summarize derives a page or group state from component states. Unknown
|
||||
// components do not make a page look broken, and a single failed component is
|
||||
// a partial problem: only when all fail is it an outage.
|
||||
func Summarize(states []string) string {
|
||||
rank := map[string]int{PageOperational: 0, PageUnknown: 0, PageDegraded: 1, PageOutage: 2}
|
||||
worst, allUnknown, allDown := PageOperational, true, len(states) > 0
|
||||
@@ -109,8 +145,22 @@ func Summarize(states []string) string {
|
||||
return worst
|
||||
}
|
||||
|
||||
func summarizeWithMaintenance(states []string, hasMaintenance bool) string {
|
||||
if len(states) == 0 {
|
||||
if hasMaintenance {
|
||||
return PageUnderMaintenance
|
||||
}
|
||||
return PageUnknown
|
||||
}
|
||||
status := Summarize(states)
|
||||
if hasMaintenance && (status == PageOperational || status == PageUnknown) {
|
||||
return PageUnderMaintenance
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
// WithMaintenance returns s as of now: components under maintenance in
|
||||
// progress show as such and the page state leaves them out.
|
||||
// progress show as such and the page and group states reflect it.
|
||||
func (s PageSnapshot) WithMaintenance(now time.Time) PageSnapshot {
|
||||
affected := map[string]bool{}
|
||||
for _, m := range s.Maintenance {
|
||||
@@ -123,27 +173,105 @@ func (s PageSnapshot) WithMaintenance(now time.Time) PageSnapshot {
|
||||
if len(affected) == 0 {
|
||||
return s
|
||||
}
|
||||
sections := make([]Section, len(s.Sections))
|
||||
var states []string
|
||||
for i, sec := range s.Sections {
|
||||
sections[i] = Section{Name: sec.Name, Components: make([]Component, len(sec.Components))}
|
||||
for j, c := range sec.Components {
|
||||
var allStates []string
|
||||
var anyMaint bool
|
||||
var apply func(src Section) (Section, []string, bool)
|
||||
apply = func(src Section) (Section, []string, bool) {
|
||||
dst := Section{
|
||||
Name: src.Name,
|
||||
Total: src.Total,
|
||||
Components: make([]Component, len(src.Components)),
|
||||
}
|
||||
var subStates []string
|
||||
subMaint := false
|
||||
for i, c := range src.Components {
|
||||
if affected[c.Name] {
|
||||
c.Status = PageUnderMaintenance
|
||||
subMaint = true
|
||||
} else {
|
||||
states = append(states, c.Status)
|
||||
subStates = append(subStates, c.Status)
|
||||
}
|
||||
sections[i].Components[j] = c
|
||||
dst.Components[i] = c
|
||||
}
|
||||
if len(src.Children) > 0 {
|
||||
dst.Children = make([]Section, len(src.Children))
|
||||
for i, child := range src.Children {
|
||||
var childStates []string
|
||||
var childMaint bool
|
||||
dst.Children[i], childStates, childMaint = apply(child)
|
||||
subStates = append(subStates, childStates...)
|
||||
subMaint = subMaint || childMaint
|
||||
}
|
||||
}
|
||||
if dst.Name != "" {
|
||||
dst.Status = summarizeWithMaintenance(subStates, subMaint)
|
||||
}
|
||||
return dst, subStates, subMaint
|
||||
}
|
||||
|
||||
sections := make([]Section, len(s.Sections))
|
||||
for i, sec := range s.Sections {
|
||||
var states []string
|
||||
var maint bool
|
||||
sections[i], states, maint = apply(sec)
|
||||
allStates = append(allStates, states...)
|
||||
anyMaint = anyMaint || maint
|
||||
}
|
||||
s.Sections = sections
|
||||
s.Status = Summarize(states)
|
||||
if len(states) == 0 || s.Status == PageOperational {
|
||||
s.Status = PageUnderMaintenance
|
||||
}
|
||||
s.Status = summarizeWithMaintenance(allStates, anyMaint)
|
||||
return s
|
||||
}
|
||||
|
||||
// ComponentByID finds a component anywhere in s.Sections by its ID.
|
||||
func (s PageSnapshot) ComponentByID(id string) (Component, bool) {
|
||||
if id == "" {
|
||||
return Component{}, false
|
||||
}
|
||||
var find func(secs []Section) (Component, bool)
|
||||
find = func(secs []Section) (Component, bool) {
|
||||
for _, sec := range secs {
|
||||
for _, c := range sec.Components {
|
||||
if c.ID == id {
|
||||
return c, true
|
||||
}
|
||||
}
|
||||
if c, ok := find(sec.Children); ok {
|
||||
return c, true
|
||||
}
|
||||
}
|
||||
return Component{}, false
|
||||
}
|
||||
return find(s.Sections)
|
||||
}
|
||||
|
||||
// IncidentsForComponent returns incidents belonging to componentName.
|
||||
func (s PageSnapshot) IncidentsForComponent(componentName string) []PageIncident {
|
||||
var out []PageIncident
|
||||
for _, inc := range s.Incidents {
|
||||
if inc.Component == componentName {
|
||||
out = append(out, inc)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// MaintenanceForComponent splits planned maintenance affecting componentName
|
||||
// into ongoing and upcoming occurrences at now.
|
||||
func (s PageSnapshot) MaintenanceForComponent(componentName string, now time.Time) (ongoing, upcoming []PageMaintenance) {
|
||||
for _, m := range s.Maintenance {
|
||||
if !slices.Contains(m.Components, componentName) {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case !now.Before(m.Start) && now.Before(m.End):
|
||||
ongoing = append(ongoing, m)
|
||||
case now.Before(m.Start):
|
||||
upcoming = append(upcoming, m)
|
||||
}
|
||||
}
|
||||
return ongoing, upcoming
|
||||
}
|
||||
|
||||
// Ongoing returns occurrences in progress at now.
|
||||
func (s PageSnapshot) Ongoing(now time.Time) []PageMaintenance {
|
||||
var out []PageMaintenance
|
||||
|
||||
Reference in new issue
Block a user