mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-27 23:59:05 +01:00
Use generic function to compute average
This commit is contained in:
parent
ceff67085b
commit
a25f4f8b8d
@ -14,7 +14,7 @@ import (
|
|||||||
* Arithmetic functions on value arrays
|
* Arithmetic functions on value arrays
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func sumAnyType[T float64 | float32 | int | int32 | int64](values []T) interface{} {
|
func sumAnyType[T float64 | float32 | int | int32 | int64](values []T) T {
|
||||||
var sum T
|
var sum T
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
sum += value
|
sum += value
|
||||||
@ -44,7 +44,7 @@ func sumfunc(args interface{}) (interface{}, error) {
|
|||||||
return 0.0, err
|
return 0.0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func minAnyType[T float64 | float32 | int | int32 | int64](values []T) (interface{}, error) {
|
func minAnyType[T float64 | float32 | int | int32 | int64](values []T) (T, error) {
|
||||||
if len(values) == 0 {
|
if len(values) == 0 {
|
||||||
return 0.0, errors.New("min function requires at least one argument")
|
return 0.0, errors.New("min function requires at least one argument")
|
||||||
}
|
}
|
||||||
@ -75,45 +75,33 @@ func minfunc(args interface{}) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func avgAnyType[T float64 | float32 | int | int32 | int64](values []T) (float64, error) {
|
||||||
|
if len(values) == 0 {
|
||||||
|
return 0.0, errors.New("average function requires at least one argument")
|
||||||
|
}
|
||||||
|
sum := sumAnyType[T](values)
|
||||||
|
return float64(sum) / float64(len(values)), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Get the average or mean value
|
// Get the average or mean value
|
||||||
func avgfunc(args interface{}) (interface{}, error) {
|
func avgfunc(args interface{}) (interface{}, error) {
|
||||||
switch values := args.(type) {
|
switch values := args.(type) {
|
||||||
case []float64:
|
case []float64:
|
||||||
var s float64 = 0
|
return avgAnyType(values)
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s / float64(len(values)), nil
|
|
||||||
case []float32:
|
case []float32:
|
||||||
var s float32 = 0
|
return avgAnyType(values)
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s / float32(len(values)), nil
|
|
||||||
case []int:
|
case []int:
|
||||||
var s int = 0
|
return avgAnyType(values)
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s / len(values), nil
|
|
||||||
case []int64:
|
case []int64:
|
||||||
var s int64 = 0
|
return avgAnyType(values)
|
||||||
for _, x := range values {
|
|
||||||
s += x
|
|
||||||
}
|
|
||||||
return s / int64(len(values)), nil
|
|
||||||
case []int32:
|
case []int32:
|
||||||
var s int32 = 0
|
return avgAnyType(values)
|
||||||
for _, x := range values {
|
default:
|
||||||
s += x
|
return 0.0, errors.New("function 'average' only on list of values (float64, float32, int, int32, int64)")
|
||||||
}
|
}
|
||||||
return s / int32(len(values)), nil
|
|
||||||
|
|
||||||
}
|
|
||||||
return 0.0, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func maxAnyType[T float64 | float32 | int | int32 | int64](values []T) (interface{}, error) {
|
func maxAnyType[T float64 | float32 | int | int32 | int64](values []T) (T, error) {
|
||||||
if len(values) == 0 {
|
if len(values) == 0 {
|
||||||
return 0.0, errors.New("max function requires at least one argument")
|
return 0.0, errors.New("max function requires at least one argument")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user