Skip to content

Commit

Permalink
Merge pull request #282 from Gentleelephant/fix-1224
Browse files Browse the repository at this point in the history
refactor: logic of notification history
  • Loading branch information
benjaminhuo authored Dec 27, 2024
2 parents ed99293 + 8e5449d commit 369e876
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
1 change: 0 additions & 1 deletion pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const (
AlertSummaryCN = "summaryCn"

ReceiverName = "receiver"
ReceiverType = "receiver_type"

Verify = "verify"
Notification = "notification"
Expand Down
8 changes: 3 additions & 5 deletions pkg/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ func (s *historyStage) Exec(ctx context.Context, l log.Logger, data interface{})

_ = level.Debug(l).Log("msg", "Start history stage", "seq", ctx.Value("seq"))

input := data.(map[string][]*template.Alert)
input := data.(map[string]*template.Alert)
d := &template.Data{}
for _, alert := range input {
for _, t := range alert {
if t.NotifySuccessful {
d.Alerts = append(d.Alerts, t)
}
if alert.NotifySuccessful {
d.Alerts = append(d.Alerts, alert)
}
}

Expand Down
36 changes: 24 additions & 12 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package notify

import (
"context"
"fmt"
"strings"
"sync"

"github.com/duke-git/lancet/v2/convertor"
Expand Down Expand Up @@ -75,14 +77,12 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{})
_ = level.Debug(l).Log("msg", "Start notify stage", "seq", ctx.Value("seq"))

input := data.(map[internal.Receiver][]*template.Data)
alertMap := make(map[string][]*template.Alert)
for r, dataList := range input {
receiver := r
alertMap := make(map[string]*template.Alert)
for _, dataList := range input {
ds := convertor.DeepClone(dataList)
s.addExtensionLabels(receiver, ds)
for _, d := range ds {
for _, alert := range d.Alerts {
alertMap[alert.ID] = append(alertMap[alert.ID], alert)
alertMap[alert.ID] = alert
}
}
}
Expand All @@ -94,9 +94,7 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{})

for _, alert := range alerts {
if a := alertMap[alert.ID]; a != nil {
for _, t := range a {
t.NotifySuccessful = true
}
a.NotifySuccessful = true
}
}
}
Expand All @@ -105,6 +103,7 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{})
for k, v := range input {
receiver := k
ds := convertor.DeepClone(v)
setReceiverList(ds, receiver, alertMap)
s.addExtensionLabels(receiver, ds)
nf, err := factories[receiver.GetType()](l, receiver, s.notifierCtl)
if err != nil {
Expand All @@ -114,6 +113,7 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{})
})
continue
}

nf.SetSentSuccessfulHandler(&handler)

for _, d := range ds {
Expand All @@ -123,19 +123,31 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{})
})
}
}

return ctx, alertMap, group.Wait()
}

func setReceiverList(alertData []*template.Data, receiver internal.Receiver, alertMap map[string]*template.Alert) {
for _, ds := range alertData {
for _, alert := range ds.Alerts {
if _, ok := alertMap[alert.ID]; ok {
if receiverLabel, ok := alertMap[alert.ID].Labels[fmt.Sprintf("%s_receiver_list", receiver.GetType())]; ok {
receivers := strings.Split(receiverLabel, ",")
receivers = append(receivers, receiver.GetName())
alertMap[alert.ID].Labels[fmt.Sprintf("%s_receiver_list", receiver.GetType())] = strings.Join(receivers, ",")
} else {
alertMap[alert.ID].Labels[fmt.Sprintf("%s_receiver_list", receiver.GetType())] = receiver.GetName()
}
}
}
}
}

func (s *notifyStage) addExtensionLabels(receiver internal.Receiver, data []*template.Data) {
for _, d := range data {
for _, alert := range d.Alerts {
if alert.Labels[constants.ReceiverName] == "" {
alert.Labels[constants.ReceiverName] = receiver.GetName()
}
if alert.Labels[constants.ReceiverType] == "" {
alert.Labels[constants.ReceiverType] = receiver.GetType()
}
}
}
}

0 comments on commit 369e876

Please sign in to comment.