Close #125: Add more uptime badge colors

This commit is contained in:
TwinProduction 2021-08-11 21:05:51 -04:00
parent f6c32a90ac
commit 77ba2169cf
2 changed files with 48 additions and 6 deletions

View File

@ -47,7 +47,6 @@ func badgeHandler(writer http.ResponseWriter, request *http.Request) {
func generateSVG(duration string, uptime *core.Uptime) []byte {
var labelWidth, valueWidth, valueWidthAdjustment int
var color string
var value float64
switch duration {
case "7d":
@ -61,11 +60,7 @@ func generateSVG(duration string, uptime *core.Uptime) []byte {
value = uptime.LastHour
default:
}
if value >= 0.8 {
color = "#40cc11"
} else {
color = "#c7130a"
}
color := getBadgeColorFromUptime(value)
sanitizedValue := strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", value*100), "0"), ".") + "%"
if strings.Contains(sanitizedValue, ".") {
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))
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
View 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))
}
}