Alerting: Remove datasource (name) from migration (#33544)

no longer needed as of https://github.com/grafana/grafana/pull/33416
for https://github.com/grafana/alerting-squad/issues/126
This commit is contained in:
Kyle Brandt 2021-04-30 07:47:01 -04:00 committed by GitHub
parent c0d28d9ed7
commit 713260f6fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 14 deletions

View File

@ -8,7 +8,7 @@ import (
"time"
)
func transConditions(set dashAlertSettings, orgID int64, dsIDMap map[[2]int64][2]string) (*condition, error) {
func transConditions(set dashAlertSettings, orgID int64, dsUIDMap dsUIDLookup) (*condition, error) {
refIDtoCondIdx := make(map[string][]int) // a map of original refIds to their corresponding condition index
for i, cond := range set.Conditions {
if len(cond.Query.Params) != 3 {
@ -56,7 +56,6 @@ func transConditions(set dashAlertSettings, orgID int64, dsIDMap map[[2]int64][2
}
// This referenced query/refID has different time ranges, so new queries are needed for each unique time range.
timeRanges := make([][2]string, 0, len(timeRangesToCondIdx)) // a sorted list of unique time ranges for the query
for tr := range timeRangesToCondIdx {
timeRanges = append(timeRanges, tr)
@ -119,8 +118,7 @@ func transConditions(set dashAlertSettings, orgID int64, dsIDMap map[[2]int64][2
}
// one could have an alert saved but datasource deleted, so can not require match.
dsInfo := dsIDMap[[2]int64{orgID, set.Conditions[condIdx].Query.DatasourceID}]
queryObj["datasource"] = dsInfo[1] // name is needed for UI to load query editor
dsUID := dsUIDMap.GetUID(orgID, set.Conditions[condIdx].Query.DatasourceID)
queryObj["refId"] = refID
encodedObj, err := json.Marshal(queryObj)
@ -140,7 +138,7 @@ func transConditions(set dashAlertSettings, orgID int64, dsIDMap map[[2]int64][2
RefID: refID,
Model: encodedObj,
RelativeTimeRange: *rTR,
DatasourceUID: dsInfo[0],
DatasourceUID: dsUID,
QueryType: queryType,
}
newCond.Data = append(newCond.Data, alertQuery)
@ -172,12 +170,10 @@ func transConditions(set dashAlertSettings, orgID int64, dsIDMap map[[2]int64][2
exprModel := struct {
Type string `json:"type"`
RefID string `json:"refId"`
Datasource string `json:"datasource"`
Conditions []classicConditionJSON `json:"conditions"`
}{
"classic_conditions",
ccRefID,
"__expr__",
conditions,
}

View File

@ -1,24 +1,30 @@
package ualert
// slurpDSIDs returns a map of [orgID, dataSourceId] -> [UID, Name].
func (m *migration) slurpDSIDs() (map[[2]int64][2]string, error) {
type dsUIDLookup map[[2]int64]string
// GetUID fetch thes datasource UID based on orgID+datasourceID
func (d dsUIDLookup) GetUID(orgID, datasourceID int64) string {
return d[[2]int64{orgID, datasourceID}]
}
// slurpDSIDs returns a map of [orgID, dataSourceId] -> UID.
func (m *migration) slurpDSIDs() (dsUIDLookup, error) {
dsIDs := []struct {
OrgID int64 `xorm:"org_id"`
ID int64 `xorm:"id"`
UID string `xorm:"uid"`
Name string
}{}
err := m.sess.SQL(`SELECT org_id, id, uid, name FROM data_source`).Find(&dsIDs)
err := m.sess.SQL(`SELECT org_id, id, uid FROM data_source`).Find(&dsIDs)
if err != nil {
return nil, err
}
idToUID := make(map[[2]int64][2]string, len(dsIDs))
idToUID := make(dsUIDLookup, len(dsIDs))
for _, ds := range dsIDs {
idToUID[[2]int64{ds.OrgID, ds.ID}] = [2]string{ds.UID, ds.Name}
idToUID[[2]int64{ds.OrgID, ds.ID}] = ds.UID
}
return idToUID, nil

View File

@ -50,7 +50,7 @@ func (m *migration) Exec(sess *xorm.Session, mg *migrator.Migrator) error {
return err
}
// [orgID, dataSourceId] -> [UID, Name]
// [orgID, dataSourceId] -> UID
dsIDMap, err := m.slurpDSIDs()
if err != nil {
return err