Files
uptimesebi/internal/web/public.templ
T
Sebastian UnterschützandClaude Opus 5.5 2409a919c3
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
feat: own incident statuses with a color; incidents before maintenance
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
2026-11-23 23:49:58 +01:00

382 lines
14 KiB
Templ
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 (
"strconv"
"strings"
"time"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
templ publicShell(s agentapi.PageSnapshot, a PublicAssets, docTitle string) {
<!DOCTYPE html>
<html lang={ i18n.Lang(ctx) }>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta http-equiv="refresh" content="60"/>
<title>{ docTitle }</title>
if a.InlineCSS == "" {
@faviconLinks()
}
if a.InlineCSS != "" {
@templ.Raw("<style>" + a.InlineCSS + "</style>")
} else {
<link rel="stylesheet" href={ a.CSSHref }/>
}
if s.Theme.Accent != "" {
@templ.Raw("<style>:root{--page-accent:" + s.Theme.Accent + "}</style>")
}
if s.Theme.CSS != "" {
@templ.Raw("<style>" + s.Theme.CSS + "</style>")
}
</head>
<body class="min-h-screen">
<main class="mx-auto max-w-3xl px-6 py-14">
{ children... }
<footer class="flex justify-between border-t border-rule pt-4 font-mono text-xs text-muted">
<span>{ i18n.T(ctx, "page.updated", "Time", s.GeneratedAt.UTC().Format(time.DateTime)+" UTC") }</span>
<span class="flex gap-4">
for _, l := range PageLegalLinks(s, a.LegalBase) {
@legalLink(l)
}
<span class="flex items-center gap-1">
@brandMark("h-3.5 w-3.5")
UptimeSebi
</span>
</span>
</footer>
</main>
</body>
</html>
}
templ publicStaleNotice(a PublicAssets) {
if !a.StaleSince.IsZero() {
<p class="mb-6 border border-degraded/50 bg-degraded/10 px-4 py-2 text-sm">
{ i18n.T(ctx, "page.stale", "Time", a.StaleSince.UTC().Format("2006-01-02 15:04")+" UTC") }
</p>
}
}
templ PublicStatusPage(s agentapi.PageSnapshot, a PublicAssets) {
@publicShell(s, a, s.Title) {
<header class="mb-10">
if s.Theme.LogoURL != "" {
<img src={ s.Theme.LogoURL } alt={ s.Title } class="mb-6 h-10"/>
}
<h1 class="text-2xl font-semibold" style="color:var(--page-accent,inherit)">{ s.Title }</h1>
if s.Description != "" {
<div class="legal-text mt-2 text-muted">
@templ.Raw(Markdown(s.Description))
</div>
}
</header>
@publicStaleNotice(a)
<section
class={ "mb-12 border-l-4 py-3 pl-4",
templ.KV("border-up", s.Status == agentapi.PageOperational),
templ.KV("border-degraded", s.Status == agentapi.PageDegraded),
templ.KV("border-down", s.Status == agentapi.PageOutage),
templ.KV("border-unknown", s.Status == agentapi.PageUnknown),
templ.KV("border-maintenance", s.Status == agentapi.PageUnderMaintenance) }
>
<p class="text-lg font-medium">{ i18n.T(ctx, "page.status."+s.Status) }</p>
</section>
@pageMaintenance(i18n.T(ctx, "page.maintenance_ongoing"), s.Ongoing(a.Now))
if len(s.Sections) > 0 {
<section class="mb-12 border-t border-rule">
for _, sec := range s.Sections {
@publicSection(sec, s, a)
}
</section>
}
@publicIncidents(s.Incidents)
@pageMaintenance(i18n.T(ctx, "page.maintenance_upcoming"), s.Upcoming(a.Now))
}
}
// publicSection renders an unnamed root component list or a collapsible nested
// group matching the monitor dashboard hierarchy.
templ publicSection(sec agentapi.Section, s agentapi.PageSnapshot, a PublicAssets) {
if sec.Name == "" {
@publicComponentList(sec.Components, s, a)
} else {
<details open class="border-b border-rule">
<summary class="flex cursor-pointer items-center gap-2.5 py-3 text-sm font-medium select-none hover:bg-rule/30">
@statusDot(SectionStatus(sec))
<span>{ sec.Name }</span>
<span class="font-mono text-xs font-normal text-muted">{ strconv.Itoa(SectionTotal(sec)) }</span>
<span class="ml-auto text-xs font-normal text-muted">{ i18n.T(ctx, "page.component."+string(secStatusName(sec))) }</span>
</summary>
<div class="ml-4 border-l border-rule pl-4">
@publicComponentList(sec.Components, s, a)
for _, child := range sec.Children {
@publicSection(child, s, a)
}
</div>
</details>
}
}
func secStatusName(sec agentapi.Section) string {
if sec.Status != "" {
return sec.Status
}
states := make([]string, len(sec.Components))
for i, c := range sec.Components {
states[i] = c.Status
}
return agentapi.Summarize(states)
}
templ publicComponentList(comps []agentapi.Component, s agentapi.PageSnapshot, a PublicAssets) {
if len(comps) > 0 {
<ul class="divide-y divide-rule border-b border-rule last:border-b-0">
for _, comp := range comps {
<li class="py-3">
if href := ComponentHref(a.ComponentBase, comp, s.ShowDetails); href != "" {
<a href={ templ.SafeURL(href) } class="block rounded-sm transition hover:bg-rule/30">
@publicComponentBody(comp, true)
</a>
} else {
@publicComponentBody(comp, false)
if s.ShowDetails && comp.Details != nil {
<details class="mt-2 border-t border-rule pt-2 text-xs">
<summary class="cursor-pointer text-muted hover:text-ink">{ i18n.T(ctx, "page.details_expand") }</summary>
<div class="mt-3">
@publicSLAGrid(comp.Details)
if len(comp.Details.Regions) > 0 {
@publicRegionsList(comp.Details.Regions)
}
</div>
</details>
}
}
</li>
}
</ul>
}
}
templ publicComponentBody(comp agentapi.Component, clickable bool) {
<div class="mb-2 flex items-center gap-2.5">
@statusDot(PageStatus(comp.Status))
<span class={ templ.KV("underline-offset-2 hover:underline", clickable) }>{ comp.Name }</span>
<span class="ml-auto flex items-center gap-2 text-sm text-muted">
<span>{ i18n.T(ctx, "page.component."+comp.Status) }</span>
if clickable {
<span aria-hidden="true" class="text-xs">→</span>
}
</span>
</div>
@bars(DayBars(comp.Days), 22)
<div class="mt-1 flex justify-between font-mono text-xs text-muted">
<span>{ i18n.T(ctx, "page.days_ago", "Count", len(comp.Days)) }</span>
<span>{ UptimeText(comp.Uptime) }</span>
<span>{ i18n.T(ctx, "page.today") }</span>
</div>
}
templ PublicComponentPage(s agentapi.PageSnapshot, comp agentapi.Component, a PublicAssets) {
@publicShell(s, a, comp.Name+" · "+s.Title) {
<p class="mb-6 text-sm">
<a href={ templ.SafeURL(BackHref(a.ComponentBase)) } class="text-muted hover:text-ink">← { s.Title }</a>
</p>
<header class="mb-8">
<h1 class="flex items-center gap-2.5 text-2xl font-semibold" style="color:var(--page-accent,inherit)">
@statusDot(PageStatus(comp.Status))
<span>{ comp.Name }</span>
</h1>
</header>
@publicStaleNotice(a)
<section
class={ "mb-10 border-l-4 py-3 pl-4",
templ.KV("border-up", comp.Status == agentapi.PageOperational),
templ.KV("border-degraded", comp.Status == agentapi.PageDegraded),
templ.KV("border-down", comp.Status == agentapi.PageOutage),
templ.KV("border-unknown", comp.Status == agentapi.PageUnknown),
templ.KV("border-maintenance", comp.Status == agentapi.PageUnderMaintenance) }
>
<p class="text-base font-medium">{ i18n.T(ctx, "page.component."+comp.Status) }</p>
</section>
{{ ongoing, upcoming := s.MaintenanceForComponent(comp.Name, a.Now) }}
@pageMaintenance(i18n.T(ctx, "page.maintenance_ongoing"), ongoing)
if comp.Details != nil {
<section class="mb-10">
<div class="mb-2 flex items-baseline justify-between">
<h2 class="text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "page.details_last_24h") }</h2>
<span class="font-mono text-sm">{ UptimeText(comp.Details.SLA24h) }</span>
</div>
@bars(HourBars(comp.Details.Hours), 24)
{{ lat := HourLatency(comp.Details.Hours, 600, 96) }}
if lat.Points != "" {
<div class="mt-4 mb-1 flex justify-between font-mono text-xs text-muted">
<span>{ i18n.T(ctx, "monitors.latency") }</span>
<span>{ i18n.T(ctx, "page.details_latency_summary", "Avg", AvgLatencyText(comp.Details.Hours), "Max", strconv.Itoa(lat.MaxMS)+" ms") }</span>
</div>
<svg viewBox={ "0 0 " + strconv.Itoa(lat.Width) + " " + strconv.Itoa(lat.Height) } preserveAspectRatio="none" class="block h-24 w-full border-b border-rule">
<polyline points={ lat.Points } fill="none" class="stroke-ink" stroke-width="1.5" vector-effect="non-scaling-stroke"></polyline>
</svg>
}
</section>
<section class="mb-10">
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.sla_table") }</h2>
@publicSLAGrid(comp.Details)
</section>
if len(comp.Details.Regions) > 0 {
<section class="mb-10">
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.regions") }</h2>
@publicRegionsList(comp.Details.Regions)
</section>
}
}
<section class="mb-10">
<div class="mb-2 flex items-baseline justify-between">
<h2 class="text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.last_90d") }</h2>
<span class="font-mono text-sm">{ UptimeText(comp.Uptime) }</span>
</div>
@bars(DayBars(comp.Days), 24)
<div class="mt-1 flex justify-between font-mono text-xs text-muted">
<span>{ i18n.T(ctx, "page.days_ago", "Count", len(comp.Days)) }</span>
<span>{ UptimeText(comp.Uptime) }</span>
<span>{ i18n.T(ctx, "page.today") }</span>
</div>
</section>
@publicIncidents(s.IncidentsForComponent(comp.Name))
@pageMaintenance(i18n.T(ctx, "page.maintenance_upcoming"), upcoming)
}
}
templ publicSLAGrid(d *agentapi.ComponentDetails) {
<dl class="grid grid-cols-2 gap-px border border-rule bg-rule sm:grid-cols-4">
for _, item := range []struct {
label string
value float64
}{
{"24h", d.SLA24h},
{"7d", d.SLA7d},
{"30d", d.SLA30d},
{"90d", d.SLA90d},
} {
<div class="bg-paper px-3 py-2">
<dt class="font-mono text-xs text-muted">{ item.label }</dt>
<dd class="font-mono text-sm">{ UptimeText(item.value) }</dd>
</div>
}
</dl>
}
templ publicRegionsList(regions []agentapi.ComponentRegion) {
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, r := range regions {
<li class="flex items-center gap-2.5 py-2">
@statusDot(PageStatus(r.Status))
<span>{ r.Name }</span>
<span class="ml-auto font-mono text-xs text-muted">
{ i18n.T(ctx, "page.component."+r.Status) }
if r.LatencyMS > 0 {
{ " · " + strconv.Itoa(r.LatencyMS) + " ms" }
}
</span>
</li>
}
</ul>
}
templ publicIncidents(incidents []agentapi.PageIncident) {
<section class="mb-12">
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "page.incidents") }</h2>
if len(incidents) == 0 {
<p class="text-sm text-muted">{ i18n.T(ctx, "page.no_incidents") }</p>
} else {
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, inc := range incidents {
<li class="py-3">
<div class="flex flex-wrap items-baseline gap-x-4 gap-y-1">
@incidentStatusLabel(IncidentStatusInfo{Key: inc.Status, Name: inc.StatusName, Color: inc.StatusColor}, inc.ResolvedAt == nil)
if inc.Title != "" {
<span class="font-medium">{ inc.Title }</span>
<span class="text-xs text-muted">{ inc.Component }</span>
} else {
<span>{ inc.Component }</span>
}
<span class="ml-auto font-mono text-xs text-muted">
{ inc.StartedAt.UTC().Format("2006-01-02 15:04") }
if inc.ResolvedAt != nil {
– { inc.ResolvedAt.UTC().Format("15:04") }
}
UTC
</span>
</div>
if len(inc.Updates) > 0 {
<ol class="ml-1 mt-3 space-y-3 border-l border-rule pl-4">
for _, u := range inc.Updates {
<li>
<p class="text-xs">
@incidentStatusLabel(IncidentStatusInfo{Key: u.Status, Name: u.StatusName, Color: u.StatusColor}, true)
<span class="font-mono text-muted">{ " · " + u.At.UTC().Format("2006-01-02 15:04") + " UTC" }</span>
</p>
<div class="legal-text mt-1">
@templ.Raw(Markdown(u.Message))
</div>
</li>
}
</ol>
}
</li>
}
</ul>
}
</section>
}
// incidentStatusLabel names an incident's status: what the organization
// said, in its own color for its own statuses, or ongoing or resolved when
// it said nothing.
templ incidentStatusLabel(st IncidentStatusInfo, open bool) {
switch {
case st.Key == "custom":
<span class="inline-flex items-center gap-1.5 font-medium">
<span class="inline-block size-2 rounded-full" style={ "background-color:" + st.Color }></span>
{ st.Name }
</span>
case st.Key != "":
<span class={ "font-medium", incidentStatusClass(st.Key) }>{ i18n.T(ctx, "incident.status."+st.Key) }</span>
case open:
<span class="font-medium text-down">{ i18n.T(ctx, "incidents.ongoing") }</span>
default:
<span class="text-up">{ i18n.T(ctx, "incidents.resolved") }</span>
}
}
templ pageMaintenance(heading string, items []agentapi.PageMaintenance) {
if len(items) > 0 {
<section class="mb-12">
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ heading }</h2>
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, m := range items {
<li class="py-3">
<div class="flex flex-wrap items-baseline gap-x-4 gap-y-1">
<span class="flex items-center gap-2 font-medium">
@statusDot(StatusMaintenance)
{ m.Title }
</span>
<span class="ml-auto font-mono text-xs text-muted">
{ m.Start.UTC().Format("2006-01-02 15:04") + " – " + m.End.UTC().Format("15:04") + " UTC" }
</span>
</div>
<p class="ml-4 mt-1 text-xs text-muted">{ strings.Join(m.Components, ", ") }</p>
if m.Description != "" {
<div class="legal-text ml-4 mt-2 text-muted">
@templ.Raw(Markdown(m.Description))
</div>
}
</li>
}
</ul>
</section>
}
}