mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Replace eval.Condition with models.Condition (#31909)
This commit is contained in:
parent
6fe2baf168
commit
53bccf1b77
@ -62,7 +62,7 @@ func (api *API) RegisterAPIEndpoints() {
|
|||||||
|
|
||||||
// conditionEvalEndpoint handles POST /api/alert-definitions/eval.
|
// conditionEvalEndpoint handles POST /api/alert-definitions/eval.
|
||||||
func (api *API) conditionEvalEndpoint(c *models.ReqContext, cmd ngmodels.EvalAlertConditionCommand) response.Response {
|
func (api *API) conditionEvalEndpoint(c *models.ReqContext, cmd ngmodels.EvalAlertConditionCommand) response.Response {
|
||||||
evalCond := eval.Condition{
|
evalCond := ngmodels.Condition{
|
||||||
RefID: cmd.Condition,
|
RefID: cmd.Condition,
|
||||||
OrgID: c.SignedInUser.OrgId,
|
OrgID: c.SignedInUser.OrgId,
|
||||||
QueriesAndExpressions: cmd.Data,
|
QueriesAndExpressions: cmd.Data,
|
||||||
@ -165,7 +165,7 @@ func (api *API) updateAlertDefinitionEndpoint(c *models.ReqContext, cmd ngmodels
|
|||||||
cmd.UID = c.Params(":alertDefinitionUID")
|
cmd.UID = c.Params(":alertDefinitionUID")
|
||||||
cmd.OrgID = c.SignedInUser.OrgId
|
cmd.OrgID = c.SignedInUser.OrgId
|
||||||
|
|
||||||
evalCond := eval.Condition{
|
evalCond := ngmodels.Condition{
|
||||||
RefID: cmd.Condition,
|
RefID: cmd.Condition,
|
||||||
OrgID: c.SignedInUser.OrgId,
|
OrgID: c.SignedInUser.OrgId,
|
||||||
QueriesAndExpressions: cmd.Data,
|
QueriesAndExpressions: cmd.Data,
|
||||||
@ -185,7 +185,7 @@ func (api *API) updateAlertDefinitionEndpoint(c *models.ReqContext, cmd ngmodels
|
|||||||
func (api *API) createAlertDefinitionEndpoint(c *models.ReqContext, cmd ngmodels.SaveAlertDefinitionCommand) response.Response {
|
func (api *API) createAlertDefinitionEndpoint(c *models.ReqContext, cmd ngmodels.SaveAlertDefinitionCommand) response.Response {
|
||||||
cmd.OrgID = c.SignedInUser.OrgId
|
cmd.OrgID = c.SignedInUser.OrgId
|
||||||
|
|
||||||
evalCond := eval.Condition{
|
evalCond := ngmodels.Condition{
|
||||||
RefID: cmd.Condition,
|
RefID: cmd.Condition,
|
||||||
OrgID: c.SignedInUser.OrgId,
|
OrgID: c.SignedInUser.OrgId,
|
||||||
QueriesAndExpressions: cmd.Data,
|
QueriesAndExpressions: cmd.Data,
|
||||||
@ -253,7 +253,7 @@ func (api *API) alertDefinitionUnpauseEndpoint(c *models.ReqContext, cmd ngmodel
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LoadAlertCondition returns a Condition object for the given alertDefinitionID.
|
// LoadAlertCondition returns a Condition object for the given alertDefinitionID.
|
||||||
func (api *API) LoadAlertCondition(alertDefinitionUID string, orgID int64) (*eval.Condition, error) {
|
func (api *API) LoadAlertCondition(alertDefinitionUID string, orgID int64) (*ngmodels.Condition, error) {
|
||||||
q := ngmodels.GetAlertDefinitionByUIDQuery{UID: alertDefinitionUID, OrgID: orgID}
|
q := ngmodels.GetAlertDefinitionByUIDQuery{UID: alertDefinitionUID, OrgID: orgID}
|
||||||
if err := api.Store.GetAlertDefinitionByUID(&q); err != nil {
|
if err := api.Store.GetAlertDefinitionByUID(&q); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -265,14 +265,14 @@ func (api *API) LoadAlertCondition(alertDefinitionUID string, orgID int64) (*eva
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &eval.Condition{
|
return &ngmodels.Condition{
|
||||||
RefID: alertDefinition.Condition,
|
RefID: alertDefinition.Condition,
|
||||||
OrgID: alertDefinition.OrgID,
|
OrgID: alertDefinition.OrgID,
|
||||||
QueriesAndExpressions: alertDefinition.Data,
|
QueriesAndExpressions: alertDefinition.Data,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *API) validateCondition(c eval.Condition, user *models.SignedInUser, skipCache bool) error {
|
func (api *API) validateCondition(c ngmodels.Condition, user *models.SignedInUser, skipCache bool) error {
|
||||||
var refID string
|
var refID string
|
||||||
|
|
||||||
if len(c.QueriesAndExpressions) == 0 {
|
if len(c.QueriesAndExpressions) == 0 {
|
||||||
|
@ -42,15 +42,6 @@ func (e *invalidEvalResultFormatError) Unwrap() error {
|
|||||||
return e.err
|
return e.err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Condition contains backend expressions and queries and the RefID
|
|
||||||
// of the query or expression that will be evaluated.
|
|
||||||
type Condition struct {
|
|
||||||
RefID string `json:"refId"`
|
|
||||||
OrgID int64 `json:"-"`
|
|
||||||
|
|
||||||
QueriesAndExpressions []models.AlertQuery `json:"queriesAndExpressions"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExecutionResults contains the unevaluated results from executing
|
// ExecutionResults contains the unevaluated results from executing
|
||||||
// a condition.
|
// a condition.
|
||||||
type ExecutionResults struct {
|
type ExecutionResults struct {
|
||||||
@ -88,12 +79,6 @@ func (s state) String() string {
|
|||||||
return [...]string{"Normal", "Alerting"}[s]
|
return [...]string{"Normal", "Alerting"}[s]
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValid checks the condition's validity.
|
|
||||||
func (c Condition) IsValid() bool {
|
|
||||||
// TODO search for refIDs in QueriesAndExpressions
|
|
||||||
return len(c.QueriesAndExpressions) != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// AlertExecCtx is the context provided for executing an alert condition.
|
// AlertExecCtx is the context provided for executing an alert condition.
|
||||||
type AlertExecCtx struct {
|
type AlertExecCtx struct {
|
||||||
OrgID int64
|
OrgID int64
|
||||||
@ -103,7 +88,7 @@ type AlertExecCtx struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// execute runs the Condition's expressions or queries.
|
// execute runs the Condition's expressions or queries.
|
||||||
func (c *Condition) execute(ctx AlertExecCtx, now time.Time, dataService *tsdb.Service) (*ExecutionResults, error) {
|
func execute(ctx AlertExecCtx, c *models.Condition, now time.Time, dataService *tsdb.Service) (*ExecutionResults, error) {
|
||||||
result := ExecutionResults{}
|
result := ExecutionResults{}
|
||||||
if !c.IsValid() {
|
if !c.IsValid() {
|
||||||
return nil, fmt.Errorf("invalid conditions")
|
return nil, fmt.Errorf("invalid conditions")
|
||||||
@ -224,13 +209,13 @@ func (evalResults Results) AsDataFrame() data.Frame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConditionEval executes conditions and evaluates the result.
|
// ConditionEval executes conditions and evaluates the result.
|
||||||
func (e *Evaluator) ConditionEval(condition *Condition, now time.Time, dataService *tsdb.Service) (Results, error) {
|
func (e *Evaluator) ConditionEval(condition *models.Condition, now time.Time, dataService *tsdb.Service) (Results, error) {
|
||||||
alertCtx, cancelFn := context.WithTimeout(context.Background(), alertingEvaluationTimeout)
|
alertCtx, cancelFn := context.WithTimeout(context.Background(), alertingEvaluationTimeout)
|
||||||
defer cancelFn()
|
defer cancelFn()
|
||||||
|
|
||||||
alertExecCtx := AlertExecCtx{OrgID: condition.OrgID, Ctx: alertCtx, ExpressionsEnabled: e.Cfg.ExpressionsEnabled}
|
alertExecCtx := AlertExecCtx{OrgID: condition.OrgID, Ctx: alertCtx, ExpressionsEnabled: e.Cfg.ExpressionsEnabled}
|
||||||
|
|
||||||
execResult, err := condition.execute(alertExecCtx, now, dataService)
|
execResult, err := execute(alertExecCtx, condition, now, dataService)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to execute conditions: %w", err)
|
return nil, fmt.Errorf("failed to execute conditions: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ func (sch *schedule) definitionRoutine(grafanaCtx context.Context, key models.Al
|
|||||||
sch.log.Debug("new alert definition version fetched", "title", alertDefinition.Title, "key", key, "version", alertDefinition.Version)
|
sch.log.Debug("new alert definition version fetched", "title", alertDefinition.Title, "key", key, "version", alertDefinition.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
condition := eval.Condition{
|
condition := models.Condition{
|
||||||
RefID: alertDefinition.Condition,
|
RefID: alertDefinition.Condition,
|
||||||
OrgID: alertDefinition.OrgID,
|
OrgID: alertDefinition.OrgID,
|
||||||
QueriesAndExpressions: alertDefinition.Data,
|
QueriesAndExpressions: alertDefinition.Data,
|
||||||
|
Loading…
Reference in New Issue
Block a user