feat(notification): add option to convert metric units in request body (#1156)

* feat(notification): add option to convert metric units in request body

* ignore gosec

* rename fields
This commit is contained in:
UUBulb
2026-01-10 17:29:10 +08:00
committed by GitHub
parent 6067a41038
commit 302d278644
5 changed files with 124 additions and 29 deletions
+40
View File
@@ -0,0 +1,40 @@
package utils
import (
"fmt"
"math"
)
// https://github.com/dustin/go-humanize/blob/master/bytes.go
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
func countDigits(n int64) int {
digits := 0
for n != 0 {
n /= 10
digits += 1
}
return digits
}
func humanateBytes(s uint64, base float64, minDigits int, sizes []string) string {
if s < 10 {
return fmt.Sprintf("%d B", s)
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[min(len(sizes)-1, int(e))] // #nosec G602
rounding := math.Pow10(minDigits - 1)
val := math.Floor(float64(s)/math.Pow(base, e)*rounding+0.5) / rounding
ff := "%%.%df %%s"
digits := max(minDigits-countDigits(int64(val)), 0)
f := fmt.Sprintf(ff, digits)
return fmt.Sprintf(f, val, suffix)
}
func Bytes(s uint64) string {
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
return humanateBytes(s, 1024, 2, sizes)
}