expr

package
v0.0.0-...-fb7f86c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2023 License: AGPL-3.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ThresholdIsAbove        = "gt"
	ThresholdIsBelow        = "lt"
	ThresholdIsWithinRange  = "within_range"
	ThresholdIsOutsideRange = "outside_range"
)
View Source
const DatasourceID = -100

DatasourceID is the fake datasource id used in requests to identify it as an expression command.

View Source
const DatasourceType = "__expr__"

DatasourceType is the string constant used as the datasource when the property is in Datasource.Type. Type in requests is used to identify what type of data source plugin the request belongs to.

View Source
const DatasourceUID = DatasourceType

DatasourceUID is the string constant used as the datasource name in requests to identify it as an expression command when use in Datasource.UID.

View Source
const (

	// DatasourceUID is the string constant used as the datasource name in requests
	// to identify it as an expression command when use in Datasource.UID.
	MLDatasourceUID = "__ml__"
)
View Source
const OldDatasourceUID = "-100"

OldDatasourceUID is the datasource uid used in requests to identify it as an expression command. It goes with the query root level datasourceUID property. It was accidentally set to the Id and is now kept for backwards compatibility. The newer Datasource.UID property should be used instead and should be set to "__expr__".

Variables

View Source
var ConversionError = errutil.NewBase(
	errutil.StatusBadRequest,
	"sse.readDataError",
).MustTemplate(
	"[{{ .Public.refId }}] got error: {{ .Error }}",
	errutil.WithPublic(
		"failed to read data from from query {{ .Public.refId }}: {{ .Public.error }}",
	),
)
View Source
var QueryError = errutil.NewBase(
	errutil.StatusBadRequest, "sse.dataQueryError").MustTemplate(
	"failed to execute query [{{ .Public.refId }}]: {{ .Error }}",
	errutil.WithPublic(
		"failed to execute query [{{ .Public.refId }}]: {{ .Public.error }}",
	))

Functions

func DataSourceModel

func DataSourceModel() *datasources.DataSource

Deprecated. Use DataSourceModelFromNodeType instead

func DataSourceModelFromNodeType

func DataSourceModelFromNodeType(kind NodeType) (*datasources.DataSource, error)

Create a datasources.DataSource struct from NodeType. Returns error if kind is TypeDatasourceNode or unknown one.

func IsDataSource

func IsDataSource(uid string) bool

IsDataSource checks if the uid points to an expression query

func IsSupportedThresholdFunc

func IsSupportedThresholdFunc(name string) bool

func MakeConversionError

func MakeConversionError(refID string, err error) error

func MakeQueryError

func MakeQueryError(refID, datasourceUID string, err error) error

func WideToMany

func WideToMany(frame *data.Frame, fixSeries func(series mathexp.Series, valueField *data.Field)) ([]mathexp.Series, error)

WideToMany converts a data package wide type Frame to one or multiple Series. A series is created for each value type column of wide frame.

This might not be a good idea long term, but works now as an adapter/shim.

Types

type AbsoluteTimeRange

type AbsoluteTimeRange struct {
	From time.Time
	To   time.Time
}

func (AbsoluteTimeRange) AbsoluteTime

func (r AbsoluteTimeRange) AbsoluteTime(_ time.Time) backend.TimeRange

type CMDNode

type CMDNode struct {
	CMDType CommandType
	Command Command
	// contains filtered or unexported fields
}

CMDNode is a DPNode that holds an expression command.

func (*CMDNode) Execute

func (gn *CMDNode) Execute(ctx context.Context, now time.Time, vars mathexp.Vars, s *Service) (mathexp.Results, error)

Execute runs the node and adds the results to vars. If the node requires other nodes they must have already been executed and their results must already by in vars.

func (*CMDNode) ID

func (b *CMDNode) ID() int64

ID returns the id of the node so it can fulfill the gonum's graph Node interface.

func (*CMDNode) NodeType

func (gn *CMDNode) NodeType() NodeType

NodeType returns the data pipeline node type.

func (*CMDNode) RefID

func (b *CMDNode) RefID() string

RefID returns the refId of the node.

func (*CMDNode) String

func (b *CMDNode) String() string

String returns a string representation of the node. In particular for %v formatting in error messages.

type Command

type Command interface {
	NeedsVars() []string
	Execute(ctx context.Context, now time.Time, vars mathexp.Vars, tracer tracing.Tracer) (mathexp.Results, error)
}

Command is an interface for all expression commands.

type CommandType

type CommandType int

CommandType is the type of the expression command.

const (
	// TypeUnknown is the CMDType for an unrecognized expression type.
	TypeUnknown CommandType = iota
	// TypeMath is the CMDType for a math expression.
	TypeMath
	// TypeReduce is the CMDType for a reduction expression.
	TypeReduce
	// TypeResample is the CMDType for a resampling expression.
	TypeResample
	// TypeClassicConditions is the CMDType for the classic condition operation.
	TypeClassicConditions
	// TypeThreshold is the CMDType for checking if a threshold has been crossed
	TypeThreshold
)

func ParseCommandType

