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) {
{ docTitle }
if a.InlineCSS == "" {
@faviconLinks()
}
if a.InlineCSS != "" {
@templ.Raw("")
} else {
}
if s.Theme.Accent != "" {
@templ.Raw("")
}
if s.Theme.CSS != "" {
@templ.Raw("")
}
{ children... }
{ i18n.T(ctx, "page.updated", "Time", s.GeneratedAt.UTC().Format(time.DateTime)+" UTC") }
for _, l := range PageLegalLinks(s, a.LegalBase) {
@legalLink(l)
}
@brandMark("h-3.5 w-3.5")
UptimeSebi
}
templ publicStaleNotice(a PublicAssets) {
if !a.StaleSince.IsZero() {
{ i18n.T(ctx, "page.stale", "Time", a.StaleSince.UTC().Format("2006-01-02 15:04")+" UTC") }
}
}
templ PublicStatusPage(s agentapi.PageSnapshot, a PublicAssets) {
@publicShell(s, a, s.Title) {
@publicStaleNotice(a)
{ i18n.T(ctx, "page.status."+s.Status) }
@pageMaintenance(i18n.T(ctx, "page.maintenance_ongoing"), s.Ongoing(a.Now))
if len(s.Sections) > 0 {
for _, sec := range s.Sections {
@publicSection(sec, s, a)
}
}
@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 {
@statusDot(SectionStatus(sec))
{ sec.Name }
{ strconv.Itoa(SectionTotal(sec)) }
{ i18n.T(ctx, "page.component."+string(secStatusName(sec))) }
@publicComponentList(sec.Components, s, a)
for _, child := range sec.Children {
@publicSection(child, s, a)
}
}
}
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 {
for _, comp := range comps {
if href := ComponentHref(a.ComponentBase, comp, s.ShowDetails); href != "" {
@publicComponentBody(comp, s, true)
} else {
@publicComponentBody(comp, s, false)
if s.ShowDetails && comp.Details != nil {
{ i18n.T(ctx, "page.details_expand") }
@publicSLAGrid(comp.Details)
if len(comp.Details.Regions) > 0 {
@publicRegionsList(comp.Details.Regions)
}
}
}
}
}
}
templ publicComponentBody(comp agentapi.Component, s agentapi.PageSnapshot, clickable bool) {
@statusDot(PageStatus(comp.Status))
{ comp.Name }
if inc, ok := s.OpenIncidentStatus(comp.Name); ok {
@incidentStatusLabel(IncidentStatusInfo{Key: inc.Status, Name: inc.StatusName, Color: inc.StatusColor}, true)
·
}
{ i18n.T(ctx, "page.component."+comp.Status) }
if clickable {
→
}
@bars(DayBars(comp.Days), 22)
{ i18n.T(ctx, "page.days_ago", "Count", len(comp.Days)) }
{ UptimeText(comp.Uptime) }
{ i18n.T(ctx, "page.today") }
}
templ PublicComponentPage(s agentapi.PageSnapshot, comp agentapi.Component, a PublicAssets) {
@publicShell(s, a, comp.Name+" · "+s.Title) {
← { s.Title }
@statusDot(PageStatus(comp.Status))
{ comp.Name }
@publicStaleNotice(a)
{ i18n.T(ctx, "page.component."+comp.Status) }
{{ ongoing, upcoming := s.MaintenanceForComponent(comp.Name, a.Now) }}
@pageMaintenance(i18n.T(ctx, "page.maintenance_ongoing"), ongoing)
if comp.Details != nil {
{ i18n.T(ctx, "page.details_last_24h") }
{ UptimeText(comp.Details.SLA24h) }
@bars(HourBars(comp.Details.Hours), 24)
{{ lat := HourLatency(comp.Details.Hours, 600, 96) }}
if lat.Points != "" {
{ i18n.T(ctx, "monitors.latency") }
{ i18n.T(ctx, "page.details_latency_summary", "Avg", AvgLatencyText(comp.Details.Hours), "Max", strconv.Itoa(lat.MaxMS)+" ms") }
}
{ i18n.T(ctx, "monitors.sla_table") }
@publicSLAGrid(comp.Details)
if len(comp.Details.Regions) > 0 {
{ i18n.T(ctx, "monitors.regions") }
@publicRegionsList(comp.Details.Regions)
}
}
{ i18n.T(ctx, "monitors.last_90d") }
{ UptimeText(comp.Uptime) }
@bars(DayBars(comp.Days), 24)
{ i18n.T(ctx, "page.days_ago", "Count", len(comp.Days)) }
{ UptimeText(comp.Uptime) }
{ i18n.T(ctx, "page.today") }
@publicIncidents(s.IncidentsForComponent(comp.Name))
@pageMaintenance(i18n.T(ctx, "page.maintenance_upcoming"), upcoming)
}
}
templ publicSLAGrid(d *agentapi.ComponentDetails) {
for _, item := range []struct {
label string
value float64
}{
{"24h", d.SLA24h},
{"7d", d.SLA7d},
{"30d", d.SLA30d},
{"90d", d.SLA90d},
} {
{ item.label }
{ UptimeText(item.value) }
}
}
templ publicRegionsList(regions []agentapi.ComponentRegion) {
for _, r := range regions {
@statusDot(PageStatus(r.Status))
{ r.Name }
{ i18n.T(ctx, "page.component."+r.Status) }
if r.LatencyMS > 0 {
{ " · " + strconv.Itoa(r.LatencyMS) + " ms" }
}
}
}
templ publicIncidents(incidents []agentapi.PageIncident) {
{ i18n.T(ctx, "page.incidents") }
if len(incidents) == 0 {
{ i18n.T(ctx, "page.no_incidents") }
} else {
for _, inc := range incidents {
@incidentStatusLabel(IncidentStatusInfo{Key: inc.Status, Name: inc.StatusName, Color: inc.StatusColor}, inc.ResolvedAt == nil)
if inc.Title != "" {
{ inc.Title }
{ inc.Component }
} else {
{ inc.Component }
}
{ inc.StartedAt.UTC().Format("2006-01-02 15:04") }
if inc.ResolvedAt != nil {
– { inc.ResolvedAt.UTC().Format("15:04") }
}
UTC
if len(inc.Updates) > 0 {
for _, u := range inc.Updates {
@incidentStatusLabel(IncidentStatusInfo{Key: u.Status, Name: u.StatusName, Color: u.StatusColor}, true)
{ " · " + u.At.UTC().Format("2006-01-02 15:04") + " UTC" }
@templ.Raw(Markdown(u.Message))
}
}
}
}
}
// 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":
{ st.Name }
case st.Key != "":
{ i18n.T(ctx, "incident.status."+st.Key) }
case open:
{ i18n.T(ctx, "incidents.ongoing") }
default:
{ i18n.T(ctx, "incidents.resolved") }
}
}
templ pageMaintenance(heading string, items []agentapi.PageMaintenance) {
if len(items) > 0 {
{ heading }
for _, m := range items {
@statusDot(StatusMaintenance)
{ m.Title }
{ m.Start.UTC().Format("2006-01-02 15:04") + " – " + m.End.UTC().Format("15:04") + " UTC" }
{ strings.Join(m.Components, ", ") }
if m.Description != "" {
@templ.Raw(Markdown(m.Description))
}
}
}
}