Alerting: Add SaveAlertInstancesForRule instance store method (#94505)

Alerting: Add SaveAlertInstancesForRule method to the InstanceStore interface
This commit is contained in:
Alexander Akhmetov
2024-10-11 13:47:44 +02:00
committed by GitHub
parent e2672021bc
commit 0a4e6ff86b
10 changed files with 85 additions and 29 deletions
+10
View File
@@ -464,6 +464,11 @@ type AlertRuleKeyWithVersion struct {
AlertRuleKey `xorm:"extends"`
}
type AlertRuleKeyWithGroup struct {
RuleGroup string
AlertRuleKey `xorm:"extends"`
}
type AlertRuleKeyWithId struct {
AlertRuleKey
ID int64
@@ -517,6 +522,11 @@ func (alertRule *AlertRule) GetKey() AlertRuleKey {
return AlertRuleKey{OrgID: alertRule.OrgID, UID: alertRule.UID}
}
// GetKeyWithGroup returns the alert definitions identifier
func (alertRule *AlertRule) GetKeyWithGroup() AlertRuleKeyWithGroup {
return AlertRuleKeyWithGroup{AlertRuleKey: alertRule.GetKey(), RuleGroup: alertRule.RuleGroup}
}
// GetGroupKey returns the identifier of a group the rule belongs to
func (alertRule *AlertRule) GetGroupKey() AlertRuleGroupKey {
return AlertRuleGroupKey{OrgID: alertRule.OrgID, NamespaceUID: alertRule.NamespaceUID, RuleGroup: alertRule.RuleGroup}
@@ -884,3 +884,27 @@ func TestTimeRangeYAML(t *testing.T) {
require.NoError(t, err)
require.Equal(t, yamlRaw, string(serialized))
}
func TestAlertRuleGetKey(t *testing.T) {
t.Run("should return correct key", func(t *testing.T) {
rule := RuleGen.GenerateRef()
expected := AlertRuleKey{
OrgID: rule.OrgID,
UID: rule.UID,
}
require.Equal(t, expected, rule.GetKey())
})
}
func TestAlertRuleGetKeyWithGroup(t *testing.T) {
t.Run("should return correct key", func(t *testing.T) {
rule := RuleGen.With(
RuleMuts.WithUniqueGroupIndex(),
).GenerateRef()
expected := AlertRuleKeyWithGroup{
AlertRuleKey: rule.GetKey(),
RuleGroup: rule.RuleGroup,
}
require.Equal(t, expected, rule.GetKeyWithGroup())
})
}
+8
View File
@@ -587,6 +587,14 @@ func GenerateRuleKey(orgID int64) AlertRuleKey {
}
}
// GenerateRuleKeyWithGroup generates a random alert rule key with group
func GenerateRuleKeyWithGroup(orgID int64) AlertRuleKeyWithGroup {
return AlertRuleKeyWithGroup{
AlertRuleKey: GenerateRuleKey(orgID),
RuleGroup: util.GenerateShortUID(),
}
}
// GenerateGroupKey generates a random group key
func GenerateGroupKey(orgID int64) AlertRuleGroupKey {
return AlertRuleGroupKey{