func ParseCommandType(s string) (CommandType, error)

ParseCommandType returns a CommandType from its string representation.

func (CommandType) String

func (gt CommandType) String() string

type ConditionEvalJSON

type ConditionEvalJSON struct {
	Params []float64 `json:"params"`
	Type   string    `json:"type"` // e.g. "gt"
}

type DSNode

type DSNode struct {
	// contains filtered or unexported fields
}

DSNode is a DPNode that holds a datasource request.

func (*DSNode) Execute

func (dn *DSNode) Execute(ctx context.Context, now time.Time, _ mathexp.Vars, s *Service) (r mathexp.Results, e error)

Execute runs the node and adds the results to vars. If the node requires other nodes they must have already been executed and their results must already by in vars.

func (*DSNode) ID

func (b *DSNode) ID() int64

ID returns the id of the node so it can fulfill the gonum's graph Node interface.

func (*DSNode) NodeType

func (dn *DSNode) NodeType() NodeType

NodeType returns the data pipeline node type.

func (*DSNode) RefID

func (b *DSNode) RefID() string

RefID returns the refId of the node.

func (*DSNode) String

func (b *DSNode) String() string

String returns a string representation of the node. In particular for %v formatting in error messages.

type DataPipeline

type DataPipeline []Node

DataPipeline is an ordered set of nodes returned from DPGraph processing.

type MLNode

type MLNode struct {
	TimeRange TimeRange
	// contains filtered or unexported fields
}

MLNode is a node of expression tree that evaluates the expression by sending the payload to Machine Learning back-end. See ml.UnmarshalCommand for supported commands.

func (*MLNode) Execute

func (m *MLNode) Execute(ctx context.Context, now time.Time, _ mathexp.Vars, s *Service) (r mathexp.Results, e error)

Execute initializes plugin API client, executes a ml.Command and then converts the result of the execution. Returns non-empty mathexp.Results if evaluation was successful. Returns QueryError if command execution failed

func (*MLNode) ID

func (b *MLNode) ID() int64

ID returns the id of the node so it can fulfill the gonum's graph Node interface.

func (*MLNode) NodeType

func (m *MLNode) NodeType() NodeType

NodeType returns the data pipeline node type.

func (*MLNode) RefID

func (b *MLNode) RefID() string

RefID returns the refId of the node.

func (*MLNode) String

func (b *MLNode) String() string

String returns a string representation of the node. In particular for %v formatting in error messages.

type MathCommand

type MathCommand struct {
	RawExpression string
	Expression    *mathexp.Expr
	// contains filtered or unexported fields
}

MathCommand is a command for a math expression such as "1 + $GA / 2"

func NewMathCommand

func NewMathCommand(refID, expr string) (*MathCommand, error)

NewMathCommand creates a new MathCommand. It will return an error if there is an error parsing expr.

func UnmarshalMathCommand

func UnmarshalMathCommand(rn *rawNode) (*MathCommand, error)

UnmarshalMathCommand creates a MathCommand from Grafana's frontend query.

func (*MathCommand) Execute

func (gm *MathCommand) Execute(ctx context.Context, _ time.Time, vars mathexp.Vars, tracer tracing.Tracer) (mathexp.Results, error)

Execute runs the command and returns the results or an error if the command failed to execute.

func (*MathCommand) NeedsVars

func (gm *MathCommand) NeedsVars() []string

NeedsVars returns the variable names (refIds) that are dependencies to execute the command and allows the command to fulfill the Command interface.

type Node

type Node interface {
	ID() int64 // ID() allows the gonum graph node interface to be fulfilled
	NodeType() NodeType
	RefID() string
	Execute(ctx context.Context, now time.Time, vars mathexp.Vars, s *Service) (mathexp.Results, error)
	String() string
}

Node is a node in a Data Pipeline. Node is either a expression command or a datasource query.

type NodeType

type NodeType int

NodeType is the type of a DPNode. Currently either a expression command or datasource query.

const (
	// TypeCMDNode is a NodeType for expression commands.
	TypeCMDNode NodeType = iota
	// TypeDatasourceNode is a NodeType for datasource queries.
	TypeDatasourceNode
	// TypeMLNode is a NodeType for Machine Learning queries.
	TypeMLNode
)

func NodeTypeFromDatasourceUID

func NodeTypeFromDatasourceUID(uid string) NodeType

NodeTypeFromDatasourceUID returns NodeType depending on the UID of the data source: TypeCMDNode if UID is DatasourceUID or OldDatasourceUID, and TypeDatasourceNode otherwise.

func (NodeType) String

func (nt NodeType) String() string

type Query

type Query struct {
	RefID         string
	TimeRange     TimeRange
	DataSource    *datasources.DataSource `json:"datasource"`
	JSON          json.RawMessage
	Interval      time.Duration
	QueryType     string
	MaxDataPoints int64
}

Query is like plugins.DataSubQuery, but with a a time range, and only the UID for the data source. Also interval is a time.Duration.

type ReduceCommand

type ReduceCommand struct {
	Reducer     string
	VarToReduce string
	// contains filtered or unexported fields
}

