Skip to content

Commit 2968011

Browse files
author
Cairry
committed
✨ feat(sender): Notification route support add effective time
1 parent e62bb41 commit 2968011

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

alert/consumer/handle.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88
"watchAlert/alert/mute"
9+
"watchAlert/alert/process"
910
"watchAlert/internal/ctx"
1011
"watchAlert/internal/models"
1112
"watchAlert/pkg/sender"
@@ -152,6 +153,7 @@ func withRuleGroupByAlerts(ctx *ctx.Context, timeInt int64, alerts []*models.Ale
152153
}
153154

154155
event := *alerts[0]
156+
event.Annotations += fmt.Sprintf("\n聚合 %d 条消息,详情请前往 WatchAlert 查看\n", len(alerts))
155157
return []*models.AlertCurEvent{&event}
156158
}
157159

@@ -164,7 +166,11 @@ func getNoticeData(ctx *ctx.Context, tenantId, noticeId string) (models.AlertNot
164166
func getNoticeRoutes(notice models.AlertNotice, severity string) []models.Route {
165167
var routes []models.Route
166168
if notice.Routes != nil {
167-
for _, route := range notice.Routes {
169+
for i, route := range notice.Routes {
170+
if process.NotInTheEffectiveTime(route.EffectiveTime) {
171+
logc.Infof(ctx.Ctx, "Notice %v route [%v] is not in effective time", notice.Name, i+1)
172+
continue
173+
}
168174
if slices.Contains(route.Severitys, severity) {
169175
routes = append(routes, route)
170176
}

alert/process/process.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,39 @@ func PushEventToFaultCenter(ctx *ctx.Context, event *models.AlertCurEvent) {
8585
// NotInTheEffectiveTime 判断是否不在生效时间内
8686
func NotInTheEffectiveTime(et models.EffectiveTime) bool {
8787
// 如果没有配置有效星期,则认为始终有效
88-
if len(et.Week) <= 0 {
88+
if len(et.Week) == 0 {
8989
return false
9090
}
9191

92-
// 获取当前日期
92+
// 当前日期
9393
currentTime := time.Now()
9494
currentWeekday := tools.TimeTransformToWeek(currentTime)
9595

9696
// 检查当前星期是否在有效范围内
97+
var isInValidWeekday bool
9798
for _, weekday := range et.Week {
9899
if currentWeekday == weekday {
99-
currentTimeSeconds := tools.TimeTransformToSeconds(currentTime)
100-
// 如果当前时间小于开始时间或大于结束时间,说明不在有效时间段内
101-
return currentTimeSeconds < et.StartTime || currentTimeSeconds > et.EndTime
100+
isInValidWeekday = true
101+
break
102102
}
103103
}
104-
// 当前星期不在有效范围内
105-
return true
104+
105+
// 如果当前星期不在有效范围内,直接返回 true
106+
if !isInValidWeekday {
107+
return true
108+
}
109+
110+
// 如果开始时间和结束时间都为0,表示全天有效
111+
if et.StartTime == 0 && et.EndTime == 0 {
112+
return false
113+
}
114+
115+
// 检查当前时间是否在指定的时间段内
116+
currentTimeSeconds := tools.TimeTransformToSeconds(currentTime)
117+
isInValidTimeRange := currentTimeSeconds >= et.StartTime && currentTimeSeconds <= et.EndTime
118+
119+
// 返回是否 不在有效时间范围内的结果
120+
return !isInValidTimeRange
106121
}
107122

108123
// RecordAlertHisEvent 记录历史告警

internal/models/notice.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Route struct {
3434
To []string `json:"to" gorm:"column:to;serializer:json"`
3535
// 抄送人
3636
CC []string `json:"cc" gorm:"column:cc;serializer:json"`
37+
// 生效时间
38+
EffectiveTime EffectiveTime `json:"effectiveTime"`
3739
}
3840

3941
type Email struct {

0 commit comments

Comments
 (0)