grafana/pkg/expr/query.go
Ryan McKinley 42b0f802de
QueryTypes: Add feature toggle to show query types in datasource apiservers (#88213)
* initial attempt

* show query types

* show query types

* with formatting

* with formatting

* more cleanup

* add feature toggle

* fix build
2024-05-23 18:46:28 +02:00

112 lines
2.5 KiB
Go

package expr
import (
"embed"
"github.com/grafana/grafana/pkg/expr/classic"
"github.com/grafana/grafana/pkg/expr/mathexp"
)
// Supported expression types
// +enum
type QueryType string
const (
// Apply a mathematical expression to results
QueryTypeMath QueryType = "math"
// Reduce query results
QueryTypeReduce QueryType = "reduce"
// Resample query results
QueryTypeResample QueryType = "resample"
// Classic query
QueryTypeClassic QueryType = "classic_conditions"
// Threshold
QueryTypeThreshold QueryType = "threshold"
// SQL query via DuckDB
QueryTypeSQL QueryType = "sql"
)
type MathQuery struct {
// General math expression
Expression string `json:"expression" jsonschema:"minLength=1,example=$A + 1,example=$A/$B"`
}
type ReduceQuery struct {
// Reference to single query result
Expression string `json:"expression" jsonschema:"minLength=1,example=$A"`
// The reducer
Reducer mathexp.ReducerID `json:"reducer"`
// Reducer Options
Settings *ReduceSettings `json:"settings,omitempty"`
}
// QueryType = resample
type ResampleQuery struct {
// The math expression
Expression string `json:"expression" jsonschema:"minLength=1,example=$A + 1,example=$A"`
// The time duration
Window string `json:"window" jsonschema:"minLength=1,example=1d,example=10m"`
// The downsample function
Downsampler mathexp.ReducerID `json:"downsampler"`
// The upsample function
Upsampler mathexp.Upsampler `json:"upsampler"`
}
type ThresholdQuery struct {
// Reference to single query result
Expression string `json:"expression" jsonschema:"minLength=1,example=$A"`
// Threshold Conditions
Conditions []ThresholdConditionJSON `json:"conditions"`
}
type ClassicQuery struct {
Conditions []classic.ConditionJSON `json:"conditions"`
}
// SQLQuery requires the sqlExpression feature flag
type SQLExpression struct {
Expression string `json:"expression" jsonschema:"minLength=1,example=SELECT * FROM A LIMIT 1"`
}
//-------------------------------
// Non-query commands
//-------------------------------
type ReduceSettings struct {
// Non-number reduce behavior
Mode ReduceMode `json:"mode"`
// Only valid when mode is replace
ReplaceWithValue *float64 `json:"replaceWithValue,omitempty"`
}
// Non-Number behavior mode
// +enum
type ReduceMode string
const (
// Drop non-numbers
ReduceModeDrop ReduceMode = "dropNN"
// Replace non-numbers
ReduceModeReplace ReduceMode = "replaceNN"
)
//go:embed query.types.json
var f embed.FS
func QueryTypeDefinitionListJSON() ([]byte, error) {
return f.ReadFile("query.types.json")
}