ReduceCommand is an expression command for reduction of a timeseries such as a min, mean, or max.

func NewReduceCommand

func NewReduceCommand(refID, reducer, varToReduce string, mapper mathexp.ReduceMapper) (*ReduceCommand, error)

NewReduceCommand creates a new ReduceCMD.

func UnmarshalReduceCommand

func UnmarshalReduceCommand(rn *rawNode) (*ReduceCommand, error)

UnmarshalReduceCommand creates a MathCMD from Grafana's frontend query.

func (*ReduceCommand) Execute

func (gr *ReduceCommand) Execute(ctx context.Context, _ time.Time, vars mathexp.Vars, tracer tracing.Tracer) (mathexp.Results, error)

Execute runs the command and returns the results or an error if the command failed to execute.

func (*ReduceCommand) NeedsVars

func (gr *ReduceCommand) NeedsVars() []string

NeedsVars returns the variable names (refIds) that are dependencies to execute the command and allows the command to fulfill the Command interface.

type RelativeTimeRange

type RelativeTimeRange struct {
	From time.Duration
	To   time.Duration
}

RelativeTimeRange is a time range relative to some absolute time.

func (RelativeTimeRange) AbsoluteTime

func (r RelativeTimeRange) AbsoluteTime(t time.Time) backend.TimeRange

type Request

type Request struct {
	Headers map[string]string
	Debug   bool
	OrgId   int64
	Queries []Query
	User    *user.SignedInUser
}

Request is similar to plugins.DataQuery but with the Time Ranges is per Query.

type ResampleCommand

type ResampleCommand struct {
	Window        time.Duration
	VarToResample string
	Downsampler   string
	Upsampler     string
	TimeRange     TimeRange
	// contains filtered or unexported fields
}

ResampleCommand is an expression command for resampling of a timeseries.

func NewResampleCommand

func NewResampleCommand(refID, rawWindow, varToResample string, downsampler string, upsampler string, tr TimeRange) (*ResampleCommand, error)

NewResampleCommand creates a new ResampleCMD.

func UnmarshalResampleCommand

func UnmarshalResampleCommand(rn *rawNode) (*ResampleCommand, error)

UnmarshalResampleCommand creates a ResampleCMD from Grafana's frontend query.

func (*ResampleCommand) Execute

func (gr *ResampleCommand) Execute(ctx context.Context, now time.Time, vars mathexp.Vars, tracer tracing.Tracer) (mathexp.Results, error)

Execute runs the command and returns the results or an error if the command failed to execute.

func (*ResampleCommand) NeedsVars

func (gr *ResampleCommand) NeedsVars() []string

NeedsVars returns the variable names (refIds) that are dependencies to execute the command and allows the command to fulfill the Command interface.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service is service representation for expression handling.

func ProvideService

func ProvideService(cfg *setting.Cfg, pluginClient plugins.Client, pCtxProvider *plugincontext.Provider,
	features featuremgmt.FeatureToggles, registerer prometheus.Registerer, tracer tracing.Tracer) *Service

func (*Service) BuildPipeline

func (s *Service) BuildPipeline(req *Request) (DataPipeline, error)

BuildPipeline builds a pipeline from a request.

func (*Service) ExecutePipeline

func (s *Service) ExecutePipeline(ctx context.Context, now time.Time, pipeline DataPipeline) (*backend.QueryDataResponse, error)

ExecutePipeline executes an expression pipeline and returns all the results.

func (*Service) TransformData

func (s *Service) TransformData(ctx context.Context, now time.Time, req *Request) (r *backend.QueryDataResponse, err error)

TransformData takes Queries which are either expressions nodes or are datasource requests.

type ThresholdCommand

type ThresholdCommand struct {
	ReferenceVar  string
	RefID         string
	ThresholdFunc string
	Conditions    []float64
}

func NewThresholdCommand

func NewThresholdCommand(refID, referenceVar, thresholdFunc string, conditions []float64) (*ThresholdCommand, error)

func UnmarshalThresholdCommand

func UnmarshalThresholdCommand(rn *rawNode) (*ThresholdCommand, error)

UnmarshalResampleCommand creates a ResampleCMD from Grafana's frontend query.

func (*ThresholdCommand) Execute

func (tc *ThresholdCommand) Execute(ctx context.Context, now time.Time, vars mathexp.Vars, tracer tracing.Tracer) (mathexp.Results, error)

func (*ThresholdCommand) NeedsVars

func (tc *ThresholdCommand) NeedsVars() []string

NeedsVars returns the variable names (refIds) that are dependencies to execute the command and allows the command to fulfill the Command interface.

type ThresholdConditionJSON

type ThresholdConditionJSON struct {
	Evaluator ConditionEvalJSON `json:"evaluator"`
}

type TimeRange

type TimeRange interface {
	AbsoluteTime(now time.Time) backend.TimeRange
}

TimeRange is a time.Time based TimeRange.

Directories

Path Synopsis
parse
Package parse builds parse trees for expressions as defined by expr.
Package parse builds parse trees for expressions as defined by expr.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL