mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-21 23:43:27 +01:00
Always display every hour in the duration specified on the chart
This commit is contained in:
parent
0e7f1d19f4
commit
a626b00b59
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user