From f15943563c9954e622371407f42a3d035890df87 Mon Sep 17 00:00:00 2001 From: Long Zhang Date: Sat, 29 Nov 2025 18:48:41 +0100 Subject: [PATCH 1/2] #61 fix the stake change alert's message Signed-off-by: Long Zhang --- td2/alert.go | 2 +- td2/utils/formatters.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 td2/utils/formatters.go diff --git a/td2/alert.go b/td2/alert.go index d5669b2..7bca8e3 100644 --- a/td2/alert.go +++ b/td2/alert.go @@ -769,7 +769,7 @@ func evaluateStakeChangeAlert(cc *ChainConfig) (bool, bool) { } else if cc.Provider.Name == "namada" { unit = "NAM" } - message := fmt.Sprintf("%s's stake has %s by %.1g%% (%.1g %s now) compared to the previous check (%.1g %s)", cc.valInfo.Moniker, trend, math.Abs(stakeChangePercent)*100, stakeNow, unit, stakeBefore, unit) + message := fmt.Sprintf("%s's stake has %s by %.1f%% (%s %s now) compared to the previous check (%s %s)", cc.valInfo.Moniker, trend, math.Abs(stakeChangePercent)*100, utils.HumanSI(stakeNow), unit, utils.HumanSI(stakeBefore), unit) if math.Abs(stakeChangePercent) >= threshold { if !alarms.exist(cc.name, alertID) { td.alert(cc.name, message, severity, false, &alertID) diff --git a/td2/utils/formatters.go b/td2/utils/formatters.go new file mode 100644 index 0000000..2b2f873 --- /dev/null +++ b/td2/utils/formatters.go @@ -0,0 +1,31 @@ +// Package utils +package utils + +import ( + "math" + "strconv" +) + +func HumanSI(v float64) string { + abs := math.Abs(v) + var value float64 + var suffix string + + switch { + case abs >= 1_000_000_000: + value = v / 1_000_000_000 + suffix = "B" + case abs >= 1_000_000: + value = v / 1_000_000 + suffix = "M" + case abs >= 1_000: + value = v / 1_000 + suffix = "K" + default: + // No suffix: return plain float without trailing zeros + return strconv.FormatFloat(v, 'f', -1, 64) + } + + // Format without trailing zeros and return uppercase suffix + return strconv.FormatFloat(value, 'f', -1, 64) + suffix +} From 7ecc3744cb1c6fb2350eff52148ce23908ab6cbe Mon Sep 17 00:00:00 2001 From: Long Zhang Date: Sat, 29 Nov 2025 18:49:08 +0100 Subject: [PATCH 2/2] use TEST_CASE to run a specific test case Signed-off-by: Long Zhang --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0a0edcb..8425423 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ +TEST_CASE=. + build: CGO_CFLAGS="-Wno-deprecated-declarations" go build -ldflags '-s -w' -trimpath -o ./tenderduty main.go test: - CGO_CFLAGS="-Wno-deprecated-declarations" go test -v ./... + CGO_CFLAGS="-Wno-deprecated-declarations" go test -v -run "$(TEST_CASE)" ./...