Files
uptimesebi/internal/web/public.go
T
Sebastian UnterschützandClaude Opus 5.5 cca4e80e54
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / test (pull_request) Successful in 9m5s
ci / lint (pull_request) Successful in 10m24s
ci / build (pull_request) Successful in 18m10s
ci / image (pull_request) Successful in 5m16s
feat(statuspages): email subscriptions, RSS feed and a JSON API
Visitors can subscribe to a page's incidents by email where the
organization allows it. They confirm their address first; confirmation
and unsubscribe links only show a button, so mail scanners do not act on
them, and mails carry RFC 8058 one-click unsubscribe headers. Subscribers
get a mail when an incident opens or resolves and for every public
update.

Every page also has an RSS feed of incidents and planned maintenance and
a read-only JSON API (status, summary, components, incidents), both
served from the cached snapshot.

Abuse protection: status page servers limit feed and API reads,
subscription requests and link actions per visitor; the API limits
subscription requests per visitor, mailbox and page, and a new limit caps
the subscriptions an organization's pages gain in seven days (platform
default and per-organization override, 1000 in the cloud). Unconfirmed
subscriptions count towards it and are deleted after a week.

The fixed cap of 200 monitors per status page is gone; the configurable
limit alone decides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-26 18:18:08 +01:00

181 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package web
import (
"fmt"
"io/fs"
"strconv"
"strings"
"time"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
)
// PublicAssets decides how a public status page loads its stylesheet:
// linked from the status page server or inlined for a static export.
type PublicAssets struct {
CSSHref string
InlineCSS string
// StaleSince, if set, tells visitors that live updates are unavailable
// and the page shows the state of that moment.
StaleSince time.Time
// LegalBase prefixes links to legal documents served as Markdown, e.g.
// "/acme/" or "/" on a custom domain. Empty leaves them out, as in a
// single-file export, which can only link elsewhere.
LegalBase string
// ComponentBase prefixes links to component detail pages, e.g. "/acme/"
// or "/". Empty in static single-file exports where subpages do not exist.
ComponentBase string
// Now is the moment the page shows, deciding which maintenance is in
// progress.
Now time.Time
// NoRefresh leaves out the periodic reload, on pages with a form.
NoRefresh bool
}
// NoticeForm is the button of a notice page that acts on a mailed link,
// so mail scanners opening the link do not act on it themselves.
type NoticeForm struct {
Action, Token, Button string
}
// Markdown renders src for display, or returns "" when it cannot.
func Markdown(src string) string {
html, err := RenderMarkdown(src)
if err != nil {
return ""
}
return html
}
// StylesheetText returns the built stylesheet for inlining.
func StylesheetText() (string, error) {
b, err := fs.ReadFile(static, "static/dist/app.css")
return string(b), err
}
// DayBars turns a component's 90-day history into bars.
func DayBars(days []agentapi.Day) []Bar {
out := make([]Bar, len(days))
for i, d := range days {
if d.Uptime < 0 {
out[i] = Bar{Status: StatusUnknown, Title: d.Date + " · –"}
continue
}
out[i] = Bar{Status: ratioStatus(d.Uptime), Title: d.Date + " · " + Percent(d.Uptime)}
}
return out
}
// HourBars turns a component's 24-hour history into bars.
func HourBars(hours []agentapi.HourBucket) []Bar {
out := make([]Bar, len(hours))
for i, h := range hours {
label := h.Time.UTC().Format("15:04") + " UTC"
if h.Uptime < 0 {
out[i] = Bar{Status: StatusUnknown, Title: label + " · –"}
continue
}
title := label + " · " + Percent(h.Uptime)
if h.AvgLatencyMS > 0 {
title += fmt.Sprintf(" · %d ms", h.AvgLatencyMS)
}
out[i] = Bar{Status: ratioStatus(h.Uptime), Title: title}
}
return out
}
// HourLatency builds the SVG polyline for a component's 24-hour latency.
func HourLatency(hours []agentapi.HourBucket, width, height int) LatencyLine {
if len(hours) < 2 {
return LatencyLine{Width: width, Height: height}
}
buckets := make([]Bucket, 0, len(hours))
for _, h := range hours {
if h.Uptime < 0 || h.AvgLatencyMS <= 0 {
continue
}
buckets = append(buckets, Bucket{Start: h.Time, Total: 1, OK: 1, Latency: float32(h.AvgLatencyMS)})
}
return Latency(buckets, hours[0].Time, hours[len(hours)-1].Time, width, height)
}
// AvgLatencyText formats the mean latency across hourly buckets with data.
func AvgLatencyText(hours []agentapi.HourBucket) string {
var sum, count int
for _, h := range hours {
if h.Uptime >= 0 && h.AvgLatencyMS > 0 {
sum += h.AvgLatencyMS
count++
}
}
if count == 0 {
return "–"
}
return strconv.Itoa(sum/count) + " ms"
}
// PageStatus maps a page state to the chart statuses used for colors.
func PageStatus(s string) Status {
switch s {
case agentapi.PageOperational:
return StatusUp
case agentapi.PageDegraded:
return StatusDegraded
case agentapi.PageOutage:
return StatusDown
case agentapi.PageUnderMaintenance:
return StatusMaintenance
}
return StatusUnknown
}
// SectionStatus returns a section's aggregated status, falling back to
// summarizing its direct components for legacy cached snapshots.
func SectionStatus(sec agentapi.Section) Status {
if sec.Status != "" {
return PageStatus(sec.Status)
}
states := make([]string, len(sec.Components))
for i, c := range sec.Components {
states[i] = c.Status
}
return PageStatus(agentapi.Summarize(states))
}
// SectionTotal returns the total component count in sec and its descendants.
func SectionTotal(sec agentapi.Section) int {
if sec.Total > 0 {
return sec.Total
}
total := len(sec.Components)
for _, child := range sec.Children {
total += SectionTotal(child)
}
return total
}
// ComponentHref returns the detail link for comp when clickable details are
// enabled on a live status page server, or "" otherwise.
func ComponentHref(base string, comp agentapi.Component, showDetails bool) string {
if !showDetails || base == "" || comp.ID == "" {
return ""
}
return base + "components/" + comp.ID
}
// BackHref turns a page base prefix ("/acme/" or "/") into the page's root URL.
func BackHref(base string) string {
if base == "" || base == "/" {
return "/"
}
return strings.TrimSuffix(base, "/")
}
// UptimeText formats a component's uptime ratio, or "–" without data.
func UptimeText(r float64) string {
if r < 0 {
return "–"
}
return Percent(r)
}