Allow sum function to handle non float types

This commit is contained in:
Holger Obermaier 2023-08-17 08:16:19 +02:00
parent 0b28c55162
commit fa8dd5992d

View File

@ -8,7 +8,6 @@ import (
"sort" "sort"
"strings" "strings"
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
topo "github.com/ClusterCockpit/cc-metric-collector/pkg/ccTopology" topo "github.com/ClusterCockpit/cc-metric-collector/pkg/ccTopology"
) )
@ -18,18 +17,43 @@ import (
// Sum up values // Sum up values
func sumfunc(args interface{}) (interface{}, error) { func sumfunc(args interface{}) (interface{}, error) {
s := 0.0 var err error
values, ok := args.([]float64) switch values := args.(type) {
if ok { case []float64:
cclog.ComponentDebug("MetricCache", "SUM FUNC START") var s float64 = 0.0
for _, x := range values { for _, x := range values {
s += x s += x
} }
cclog.ComponentDebug("MetricCache", "SUM FUNC END", s) return s, nil
} else { case []float32:
cclog.ComponentDebug("MetricCache", "SUM FUNC CAST FAILED") 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 // Get the minimum value