Add percentChange function

This commit is contained in:
Svilen Markov
2025-05-11 21:09:12 +01:00
parent 7bbf103e01
commit baaf306ebf
3 changed files with 9 additions and 0 deletions

View File

@@ -18,6 +18,13 @@ var sequentialWhitespacePattern = regexp.MustCompile(`\s+`)
var whitespaceAtBeginningOfLinePattern = regexp.MustCompile(`(?m)^\s+`)
func percentChange(current, previous float64) float64 {
if previous == 0 {
if current == 0 {
return 0 // 0% change if both are 0
}
return 100 // 100% increase if going from 0 to something
}
return (current/previous - 1) * 100
}

View File

@@ -546,6 +546,7 @@ var customAPITemplateFuncs = func() template.FuncMap {
regex := getCachedRegexp(pattern)
return itemAtIndexOrDefault(regex.FindStringSubmatch(s), 1, "")
},
"percentChange": percentChange,
"sortByString": func(key, order string, results []decoratedGJSONResult) []decoratedGJSONResult {
sort.Slice(results, func(a, b int) bool {
if order == "asc" {