AlertingNG: Add For+Annotations to Grafana_Alert (#32793)

* add db columns
* Fix deserialisation issue of AlertRule For field (#32848)
* Update to latest alerting-api

Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com>
This commit is contained in:
Kyle Brandt
2021-04-09 16:50:04 +02:00
committed by GitHub
co-authored by Sofia Papagiannaki
parent fa45fc1833
commit 80dfa83380
8 changed files with 64 additions and 13 deletions
+4
View File
@@ -211,6 +211,8 @@ func toGettableExtendedRuleNode(r ngmodels.AlertRule) apimodels.GettableExtended
RuleGroup: r.RuleGroup,
NoDataState: apimodels.NoDataState(r.NoDataState),
ExecErrState: apimodels.ExecutionErrorState(r.ExecErrState),
For: r.For,
Annotations: r.Annotations,
},
}
}
@@ -225,6 +227,8 @@ func toPostableExtendedRuleNode(r ngmodels.AlertRule) apimodels.PostableExtended
UID: r.UID,
NoDataState: apimodels.NoDataState(r.NoDataState),
ExecErrState: apimodels.ExecutionErrorState(r.ExecErrState),
For: r.For,
Annotations: r.Annotations,
},
}
}
+21 -11
View File
@@ -158,23 +158,26 @@ func (api *API) ruleGroupByOldID(c *models.ReqContext) response.Response {
return response.Error(400, "unable to translate nodata/exec error settings",
fmt.Errorf("unable to translate nodata/exec error settings for alert id %v: %w", id, err))
}
// TODO: What to do with Rule Tags
// ruleTags := map[string]string{}
// for k, v := range oldAlert.Settings.Get("alertRuleTags").MustMap() {
// sV, ok := v.(string)
// if !ok {
// return response.Error(400, "unable to unmarshal rule tags",
// fmt.Errorf("unexpected type %T for tag %v", v, k))
// }
// ruleTags[k] = sV
// }
// TODO: Need place to put FOR duration
ruleTags := map[string]string{}
for k, v := range oldAlert.Settings.Get("alertRuleTags").MustMap() {
sV, ok := v.(string)
if !ok {
return response.Error(400, "unable to unmarshal rule tags",
fmt.Errorf("unexpected type %T for tag %v", v, k))
}
ruleTags[k] = sV
}
rule := ngmodels.AlertRule{
Title: oldAlert.Name,
Data: sseCond.Data,
Condition: sseCond.Condition,
NoDataState: *noDataSetting,
ExecErrState: *execErrSetting,
For: ngmodels.Duration(oldAlert.For),
Annotations: ruleTags,
}
rgc := apimodels.PostableRuleGroupConfig{
// TODO? Generate new name on conflict?
@@ -204,6 +207,7 @@ func (api *API) ruleGroupByOldID(c *models.ReqContext) response.Response {
}
return response.JSON(200, cmd)
}
func transAdjustInterval(freq int64) model.Duration {
// 10 corresponds to the SchedulerCfg, but TODO not worrying about fetching for now.
var baseFreq int64 = 10
@@ -212,6 +216,7 @@ func transAdjustInterval(freq int64) model.Duration {
}
return model.Duration(time.Duration((freq - (freq % baseFreq))) * time.Second)
}
func transGetAlertById(id int64, user models.SignedInUser) (*models.Alert, int, error) {
getAlert := &models.GetAlertByIdQuery{
Id: id,
@@ -224,6 +229,7 @@ func transGetAlertById(id int64, user models.SignedInUser) (*models.Alert, int,
}
return getAlert.Result, 0, nil
}
func transGetAlertsDashById(dashboardId int64, user models.SignedInUser) (*models.Dashboard, int, error) {
getDash := &models.GetDashboardQuery{
Id: dashboardId,
@@ -234,6 +240,7 @@ func transGetAlertsDashById(dashboardId int64, user models.SignedInUser) (*model
}
return getDash.Result, 0, nil
}
func transToSSECondition(m *models.Alert, user models.SignedInUser) (*ngmodels.Condition, error) {
sb, err := m.Settings.ToDB()
if err != nil {
@@ -245,6 +252,7 @@ func transToSSECondition(m *models.Alert, user models.SignedInUser) (*ngmodels.C
}
return evalCond, nil
}
func transNoDataExecSettings(m *models.Alert, user models.SignedInUser) (*ngmodels.NoDataState, *ngmodels.ExecutionErrorState, error) {
oldNoData := m.Settings.Get("noDataState").MustString()
noDataSetting, err := transNoData(oldNoData)
@@ -258,6 +266,7 @@ func transNoDataExecSettings(m *models.Alert, user models.SignedInUser) (*ngmode
}
return &noDataSetting, &execErrSetting, nil
}
func transNoData(s string) (ngmodels.NoDataState, error) {
switch s {
case "ok":
@@ -271,6 +280,7 @@ func transNoData(s string) (ngmodels.NoDataState, error) {
}
return ngmodels.NoData, fmt.Errorf("unrecognized No Data setting %v", s)
}
func transExecErr(s string) (ngmodels.ExecutionErrorState, error) {
switch s {
case "alerting":
@@ -6,6 +6,10 @@
"grafana_alert": {
"title": "prom query with SSE",
"condition": "condition",
"for": 5,
"annotations": {
"foo": "bar"
},
"data": [
{
"refId": "query",