A self-hosted dashboard that puts all your feeds in one place
Go aggregatordashboarddockerfeed-readergohomelabhomepagemonitoringredditrssrss-readerself-hostedstartpageyoutube
Add percentChange function
Svilen Markovbaaf306ebfd8
diff --git a/docs/custom-api.md b/docs/custom-api.mdindex 3d8363c..cd88efa 100644--- a/docs/custom-api.md+++ b/docs/custom-api.md@@ -445,6 +445,7 @@ The following helper functions provided by Glance are available:- `sortByTime(key string, layout string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by a time key in either ascending or descending order. The format must be provided in Go's [date format](https://pkg.go.dev/time#pkg-constants).- `concat(strings ...string) string`: Concatenates multiple strings together.- `unique(key string, arr []JSON) []JSON`: Returns a unique array of JSON objects based on the given key.+- `percentChange(current float, previous float) float`: Calculates the percentage change between two numbers.The following helper functions provided by Go's `text/template` are available:diff --git a/internal/glance/utils.go b/internal/glance/utils.goindex 3411f83..21cd69b 100644--- a/internal/glance/utils.go+++ b/internal/glance/utils.go@@ -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}diff --git a/internal/glance/widget-custom-api.go b/internal/glance/widget-custom-api.goindex b90b3f9..e9c744f 100644--- a/internal/glance/widget-custom-api.go+++ b/internal/glance/widget-custom-api.go@@ -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" {