40 Star 147 Fork 3

Gitee 极速下载/grafana

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/grafana/grafana
克隆/下载
alert_notifications.go 5.31 KB
一键复制 编辑 原始数据 按行查看 历史
bergquist 提交于 2019-01-31 16:43 . changes some info logging to debug
package notifiers
import (
"errors"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
)
var (
ErrInvalidConfigTooManyDefault = errors.New("Alert notification provisioning config is invalid. Only one alert notification can be marked as default")
)
func Provision(configDirectory string) error {
dc := newNotificationProvisioner(log.New("provisioning.notifiers"))
return dc.applyChanges(configDirectory)
}
type NotificationProvisioner struct {
log log.Logger
cfgProvider *configReader
}
func newNotificationProvisioner(log log.Logger) NotificationProvisioner {
return NotificationProvisioner{
log: log,
cfgProvider: &configReader{log: log},
}
}
func (dc *NotificationProvisioner) apply(cfg *notificationsAsConfig) error {
if err := dc.deleteNotifications(cfg.DeleteNotifications); err != nil {
return err
}
if err := dc.mergeNotifications(cfg.Notifications); err != nil {
return err
}
return nil
}
func (dc *NotificationProvisioner) deleteNotifications(notificationToDelete []*deleteNotificationConfig) error {
for _, notification := range notificationToDelete {
dc.log.Info("Deleting alert notification", "name", notification.Name, "uid", notification.Uid)
if notification.OrgId == 0 && notification.OrgName != "" {
getOrg := &models.GetOrgByNameQuery{Name: notification.OrgName}
if err := bus.Dispatch(getOrg); err != nil {
return err
}
notification.OrgId = getOrg.Result.Id
} else if notification.OrgId < 0 {
notification.OrgId = 1
}
getNotification := &models.GetAlertNotificationsWithUidQuery{Uid: notification.Uid, OrgId: notification.OrgId}
if err := bus.Dispatch(getNotification); err != nil {
return err
}
if getNotification.Result != nil {
cmd := &models.DeleteAlertNotificationWithUidCommand{Uid: getNotification.Result.Uid, OrgId: getNotification.OrgId}
if err := bus.Dispatch(cmd); err != nil {
return err
}
}
}
return nil
}
func (dc *NotificationProvisioner) mergeNotifications(notificationToMerge []*notificationFromConfig) error {
for _, notification := range notificationToMerge {
if notification.OrgId == 0 && notification.OrgName != "" {
getOrg := &models.GetOrgByNameQuery{Name: notification.OrgName}
if err := bus.Dispatch(getOrg); err != nil {
return err
}
notification.OrgId = getOrg.Result.Id
} else if notification.OrgId < 0 {
notification.OrgId = 1
}
cmd := &models.GetAlertNotificationsWithUidQuery{OrgId: notification.OrgId, Uid: notification.Uid}
err := bus.Dispatch(cmd)
if err != nil {
return err
}
if cmd.Result == nil {
dc.log.Debug("inserting alert notification from configuration", "name", notification.Name, "uid", notification.Uid)
insertCmd := &models.CreateAlertNotificationCommand{
Uid: notification.Uid,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,
Settings: notification.SettingsToJson(),
OrgId: notification.OrgId,
DisableResolveMessage: notification.DisableResolveMessage,
Frequency: notification.Frequency,
SendReminder: notification.SendReminder,
}
if err := bus.Dispatch(insertCmd); err != nil {
return err
}
} else {
dc.log.Debug("updating alert notification from configuration", "name", notification.Name)
updateCmd := &models.UpdateAlertNotificationWithUidCommand{
Uid: notification.Uid,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,
Settings: notification.SettingsToJson(),
OrgId: notification.OrgId,
DisableResolveMessage: notification.DisableResolveMessage,
Frequency: notification.Frequency,
SendReminder: notification.SendReminder,
}
if err := bus.Dispatch(updateCmd); err != nil {
return err
}
}
}
return nil
}
func (cfg *notificationsAsConfig) mapToNotificationFromConfig() *notificationsAsConfig {
r := &notificationsAsConfig{}
if cfg == nil {
return r
}
for _, notification := range cfg.Notifications {
r.Notifications = append(r.Notifications, &notificationFromConfig{
Uid: notification.Uid,
OrgId: notification.OrgId,
OrgName: notification.OrgName,
Name: notification.Name,
Type: notification.Type,
IsDefault: notification.IsDefault,
Settings: notification.Settings,
DisableResolveMessage: notification.DisableResolveMessage,
Frequency: notification.Frequency,
SendReminder: notification.SendReminder,
})
}
for _, notification := range cfg.DeleteNotifications {
r.DeleteNotifications = append(r.DeleteNotifications, &deleteNotificationConfig{
Uid: notification.Uid,
OrgId: notification.OrgId,
OrgName: notification.OrgName,
Name: notification.Name,
})
}
return r
}
func (dc *NotificationProvisioner) applyChanges(configPath string) error {
configs, err := dc.cfgProvider.readConfig(configPath)
if err != nil {
return err
}
for _, cfg := range configs {
if err := dc.apply(cfg); err != nil {
return err
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mirrors/grafana.git
git@gitee.com:mirrors/grafana.git
mirrors
grafana
grafana
v6.0.0-beta2

搜索帮助