2023-02-27 16:57:15 -06:00
package api
import (
"time"
"github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/models"
)
// AlertRuleFromProvisionedAlertRule converts definitions.ProvisionedAlertRule to models.AlertRule
func AlertRuleFromProvisionedAlertRule ( a definitions . ProvisionedAlertRule ) ( models . AlertRule , error ) {
return models . AlertRule {
ID : a . ID ,
UID : a . UID ,
OrgID : a . OrgID ,
NamespaceUID : a . FolderUID ,
RuleGroup : a . RuleGroup ,
Title : a . Title ,
Condition : a . Condition ,
Data : a . Data ,
Updated : a . Updated ,
2023-02-28 15:21:41 -06:00
NoDataState : models . NoDataState ( a . NoDataState ) , // TODO there must be a validation
ExecErrState : models . ExecutionErrorState ( a . ExecErrState ) , // TODO there must be a validation
2023-02-27 16:57:15 -06:00
For : time . Duration ( a . For ) ,
Annotations : a . Annotations ,
Labels : a . Labels ,
IsPaused : a . IsPaused ,
} , nil
}
// ProvisionedAlertRuleFromAlertRule converts models.AlertRule to definitions.ProvisionedAlertRule and sets provided provenance status
func ProvisionedAlertRuleFromAlertRule ( rule models . AlertRule , provenance models . Provenance ) definitions . ProvisionedAlertRule {
return definitions . ProvisionedAlertRule {
ID : rule . ID ,
UID : rule . UID ,
OrgID : rule . OrgID ,
FolderUID : rule . NamespaceUID ,
RuleGroup : rule . RuleGroup ,
Title : rule . Title ,
For : model . Duration ( rule . For ) ,
Condition : rule . Condition ,
Data : rule . Data ,
Updated : rule . Updated ,
2023-02-28 15:21:41 -06:00
NoDataState : definitions . NoDataState ( rule . NoDataState ) , // TODO there may be a validation
ExecErrState : definitions . ExecutionErrorState ( rule . ExecErrState ) , // TODO there may be a validation
2023-02-27 16:57:15 -06:00
Annotations : rule . Annotations ,
Labels : rule . Labels ,
Provenance : definitions . Provenance ( provenance ) , // TODO validate enum conversion?
IsPaused : rule . IsPaused ,
}
}
// ProvisionedAlertRuleFromAlertRules converts a collection of models.AlertRule to definitions.ProvisionedAlertRules with provenance status models.ProvenanceNone
func ProvisionedAlertRuleFromAlertRules ( rules [ ] * models . AlertRule ) definitions . ProvisionedAlertRules {
result := make ( [ ] definitions . ProvisionedAlertRule , 0 , len ( rules ) )
for _ , r := range rules {
result = append ( result , ProvisionedAlertRuleFromAlertRule ( * r , models . ProvenanceNone ) )
}
return result
}
func AlertRuleGroupFromApi ( a definitions . AlertRuleGroup ) ( models . AlertRuleGroup , error ) {
ruleGroup := models . AlertRuleGroup {
Title : a . Title ,
FolderUID : a . FolderUID ,
Interval : a . Interval ,
}
for i := range a . Rules {
converted , err := AlertRuleFromProvisionedAlertRule ( a . Rules [ i ] )
if err != nil {
return models . AlertRuleGroup { } , err
}
ruleGroup . Rules = append ( ruleGroup . Rules , converted )
}
return ruleGroup , nil
}
func AlertRuleGroupToApi ( d models . AlertRuleGroup ) definitions . AlertRuleGroup {
rules := make ( [ ] definitions . ProvisionedAlertRule , 0 , len ( d . Rules ) )
for i := range d . Rules {
rules = append ( rules , ProvisionedAlertRuleFromAlertRule ( d . Rules [ i ] , d . Provenance ) )
}
return definitions . AlertRuleGroup {
Title : d . Title ,
FolderUID : d . FolderUID ,
Interval : d . Interval ,
Rules : rules ,
}
}