Expressions: Add model struct for the query types (not map[string]any) (#82745)

This commit is contained in:
Ryan McKinley
2024-02-17 02:59:11 +02:00
committed by GitHub
parent 46a77c0074
commit f23f50f58d
15 changed files with 2074 additions and 1776 deletions
+27 -15
View File
@@ -3,13 +3,30 @@ package mathexp
import (
"fmt"
"math"
"strings"
"github.com/grafana/grafana-plugin-sdk-go/data"
)
type ReducerFunc = func(fv *Float64Field) *float64
// The reducer function
// +enum
type ReducerID string
const (
ReducerSum ReducerID = "sum"
ReducerMean ReducerID = "mean"
ReducerMin ReducerID = "min"
ReducerMax ReducerID = "max"
ReducerCount ReducerID = "count"
ReducerLast ReducerID = "last"
)
// GetSupportedReduceFuncs returns collection of supported function names
func GetSupportedReduceFuncs() []ReducerID {
return []ReducerID{ReducerSum, ReducerMean, ReducerMin, ReducerMax, ReducerCount, ReducerLast}
}
func Sum(fv *Float64Field) *float64 {
var sum float64
for i := 0; i < fv.Len(); i++ {
@@ -81,34 +98,29 @@ func Last(fv *Float64Field) *float64 {
return fv.GetValue(fv.Len() - 1)
}
func GetReduceFunc(rFunc string) (ReducerFunc, error) {
switch strings.ToLower(rFunc) {
case "sum":
func GetReduceFunc(rFunc ReducerID) (ReducerFunc, error) {
switch rFunc {
case ReducerSum:
return Sum, nil
case "mean":
case ReducerMean:
return Avg, nil
case "min":
case ReducerMin:
return Min, nil
case "max":
case ReducerMax:
return Max, nil
case "count":
case ReducerCount:
return Count, nil
case "last":
case ReducerLast:
return Last, nil
default:
return nil, fmt.Errorf("reduction %v not implemented", rFunc)
}
}
// GetSupportedReduceFuncs returns collection of supported function names
func GetSupportedReduceFuncs() []string {
return []string{"sum", "mean", "min", "max", "count", "last"}
}
// Reduce turns the Series into a Number based on the given reduction function
// if ReduceMapper is defined it applies it to the provided series and performs reduction of the resulting series.
// Otherwise, the reduction operation is done against the original series.
func (s Series) Reduce(refID, rFunc string, mapper ReduceMapper) (Number, error) {
func (s Series) Reduce(refID string, rFunc ReducerID, mapper ReduceMapper) (Number, error) {
var l data.Labels
if s.GetLabels() != nil {
l = s.GetLabels().Copy()
+3 -3
View File
@@ -30,7 +30,7 @@ var seriesEmpty = Vars{
func TestSeriesReduce(t *testing.T) {
var tests = []struct {
name string
red string
red ReducerID
vars Vars
varToReduce string
errIs require.ErrorAssertionFunc
@@ -217,7 +217,7 @@ var seriesNonNumbers = Vars{
func TestSeriesReduceDropNN(t *testing.T) {
var tests = []struct {
name string
red string
red ReducerID
vars Vars
varToReduce string
results Results
@@ -304,7 +304,7 @@ func TestSeriesReduceReplaceNN(t *testing.T) {
replaceWith := rand.Float64()
var tests = []struct {
name string
red string
red ReducerID
vars Vars
varToReduce string
results Results