Skip to content

Commit

Permalink
[ospp] support custom notice template (#1233)
Browse files Browse the repository at this point in the history
Signed-off-by: Eden4701 <Eden4701@163.com>
Co-authored-by: ulikehhh <2762242004@qq.com>
  • Loading branch information
Eden4701 and ulikehhh authored Oct 15, 2023
1 parent c41d10b commit 2fb5c5c
Show file tree
Hide file tree
Showing 62 changed files with 1,983 additions and 552 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ private Message(byte code, String msg) {
private Message(T data) {
this.data = data;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public class NoticeRule {
@NotNull
private String receiverName;

@Schema(title = "Template ID",
description = "模板ID",
example = "4324324", accessMode = READ_WRITE)
private Long templateId;

@Schema(title = "Template identification",
description = "通知模板标识",
example = "demo", accessMode = READ_WRITE)
@Length(max = 100)
private String templateName;

@Schema(title = "Whether to enable this policy",
description = "是否启用此策略",
example = "true", accessMode = READ_WRITE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.hertzbeat.common.entity.manager;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
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;

import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY;
import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE;

/**
* Notification strategy entity
* 通知策略
*
*
*/
@Entity
@Table(name = "hzb_notice_template")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "Notify Policy Template | 通知模板实体")
@EntityListeners(AuditingEntityListener.class)
public class NoticeTemplate {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Schema(title = "Notification Template Entity Primary Key Index ID",
description = "通知模板实体主键索引ID",
example = "87584674384", accessMode = READ_ONLY)
private Long id;

@Schema(title = "Template name",
description = "模板名称",
example = "dispatch-1", accessMode = READ_WRITE)
@Length(max = 100)
@NotBlank
private String name;

@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 10-Enterprise WeChat app message",
description = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人 6-飞书机器人 7-Telegram机器人 8-SlackWebHook 9-Discord机器人 10-企业微信-应用消息",
accessMode = READ_WRITE)
@Min(0)
@NotNull
private Byte type;

@Schema(title = "Is it a preset template: true- preset template false- custom template.",
description = "是否为预设模板: true-预设模板 false-自定义模板",
accessMode = READ_WRITE)
@NotNull
private Boolean presetTemplate;


@Schema(title = "Template content",
description = "模板内容",
example = "[${title}]\n" +
"${targetLabel} : ${target}\n" +
"<#if (monitorId??)>${monitorIdLabel} : ${monitorId} </#if>\n" +
"<#if (monitorName??)>${monitorNameLabel} : ${monitorName} </#if>\n" +
"${priorityLabel} : ${priority}\n" +
"${triggerTimeLabel} : ${triggerTime}\n" +
"${contentLabel} : ${content}", accessMode = READ_WRITE)
@Length(max = 100000)
@NotBlank
@Column(name = "template_content", columnDefinition = "MEDIUMTEXT")
private String templateContent;

@Schema(title = "The creator of this record", description = "此条记录创建者", example = "tom", accessMode = READ_ONLY)
@CreatedBy
private String creator;

@Schema(title = "This record was last modified by",
description = "此条记录最新修改者",
example = "tom", accessMode = READ_ONLY)
@LastModifiedBy
private String modifier;

@Schema(title = "This record creation time (millisecond timestamp)",
description = "记录创建时间", accessMode = READ_ONLY)
@CreatedDate
private LocalDateTime gmtCreate;

@Schema(title = "Record the latest modification time (timestamp in milliseconds)",
description = "记录最新修改时间", accessMode = READ_ONLY)
@LastModifiedDate
private LocalDateTime gmtUpdate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Pair<K, V> {
private K left;
private final V right;

private Pair(K left, V right) {
public Pair(K left, V right) {
this.left = left;
this.right = right;
}
Expand Down
5 changes: 5 additions & 0 deletions manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- freemaker依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@

import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver;
import org.dromara.hertzbeat.common.entity.manager.NoticeTemplate;
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;

/**
*
*
*/
public interface AlertNotifyHandler {

/**
* 发送报警通知
*
* @param receiver Notification configuration information 通知配置信息
* @param alert Alarm information 告警信息
* @param receiver Notification configuration information 通知配置信息
* @param noticeTemplate Notification configuration information 通知配置信息
* @param alert Alarm information 告警信息
* @throws AlertNoticeException when send receiver error
*/
void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException;
void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert alert) throws AlertNoticeException;

/**
* 通知类型
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* 报警持久化
*
*
*
*/
public interface AlertStoreHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
package org.dromara.hertzbeat.manager.component.alerter;

import com.google.common.collect.Maps;
import org.dromara.hertzbeat.common.queue.CommonDataQueue;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.alert.AlerterWorkerPool;
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver;
import org.dromara.hertzbeat.common.entity.manager.NoticeTemplate;
import org.dromara.hertzbeat.common.queue.CommonDataQueue;
import org.dromara.hertzbeat.manager.service.NoticeConfigService;
import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.manager.support.exception.IgnoreException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
Expand All @@ -37,7 +38,6 @@
* 告警信息入库分发
*
*
*
*/
@Component
@Slf4j
Expand Down Expand Up @@ -79,19 +79,35 @@ public void afterPropertiesSet() throws Exception {
* @param alert alert msg
* @return send success or failed
*/
public boolean sendNoticeMsg(NoticeReceiver receiver, Alert alert) {
public boolean sendNoticeMsg(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert alert) {
if (receiver == null || receiver.getType() == null) {
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);
alertNotifyHandlerMap.get(type).send(receiver, noticeTemplate, alert);
return true;
}
return false;
}

private List<Long> matchReceiverIdByNoticeRules(Alert alert) {
return noticeConfigService.getReceiverIdFilterRule(alert);
}

private List<Long> matchTemplateIdByNoticeRules(Alert alert) {
return noticeConfigService.getTemplateIdFilterRule(alert);
}

private NoticeReceiver getOneReciverById(List<Long> ids) {
return noticeConfigService.getOneReciverByIdInFilterRule(ids);
}

private NoticeTemplate getOneTemplateById(List<Long> ids) {
return noticeConfigService.getOneTemplateByIdInFilterRule(ids);
}

private List<NoticeReceiver> matchReceiverByNoticeRules(Alert alert) {
return noticeConfigService.getReceiverFilterRule(alert);
}
Expand Down Expand Up @@ -119,17 +135,25 @@ public void run() {
}

private void sendNotify(Alert alert) {
// Forward configured email WeChat webhook
List<NoticeReceiver> receivers = matchReceiverByNoticeRules(alert);
List<Long> receivers = matchReceiverIdByNoticeRules(alert);
List<Long> templates = matchTemplateIdByNoticeRules(alert);
// todo Send notification here temporarily single thread 发送通知这里暂时单线程
for (NoticeReceiver receiver : receivers) {
for (int i = 0; i < receivers.size(); i++) {
try {
sendNoticeMsg(receiver, alert);
if((templates.subList(i, i + 1)).get(0)==null){
sendNoticeMsg(getOneReciverById(receivers.subList(i, i + 1)),
null, alert);
}
else {
sendNoticeMsg(getOneReciverById(receivers.subList(i, i + 1)),
getOneTemplateById(templates.subList(i, i + 1)), alert);
}
} catch (AlertNoticeException e) {
log.warn("DispatchTask sendNoticeMsg error, message: {}", e.getMessage());
}
}

}
}

}
}
Loading

0 comments on commit 2fb5c5c

Please sign in to comment.