eval

package
v0.0.1-test Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package eval executes the condition for an alert definition, evaluates the condition results, and returns the alert instance states.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResultGen

func ResultGen(mutators ...ResultMutator) func() Result

Types

type AlertExecCtx

type AlertExecCtx struct {
	User               *user.SignedInUser
	ExpressionsEnabled bool
	Log                log.Logger

	Ctx context.Context
}

AlertExecCtx is the context provided for executing an alert condition.

type Evaluator

type Evaluator interface {
	// ConditionEval executes conditions and evaluates the result.
	ConditionEval(ctx context.Context, user *user.SignedInUser, condition models.Condition, now time.Time) Results
	// QueriesAndExpressionsEval executes queries and expressions and returns the result.
	QueriesAndExpressionsEval(ctx context.Context, user *user.SignedInUser, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error)
	// Validate validates that the condition is correct. Returns nil if the condition is correct. Otherwise, error that describes the failure
	Validate(ctx context.Context, user *user.SignedInUser, condition models.Condition) error
}

func NewEvaluator

func NewEvaluator(
	cfg *setting.Cfg,
	log log.Logger,
	datasourceCache datasources.CacheService,
	expressionService *expr.Service) Evaluator

type ExecutionResults

type ExecutionResults struct {
	// Condition contains the results of the condition
	Condition data.Frames

	// Results contains the results of all queries, reduce and math expressions
	Results map[string]data.Frames

	// NoData contains the DatasourceUID for RefIDs that returned no data.
	NoData map[string]string

	Error error
}

ExecutionResults contains the unevaluated results from executing a condition.

type FakeEvaluator

type FakeEvaluator struct {
	mock.Mock
}

FakeEvaluator is an autogenerated mock type for the Evaluator type

func (*FakeEvaluator) ConditionEval

func (_m *FakeEvaluator) ConditionEval(ctx context.Context, _a1 *user.SignedInUser, condition models.Condition, now time.Time) Results

ConditionEval provides a mock function with given fields: ctx, _a1, condition, now

func (*FakeEvaluator) EXPECT

func (_m *FakeEvaluator) EXPECT() *FakeEvaluator_Expecter

func (*FakeEvaluator) QueriesAndExpressionsEval

func (_m *FakeEvaluator) QueriesAndExpressionsEval(ctx context.Context, _a1 *user.SignedInUser, data []models.AlertQuery, now time.Time) (*backend.QueryDataResponse, error)

QueriesAndExpressionsEval provides a mock function with given fields: ctx, _a1, data, now

func (*FakeEvaluator) Validate

func (_m *FakeEvaluator) Validate(ctx context.Context, _a1 *user.SignedInUser, condition models.Condition) error

Validate provides a mock function with given fields: ctx, _a1, condition

type FakeEvaluator_ConditionEval_Call

type FakeEvaluator_ConditionEval_Call struct {
	*mock.Call
}

FakeEvaluator_ConditionEval_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ConditionEval'

func (*FakeEvaluator_ConditionEval_Call) Return

func (*FakeEvaluator_ConditionEval_Call) Run

type FakeEvaluator_Expecter

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

func (*FakeEvaluator_Expecter) ConditionEval

func (_e *FakeEvaluator_Expecter) ConditionEval(ctx interface{}, _a1 interface{}, condition interface{}, now interface{}) *FakeEvaluator_ConditionEval_Call

ConditionEval is a helper method to define mock.On call

  • ctx context.Context
  • _a1 *user.SignedInUser
  • condition models.Condition
  • now time.Time

func (*FakeEvaluator_Expecter) QueriesAndExpressionsEval

func (_e *FakeEvaluator_Expecter) QueriesAndExpressionsEval(ctx interface{}, _a1 interface{}, data interface{}, now interface{}) *FakeEvaluator_QueriesAndExpressionsEval_Call

QueriesAndExpressionsEval is a helper method to define mock.On call

  • ctx context.Context
  • _a1 *user.SignedInUser
  • data []models.AlertQuery
  • now time.Time

func (*FakeEvaluator_Expecter) Validate

func (_e *FakeEvaluator_Expecter) Validate(ctx interface{}, _a1 interface{}, condition interface{}) *FakeEvaluator_Validate_Call

Validate is a helper method to define mock.On call

  • ctx context.Context
  • _a1 *user.SignedInUser
  • condition models.Condition

type FakeEvaluator_QueriesAndExpressionsEval_Call

type FakeEvaluator_QueriesAndExpressionsEval_Call struct {
	*mock.Call
}

FakeEvaluator_QueriesAndExpressionsEval_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'QueriesAndExpressionsEval'

func (*FakeEvaluator_QueriesAndExpressionsEval_Call) Return

func (*FakeEvaluator_QueriesAndExpressionsEval_Call) Run

type FakeEvaluator_Validate_Call

type FakeEvaluator_Validate_Call struct {
	*mock.Call
}

FakeEvaluator_Validate_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Validate'

func (*FakeEvaluator_Validate_Call) Return

func (*FakeEvaluator_Validate_Call) Run

type NumberValueCapture

type NumberValueCapture struct {
	Var    string // RefID
	Labels data.Labels
	Value  *float64
}

type Result

type Result struct {
	Instance data.Labels
	State    State // Enum

	// Error message for Error state. should be nil if State != Error.
	Error error

	// Results contains the results of all queries, reduce and math expressions
	Results map[string]data.Frames

	// Values contains the RefID and value of reduce and math expressions.
	// It does not contain values for classic conditions as the values
	// in classic conditions do not have a RefID.
	Values map[string]NumberValueCapture

	EvaluatedAt        time.Time
	EvaluationDuration time.Duration
	// EvaluationString is a string representation of evaluation data such
	// as EvalMatches (from "classic condition"), and in the future from operations
	// like SSE "math".
	EvaluationString string
}

Result contains the evaluated State of an alert instance identified by its labels.

type ResultMutator

type ResultMutator func(r *Result)

func WithEvaluatedAt

func WithEvaluatedAt(time time.Time) ResultMutator

func WithLabels

func WithLabels(labels data.Labels) ResultMutator

func WithState

func WithState(state State) ResultMutator

type Results

type Results []Result

Results is a slice of evaluated alert instances states.

func GenerateResults

func GenerateResults(count int, generator func() Result) Results

func (Results) AsDataFrame

func (evalResults Results) AsDataFrame() data.Frame

AsDataFrame forms the EvalResults in Frame suitable for displaying in the table panel of the front end. It displays one row per alert instance, with a column for each label and one for the alerting state.

func (Results) HasErrors

func (evalResults Results) HasErrors() bool

type State

type State int

State is an enum of the evaluation State for an alert instance.

const (
	// Normal is the eval state for an alert instance condition
	// that evaluated to false.
	Normal State = iota

	// Alerting is the eval state for an alert instance condition
	// that evaluated to true (Alerting).
	Alerting

	// Pending is the eval state for an alert instance condition
	// that evaluated to true (Alerting) but has not yet met
	// the For duration defined in AlertRule.
	Pending

	// NoData is the eval state for an alert rule condition
	// that evaluated to NoData.
	NoData

	// Error is the eval state for an alert rule condition
	// that evaluated to Error.
	Error
)

func RandomState

func RandomState() State

func (State) IsValid

func (s State) IsValid() bool

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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