Files
uptimesebi/internal/agentapi/pages.go
T
Sebastian UnterschützandClaude Opus 5.5 43b0a35f20 feat: maintenance windows and Markdown descriptions
Monitors and groups get one-off or recurring maintenance. While it is in
progress, failures neither open incidents nor alert, so the SLA is not
affected; successes still resolve earlier incidents. The console shows
monitors under maintenance and lists windows on a new page and on
monitor pages. Status pages announce maintenance in progress and planned
for the next two weeks and mark affected components; the occurrences are
part of the snapshot and evaluated when the page is shown, so this also
works from cache during an API outage. Maintenance and status page
descriptions are rendered as Markdown.

Closes #71

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 10:38:31 +01:00

203 lines
5.9 KiB
Go

package agentapi
import (
"context"
"errors"
"net/http"
"net/url"
"time"
)
// Page states, from best to worst.
const (
PageOperational = "operational"
PageDegraded = "degraded"
PageOutage = "outage"
PageUnknown = "unknown"
// PageUnderMaintenance marks planned maintenance in progress.
PageUnderMaintenance = "maintenance"
)
// Theme customizes a page.
type Theme struct {
// Accent is a #rrggbb color for headings and links.
Accent string `json:"accent,omitempty"`
LogoURL string `json:"logo_url,omitempty"`
// CSS is appended to the page's styles.
CSS string `json:"css,omitempty"`
}
// Day is one bar of a component's 90-day history. Uptime is -1 without data.
type Day struct {
Date string `json:"date"`
Uptime float64 `json:"uptime"`
}
// 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"`
}
// Section groups components under a heading; the first may be unnamed.
type Section struct {
Name string `json:"name"`
Components []Component `json:"components"`
}
// PageIncident is an incident of a listed component.
type PageIncident struct {
Component string `json:"component"`
StartedAt time.Time `json:"started_at"`
ResolvedAt *time.Time `json:"resolved_at,omitempty"`
}
// PageSnapshot is everything needed to render a status page.
type PageSnapshot struct {
Slug string `json:"slug"`
Title string `json:"title"`
Description string `json:"description"`
Theme Theme `json:"theme"`
Status string `json:"status"`
Sections []Section `json:"sections"`
Incidents []PageIncident `json:"incidents"`
GeneratedAt time.Time `json:"generated_at"`
// Legal holds the legal notice and privacy policy shown on the page.
// They travel with the snapshot so they stay available during an API
// outage, which some jurisdictions require.
Legal []LegalDoc `json:"legal,omitempty"`
// Maintenance lists occurrences of planned maintenance from shortly
// before generation to two weeks ahead. Whether one is in progress is
// decided when the page is shown, see WithMaintenance, so a cached
// snapshot stays right while the API is unreachable.
Maintenance []PageMaintenance `json:"maintenance,omitempty"`
}
// PageMaintenance is one occurrence of planned maintenance.
type PageMaintenance struct {
Title string `json:"title"`
// Description is Markdown.
Description string `json:"description,omitempty"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
// Components are the names of the affected components.
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.
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
for _, s := range states {
if rank[s] > rank[worst] {
worst = s
}
allUnknown = allUnknown && s == PageUnknown
allDown = allDown && s == PageOutage
}
switch {
case allUnknown:
return PageUnknown
case worst == PageOutage && !allDown:
return PageDegraded
}
return worst
}
// WithMaintenance returns s as of now: components under maintenance in
// progress show as such and the page state leaves them out.
func (s PageSnapshot) WithMaintenance(now time.Time) PageSnapshot {
affected := map[string]bool{}
for _, m := range s.Maintenance {
if !now.Before(m.Start) && now.Before(m.End) {
for _, c := range m.Components {
affected[c] = true
}
}
}
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 {
if affected[c.Name] {
c.Status = PageUnderMaintenance
} else {
states = append(states, c.Status)
}
sections[i].Components[j] = c
}
}
s.Sections = sections
s.Status = Summarize(states)
if len(states) == 0 || s.Status == PageOperational {
s.Status = PageUnderMaintenance
}
return s
}
// Ongoing returns occurrences in progress at now.
func (s PageSnapshot) Ongoing(now time.Time) []PageMaintenance {
var out []PageMaintenance
for _, m := range s.Maintenance {
if !now.Before(m.Start) && now.Before(m.End) {
out = append(out, m)
}
}
return out
}
// Upcoming returns occurrences that have not started at now.
func (s PageSnapshot) Upcoming(now time.Time) []PageMaintenance {
var out []PageMaintenance
for _, m := range s.Maintenance {
if now.Before(m.Start) {
out = append(out, m)
}
}
return out
}
// LegalDoc is a legal notice ("imprint") or privacy policy ("privacy"):
// a link to a page elsewhere or Markdown to render.
type LegalDoc struct {
Kind string `json:"kind"`
URL string `json:"url,omitempty"`
Markdown string `json:"markdown,omitempty"`
}
// LegalDocByKind returns the document of kind, if any.
func (s PageSnapshot) LegalDocByKind(kind string) (LegalDoc, bool) {
for _, d := range s.Legal {
if d.Kind == kind {
return d, true
}
}
return LegalDoc{}, false
}
// ErrNotFound means no published page matches.
var ErrNotFound = errors.New("agentapi: not found")
// Page fetches the published page for a custom domain host, or for slug if
// host is empty.
func (c *Client) Page(ctx context.Context, host, slug string) (PageSnapshot, error) {
q := url.Values{}
if host != "" {
q.Set("host", host)
} else {
q.Set("slug", slug)
}
var p PageSnapshot
err := c.do(ctx, http.MethodGet, "/pages?"+q.Encode(), nil, &p)
return p, err
}