AlertNotifications: Translate notifications IDs to UIDs in Rule builder (#19882)

* AlertNotifications: Translate notifications IDs to UIDs in alert Rule builder

* Avoid shadowing errors, raise validation error on non-existing notification id

* create a cache for notification Uids to minimize db overhead

* add cache usage test

* avoid caching empty notification Uids

* isolate db in alert notificationUid caching tests
This commit is contained in:
Dima Ryskin
2020-03-18 15:00:56 +02:00
committed by GitHub
parent 492264abc3
commit 44b7f3ea1c
6 changed files with 198 additions and 8 deletions
@@ -67,6 +67,32 @@ func GetAlertNotifications(query *models.GetAlertNotificationsQuery) error {
return getAlertNotificationInternal(query, newSession())
}
func (ss *SqlStore) addAlertNotificationUidByIdHandler() {
bus.AddHandler("sql", ss.GetAlertNotificationUidWithId)
}
func (ss *SqlStore) GetAlertNotificationUidWithId(query *models.GetAlertNotificationUidQuery) error {
cacheKey := newAlertNotificationUidCacheKey(query.OrgId, query.Id)
if cached, found := ss.CacheService.Get(cacheKey); found {
query.Result = cached.(string)
return nil
}
err := getAlertNotificationUidInternal(query, newSession())
if err != nil {
return err
}
ss.CacheService.Set(cacheKey, query.Result, -1) //Infinite, never changes
return nil
}
func newAlertNotificationUidCacheKey(orgID, notificationId int64) string {
return fmt.Sprintf("notification-uid-by-org-%d-and-id-%d", orgID, notificationId)
}
func GetAlertNotificationsWithUid(query *models.GetAlertNotificationsWithUidQuery) error {
return getAlertNotificationWithUidInternal(query, newSession())
}
@@ -124,6 +150,35 @@ func GetAlertNotificationsWithUidToSend(query *models.GetAlertNotificationsWithU
return nil
}
func getAlertNotificationUidInternal(query *models.GetAlertNotificationUidQuery, sess *DBSession) error {
var sql bytes.Buffer
params := make([]interface{}, 0)
sql.WriteString(`SELECT
alert_notification.uid
FROM alert_notification
`)
sql.WriteString(` WHERE alert_notification.org_id = ?`)
params = append(params, query.OrgId)
sql.WriteString(` AND alert_notification.id = ?`)
params = append(params, query.Id)
results := make([]string, 0)
if err := sess.SQL(sql.String(), params...).Find(&results); err != nil {
return err
}
if len(results) == 0 {
return fmt.Errorf("Alert notification [ Id: %v, OrgId: %v ] not found", query.Id, query.OrgId)
}
query.Result = results[0]
return nil
}
func getAlertNotificationInternal(query *models.GetAlertNotificationsQuery, sess *DBSession) error {
var sql bytes.Buffer
params := make([]interface{}, 0)