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)" ./... 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 +}