From 77ba2169cf32b358b607746f9c52facf5d85b87b Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Wed, 11 Aug 2021 21:05:51 -0400 Subject: [PATCH] Close #125: Add more uptime badge colors --- controller/badge.go | 22 ++++++++++++++++------ controller/badge_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 controller/badge_test.go diff --git a/controller/badge.go b/controller/badge.go index 8ac07976..db9c91ae 100644 --- a/controller/badge.go +++ b/controller/badge.go @@ -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 { `, 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" +} diff --git a/controller/badge_test.go b/controller/badge_test.go new file mode 100644 index 00000000..769b676e --- /dev/null +++ b/controller/badge_test.go @@ -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)) + } +}