From 1608274e582fafcfb0b2b790ed00ea1c7f355026 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Sun, 24 Sep 2023 21:31:05 +0800 Subject: [PATCH] update alarm notice wework app send content ui (#1258) --- .../queue/impl/KafkaCommonDataQueue.java | 3 +++ ...a => WeWorkAppAlertNotifyHandlerImpl.java} | 20 ++++++++--------- .../manager/pojo/dto/WeChatAppDTO.java | 22 +++++++++++++++++-- .../service/impl/MonitorServiceImpl.java | 2 ++ .../templates/alertNotifyWeWorkApp.txt | 7 ++++++ .../WeChatAppAlertNotifyHandlerImplTest.java | 2 +- 6 files changed, 42 insertions(+), 14 deletions(-) rename manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/{WeChatAppAlertNotifyHandlerImpl.java => WeWorkAppAlertNotifyHandlerImpl.java} (88%) create mode 100644 manager/src/main/resources/templates/alertNotifyWeWorkApp.txt diff --git a/common/src/main/java/org/dromara/hertzbeat/common/queue/impl/KafkaCommonDataQueue.java b/common/src/main/java/org/dromara/hertzbeat/common/queue/impl/KafkaCommonDataQueue.java index f5919df20d2..e833f186f3c 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/queue/impl/KafkaCommonDataQueue.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/queue/impl/KafkaCommonDataQueue.java @@ -202,6 +202,9 @@ public CollectRep.MetricsData pollMetricsDataToPersistentStorage() throws Interr @Override public CollectRep.MetricsData pollMetricsDataToRealTimeStorage() throws InterruptedException { CollectRep.MetricsData realTimeMetricsData = metricsDataToRealTimeStorageQueue.poll(); + if (realTimeMetricsData != null) { + return realTimeMetricsData; + } lock4.lockInterruptibly(); try { ConsumerRecords records = metricsDataToRealTimeStorageConsumer.poll(Duration.ofSeconds(1)); diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeWorkAppAlertNotifyHandlerImpl.java similarity index 88% rename from manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImpl.java rename to manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeWorkAppAlertNotifyHandlerImpl.java index ae886633701..7af24d1c8df 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeWorkAppAlertNotifyHandlerImpl.java @@ -6,7 +6,6 @@ import org.dromara.hertzbeat.common.entity.alerter.Alert; import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver; import org.dromara.hertzbeat.common.util.JsonUtil; -import org.dromara.hertzbeat.manager.component.alerter.AlertNotifyHandler; import org.dromara.hertzbeat.manager.pojo.dto.WeChatAppDTO; import org.dromara.hertzbeat.manager.pojo.dto.WeChatAppReq; import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException; @@ -28,7 +27,7 @@ @Component @RequiredArgsConstructor @Slf4j -public class WeChatAppAlertNotifyHandlerImpl implements AlertNotifyHandler { +public class WeWorkAppAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl { /** * send weChat app message url @@ -45,11 +44,6 @@ public class WeChatAppAlertNotifyHandlerImpl implements AlertNotifyHandler { */ private static final String DEFAULT_ALL = "@all"; - /** - * send message type - */ - private static final String DEFAULT_TYPE = "text"; - private final RestTemplate restTemplate; @Override @@ -62,13 +56,13 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti ResponseEntity entityResponse = restTemplate.getForEntity(String.format(SECRET_URL, corpId, appSecret), WeChatAppReq.class); if (Objects.nonNull(entityResponse.getBody())) { String accessToken = entityResponse.getBody().getAccessToken(); - WeChatAppDTO.TextDTO textDTO = new WeChatAppDTO.TextDTO(); - textDTO.setContent(generateContent(alert)); + WeChatAppDTO.MarkdownDTO markdown = new WeChatAppDTO.MarkdownDTO(); + markdown.setContent(renderContent(alert)); WeChatAppDTO weChatAppDTO = WeChatAppDTO.builder() .toUser(DEFAULT_ALL) - .msgType(DEFAULT_TYPE) + .msgType(WeChatAppDTO.MARKDOWN) + .markdown(markdown) .agentId(agentId) - .text(textDTO) .build(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); @@ -98,4 +92,8 @@ private String generateContent(Alert alert){ return JsonUtil.toJson(contentMap); } + @Override + protected String templateName() { + return "alertNotifyWeWorkApp"; + } } diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/WeChatAppDTO.java b/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/WeChatAppDTO.java index 410ea3270ce..9e9b18a9676 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/WeChatAppDTO.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/pojo/dto/WeChatAppDTO.java @@ -9,7 +9,6 @@ /** * WeChatAppDTO * @author hdd - * @create 2023/04/05 */ @Data @Builder @@ -17,6 +16,11 @@ @NoArgsConstructor public class WeChatAppDTO { + /** + * markdown格式 + */ + public static final String MARKDOWN = "markdown"; + @JsonProperty(value = "touser") private String toUser; @@ -32,9 +36,23 @@ public class WeChatAppDTO { @JsonProperty(value = "agentid") private Integer agentId; - + /** + * text message + */ private TextDTO text; + /** + * markdown消息 + */ + private MarkdownDTO markdown; + + @Data + public static class MarkdownDTO { + /** + * 消息内容 + */ + private String content; + } @Data public static class TextDTO { diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java index c494afe2b03..522f983668c 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -563,6 +563,7 @@ public void deleteMonitor(long id) throws RuntimeException { paramDao.deleteParamsByMonitorId(id); tagMonitorBindDao.deleteTagMonitorBindsByMonitorId(id); alertDefineBindDao.deleteAlertDefineMonitorBindsByMonitorIdEquals(id); + collectorMonitorBindDao.deleteCollectorMonitorBindsByMonitorId(id); collectJobScheduling.cancelAsyncCollectJob(monitor.getJobId()); applicationContext.publishEvent(new MonitorDeletedEvent(applicationContext, monitor.getId())); } @@ -581,6 +582,7 @@ public void deleteMonitors(Set ids) throws RuntimeException { for (Monitor monitor : monitors) { // delete tag 删除监控对应的标签 tagService.deleteMonitorSystemTags(monitor); + collectorMonitorBindDao.deleteCollectorMonitorBindsByMonitorId(monitor.getId()); collectJobScheduling.cancelAsyncCollectJob(monitor.getJobId()); applicationContext.publishEvent(new MonitorDeletedEvent(applicationContext, monitor.getId())); } diff --git a/manager/src/main/resources/templates/alertNotifyWeWorkApp.txt b/manager/src/main/resources/templates/alertNotifyWeWorkApp.txt new file mode 100644 index 00000000000..c1af79a441f --- /dev/null +++ b/manager/src/main/resources/templates/alertNotifyWeWorkApp.txt @@ -0,0 +1,7 @@ +[(${title})] +[(${targetLabel})] : [(${target})] +[# th:if="${monitorId != null}"][(${monitorIdLabel})] : [(${monitorId})][/] +[# th:if="${monitorName != null}"][(${monitorNameLabel})] : [(${monitorName})][/] +[(${priorityLabel})] : [(${priority})] +[(${triggerTimeLabel})] : [(${triggerTime})] +[(${contentLabel})] : [(${content})] \ No newline at end of file diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImplTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImplTest.java index 49f4dd798ee..7e15b371a6a 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImplTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/component/alerter/impl/WeChatAppAlertNotifyHandlerImplTest.java @@ -23,7 +23,7 @@ public class WeChatAppAlertNotifyHandlerImplTest extends AbstractSpringIntegrati @Resource - private WeChatAppAlertNotifyHandlerImpl weChatAppAlertNotifyHandler; + private WeWorkAppAlertNotifyHandlerImpl weChatAppAlertNotifyHandler; @Test