Always display every hour in the duration specified on the chart

This commit is contained in:
TwinProduction 2021-08-21 21:25:49 -04:00
parent 0e7f1d19f4
commit a626b00b59

View File

@ -1,6 +1,7 @@
package controller package controller
import ( import (
"log"
"math" "math"
"net/http" "net/http"
"sort" "sort"
@ -34,9 +35,9 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) {
var from time.Time var from time.Time
switch duration { switch duration {
case "7d": case "7d":
from = time.Now().Add(-24 * 7 * time.Hour) from = time.Now().Truncate(time.Hour).Add(-24 * 7 * time.Hour)
case "24h": case "24h":
from = time.Now().Add(-24 * time.Hour) from = time.Now().Truncate(time.Hour).Add(-24 * time.Hour)
default: default:
writer.WriteHeader(http.StatusBadRequest) writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("Durations supported: 7d, 24h")) _, _ = writer.Write([]byte("Durations supported: 7d, 24h"))
@ -54,6 +55,11 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) {
_, _ = writer.Write([]byte(err.Error())) _, _ = writer.Write([]byte(err.Error()))
return return
} }
if len(hourlyAverageResponseTime) == 0 {
writer.WriteHeader(http.StatusNoContent)
_, _ = writer.Write(nil)
return
}
series := chart.TimeSeries{ series := chart.TimeSeries{
Name: "Average response time per hour", Name: "Average response time per hour",
Style: chart.Style{ Style: chart.Style{
@ -62,8 +68,16 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) {
}, },
} }
keys := make([]int, 0, len(hourlyAverageResponseTime)) keys := make([]int, 0, len(hourlyAverageResponseTime))
earliestTimestamp := int64(0)
for hourlyTimestamp := range hourlyAverageResponseTime { for hourlyTimestamp := range hourlyAverageResponseTime {
keys = append(keys, int(hourlyTimestamp)) 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) sort.Ints(keys)
var maxAverageResponseTime float64 var maxAverageResponseTime float64
@ -102,9 +116,7 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) {
} }
writer.Header().Set("Content-Type", "image/svg+xml") writer.Header().Set("Content-Type", "image/svg+xml")
if err := graph.Render(chart.SVG, writer); err != nil { if err := graph.Render(chart.SVG, writer); err != nil {
writer.Header().Set("Content-Type", "text/plain") log.Println("[controller][responseTimeChartHandler] Failed to render response time chart:", err.Error())
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return return
} }
} }