diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-08-07 21:21:18 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-08-07 21:24:33 -0400 |
commit | 1f88a19b6b92560e63df80a92d13744a44fad0aa (patch) | |
tree | d527e2d43667099af88b118e5f5674c278e657c7 /internal | |
parent | ce64aa9499e4497e193749bf488613afcb6bc47d (diff) |
Refactor status API
This lets me easily continuously update the state of the status
Diffstat (limited to 'internal')
-rw-r--r-- | internal/api/handlers.go | 165 |
1 files changed, 91 insertions, 74 deletions
diff --git a/internal/api/handlers.go b/internal/api/handlers.go index 638dc6a..277552d 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -122,17 +122,21 @@ const ( ServiceAnalytics = "analytics" ) -func StatusHandler(w http.ResponseWriter, r *http.Request) { - type response struct { - Status string `json:"status"` - Description string `json:"description"` - Uptime float64 `json:"uptime"` - ResponseTime int `json:"responseTime"` - ActiveIncidents int `json:"activeIncidents"` - ScheduledMaintenance int `json:"scheduledMaintenance"` - } +type statusData struct { + overallStatus overallStatus + services []service + metrics metrics + incidents []incident + maintenanceEvents []maintenanceEvent + uptimeEvents []uptimeEvent +} + +var status statusData - // res := response{ +func init() { + // Backfill status + + // status.overallStatus = overallStatus{ // Status: StatusDown, // Description: "Everything is on fire", // Uptime: 0.0, @@ -140,7 +144,7 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) { // ActiveIncidents: 9001, // ScheduledMaintenance: 0, // } - res := response{ + status.overallStatus = overallStatus{ Status: StatusDegraded, Description: "Everything is not great", Uptime: 50.0, @@ -149,24 +153,8 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) { ScheduledMaintenance: 1, } - jsonData, _ := json.Marshal(res) - fmt.Fprint(w, string(jsonData)) -} - -func StatusServicesHandler(w http.ResponseWriter, r *http.Request) { - type service struct { - Id string `json:"id"` - Name string `json:"name"` - Description string `json:"description"` - Icon string `json:"icon"` - Status string `json:"status"` - ResponseTime int `json:"responseTime"` - Uptime float64 `json:"uptime"` - } - - response := []service{} - - response = append(response, service{ + status.services = []service{} + status.services = append(status.services, service{ Id: ServiceAPIGateway, Name: "API Gateway", Description: "Core API services", @@ -176,7 +164,7 @@ func StatusServicesHandler(w http.ResponseWriter, r *http.Request) { Uptime: 69.420, }) - response = append(response, service{ + status.services = append(status.services, service{ Id: "logs", Name: "Log Warden", Description: "The master of the Logs", @@ -186,68 +174,97 @@ func StatusServicesHandler(w http.ResponseWriter, r *http.Request) { Uptime: 99.999, }) - jsonData, _ := json.Marshal(response) - fmt.Fprint(w, string(jsonData)) -} - -func StatusMetricsHandler(w http.ResponseWriter, r *http.Request) { - type response struct { - Uptime float64 `json:"uptime"` - ResponseTime int `json:"responseTime"` - RequestVolume int `json:"requestVolume"` - ErrorRate float64 `json:"errorRate"` - } - - res := response{ + status.metrics = metrics{ Uptime: 90.5, ResponseTime: 169, RequestVolume: 5, ErrorRate: 106.0, } - jsonData, _ := json.Marshal(res) + status.incidents = []incident{} + + status.maintenanceEvents = []maintenanceEvent{} + + status.uptimeEvents = []uptimeEvent{} +} + +type overallStatus struct { + Status string `json:"status"` + Description string `json:"description"` + Uptime float64 `json:"uptime"` + ResponseTime int `json:"responseTime"` + ActiveIncidents int `json:"activeIncidents"` + ScheduledMaintenance int `json:"scheduledMaintenance"` +} + +type service struct { + Id string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Icon string `json:"icon"` + Status string `json:"status"` + ResponseTime int `json:"responseTime"` + Uptime float64 `json:"uptime"` +} + +type metrics struct { + Uptime float64 `json:"uptime"` + ResponseTime int `json:"responseTime"` + RequestVolume int `json:"requestVolume"` + ErrorRate float64 `json:"errorRate"` +} + +type incident struct { + Id string `json:"id"` + Title string `json:"title"` + Description string `json:"description"` + Status string `json:"status"` + Severity string `json:"severity"` + StartTime time.Time `json:"startTime"` + AffectedServices []string `json:"affectedServices"` +} + +type maintenanceEvent struct { + Id string `json:"id"` + Title string `json:"title"` + Description string `json:"description"` + StartTime time.Time `json:"startTime"` + EndTime time.Time `json:"endTime"` + AffectedServices []string `json:"affectedServices"` +} + +type uptimeEvent struct { + Date time.Time `json:"date"` + Uptime float64 `json:"uptime"` +} + +func StatusHandler(w http.ResponseWriter, r *http.Request) { + jsonData, _ := json.Marshal(status.overallStatus) + fmt.Fprint(w, string(jsonData)) +} + +func StatusServicesHandler(w http.ResponseWriter, r *http.Request) { + jsonData, _ := json.Marshal(status.services) + fmt.Fprint(w, string(jsonData)) +} + +func StatusMetricsHandler(w http.ResponseWriter, r *http.Request) { + jsonData, _ := json.Marshal(status.metrics) fmt.Fprintf(w, string(jsonData)) } func StatusIncidentsHandler(w http.ResponseWriter, r *http.Request) { - type incident struct { - Id string `json:"id"` - Title string `json:"title"` - Description string `json:"description"` - Status string `json:"status"` - Severity string `json:"severity"` - StartTime time.Time `json:"startTime"` - AffectedServices []string `json:"affectedServices"` - } - response := []incident{} - - jsonData, _ := json.Marshal(response) + jsonData, _ := json.Marshal(status.incidents) fmt.Fprintf(w, string(jsonData)) } func StatusMaintenanceHandler(w http.ResponseWriter, r *http.Request) { - type event struct { - Id string `json:"id"` - Title string `json:"title"` - Description string `json:"description"` - StartTime time.Time `json:"startTime"` - EndTime time.Time `json:"endTime"` - AffectedServices []string `json:"affectedServices"` - } - response := []event{} - - jsonData, _ := json.Marshal(response) + jsonData, _ := json.Marshal(status.maintenanceEvents) fmt.Fprintf(w, string(jsonData)) } func StatusUptimeHandler(w http.ResponseWriter, r *http.Request) { - type event struct { - Date time.Time `json:"date"` - Uptime float64 `json:"uptime"` - } - response := []event{} - - jsonData, _ := json.Marshal(response) + jsonData, _ := json.Marshal(status.uptimeEvents) fmt.Fprintf(w, string(jsonData)) } |