The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/services/sqlstore/migrations/ualert/dash_alert.go

127 lines
2.9 KiB

package ualert
import (
"encoding/json"
"fmt"
"time"
)
type dashAlert struct {
Id int64
OrgId int64
DashboardId int64
PanelId int64
Name string
Message string
Frequency int64
For time.Duration
State string
Settings json.RawMessage
ParsedSettings *dashAlertSettings
DashboardUID string // Set from separate call
}
var slurpDashSQL = `
SELECT id,
org_id,
dashboard_id,
panel_id,
org_id,
name,
message,
frequency,
%s,
state,
settings
FROM
alert
`
// slurpDashAlerts loads all alerts from the alert database table into the
// the dashAlert type.
// Additionally it unmarshals the json settings for the alert into the
// ParsedSettings property of the dash alert.
func (m *migration) slurpDashAlerts() ([]dashAlert, error) {
dashAlerts := []dashAlert{}
err := m.sess.SQL(fmt.Sprintf(slurpDashSQL, m.mg.Dialect.Quote("for"))).Find(&dashAlerts)
if err != nil {
return nil, err
}
for i := range dashAlerts {
err = json.Unmarshal(dashAlerts[i].Settings, &dashAlerts[i].ParsedSettings)
if err != nil {
return nil, err
}
}
return dashAlerts, nil
}
// dashAlertSettings is a type for the JSON that is in the settings field of
// the alert table.
type dashAlertSettings struct {
NoDataState string `json:"noDataState"`
ExecutionErrorState string `json:"executionErrorState"`
Conditions []dashAlertCondition `json:"conditions"`
AlertRuleTags map[string]string `json:"alertRuleTags"`
Notifications []dashAlertNot `json:"notifications"`
}
// dashAlertNot is the object that represents the Notifications array in
// dashAlertSettings
type dashAlertNot struct {
UID string `json:"uid,omitempty"`
ID int64 `json:"id,omitempty"`
}
// dashAlertingConditionJSON is like classic.ClassicConditionJSON except that it
// includes the model property with the query.
type dashAlertCondition struct {
Evaluator conditionEvalJSON `json:"evaluator"`
Operator struct {
Type string `json:"type"`
} `json:"operator"`
Query struct {
Params []string `json:"params"`
DatasourceID int64 `json:"datasourceId"`
Model json.RawMessage
} `json:"query"`
Reducer struct {
// Params []interface{} `json:"params"` (Unused)
Type string `json:"type"`
}
}
type conditionEvalJSON struct {
Params []float64 `json:"params"`
Type string `json:"type"` // e.g. "gt"
}
// slurpDashUIDs returns a map of [orgID, dashboardId] -> dashUID.
func (m *migration) slurpDashUIDs() (map[[2]int64]string, error) {
dashIDs := []struct {
OrgID int64 `xorm:"org_id"`
ID int64 `xorm:"id"`
UID string `xorm:"uid"`
}{}
err := m.sess.SQL(`SELECT org_id, id, uid FROM dashboard`).Find(&dashIDs)
if err != nil {
return nil, err
}
idToUID := make(map[[2]int64]string, len(dashIDs))
for _, ds := range dashIDs {
idToUID[[2]int64{ds.OrgID, ds.ID}] = ds.UID
}
return idToUID, nil
}