package server import ( "bytes" "fmt" "html/template" "net/http" "strconv" "strings" "git.gsdistance.org/tyuXX/RUD/internal/config" "git.gsdistance.org/tyuXX/RUD/internal/store" ) type monitorPageData struct { Title string Theme config.ThemeConfig CustomCSS template.CSS BackLink string Index bool Monitor *store.MonitorState MonitorID string Monitors []store.MonitorState Days int HourlyLabel string HasStats bool RangeChecks int RangeUptime float64 RangeAvgLat float64 RangeUp int RangeDegraded int RangeDown int UptimeChart template.HTML LatencyChart template.HTML HourlyChart template.HTML } func (s *Server) serveMonitor(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet && r.Method != http.MethodHead { w.Header().Set("Allow", "GET, HEAD") http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } snap := s.store.Snapshot() backLink := s.cfg.Server.Endpoint if backLink == "" { backLink = "/" } // Monitor ID comes from /monitor/ path or ?id= query param. id := strings.Trim(strings.TrimPrefix(r.URL.Path, "/monitor"), "/") if id == "" { id = r.URL.Query().Get("id") } data := monitorPageData{ Title: "Monitors — " + s.pageTitle(), Theme: s.cfg.Page.Theme, CustomCSS: template.CSS(s.cfg.Page.Theme.CustomCSS), BackLink: backLink, MonitorID: id, Days: s.statsDays(r), } if id == "" { data.Index = true data.Monitors = snap.Monitors } else if m, ok := snap.MonitorsByID[id]; ok { mc := m data.Monitor = &mc data.Title = mc.Name + " — " + s.pageTitle() s.fillStats(&data, id) } else { // Unknown to the store; still try stats in case it existed before. s.fillStats(&data, id) } w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0") w.Header().Set("Content-Type", "text/html; charset=utf-8") var buf bytes.Buffer if err := templates.ExecuteTemplate(&buf, "monitor", data); err != nil { http.Error(w, "template error", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) if r.Method == http.MethodHead { return } _, _ = w.Write(buf.Bytes()) } func (s *Server) pageTitle() string { if s.cfg.Page.Title != "" { return s.cfg.Page.Title } return "R u ded?" } // statsDays resolves the requested history window, capped at the configured // stats retention period. func (s *Server) statsDays(r *http.Request) int { retention := s.cfg.Storage.StatsRetentionDays if retention <= 0 { retention = 90 } days := retention if v := r.URL.Query().Get("days"); v != "" { if n, err := strconv.Atoi(v); err == nil && n > 0 { days = n } } if days > retention { days = retention } return days } // fillStats queries the stats DB and renders the SVG charts for a monitor. func (s *Server) fillStats(data *monitorPageData, monitorID string) { if s.stats == nil { return } daily, err := s.stats.DailyStats(monitorID, data.Days) if err != nil { return } hours := data.Days * 24 if hours > 168 { hours = 168 } hourly, err := s.stats.HourlyStats(monitorID, hours) if err != nil { hourly = nil } if len(daily) == 0 && len(hourly) == 0 { return } data.HasStats = true data.HourlyLabel = fmt.Sprintf("last %d hours", hours) var checks, up, degraded, down int var uptimeSum, latSum float64 for _, d := range daily { checks += d.Checks up += d.Up degraded += d.Degraded down += d.Down uptimeSum += d.UptimePct * float64(d.Checks) latSum += d.AvgLatencyMs * float64(d.Checks) } data.RangeChecks = checks data.RangeUp = up data.RangeDegraded = degraded data.RangeDown = down if checks > 0 { data.RangeUptime = uptimeSum / float64(checks) data.RangeAvgLat = latSum / float64(checks) } th := s.cfg.Page.Theme data.UptimeChart = uptimeBarChart(dailyBarBuckets(daily), 720, 140, th.UpColor, th.DegradedColor, th.DownColor) data.LatencyChart = dailyLatencyChart(daily, 720, 120, th.AccentColor) data.HourlyChart = uptimeBarChart(hourlyBarBuckets(hourly), 720, 120, th.UpColor, th.DegradedColor, th.DownColor) }