Files
uptimesebi/internal/web/statuspages.templ
T
Sebastian UnterschützandClaude Opus 5.5 52d2ce25c9
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m59s
ci / test (pull_request) Successful in 12m44s
ci / build (pull_request) Successful in 18m14s
ci / image (pull_request) Canceled after 1m1s
perf(statuspages): page the monitor list and batch the group import
With thousands of monitors on a status page the editor was tens of
megabytes, as every row carried the options of all groups, and
importing the monitor groups updated each monitor in its own statement.

- The monitors of the editor are shown 50 at a time. All rows stay in
  the form, as saving replaces the page's monitors.
- Rows carry their own group only and get the other options from the
  row template once shown or focused.
- The filter matches a monitor's name and group path only, no longer
  the names of the groups in its select.
- The import creates the groups one nesting level at a time and files
  all monitors in one statement.

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

493 lines
21 KiB
Templ

package web
import (
"strconv"
"strings"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
// PageRow is a status page in the list.
type PageRow struct {
ID string
Title string
Slug string
Published bool
URL string
Domain string
Suspended bool
}
// PagesView is the data of the status page list.
type PagesView struct {
Pages []PageRow
CanWrite bool
}
templ StatusPagesPage(p Page, me Me, v PagesView) {
@consoleShell(p, me) {
@settingsHeading("pages.title", "pages.intro")
if len(v.Pages) > 0 {
<ul class="mb-12 divide-y divide-rule border-y border-rule text-sm">
for _, pg := range v.Pages {
<li class="flex flex-wrap items-center gap-x-4 gap-y-1 py-2.5">
if pg.Suspended {
@statusDot(StatusDown)
} else if pg.Published {
@statusDot(StatusUp)
} else {
@statusDot(StatusPaused)
}
<a href={ templ.SafeURL("/statuspages/" + pg.ID) } class="hover:underline">{ pg.Title }</a>
<span class="font-mono text-xs text-muted">{ pg.Slug }</span>
if pg.Domain != "" {
<span class="font-mono text-xs text-muted">{ pg.Domain }</span>
}
if pg.Suspended {
<span class="ml-auto text-xs text-down">{ i18n.T(ctx, "pages.suspended") }</span>
} else if pg.Published && pg.URL == "" {
<span class="ml-auto text-xs text-muted">{ i18n.T(ctx, "pages.own_servers_short") }</span>
} else if pg.Published {
<a href={ templ.SafeURL(pg.URL) } class="ml-auto text-muted hover:text-ink" target="_blank" rel="noopener">{ i18n.T(ctx, "pages.open") } ↗</a>
} else {
<span class="ml-auto text-xs text-muted">{ i18n.T(ctx, "pages.draft") }</span>
}
</li>
}
</ul>
}
if v.CanWrite {
<section class="max-w-md">
@sectionHeading("pages.create")
<form data-api="/org/statuspages" data-next-from="id" data-next="/statuspages/">
@formError()
@valueField(i18n.T(ctx, "pages.page_title"), "title", "", "Acme status", true)
@valueField(i18n.T(ctx, "pages.slug"), "slug", "", "acme", true)
@submit(i18n.T(ctx, "pages.create"))
</form>
</section>
}
}
}
// PageGroupRow is one nested group of a status page in tree order.
type PageGroupRow struct {
ID string
Name string
ParentID string
Position int
Depth int
Label string
}
// PageMonitorOption is a monitor in the page editor.
type PageMonitorOption struct {
ID string
Name string
MonitorGroupPath string
Included bool
GroupID string
Label string
}
// PageEditor is the data of the status page editor.
type PageEditor struct {
// OwnServers is set when the organization's own status page servers
// serve its pages instead of the platform's.
OwnServers bool
ID string
Title string
Slug string
Description string
Published bool
ShowDetails bool
Accent string
LogoURL string
CSS string
URL string
Domain string
DomainVerified bool
// CNAMEValue is the target unique to the page that its domain points
// at; empty when the platform's status page host is not known.
CNAMEValue string
// VerifiedBy is "cname" or "txt", empty for domains verified before
// either was recorded.
VerifiedBy string
// MissingSince and SwitchOffAt are set while the record of a verified
// domain cannot be found.
MissingSince string
SwitchOffAt string
// DomainChangesPerDay is how often the domain may be changed a day.
DomainChangesPerDay int
// MonitorLimit is how many monitors the page may show; 0 is any.
MonitorLimit int
// Subscriptions lets visitors subscribe by email; MailAvailable says
// whether the platform can send mail at all.
Subscriptions bool
MailAvailable bool
// SubscriberLimit is the organization's weekly limit; 0 is any.
SubscriberLimit int
SubscribersConfirmed int
SubscribersPending int
Subscribers []PageSubscriberRow
RecordName string
RecordValue string
Suspended bool
SuspendedReason string
Groups []PageGroupRow
HasOrgGroups bool
Monitors []PageMonitorOption
}
templ StatusPageEditor(p Page, me Me, e PageEditor) {
@consoleShell(p, me) {
<p class="mb-4 text-sm"><a href="/statuspages" class="text-muted hover:text-ink">← { i18n.T(ctx, "pages.title") }</a></p>
if e.Suspended {
<p class="mb-6 border-l-2 border-down pl-3 text-sm" role="alert">
{ i18n.T(ctx, "pages.suspended_notice") }
if e.SuspendedReason != "" {
{ " " + i18n.T(ctx, "console.suspended_reason", "Reason", e.SuspendedReason) }
}
</p>
}
<div class="mb-8 flex flex-wrap items-baseline justify-between gap-4">
<h1 class="text-lg font-semibold">{ e.Title }</h1>
<span class="flex gap-5 text-sm">
if e.Published && e.URL != "" {
<a href={ templ.SafeURL(e.URL) } target="_blank" rel="noopener" class="text-muted hover:text-ink">{ i18n.T(ctx, "pages.open") } ↗</a>
}
<a href={ templ.SafeURL("/api/v1/org/statuspages/" + e.ID + "/export") } class="text-muted hover:text-ink">{ i18n.T(ctx, "pages.export") }</a>
</span>
</div>
if e.OwnServers {
<p class="mb-6 border-l-2 border-ink pl-3 text-sm">{ i18n.T(ctx, "pages.own_servers") }</p>
}
<div class="grid gap-12 md:grid-cols-2">
<section>
@sectionHeading("pages.settings")
<form data-api={ "/org/statuspages/" + e.ID } data-method="put" data-next={ "/statuspages/" + e.ID }>
@formError()
@valueField(i18n.T(ctx, "pages.page_title"), "title", e.Title, "", true)
@valueField(i18n.T(ctx, "pages.slug"), "slug", e.Slug, "", true)
<div class="mb-4">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "pages.description") }</span>
@MarkdownEditor("description", e.Description, 5, 10000, MarkdownInline)
</div>
@checkbox(i18n.T(ctx, "pages.published"), "published", e.Published)
@checkbox(i18n.T(ctx, "pages.show_details"), "show_component_details", e.ShowDetails)
<p class="-mt-2 mb-4 text-xs text-muted">{ i18n.T(ctx, "pages.show_details_hint") }</p>
@checkbox(i18n.T(ctx, "pages.subscriptions"), "subscriptions", e.Subscriptions)
<p class="-mt-2 mb-4 text-xs text-muted">
{ i18n.T(ctx, "pages.subscriptions_hint") }
if !e.MailAvailable {
{ " " + i18n.T(ctx, "pages.subscriptions_no_mail") }
}
</p>
<h3 class="mt-6 mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "pages.theme") }</h3>
@valueField(i18n.T(ctx, "pages.accent"), "theme.accent", e.Accent, "#1f6feb", false)
@valueField(i18n.T(ctx, "pages.logo"), "theme.logo_url", e.LogoURL, "https://example.com/logo.svg", false)
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "pages.css") }</span>
<textarea name="theme.css" rows="4" class="w-full border border-rule bg-transparent px-2.5 py-1.5 font-mono text-xs outline-none focus:border-ink">{ e.CSS }</textarea>
</label>
@submit(i18n.T(ctx, "common.save"))
</form>
<button
type="button"
data-delete={ "/org/statuspages/" + e.ID }
data-confirm={ i18n.T(ctx, "pages.delete_confirm") }
data-next="/statuspages"
class="mt-6 text-sm text-down hover:underline"
>{ i18n.T(ctx, "pages.delete") }</button>
if e.URL != "" {
<section class="mt-12">
@sectionHeading("pages.feeds")
<p class="mb-2 text-sm text-muted">{ i18n.T(ctx, "pages.feeds_hint") }</p>
<ul class="space-y-1 font-mono text-xs">
for _, path := range []string{"feed.rss", "api/v1/status", "api/v1/summary", "api/v1/components", "api/v1/incidents"} {
<li><a href={ templ.SafeURL(strings.TrimSuffix(e.URL, "/") + "/" + path) } target="_blank" rel="noopener" class="hover:underline">{ strings.TrimSuffix(e.URL, "/") + "/" + path }</a></li>
}
</ul>
</section>
}
if e.Subscriptions || e.SubscribersConfirmed+e.SubscribersPending > 0 {
@pageSubscribers(e)
}
</section>
<div>
<section class="mb-12">
<div class="mb-3 flex flex-wrap items-baseline justify-between gap-2">
@sectionHeading("pages.groups")
if e.HasOrgGroups {
<button
type="button"
data-post={ "/org/statuspages/" + e.ID + "/groups/import" }
data-confirm={ i18n.T(ctx, "pages.groups_import_confirm") }
data-ok={ i18n.T(ctx, "pages.groups_imported") }
data-reload
class="border border-rule px-2.5 py-1 text-xs hover:border-ink"
>{ i18n.T(ctx, "pages.groups_import") }</button>
}
</div>
<p class="mb-4 text-xs text-muted">{ i18n.T(ctx, "pages.groups_intro") }</p>
if len(e.Groups) > 0 {
<ul class="mb-4 divide-y divide-rule border-y border-rule text-sm">
for _, g := range e.Groups {
<li class="py-2" style={ indent(g.Depth) }>
<details>
<summary class="flex cursor-pointer items-center justify-between gap-2 select-none hover:text-ink">
<span>{ g.Name }</span>
<span class="text-xs text-muted">{ i18n.T(ctx, "common.edit") }</span>
</summary>
<form data-api={ "/org/statuspages/" + e.ID + "/groups/" + g.ID } data-method="put" data-next={ "/statuspages/" + e.ID } class="mt-3 max-w-sm">
@formError()
@valueField(i18n.T(ctx, "sso.field.name"), "name", g.Name, "", true)
<label class="mb-3 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "groups.parent") }</span>
<select name="parent_id" data-nullable class={ inputClass }>
<option value="">{ i18n.T(ctx, "groups.none") }</option>
for _, opt := range e.Groups {
if opt.ID != g.ID {
<option value={ opt.ID } selected?={ opt.ID == g.ParentID }>{ opt.Label }</option>
}
}
</select>
</label>
<label class="mb-3 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "pages.group_position") }</span>
<input type="number" name="position" data-number value={ strconv.Itoa(g.Position) } class={ inputClass }/>
</label>
<div class="flex items-center gap-4">
@submit(i18n.T(ctx, "common.save"))
<button
type="button"
data-delete={ "/org/statuspages/" + e.ID + "/groups/" + g.ID }
data-confirm={ i18n.T(ctx, "groups.delete_confirm") }
data-next={ "/statuspages/" + e.ID }
class="text-sm text-down hover:underline"
>{ i18n.T(ctx, "common.delete") }</button>
</div>
</form>
</details>
</li>
}
</ul>
}
<form data-api={ "/org/statuspages/" + e.ID + "/groups" } data-next={ "/statuspages/" + e.ID } class="border border-rule p-3">
@formError()
<div class="grid gap-3 sm:grid-cols-[1fr_1fr_5rem]">
<label class="block">
<span class="mb-1 block text-xs text-muted">{ i18n.T(ctx, "groups.create") }</span>
<input name="name" required maxlength="100" placeholder={ i18n.T(ctx, "sso.field.name") } class={ inputClass }/>
</label>
<label class="block">
<span class="mb-1 block text-xs text-muted">{ i18n.T(ctx, "groups.parent") }</span>
<select name="parent_id" data-nullable class={ inputClass }>
<option value="">{ i18n.T(ctx, "groups.none") }</option>
for _, opt := range e.Groups {
<option value={ opt.ID }>{ opt.Label }</option>
}
</select>
</label>
<label class="block">
<span class="mb-1 block text-xs text-muted">{ i18n.T(ctx, "pages.group_position") }</span>
<input type="number" name="position" data-number value={ strconv.Itoa(len(e.Groups)) } class={ inputClass }/>
</label>
</div>
<div class="mt-3">
@submit(i18n.T(ctx, "groups.create"))
</div>
</form>
</section>
<section class="mb-12">
@sectionHeading("pages.components")
<form data-api={ "/org/statuspages/" + e.ID + "/monitors" } data-method="put" data-rows="monitors" data-next={ "/statuspages/" + e.ID }>
@formError()
if len(e.Monitors) == 0 {
<p class="mb-4 text-sm text-muted">{ i18n.T(ctx, "console.monitors.empty") }</p>
} else {
if e.MonitorLimit > 0 {
<p class="mb-2 text-xs text-muted">{ i18n.T(ctx, "pages.monitor_limit", "Max", e.MonitorLimit) }</p>
}
<p class="mb-2 flex gap-4 text-xs">
<button type="button" data-check-all="on" data-check-target="[data-field=include]" class="text-muted hover:text-ink">{ i18n.T(ctx, "pages.monitors_all") }</button>
<button type="button" data-check-all="off" data-check-target="[data-field=include]" class="text-muted hover:text-ink">{ i18n.T(ctx, "pages.monitors_none") }</button>
</p>
}
if len(e.Monitors) > monitorFilterFrom {
<input type="search" data-filter-rows="#page-monitors" placeholder={ i18n.T(ctx, "pages.monitors_filter") } aria-label={ i18n.T(ctx, "pages.monitors_filter") } class="mb-2 w-full border border-rule bg-transparent px-2 py-1 text-sm outline-none focus:border-ink"/>
}
<template data-row-fields>
@pageMonitorFields(e, PageMonitorOption{})
</template>
<ul id="page-monitors" class="mb-4 divide-y divide-rule border-y border-rule text-sm" data-page-size={ strconv.Itoa(monitorPageSize) }>
for _, m := range e.Monitors {
<li data-row class="grid grid-cols-[auto_1fr] items-center gap-x-3 gap-y-1.5 py-2">
<input type="checkbox" data-field="include" checked?={ m.Included } class="accent-ink" aria-label={ m.Name }/>
<span data-filter-text class="flex flex-wrap items-baseline gap-x-2">
<span>{ m.Name }</span>
if m.MonitorGroupPath != "" {
<span class="font-mono text-xs text-muted">{ m.MonitorGroupPath }</span>
}
</span>
<input type="hidden" data-field="monitor_id" value={ m.ID }/>
// Organizations may have thousands of monitors: only those on
// the page carry their fields, the others get them when ticked.
if m.Included {
@pageMonitorFields(e, m)
}
</li>
}
</ul>
if len(e.Monitors) > monitorPageSize {
// All rows stay in the form, as saving replaces the page's
// monitors; the pager only shows them a page at a time.
<nav data-row-pager="#page-monitors" class="-mt-2 mb-4 flex flex-wrap items-center gap-x-5 gap-y-2 text-sm" aria-label={ i18n.T(ctx, "pager.label") }>
<button type="button" data-row-page="prev" class="text-muted hover:text-ink disabled:opacity-40">← { i18n.T(ctx, "pager.previous") }</button>
<span data-row-page-status data-template={ i18n.T(ctx, "pages.monitors_range") } class="text-xs text-muted"></span>
<button type="button" data-row-page="next" class="text-muted hover:text-ink disabled:opacity-40">{ i18n.T(ctx, "pager.next") } →</button>
</nav>
}
@submit(i18n.T(ctx, "common.save"))
</form>
</section>
<section>
@sectionHeading("pages.domain")
<p class="mb-4 text-sm text-muted">{ i18n.T(ctx, "pages.domain_intro") }</p>
<form data-api={ "/org/statuspages/" + e.ID + "/domain" } data-method="put" data-next={ "/statuspages/" + e.ID }>
@formError()
@valueField(i18n.T(ctx, "pages.domain_name"), "domain", e.Domain, "status.example.com", false)
@submit(i18n.T(ctx, "common.save"))
</form>
if e.Domain != "" {
<div class="mt-6 space-y-4 border border-rule p-4 text-sm">
if e.DomainVerified {
<p>
<span class="text-up">●</span>
switch e.VerifiedBy {
case "cname":
{ i18n.T(ctx, "pages.domain_verified_by_cname") }
case "txt":
{ i18n.T(ctx, "pages.domain_verified_by_txt") }
default:
{ i18n.T(ctx, "pages.domain_step_verified") }
}
</p>
if e.MissingSince != "" {
<p class="border-l-2 border-down pl-3" role="alert">{ i18n.T(ctx, "pages.domain_missing", "Since", e.MissingSince, "Date", e.SwitchOffAt) }</p>
} else if e.VerifiedBy != "" {
<p class="text-xs text-muted">{ i18n.T(ctx, "pages.domain_keep") }</p>
}
} else if e.CNAMEValue != "" && !e.OwnServers {
<p>{ i18n.T(ctx, "pages.domain_cname_step") }</p>
@dnsRecordList("CNAME", e.Domain, e.CNAMEValue+".")
@verifyButton(e.ID, "pages.verify_now")
<details>
<summary class="cursor-pointer text-xs text-muted hover:text-ink">{ i18n.T(ctx, "pages.domain_alt_summary") }</summary>
<p class="mb-3 mt-3 text-xs">{ i18n.T(ctx, "pages.domain_alt", "Target", e.CNAMEValue) }</p>
@dnsRecordList("TXT", e.RecordName, e.RecordValue)
@verifyButton(e.ID, "pages.verify")
</details>
} else {
<p>{ i18n.T(ctx, "pages.domain_step_verify") }</p>
@dnsRecordList("TXT", e.RecordName, e.RecordValue)
@verifyButton(e.ID, "pages.verify")
if e.OwnServers {
<p>{ i18n.T(ctx, "pages.domain_step_cname_own") }</p>
} else {
<p>{ i18n.T(ctx, "pages.domain_step_cname_unknown") }</p>
}
<p class="text-xs text-muted">{ i18n.T(ctx, "pages.domain_apex") }</p>
}
<p class="text-xs text-muted">{ i18n.T(ctx, "pages.domain_step_tls", "Count", e.DomainChangesPerDay) }</p>
</div>
}
</section>
</div>
</div>
}
}
templ dnsRecordList(kind, name, value string) {
<dl class="grid grid-cols-[max-content_1fr] gap-x-4 gap-y-1 font-mono text-xs">
<dt class="text-muted">{ kind }</dt>
<dd class="break-all">{ name }</dd>
<dt class="text-muted">{ i18n.T(ctx, "pages.value") }</dt>
<dd class="break-all">{ value }</dd>
</dl>
}
templ verifyButton(pageID, label string) {
<button type="button" data-post={ "/org/statuspages/" + pageID + "/domain/verify" } data-ok={ i18n.T(ctx, "pages.domain_verified") } data-reload class="mt-3 border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, label) }</button>
}
// monitorFilterFrom is how many monitors the page editor lists before it
// offers to filter them.
const monitorFilterFrom = 15
// monitorPageSize is how many monitors the page editor shows at a time.
const monitorPageSize = 50
// pageMonitorFields are the group and label of m on the page.
templ pageMonitorFields(e PageEditor, m PageMonitorOption) {
<span data-row-extra></span>
<span class="flex gap-2">
if m.ID == "" {
<select data-field="group_id" data-nullable aria-label={ i18n.T(ctx, "pages.group") } class="w-1/2 border border-rule bg-transparent px-2 py-1 text-xs outline-none focus:border-ink">
<option value="">{ i18n.T(ctx, "pages.no_group") }</option>
for _, opt := range e.Groups {
<option value={ opt.ID }>{ opt.Label }</option>
}
</select>
} else {
// With thousands of monitors and hundreds of groups the options of
// every row would make the page tens of megabytes: rows carry their
// group only and get the others from the template when shown.
<select data-field="group_id" data-nullable data-lazy-options aria-label={ i18n.T(ctx, "pages.group") } class="w-1/2 border border-rule bg-transparent px-2 py-1 text-xs outline-none focus:border-ink">
<option value="">{ i18n.T(ctx, "pages.no_group") }</option>
for _, opt := range e.Groups {
if opt.ID == m.GroupID {
<option value={ opt.ID } selected>{ opt.Label }</option>
}
}
</select>
}
<input data-field="label" value={ m.Label } placeholder={ i18n.T(ctx, "pages.label") } class="w-1/2 border border-rule bg-transparent px-2 py-1 text-xs outline-none focus:border-ink"/>
</span>
}
// PageSubscriberRow is an email subscriber of a page.
type PageSubscriberRow struct {
ID, Email, Since string
Pending bool
}
templ pageSubscribers(e PageEditor) {
<section id="subscribers" class="mt-12">
@sectionHeading("pages.subscribers")
<p class="mb-2 text-sm">{ i18n.T(ctx, "pages.subscribers_counts", "Confirmed", e.SubscribersConfirmed, "Pending", e.SubscribersPending) }</p>
if e.SubscriberLimit > 0 {
<p class="mb-3 text-xs text-muted">{ i18n.T(ctx, "pages.subscribers_limit", "Max", e.SubscriberLimit) }</p>
}
if len(e.Subscribers) == 0 {
<p class="text-sm text-muted">{ i18n.T(ctx, "pages.subscribers_none") }</p>
} else {
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, sub := range e.Subscribers {
<li class="flex flex-wrap items-baseline gap-x-3 gap-y-1 py-2">
<span class="font-mono text-xs">{ sub.Email }</span>
if sub.Pending {
<span class="text-xs text-muted">{ i18n.T(ctx, "pages.subscriber_pending") }</span>
}
<span class="ml-auto font-mono text-xs text-muted">{ sub.Since }</span>
<button type="button" data-delete={ "/org/statuspages/" + e.ID + "/subscribers/" + sub.ID } data-next={ "/statuspages/" + e.ID + "#subscribers" } class="text-xs text-muted hover:text-down">{ i18n.T(ctx, "pages.subscriber_remove") }</button>
</li>
}
</ul>
if len(e.Subscribers) < e.SubscribersConfirmed+e.SubscribersPending {
<p class="mt-2 text-xs text-muted">{ i18n.T(ctx, "pages.subscribers_more", "Count", len(e.Subscribers)) }</p>
}
}
</section>
}