AlertingNG: rename Condition properties to match front end (#32267)
This commit is contained in:
@@ -192,9 +192,9 @@ func (api *API) conditionEvalOldEndpointByID(c *models.ReqContext) response.Resp
|
||||
// conditionEvalEndpoint handles POST /api/alert-definitions/eval.
|
||||
func (api *API) conditionEvalEndpoint(c *models.ReqContext, cmd ngmodels.EvalAlertConditionCommand) response.Response {
|
||||
evalCond := ngmodels.Condition{
|
||||
RefID: cmd.Condition,
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
QueriesAndExpressions: cmd.Data,
|
||||
Condition: cmd.Condition,
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
Data: cmd.Data,
|
||||
}
|
||||
if err := api.validateCondition(evalCond, c.SignedInUser, c.SkipCache); err != nil {
|
||||
return response.Error(400, "invalid condition", err)
|
||||
@@ -295,9 +295,9 @@ func (api *API) updateAlertDefinitionEndpoint(c *models.ReqContext, cmd ngmodels
|
||||
cmd.OrgID = c.SignedInUser.OrgId
|
||||
|
||||
evalCond := ngmodels.Condition{
|
||||
RefID: cmd.Condition,
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
QueriesAndExpressions: cmd.Data,
|
||||
Condition: cmd.Condition,
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
Data: cmd.Data,
|
||||
}
|
||||
if err := api.validateCondition(evalCond, c.SignedInUser, c.SkipCache); err != nil {
|
||||
return response.Error(400, "invalid condition", err)
|
||||
@@ -315,9 +315,9 @@ func (api *API) createAlertDefinitionEndpoint(c *models.ReqContext, cmd ngmodels
|
||||
cmd.OrgID = c.SignedInUser.OrgId
|
||||
|
||||
evalCond := ngmodels.Condition{
|
||||
RefID: cmd.Condition,
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
QueriesAndExpressions: cmd.Data,
|
||||
Condition: cmd.Condition,
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
Data: cmd.Data,
|
||||
}
|
||||
if err := api.validateCondition(evalCond, c.SignedInUser, c.SkipCache); err != nil {
|
||||
return response.Error(400, "invalid condition", err)
|
||||
@@ -395,22 +395,22 @@ func (api *API) LoadAlertCondition(alertDefinitionUID string, orgID int64) (*ngm
|
||||
}
|
||||
|
||||
return &ngmodels.Condition{
|
||||
RefID: alertDefinition.Condition,
|
||||
OrgID: alertDefinition.OrgID,
|
||||
QueriesAndExpressions: alertDefinition.Data,
|
||||
Condition: alertDefinition.Condition,
|
||||
OrgID: alertDefinition.OrgID,
|
||||
Data: alertDefinition.Data,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (api *API) validateCondition(c ngmodels.Condition, user *models.SignedInUser, skipCache bool) error {
|
||||
var refID string
|
||||
|
||||
if len(c.QueriesAndExpressions) == 0 {
|
||||
if len(c.Data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, query := range c.QueriesAndExpressions {
|
||||
if c.RefID == query.RefID {
|
||||
refID = c.RefID
|
||||
for _, query := range c.Data {
|
||||
if c.Condition == query.RefID {
|
||||
refID = c.Condition
|
||||
}
|
||||
|
||||
datasourceUID, err := query.GetDatasource()
|
||||
@@ -433,7 +433,7 @@ func (api *API) validateCondition(c ngmodels.Condition, user *models.SignedInUse
|
||||
}
|
||||
|
||||
if refID == "" {
|
||||
return fmt.Errorf("condition %s not found in any query or expression", c.RefID)
|
||||
return fmt.Errorf("condition %s not found in any query or expression", c.Condition)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -110,8 +110,8 @@ func GetQueryDataRequest(ctx AlertExecCtx, c *models.Condition, now time.Time) (
|
||||
Queries: []backend.DataQuery{},
|
||||
}
|
||||
|
||||
for i := range c.QueriesAndExpressions {
|
||||
q := c.QueriesAndExpressions[i]
|
||||
for i := range c.Data {
|
||||
q := c.Data[i]
|
||||
model, err := q.GetModel()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get query model: %w", err)
|
||||
@@ -157,7 +157,7 @@ func execute(ctx AlertExecCtx, c *models.Condition, now time.Time, dataService *
|
||||
}
|
||||
|
||||
for refID, res := range pbRes.Responses {
|
||||
if refID != c.RefID {
|
||||
if refID != c.Condition {
|
||||
continue
|
||||
}
|
||||
result.Results = res.Frames
|
||||
|
||||
@@ -134,14 +134,17 @@ type UpdateAlertDefinitionPausedCommand struct {
|
||||
// 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:"-"`
|
||||
// Condition is the RefID of the query or expression from
|
||||
// the Data property to get the results for.
|
||||
Condition string `json:"condition"`
|
||||
OrgID int64 `json:"-"`
|
||||
|
||||
QueriesAndExpressions []AlertQuery `json:"queriesAndExpressions"`
|
||||
// Data is an array of data source queries and/or server side expressions.
|
||||
Data []AlertQuery `json:"data"`
|
||||
}
|
||||
|
||||
// IsValid checks the condition's validity.
|
||||
func (c Condition) IsValid() bool {
|
||||
// TODO search for refIDs in QueriesAndExpressions
|
||||
return len(c.QueriesAndExpressions) != 0
|
||||
return len(c.Data) != 0
|
||||
}
|
||||
|
||||
@@ -64,9 +64,9 @@ func (sch *schedule) definitionRoutine(grafanaCtx context.Context, key models.Al
|
||||
}
|
||||
|
||||
condition := models.Condition{
|
||||
RefID: alertDefinition.Condition,
|
||||
OrgID: alertDefinition.OrgID,
|
||||
QueriesAndExpressions: alertDefinition.Data,
|
||||
Condition: alertDefinition.Condition,
|
||||
OrgID: alertDefinition.OrgID,
|
||||
Data: alertDefinition.Data,
|
||||
}
|
||||
results, err := sch.evaluator.ConditionEval(&condition, ctx.now, sch.dataService)
|
||||
end = timeNow()
|
||||
|
||||
Reference in New Issue
Block a user