mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Default pending period (for) to 0s when the rule is created using file provisioning (#94646)
Alerting: default for to 0s when the rule is created using file provisioning
This commit is contained in:
parent
384562e1ad
commit
bfbbdf5efb
@ -97,11 +97,17 @@ func (rule *AlertRuleV1) mapToModel(orgID int64) (models.AlertRule, error) {
|
|||||||
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse: no UID set", alertRule.Title)
|
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse: no UID set", alertRule.Title)
|
||||||
}
|
}
|
||||||
alertRule.OrgID = orgID
|
alertRule.OrgID = orgID
|
||||||
duration, err := model.ParseDuration(rule.For.Value())
|
|
||||||
if err != nil {
|
duration := model.Duration(0)
|
||||||
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse: %w", alertRule.Title, err)
|
if rule.For.Value() != "" {
|
||||||
|
var err error
|
||||||
|
duration, err = model.ParseDuration(rule.For.Value())
|
||||||
|
if err != nil {
|
||||||
|
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse 'for' field: %w", alertRule.Title, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
alertRule.For = time.Duration(duration)
|
alertRule.For = time.Duration(duration)
|
||||||
|
|
||||||
dasboardUID := rule.DasboardUID.Value()
|
dasboardUID := rule.DasboardUID.Value()
|
||||||
dashboardUID := rule.DashboardUID.Value()
|
dashboardUID := rule.DashboardUID.Value()
|
||||||
alertRule.DashboardUID = withFallback(dashboardUID, dasboardUID) // Use correct spelling over supported typo.
|
alertRule.DashboardUID = withFallback(dashboardUID, dasboardUID) // Use correct spelling over supported typo.
|
||||||
|
@ -102,11 +102,12 @@ func TestRules(t *testing.T) {
|
|||||||
_, err := rule.mapToModel(1)
|
_, err := rule.mapToModel(1)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
})
|
})
|
||||||
t.Run("a rule with out a for duration should error", func(t *testing.T) {
|
t.Run("a rule without a for duration should default to 0s", func(t *testing.T) {
|
||||||
rule := validRuleV1(t)
|
rule := validRuleV1(t)
|
||||||
rule.For = values.StringValue{}
|
rule.For = values.StringValue{}
|
||||||
_, err := rule.mapToModel(1)
|
ruleMapped, err := rule.mapToModel(1)
|
||||||
require.Error(t, err)
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, time.Duration(0), ruleMapped.For)
|
||||||
})
|
})
|
||||||
t.Run("a rule with an invalid for duration should error", func(t *testing.T) {
|
t.Run("a rule with an invalid for duration should error", func(t *testing.T) {
|
||||||
rule := validRuleV1(t)
|
rule := validRuleV1(t)
|
||||||
|
Loading…
Reference in New Issue
Block a user