From fa8dd5992d0db30f40d1da3363cc1e0827d7be84 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Thu, 17 Aug 2023 08:16:19 +0200 Subject: [PATCH] Allow sum function to handle non float types --- .../metricAggregatorFunctions.go | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/internal/metricAggregator/metricAggregatorFunctions.go b/internal/metricAggregator/metricAggregatorFunctions.go index bdbf65f..91a2c93 100644 --- a/internal/metricAggregator/metricAggregatorFunctions.go +++ b/internal/metricAggregator/metricAggregatorFunctions.go @@ -8,7 +8,6 @@ import ( "sort" "strings" - cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger" topo "github.com/ClusterCockpit/cc-metric-collector/pkg/ccTopology" ) @@ -18,18 +17,43 @@ import ( // Sum up values func sumfunc(args interface{}) (interface{}, error) { - s := 0.0 - values, ok := args.([]float64) - if ok { - cclog.ComponentDebug("MetricCache", "SUM FUNC START") + var err error + switch values := args.(type) { + case []float64: + var s float64 = 0.0 for _, x := range values { s += x } - cclog.ComponentDebug("MetricCache", "SUM FUNC END", s) - } else { - cclog.ComponentDebug("MetricCache", "SUM FUNC CAST FAILED") + return s, nil + case []float32: + var s float32 = 0.0 + for _, x := range values { + s += x + } + return s, nil + case []int: + var s int = 0 + for _, x := range values { + s += x + } + return s, nil + case []int64: + var s int64 = 0 + for _, x := range values { + s += x + } + return s, nil + case []int32: + var s int32 = 0 + for _, x := range values { + s += x + } + return s, nil + default: + err = errors.New("function 'sum' only on list of values (float64, float32, int, int32, int64)") } - return s, nil + + return 0.0, err } // Get the minimum value