From 0eb6f0b6d7542bc69a4e22a24fba82cade20e84e Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Thu, 8 Aug 2024 12:20:33 +0900 Subject: [PATCH] (fix) timestamp and color fix --- app/go.mod | 5 ++++- app/go.sum | 4 ++-- app/pkg/badge.go | 35 ++++++++++++++++++++++++++--------- app/pkg/echo.go | 8 ++++---- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/app/go.mod b/app/go.mod index ea26d4d..9c31aac 100644 --- a/app/go.mod +++ b/app/go.mod @@ -21,7 +21,10 @@ require ( ) // github.com/kevincobain2000/go-progress-svg => /Users/pulkit.kathuria/git/go-progress-svg -replace github.com/narqo/go-badge => github.com/kevincobain2000/go-badge v0.0.0-20240527052429-de9eb158774b +// replace github.com/narqo/go-badge => github.com/kevincobain2000/go-badge master +replace github.com/narqo/go-badge => github.com/kevincobain2000/go-badge v0.0.0-20240808031701-59995e0da671 + +// replace github.com/narqo/go-badge => /Users/pulkit.kathuria/git/go-badge require ( filippo.io/edwards25519 v1.1.0 // indirect diff --git a/app/go.sum b/app/go.sum index 068223d..f761f51 100644 --- a/app/go.sum +++ b/app/go.sum @@ -63,8 +63,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI= github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk= -github.com/kevincobain2000/go-badge v0.0.0-20240527052429-de9eb158774b h1:rszueD1SXo5shNhpRxP9s1JQz4urhf7E8kmjTgF4RJE= -github.com/kevincobain2000/go-badge v0.0.0-20240527052429-de9eb158774b/go.mod h1:m9BzkaxwU4IfPQi9ko23cmuFltayFe8iS0dlRlnEWiM= +github.com/kevincobain2000/go-badge v0.0.0-20240808031701-59995e0da671 h1:GZFO+lyVmb9Cp1vA6UrwX8cSFvST1HxOU1F/5TFdrrI= +github.com/kevincobain2000/go-badge v0.0.0-20240808031701-59995e0da671/go.mod h1:m9BzkaxwU4IfPQi9ko23cmuFltayFe8iS0dlRlnEWiM= github.com/kevincobain2000/go-progress-svg v1.0.5 h1:wTIG191GJWkqNNtQmtmaUgb0d9fWXnGUa6g/1/sA1Dg= github.com/kevincobain2000/go-progress-svg v1.0.5/go.mod h1:d6tIZwxFM0QdM16xoVRFKCnrFZTDBR0deQhe5dy2QXg= github.com/kevincobain2000/instachart v1.0.20 h1:pGhgXYE5rlhBHXqwIc1UBLhdOM/EkAnKPA9rzq3J7Ik= diff --git a/app/pkg/badge.go b/app/pkg/badge.go index e503d71..6f3ffa4 100644 --- a/app/pkg/badge.go +++ b/app/pkg/badge.go @@ -26,18 +26,16 @@ func (b *Badge) Get(req *BadgeRequest, t *models.Type) ([]byte, error) { label := t.Name + " | " + req.Branch if t.Metric == "" { - badge.SetSocialTemplate() - return badge.RenderBytes(label, scoreStr, "#00000", "#00000", "#fff") + return badge.RenderBytesSocial(label, scoreStr, "#00000", "#00000", "#fff") } - badge.SetFlatTemplate() scoreStr += ret.Metric - badgeColor, labelColor, color := b.getBadgeColors(t.Metric) - return badge.RenderBytes(label, scoreStr, badgeColor, labelColor, color) + badgeColor, labelColor, color := b.getBadgeColors(t.Metric, ret.Score) + return badge.RenderBytesFlat(label, scoreStr, badgeColor, labelColor, color) } func (b *Badge) Get404(req *BadgeRequest) ([]byte, error) { - return badge.RenderBytes(req.Branch+"|"+req.Type, "404", "#fff", "white", "red") + return badge.RenderBytesFlat(req.Branch+"|"+req.Type, "404", "#fff", "white", "red") } func (b *Badge) GetType(name string) (*models.Type, error) { @@ -49,13 +47,32 @@ func (b *Badge) GetType(name string) (*models.Type, error) { return b.typeModel.Get(name) } -func (e *Badge) getBadgeColors(metric string) (badge.Color, badge.Color, badge.Color) { +func (e *Badge) getBadgeColors(metric string, score float64) (badge.Color, badge.Color, badge.Color) { metric = strings.ToLower(metric) if metric == "%" { - return "#fff", "#fff", "blue" + var color badge.Color + var textColor badge.Color + switch { + case score >= 90: + color = "green" + textColor = "black" + case score >= 75: + color = "blue" + textColor = "white" + case score >= 50: + color = "orange" + textColor = "black" + case score < 50: + color = "red" + textColor = "white" + default: + color = "white" + textColor = "black" + } + return "#fff", textColor, color } if metric == "kb" || metric == "mb" || metric == "gb" { - return "#fff", "gray", "#F8C8DC" + return "#fff", "gray", "yellow" } if metric == "ms" || metric == "s" || diff --git a/app/pkg/echo.go b/app/pkg/echo.go index 4bc9b42..9680840 100644 --- a/app/pkg/echo.go +++ b/app/pkg/echo.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "os" + "time" "github.com/go-echarts/statsview" "github.com/go-echarts/statsview/viewer" @@ -79,12 +80,11 @@ func HTTPErrorHandler(err error, c echo.Context) { } func ltsv() string { - time_custom := "2006-01-02 15:04:05" - _ = time_custom + timeCustom := time.Now().Format("2006-01-02 15:04:05") var format string - format += "time:${time_custom}\t" + format += fmt.Sprintf("time:%s\t", timeCustom) format += "host:${remote_ip}\t" - format += "forwardedfor:${header:x-forwarded-for}\t" + format += "xff:${header:x-forwarded-for}\t" format += "req:-\t" format += "status:${status}\t" format += "method:${method}\t"