From a626b00b59da455fc8fad5f32731f30af959a678 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sat, 21 Aug 2021 21:25:49 -0400 Subject: [PATCH] Always display every hour in the duration specified on the chart --- controller/chart.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/controller/chart.go b/controller/chart.go index 6e23139d..3a343648 100644 --- a/controller/chart.go +++ b/controller/chart.go @@ -1,6 +1,7 @@ package controller import ( + "log" "math" "net/http" "sort" @@ -34,9 +35,9 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) { var from time.Time switch duration { case "7d": - from = time.Now().Add(-24 * 7 * time.Hour) + from = time.Now().Truncate(time.Hour).Add(-24 * 7 * time.Hour) case "24h": - from = time.Now().Add(-24 * time.Hour) + from = time.Now().Truncate(time.Hour).Add(-24 * time.Hour) default: writer.WriteHeader(http.StatusBadRequest) _, _ = writer.Write([]byte("Durations supported: 7d, 24h")) @@ -54,6 +55,11 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) { _, _ = writer.Write([]byte(err.Error())) return } + if len(hourlyAverageResponseTime) == 0 { + writer.WriteHeader(http.StatusNoContent) + _, _ = writer.Write(nil) + return + } series := chart.TimeSeries{ Name: "Average response time per hour", Style: chart.Style{ @@ -62,8 +68,16 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) { }, } keys := make([]int, 0, len(hourlyAverageResponseTime)) + earliestTimestamp := int64(0) for hourlyTimestamp := range hourlyAverageResponseTime { keys = append(keys, int(hourlyTimestamp)) + if earliestTimestamp == 0 || hourlyTimestamp < earliestTimestamp { + earliestTimestamp = hourlyTimestamp + } + } + for earliestTimestamp > from.Unix() { + earliestTimestamp -= int64(time.Hour.Seconds()) + keys = append(keys, int(earliestTimestamp)) } sort.Ints(keys) var maxAverageResponseTime float64 @@ -102,9 +116,7 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) { } writer.Header().Set("Content-Type", "image/svg+xml") if err := graph.Render(chart.SVG, writer); err != nil { - writer.Header().Set("Content-Type", "text/plain") - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte(err.Error())) + log.Println("[controller][responseTimeChartHandler] Failed to render response time chart:", err.Error()) return } }