Skip to content

Commit

Permalink
feat(sendInterval): 增加告警通知间隔配置 #147 (#148)
Browse files Browse the repository at this point in the history
![发送间隔](https://github.com/user-attachments/assets/82737571-fdea-4240-a510-3f5036346c3b)
比较需要告警通知间隔的可配置话
  • Loading branch information
Johnson1998 authored Jul 20, 2024
1 parent 1ae1027 commit 4965fa4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 59 deletions.
37 changes: 34 additions & 3 deletions app/prom_server/internal/data/repositiryimpl/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package msg

import (
"context"
"strconv"
"time"

"github.com/aide-family/moon/api/perrors"
Expand Down Expand Up @@ -50,7 +51,10 @@ func getHookAlarmTemplateMap(templates []*bo.NotifyTemplateBO) (map[vobj.NotifyA

func (l *msgRepoImpl) SendAlarm(ctx context.Context, req ...*bo.AlarmMsgBo) error {
for _, v := range req {
if !l.cacheNotify(v.AlarmInfo) {
if v.StrategyBO == nil {
continue
}
if !l.cacheNotify(v.AlarmInfo, v.StrategyBO.SendInterval) {
continue
}

Expand All @@ -66,10 +70,10 @@ func (l *msgRepoImpl) SendAlarm(ctx context.Context, req ...*bo.AlarmMsgBo) erro
return nil
}

func (l *msgRepoImpl) cacheNotify(alarmInfo *bo.AlertBo) bool {
func (l *msgRepoImpl) cacheNotify(alarmInfo *bo.AlertBo, sendInterval string) bool {
fingerprint := hash.MD5(alarmInfo.Fingerprint + ":" + alarmInfo.Status)
// 判断是否发送过告警, 如果已经发送过, 不再发送
return l.d.Cache().SetNX(context.Background(), consts.AlarmNotifyCache.Key(fingerprint).String(), alarmInfo.Bytes(), 2*time.Hour)
return l.d.Cache().SetNX(context.Background(), consts.AlarmNotifyCache.Key(fingerprint).String(), alarmInfo.Bytes(), ConvertTimeFromStringToDuration(sendInterval))
}

func (l *msgRepoImpl) sendAlarmToChatGroups(ctx context.Context, chatGroups []*bo.ChatGroupBO, hookTemplateMap map[vobj.NotifyApp]string, alarmInfo *bo.AlertBo) {
Expand Down Expand Up @@ -157,3 +161,30 @@ func NewMsgRepo(data *data.Data, logger log.Logger) repository.MsgRepo {
d: data,
}
}

func ConvertTimeFromStringToDuration(duration string) time.Duration {
return time.Duration(BuildDuration(duration)) * time.Second
}

// BuildDuration 字符串转为api时间
func BuildDuration(duration string) int64 {
durationLen := len(duration)
if duration == "" || durationLen < 2 {
return 0
}
value, _ := strconv.Atoi(duration[:durationLen-1])
// 获取字符串最后一个字符
unit := string(duration[durationLen-1])
switch unit {
case "s":
return int64(value)
case "m":
return int64(value) * 60
case "h":
return int64(value) * 60 * 60
case "d":
return int64(value) * 60 * 60 * 24
default:
return 0
}
}
114 changes: 58 additions & 56 deletions web/src/pages/home/monitor/strategy-group/strategy/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -660,31 +660,59 @@ export const strategyEditOptions: (DataFormItem | DataFormItem[])[] = [
]
}
],
{
name: 'alarmPageIds',
label: '告警页面',
id: 'alarmPageIds',
dataProps: {
type: 'select-fetch',
parentProps: {
selectProps: {
placeholder: '请选择告警页面',
mode: 'multiple'
},
handleFetch: getAlarmPages,
defaultOptions: []
}
[
{
name: 'alarmPageIds',
label: '告警页面',
id: 'alarmPageIds',
dataProps: {
type: 'select-fetch',
parentProps: {
selectProps: {
placeholder: '请选择告警页面',
mode: 'multiple'
},
handleFetch: getAlarmPages,
defaultOptions: []
}
},
formItemProps: {
tooltip: <p>报警页面: 当该规则触发时, 页面将跳转到报警页面</p>
},
rules: [
{
required: true,
message: '请选择报警页面'
}
]
},
formItemProps: {
tooltip: <p>报警页面: 当该规则触发时, 页面将跳转到报警页面</p>
{
name: 'sendInterval',
label: '告警通知间隔',
id: 'sendInterval',
dataProps: {
type: 'time-value',
parentProps: {
name: 'sendInterval',
placeholder: ['请输入通知间隔时间', '选择单位'],
unitOptions: durationOptions
}
},
formItemProps: {
tooltip: (
<p>
告警通知间隔: 告警通知间隔, 在一定时间内没有消警,
则再次触发告警通知的时间
</p>
)
},
rules: [
{
validator: checkDuration('告警通知间隔时间')
}
]
},
rules: [
{
required: true,
message: '请选择报警页面'
}
]
},
],
// [
// {
// name: 'maxSuppress',
Expand Down Expand Up @@ -714,32 +742,6 @@ export const strategyEditOptions: (DataFormItem | DataFormItem[])[] = [
// ]
// },
// {
// name: 'sendInterval',
// label: '告警通知间隔',
// id: 'sendInterval',
// dataProps: {
// type: 'time-value',
// parentProps: {
// name: 'sendInterval',
// placeholder: ['请输入通知间隔时间', '选择单位'],
// unitOptions: durationOptions
// }
// },
// formItemProps: {
// tooltip: (
// <p>
// 告警通知间隔: 告警通知间隔, 在一定时间内没有消警,
// 则再次触发告警通知的时间
// </p>
// )
// },
// rules: [
// {
// validator: checkDuration('告警通知间隔时间')
// }
// ]
// },
// {
// name: 'sendRecover',
// label: '告警恢复通知',
// id: 'sendRecover',
Expand Down Expand Up @@ -904,13 +906,13 @@ export const tourSteps = (refs: {
// placement: 'bottomRight',
// target: () => document.getElementById('maxSuppress')!
// },
// {
// title: '告警通知间隔配置',
// description:
// '告警发生时候会立即告警, 当持续通知间隔时长时, 会再次告警, 该时间默认时2小时',
// placement: 'bottomRight',
// target: () => document.getElementById('sendInterval')!
// },
{
title: '告警通知间隔配置',
description:
'告警发生时候会立即告警, 当持续通知间隔时长时, 会再次告警, 该时间默认时2小时',
placement: 'bottomRight',
target: () => document.getElementById('sendInterval')!
},
// {
// title: '告警恢复通知配置',
// description: '告警恢复后是否发送告警恢复通知的开关, 默认是开启的',
Expand Down

0 comments on commit 4965fa4

Please sign in to comment.