mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-24 17:04:42 +01:00
Close #125: Add more uptime badge colors
This commit is contained in:
parent
f6c32a90ac
commit
77ba2169cf
@ -47,7 +47,6 @@ func badgeHandler(writer http.ResponseWriter, request *http.Request) {
|
|||||||
|
|
||||||
func generateSVG(duration string, uptime *core.Uptime) []byte {
|
func generateSVG(duration string, uptime *core.Uptime) []byte {
|
||||||
var labelWidth, valueWidth, valueWidthAdjustment int
|
var labelWidth, valueWidth, valueWidthAdjustment int
|
||||||
var color string
|
|
||||||
var value float64
|
var value float64
|
||||||
switch duration {
|
switch duration {
|
||||||
case "7d":
|
case "7d":
|
||||||
@ -61,11 +60,7 @@ func generateSVG(duration string, uptime *core.Uptime) []byte {
|
|||||||
value = uptime.LastHour
|
value = uptime.LastHour
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
if value >= 0.8 {
|
color := getBadgeColorFromUptime(value)
|
||||||
color = "#40cc11"
|
|
||||||
} else {
|
|
||||||
color = "#c7130a"
|
|
||||||
}
|
|
||||||
sanitizedValue := strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", value*100), "0"), ".") + "%"
|
sanitizedValue := strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", value*100), "0"), ".") + "%"
|
||||||
if strings.Contains(sanitizedValue, ".") {
|
if strings.Contains(sanitizedValue, ".") {
|
||||||
valueWidthAdjustment = -10
|
valueWidthAdjustment = -10
|
||||||
@ -104,3 +99,18 @@ func generateSVG(duration string, uptime *core.Uptime) []byte {
|
|||||||
</svg>`, width, width, labelWidth, color, labelWidth, valueWidth, labelWidth, width, labelX, duration, labelX, duration, valueX, sanitizedValue, valueX, sanitizedValue))
|
</svg>`, width, width, labelWidth, color, labelWidth, valueWidth, labelWidth, width, labelX, duration, labelX, duration, valueX, sanitizedValue, valueX, sanitizedValue))
|
||||||
return svg
|
return svg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getBadgeColorFromUptime(uptime float64) string {
|
||||||
|
if uptime >= 0.975 {
|
||||||
|
return "#40cc11"
|
||||||
|
} else if uptime >= 0.95 {
|
||||||
|
return "#94cc11"
|
||||||
|
} else if uptime >= 0.9 {
|
||||||
|
return "#ccc311"
|
||||||
|
} else if uptime >= 0.8 {
|
||||||
|
return "#ccb311"
|
||||||
|
} else if uptime >= 0.5 {
|
||||||
|
return "#cc8111"
|
||||||
|
}
|
||||||
|
return "#c7130a"
|
||||||
|
}
|
||||||
|
32
controller/badge_test.go
Normal file
32
controller/badge_test.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetBadgeColorFromUptime(t *testing.T) {
|
||||||
|
if getBadgeColorFromUptime(1) != "#40cc11" {
|
||||||
|
t.Error("expected #40cc11 from an uptime of 1, got", getBadgeColorFromUptime(1))
|
||||||
|
}
|
||||||
|
if getBadgeColorFromUptime(0.95) != "#94cc11" {
|
||||||
|
t.Error("expected #94cc11 from an uptime of 0.95, got", getBadgeColorFromUptime(0.95))
|
||||||
|
}
|
||||||
|
if getBadgeColorFromUptime(0.9) != "#ccc311" {
|
||||||
|
t.Error("expected #c9cc11 from an uptime of 0.9, got", getBadgeColorFromUptime(0.9))
|
||||||
|
}
|
||||||
|
if getBadgeColorFromUptime(0.85) != "#ccb311" {
|
||||||
|
t.Error("expected #ccb311 from an uptime of 0.85, got", getBadgeColorFromUptime(0.85))
|
||||||
|
}
|
||||||
|
if getBadgeColorFromUptime(0.75) != "#cc8111" {
|
||||||
|
t.Error("expected #cc8111 from an uptime of 0.75, got", getBadgeColorFromUptime(0.75))
|
||||||
|
}
|
||||||
|
if getBadgeColorFromUptime(0.6) != "#cc8111" {
|
||||||
|
t.Error("expected #cc8111 from an uptime of 0.6, got", getBadgeColorFromUptime(0.6))
|
||||||
|
}
|
||||||
|
if getBadgeColorFromUptime(0.25) != "#c7130a" {
|
||||||
|
t.Error("expected #c7130a from an uptime of 0.25, got", getBadgeColorFromUptime(0.25))
|
||||||
|
}
|
||||||
|
if getBadgeColorFromUptime(0) != "#c7130a" {
|
||||||
|
t.Error("expected #c7130a from an uptime of 0, got", getBadgeColorFromUptime(0))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user