AlertingMigration: Create alert_rule_version entry (#33585)
Create the alert rule version entry during the migration so it is consistent with rules created via api. for https://github.com/grafana/alerting-squad/issues/123
This commit is contained in:
@@ -14,6 +14,7 @@ type alertRule struct {
|
|||||||
Condition string
|
Condition string
|
||||||
Data []alertQuery
|
Data []alertQuery
|
||||||
IntervalSeconds int64
|
IntervalSeconds int64
|
||||||
|
Version int64
|
||||||
Uid string
|
Uid string
|
||||||
NamespaceUid string
|
NamespaceUid string
|
||||||
RuleGroup string
|
RuleGroup string
|
||||||
@@ -22,7 +23,53 @@ type alertRule struct {
|
|||||||
For duration
|
For duration
|
||||||
Updated time.Time
|
Updated time.Time
|
||||||
Annotations map[string]string
|
Annotations map[string]string
|
||||||
// Labels map[string]string (Labels are not Created in the migration)
|
Labels map[string]string // (Labels are not Created in the migration)
|
||||||
|
}
|
||||||
|
|
||||||
|
type alertRuleVersion struct {
|
||||||
|
RuleOrgID int64 `xorm:"rule_org_id"`
|
||||||
|
RuleUID string `xorm:"rule_uid"`
|
||||||
|
RuleNamespaceUID string `xorm:"rule_namespace_uid"`
|
||||||
|
RuleGroup string
|
||||||
|
ParentVersion int64
|
||||||
|
RestoredFrom int64
|
||||||
|
Version int64
|
||||||
|
|
||||||
|
Created time.Time
|
||||||
|
Title string
|
||||||
|
Condition string
|
||||||
|
Data []alertQuery
|
||||||
|
IntervalSeconds int64
|
||||||
|
NoDataState string
|
||||||
|
ExecErrState string
|
||||||
|
// ideally this field should have been apimodels.ApiDuration
|
||||||
|
// but this is currently not possible because of circular dependencies
|
||||||
|
For duration
|
||||||
|
Annotations map[string]string
|
||||||
|
Labels map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *alertRule) makeVersion() *alertRuleVersion {
|
||||||
|
return &alertRuleVersion{
|
||||||
|
RuleOrgID: a.OrgId,
|
||||||
|
RuleUID: a.Uid,
|
||||||
|
RuleNamespaceUID: a.NamespaceUid,
|
||||||
|
RuleGroup: a.RuleGroup,
|
||||||
|
ParentVersion: 0,
|
||||||
|
RestoredFrom: 0,
|
||||||
|
Version: 1,
|
||||||
|
|
||||||
|
Created: time.Now().UTC(),
|
||||||
|
Title: a.Title,
|
||||||
|
Condition: a.Condition,
|
||||||
|
Data: a.Data,
|
||||||
|
IntervalSeconds: a.IntervalSeconds,
|
||||||
|
NoDataState: a.NoDataState,
|
||||||
|
ExecErrState: a.ExecErrState,
|
||||||
|
For: a.For,
|
||||||
|
Annotations: a.Annotations,
|
||||||
|
Labels: map[string]string{},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMigrationInfo(da dashAlert) string {
|
func getMigrationInfo(da dashAlert) string {
|
||||||
@@ -41,15 +88,17 @@ func (m *migration) makeAlertRule(cond condition, da dashAlert, folderUID string
|
|||||||
ar := &alertRule{
|
ar := &alertRule{
|
||||||
OrgId: da.OrgId,
|
OrgId: da.OrgId,
|
||||||
Title: da.Name, // TODO: Make sure all names are unique, make new name on constraint insert error.
|
Title: da.Name, // TODO: Make sure all names are unique, make new name on constraint insert error.
|
||||||
|
Uid: util.GenerateShortUID(),
|
||||||
Condition: cond.Condition,
|
Condition: cond.Condition,
|
||||||
Data: cond.Data,
|
Data: cond.Data,
|
||||||
IntervalSeconds: ruleAdjustInterval(da.Frequency),
|
IntervalSeconds: ruleAdjustInterval(da.Frequency),
|
||||||
|
Version: 1,
|
||||||
NamespaceUid: folderUID, // Folder already created, comes from env var.
|
NamespaceUid: folderUID, // Folder already created, comes from env var.
|
||||||
RuleGroup: da.Name,
|
RuleGroup: da.Name,
|
||||||
For: duration(da.For),
|
For: duration(da.For),
|
||||||
Updated: time.Now().UTC(),
|
Updated: time.Now().UTC(),
|
||||||
Annotations: annotations,
|
Annotations: annotations,
|
||||||
Uid: util.GenerateShortUID(),
|
Labels: map[string]string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|||||||
@@ -174,6 +174,12 @@ func (m *migration) Exec(sess *xorm.Session, mg *migrator.Migrator) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create entry in alert_rule_version
|
||||||
|
_, err = m.sess.Insert(rule.makeVersion())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user