Skip to content

Commit

Permalink
update noticeRule and NoticeReceiver, enable multi select (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 committed Feb 17, 2023
1 parent f33cd0c commit e742753
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 313 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY;
import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE;
Expand Down Expand Up @@ -72,7 +73,8 @@ public class NoticeReceiver {
accessMode = READ_WRITE)
@Min(0)
@NotNull
private Byte type;
@Convert(converter = JsonByteListAttributeConverter.class)
private List<Byte> type;

@Schema(title = "Mobile number: Valid when the notification method is SMS",
description = "手机号 : 通知方式为手机短信时有效",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -100,6 +101,14 @@ public class NoticeRule {
@Convert(converter = JsonTagListAttributeConverter.class)
private List<TagItem> tags;

@Schema(title = "Notification information method: 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot 5-DingTalk Robot 6-FeiShu Robot 7-Telegram Bot 8-SlackWebHook 9-Discord Bot",
description = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人 6-飞书机器人 7-Telegram机器人 8-SlackWebHook 9-Discord机器人",
accessMode = READ_WRITE)
@Min(0)
@NotNull
@Convert(converter = JsonByteListAttributeConverter.class)
private List<Byte> type;

@Schema(title = "The creator of this record", description = "此条记录创建者", example = "tom", accessMode = READ_ONLY)
@CreatedBy
private String creator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,17 @@ public void afterPropertiesSet() throws Exception {
* @return send success or failed
*/
public boolean sendNoticeMsg(NoticeReceiver receiver, Alert alert) {
if(receiver == null || receiver.getType() == null){
if(receiver == null || receiver.getType() == null || receiver.getType().isEmpty()){
log.warn("DispatcherAlarm-sendNoticeMsg params is empty alert:[{}], receiver:[{}]", alert, receiver);
return false;
}
byte type = receiver.getType();
if (alertNotifyHandlerMap.containsKey(type)) {
alertNotifyHandlerMap.get(type).send(receiver, alert);
return true;
List<Byte> types = receiver.getType();
for (byte type : types) {
if (alertNotifyHandlerMap.containsKey(type)) {
alertNotifyHandlerMap.get(type).send(receiver, alert);
}
}
return false;
return true;
}

private List<NoticeReceiver> matchReceiverByNoticeRules(Alert alert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -109,7 +110,7 @@ public List<NoticeReceiver> getReceiverFilterRule(Alert alert) {

// The temporary rule is to forward all, and then implement more matching rules: alarm status selection, monitoring type selection, etc.
// 规则是全部转发, 告警状态选择, 监控类型选择等(按照tags标签和告警级别过滤匹配)
Set<Long> filterReceivers = rules.stream()
Map<Long, NoticeRule> filterReceiverMap = rules.stream()
.filter(rule -> {
if (rule.isFilterAll()) {
return true;
Expand All @@ -133,9 +134,13 @@ public List<NoticeReceiver> getReceiverFilterRule(Alert alert) {
}
return true;
})
.map(NoticeRule::getReceiverId)
.collect(Collectors.toSet());
return noticeReceiverDao.findAllById(filterReceivers);
.collect(Collectors.toMap(NoticeRule::getReceiverId, item -> item));
List<NoticeReceiver> receivers = noticeReceiverDao.findAllById(filterReceiverMap.keySet());
for (NoticeReceiver receiver : receivers) {
NoticeRule rule = filterReceiverMap.get(receiver.getId());
receiver.setType(rule.getType());
}
return receivers;
}

@Override
Expand Down

This file was deleted.

Loading

0 comments on commit e742753

Please sign in to comment.