@ -13,7 +13,7 @@ type InstanceStore interface {
GetAlertInstance ( cmd * models . GetAlertInstanceQuery ) error
GetAlertInstance ( cmd * models . GetAlertInstanceQuery ) error
ListAlertInstances ( cmd * models . ListAlertInstancesQuery ) error
ListAlertInstances ( cmd * models . ListAlertInstancesQuery ) error
SaveAlertInstance ( cmd * models . SaveAlertInstanceCommand ) error
SaveAlertInstance ( cmd * models . SaveAlertInstanceCommand ) error
FetchOrgIds ( cmd * models . FetchUniqueOrgIdsQuery ) error
FetchOrgIds ( ) ( [ ] int64 , error )
}
}
// GetAlertInstance is a handler for retrieving an alert instance based on OrgId, AlertDefintionID, and
// GetAlertInstance is a handler for retrieving an alert instance based on OrgId, AlertDefintionID, and
@ -24,8 +24,8 @@ func (st DBstore) GetAlertInstance(cmd *models.GetAlertInstanceQuery) error {
s := strings . Builder { }
s := strings . Builder { }
s . WriteString ( ` SELECT * FROM alert_instance
s . WriteString ( ` SELECT * FROM alert_instance
WHERE
WHERE
def _org_id= ? AND
rule _org_id= ? AND
def _uid= ? AND
rule _uid= ? AND
labels_hash = ?
labels_hash = ?
` )
` )
@ -38,7 +38,7 @@ func (st DBstore) GetAlertInstance(cmd *models.GetAlertInstanceQuery) error {
has , err := sess . SQL ( s . String ( ) , params ... ) . Get ( & instance )
has , err := sess . SQL ( s . String ( ) , params ... ) . Get ( & instance )
if ! has {
if ! has {
return fmt . Errorf ( "instance not found for labels %v (hash: %v), alert definition %v (org %v)" , cmd . Labels , hash , cmd . RuleUID , cmd . RuleOrgID )
return fmt . Errorf ( "instance not found for labels %v (hash: %v), alert rule %v (org %v)" , cmd . Labels , hash , cmd . RuleUID , cmd . RuleOrgID )
}
}
if err != nil {
if err != nil {
return err
return err
@ -63,10 +63,10 @@ func (st DBstore) ListAlertInstances(cmd *models.ListAlertInstancesQuery) error
params = append ( params , p ... )
params = append ( params , p ... )
}
}
addToQuery ( "SELECT alert_instance.*, alert_definition.title AS def_title FROM alert_instance LEFT JOIN alert_definition ON alert_instance.def_org_id = alert_definition.org_id AND alert_instance.def_uid = alert_definition.uid WHERE def _org_id = ?" , cmd . RuleOrgID )
addToQuery ( "SELECT alert_instance.*, alert_rule.title AS rule_title FROM alert_instance LEFT JOIN alert_rule ON alert_instance.rule_org_id = alert_rule.org_id AND alert_instance.rule_uid = alert_rule.uid WHERE rule _org_id = ?" , cmd . RuleOrgID )
if cmd . RuleUID != "" {
if cmd . RuleUID != "" {
addToQuery ( ` AND def _uid = ? ` , cmd . RuleUID )
addToQuery ( ` AND rule _uid = ? ` , cmd . RuleUID )
}
}
if cmd . State != "" {
if cmd . State != "" {
@ -109,8 +109,8 @@ func (st DBstore) SaveAlertInstance(cmd *models.SaveAlertInstanceCommand) error
upsertSQL := st . SQLStore . Dialect . UpsertSQL (
upsertSQL := st . SQLStore . Dialect . UpsertSQL (
"alert_instance" ,
"alert_instance" ,
[ ] string { "def_org_id" , "def _uid" , "labels_hash" } ,
[ ] string { "rule_org_id" , "rule _uid" , "labels_hash" } ,
[ ] string { "def_org_id" , "def _uid" , "labels" , "labels_hash" , "current_state" , "current_state_since" , "current_state_end" , "last_eval_time" } )
[ ] string { "rule_org_id" , "rule _uid" , "labels" , "labels_hash" , "current_state" , "current_state_since" , "current_state_end" , "last_eval_time" } )
_ , err = sess . SQL ( upsertSQL , params ... ) . Query ( )
_ , err = sess . SQL ( upsertSQL , params ... ) . Query ( )
if err != nil {
if err != nil {
return err
return err
@ -120,10 +120,10 @@ func (st DBstore) SaveAlertInstance(cmd *models.SaveAlertInstanceCommand) error
} )
} )
}
}
func ( st DBstore ) FetchOrgIds ( cmd * models . FetchUniqueOrgIdsQuery ) error {
func ( st DBstore ) FetchOrgIds ( ) ( [ ] int64 , error ) {
return st . SQLStore . WithDbSession ( context . Background ( ) , func ( sess * sqlstore . DBSession ) error {
orgIds := [ ] int64 { }
orgIds := make ( [ ] * models . FetchUniqueOrgIdsQueryResult , 0 )
err := st . SQLStore . WithDbSession ( context . Background ( ) , func ( sess * sqlstore . DBSession ) error {
s := strings . Builder { }
s := strings . Builder { }
params := make ( [ ] interface { } , 0 )
params := make ( [ ] interface { } , 0 )
@ -132,13 +132,13 @@ func (st DBstore) FetchOrgIds(cmd *models.FetchUniqueOrgIdsQuery) error {
params = append ( params , p ... )
params = append ( params , p ... )
}
}
addToQuery ( "SELECT DISTINCT def _org_id FROM alert_instance" )
addToQuery ( "SELECT DISTINCT rule _org_id FROM alert_instance" )
if err := sess . SQL ( s . String ( ) , params ... ) . Find ( & orgIds ) ; err != nil {
if err := sess . SQL ( s . String ( ) , params ... ) . Find ( & orgIds ) ; err != nil {
return err
return err
}
}
cmd . Result = orgIds
return nil
return nil
} )
} )
return orgIds , err
}
}