Browse Source

[Improvement][alert] Optimize alarm information (#3559)

* #3299  Json string parsing problem caused by non-standard json format.

* #3299  Json string parsing problem caused by non-standard json format.

* #3299  Json string parsing problem caused by non-standard json format. fix  code style

* #3299  Json string parsing problem caused by non-standard json format. fix  code style

* Optimize alarm information

* fix code style

* add license

* fix code smell

* Update dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java

Co-authored-by: Yichao Yang <1048262223@qq.com>

* fix code smell

* fix code smell

* 修改ut 和replace问题。

* 修改ut

Co-authored-by: wangjianda <Felix@thinkingdata.com>
Co-authored-by: Yichao Yang <1048262223@qq.com>
pull/3/MERGE
felix.wang 4 years ago committed by GitHub
parent
commit
2ba529a96c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 89
      dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java
  2. 53
      dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtilsTest.java
  3. 23
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/AlertEvent.java
  4. 23
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/AlertWarnLevel.java
  5. 58
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java
  6. 236
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/ProcessAlertContent.java
  7. 85
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/ServerAlertContent.java
  8. 71
      dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/AlertManager.java

89
dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtils.java

@ -14,13 +14,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.alert.utils; package org.apache.dolphinscheduler.alert.utils;
import org.apache.dolphinscheduler.common.enums.ShowType; import org.apache.dolphinscheduler.common.enums.ShowType;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.common.utils.*;
import org.apache.dolphinscheduler.plugin.model.AlertData; import org.apache.dolphinscheduler.plugin.model.AlertData;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@ -29,11 +30,17 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Enterprise WeChat utils * Enterprise WeChat utils
@ -41,25 +48,21 @@ import java.util.*;
public class EnterpriseWeChatUtils { public class EnterpriseWeChatUtils {
public static final Logger logger = LoggerFactory.getLogger(EnterpriseWeChatUtils.class); public static final Logger logger = LoggerFactory.getLogger(EnterpriseWeChatUtils.class);
public static final String ENTERPRISE_WE_CHAT_AGENT_ID = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_AGENT_ID);
public static final String ENTERPRISE_WE_CHAT_USERS = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_USERS);
private static final String ENTERPRISE_WE_CHAT_CORP_ID = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_CORP_ID); private static final String ENTERPRISE_WE_CHAT_CORP_ID = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_CORP_ID);
private static final String ENTERPRISE_WE_CHAT_SECRET = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_SECRET); private static final String ENTERPRISE_WE_CHAT_SECRET = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_SECRET);
private static final String ENTERPRISE_WE_CHAT_TOKEN_URL = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_TOKEN_URL); private static final String ENTERPRISE_WE_CHAT_TOKEN_URL = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_TOKEN_URL);
private static final String ENTERPRISE_WE_CHAT_TOKEN_URL_REPLACE = ENTERPRISE_WE_CHAT_TOKEN_URL == null ? null : ENTERPRISE_WE_CHAT_TOKEN_URL private static final String ENTERPRISE_WE_CHAT_TOKEN_URL_REPLACE = ENTERPRISE_WE_CHAT_TOKEN_URL == null ? null : ENTERPRISE_WE_CHAT_TOKEN_URL
.replaceAll("\\{corpId\\}", ENTERPRISE_WE_CHAT_CORP_ID) .replaceAll("\\{corpId}", ENTERPRISE_WE_CHAT_CORP_ID)
.replaceAll("\\{secret\\}", ENTERPRISE_WE_CHAT_SECRET); .replaceAll("\\{secret}", ENTERPRISE_WE_CHAT_SECRET);
private static final String ENTERPRISE_WE_CHAT_PUSH_URL = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_PUSH_URL); private static final String ENTERPRISE_WE_CHAT_PUSH_URL = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_PUSH_URL);
private static final String ENTERPRISE_WE_CHAT_TEAM_SEND_MSG = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_TEAM_SEND_MSG); private static final String ENTERPRISE_WE_CHAT_TEAM_SEND_MSG = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_TEAM_SEND_MSG);
private static final String ENTERPRISE_WE_CHAT_USER_SEND_MSG = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_USER_SEND_MSG); private static final String ENTERPRISE_WE_CHAT_USER_SEND_MSG = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_USER_SEND_MSG);
public static final String ENTERPRISE_WE_CHAT_AGENT_ID = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_AGENT_ID); private static final String agentIdRegExp = "\\{agentId}";
private static final String msgRegExp = "\\{msg}";
public static final String ENTERPRISE_WE_CHAT_USERS = PropertyUtils.getString(Constants.ENTERPRISE_WECHAT_USERS); private static final String userRegExp = "\\{toUser}";
/** /**
* get Enterprise WeChat is enable * get Enterprise WeChat is enable
@ -120,9 +123,9 @@ public class EnterpriseWeChatUtils {
* @return Enterprise WeChat send message * @return Enterprise WeChat send message
*/ */
public static String makeTeamSendMsg(String toParty, String agentId, String msg) { public static String makeTeamSendMsg(String toParty, String agentId, String msg) {
return ENTERPRISE_WE_CHAT_TEAM_SEND_MSG.replaceAll("\\{toParty\\}", toParty) return ENTERPRISE_WE_CHAT_TEAM_SEND_MSG.replaceAll("\\{toParty}", toParty)
.replaceAll("\\{agentId\\}", agentId) .replaceAll(agentIdRegExp, agentId)
.replaceAll("\\{msg\\}", msg); .replaceAll(msgRegExp, msg);
} }
/** /**
@ -135,9 +138,9 @@ public class EnterpriseWeChatUtils {
*/ */
public static String makeTeamSendMsg(Collection<String> toParty, String agentId, String msg) { public static String makeTeamSendMsg(Collection<String> toParty, String agentId, String msg) {
String listParty = FuncUtils.mkString(toParty, "|"); String listParty = FuncUtils.mkString(toParty, "|");
return ENTERPRISE_WE_CHAT_TEAM_SEND_MSG.replaceAll("\\{toParty\\}", listParty) return ENTERPRISE_WE_CHAT_TEAM_SEND_MSG.replaceAll("\\{toParty}", listParty)
.replaceAll("\\{agentId\\}", agentId) .replaceAll(agentIdRegExp, agentId)
.replaceAll("\\{msg\\}", msg); .replaceAll(msgRegExp, msg);
} }
/** /**
@ -149,9 +152,9 @@ public class EnterpriseWeChatUtils {
* @return Enterprise WeChat send message * @return Enterprise WeChat send message
*/ */
public static String makeUserSendMsg(String toUser, String agentId, String msg) { public static String makeUserSendMsg(String toUser, String agentId, String msg) {
return ENTERPRISE_WE_CHAT_USER_SEND_MSG.replaceAll("\\{toUser\\}", toUser) return ENTERPRISE_WE_CHAT_USER_SEND_MSG.replaceAll("\\{toUser}", toUser)
.replaceAll("\\{agentId\\}", agentId) .replaceAll(agentIdRegExp, agentId)
.replaceAll("\\{msg\\}", msg); .replaceAll(msgRegExp, msg);
} }
/** /**
@ -164,9 +167,9 @@ public class EnterpriseWeChatUtils {
*/ */
public static String makeUserSendMsg(Collection<String> toUser, String agentId, String msg) { public static String makeUserSendMsg(Collection<String> toUser, String agentId, String msg) {
String listUser = FuncUtils.mkString(toUser, "|"); String listUser = FuncUtils.mkString(toUser, "|");
return ENTERPRISE_WE_CHAT_USER_SEND_MSG.replaceAll("\\{toUser\\}", listUser) return ENTERPRISE_WE_CHAT_USER_SEND_MSG.replaceAll(userRegExp, listUser)
.replaceAll("\\{agentId\\}", agentId) .replaceAll(agentIdRegExp, agentId)
.replaceAll("\\{msg\\}", msg); .replaceAll(msgRegExp, msg);
} }
/** /**
@ -179,7 +182,7 @@ public class EnterpriseWeChatUtils {
* @throws IOException the IOException * @throws IOException the IOException
*/ */
public static String sendEnterpriseWeChat(String charset, String data, String token) throws IOException { public static String sendEnterpriseWeChat(String charset, String data, String token) throws IOException {
String enterpriseWeChatPushUrlReplace = ENTERPRISE_WE_CHAT_PUSH_URL.replaceAll("\\{token\\}", token); String enterpriseWeChatPushUrlReplace = ENTERPRISE_WE_CHAT_PUSH_URL.replaceAll("\\{token}", token);
CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpClient httpClient = HttpClients.createDefault();
try { try {
@ -215,13 +218,13 @@ public class EnterpriseWeChatUtils {
if (null != mapItemsList) { if (null != mapItemsList) {
for (LinkedHashMap mapItems : mapItemsList) { for (LinkedHashMap mapItems : mapItemsList) {
Set<Map.Entry<String, String>> entries = mapItems.entrySet(); Set<Map.Entry<String, Object>> entries = mapItems.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator(); Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
StringBuilder t = new StringBuilder(String.format("`%s`%s", title, Constants.MARKDOWN_ENTER)); StringBuilder t = new StringBuilder(String.format("`%s`%s", title, Constants.MARKDOWN_ENTER));
while (iterator.hasNext()) { while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next(); Map.Entry<String, Object> entry = iterator.next();
t.append(Constants.MARKDOWN_QUOTE); t.append(Constants.MARKDOWN_QUOTE);
t.append(entry.getKey()).append(":").append(entry.getValue()); t.append(entry.getKey()).append(":").append(entry.getValue());
t.append(Constants.MARKDOWN_ENTER); t.append(Constants.MARKDOWN_ENTER);
@ -241,23 +244,24 @@ public class EnterpriseWeChatUtils {
*/ */
public static String markdownText(String title, String content) { public static String markdownText(String title, String content) {
if (StringUtils.isNotEmpty(content)) { if (StringUtils.isNotEmpty(content)) {
List<String> list; List<LinkedHashMap> mapItemsList = JSONUtils.toList(content, LinkedHashMap.class);
try { if (null != mapItemsList) {
list = JSONUtils.toList(content, String.class);
} catch (Exception e) {
logger.error("json format exception", e);
return null;
}
StringBuilder contents = new StringBuilder(100); StringBuilder contents = new StringBuilder(100);
contents.append(String.format("`%s`%n", title)); contents.append(String.format("`%s`%n", title));
for (String str : list) { for (LinkedHashMap mapItems : mapItemsList) {
Set<Map.Entry<String, Object>> entries = mapItems.entrySet();
Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
contents.append(Constants.MARKDOWN_QUOTE); contents.append(Constants.MARKDOWN_QUOTE);
contents.append(str); contents.append(entry.getKey()).append(":").append(entry.getValue());
contents.append(Constants.MARKDOWN_ENTER); contents.append(Constants.MARKDOWN_ENTER);
} }
}
return contents.toString(); return contents.toString();
}
} }
return null; return null;
@ -278,4 +282,5 @@ public class EnterpriseWeChatUtils {
return result; return result;
} }
} }

53
dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/EnterpriseWeChatUtilsTest.java

@ -14,26 +14,28 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.alert.utils; package org.apache.dolphinscheduler.alert.utils;
import org.apache.dolphinscheduler.common.enums.AlertType; import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.enums.ShowType; import org.apache.dolphinscheduler.common.enums.ShowType;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.plugin.model.AlertData; import org.apache.dolphinscheduler.plugin.model.AlertData;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import java.io.IOException;
import java.util.*;
import org.apache.dolphinscheduler.common.utils.*;
/** /**
* Please manually modify the configuration file before testing. * Please manually modify the configuration file before testing.
* file: alert.properties * file: alert.properties
@ -55,8 +57,12 @@ public class EnterpriseWeChatUtilsTest {
private static final String enterpriseWechatUsers = "LiGang,journey"; private static final String enterpriseWechatUsers = "LiGang,journey";
private static final String msg = "hello world"; private static final String msg = "hello world";
private static final String enterpriseWechatTeamSendMsg = "{\\\"toparty\\\":\\\"{toParty}\\\",\\\"agentid\\\":\\\"{agentId}\\\",\\\"msgtype\\\":\\\"text\\\",\\\"text\\\":{\\\"content\\\":\\\"{msg}\\\"},\\\"safe\\\":\\\"0\\\"}"; private static final String enterpriseWechatTeamSendMsg = "{\\\"toparty\\\":\\\"{toParty}\\\",\\\"agentid\\\":\\\"{agentId}\\\""
private static final String enterpriseWechatUserSendMsg = "{\\\"touser\\\":\\\"{toUser}\\\",\\\"agentid\\\":\\\"{agentId}\\\",\\\"msgtype\\\":\\\"markdown\\\",\\\"markdown\\\":{\\\"content\\\":\\\"{msg}\\\"}}"; +
",\\\"msgtype\\\":\\\"text\\\",\\\"text\\\":{\\\"content\\\":\\\"{msg}\\\"},\\\"safe\\\":\\\"0\\\"}";
private static final String enterpriseWechatUserSendMsg = "{\\\"touser\\\":\\\"{toUser}\\\",\\\"agentid\\\":\\\"{agentId}\\\""
+
",\\\"msgtype\\\":\\\"markdown\\\",\\\"markdown\\\":{\\\"content\\\":\\\"{msg}\\\"}}";
@Before @Before
public void init() { public void init() {
@ -72,7 +78,6 @@ public class EnterpriseWeChatUtilsTest {
Assert.assertTrue(weChartEnable); Assert.assertTrue(weChartEnable);
} }
@Test @Test
public void testMakeTeamSendMsg1() { public void testMakeTeamSendMsg1() {
String sendMsg = EnterpriseWeChatUtils.makeTeamSendMsg(toParty, enterpriseWechatSecret, msg); String sendMsg = EnterpriseWeChatUtils.makeTeamSendMsg(toParty, enterpriseWechatSecret, msg);
@ -82,7 +87,6 @@ public class EnterpriseWeChatUtilsTest {
} }
@Test @Test
public void testMakeTeamSendMsg2() { public void testMakeTeamSendMsg2() {
List<String> parties = new ArrayList<>(); List<String> parties = new ArrayList<>();
@ -140,16 +144,25 @@ public class EnterpriseWeChatUtilsTest {
} }
private Alert createAlertForText() { private Alert createAlertForText() {
String content ="[\"id:69\"," + String content = "[{\"id\":\"69\","
"\"name:UserBehavior-0--1193959466\"," + +
"\"Job name: Start workflow\"," + "\"name\":\"UserBehavior-0--1193959466\","
"\"State: SUCCESS\"," + +
"\"Recovery:NO\"," + "\"Job name\":\"Start workflow\","
"\"Run time: 1\"," + +
"\"Start time: 2018-08-06 10:31:34.0\"," + "\"State\":\"SUCCESS\","
"\"End time: 2018-08-06 10:31:49.0\"," + +
"\"Host: 192.168.xx.xx\"," + "\"Recovery\":\"NO\","
"\"Notify group :4\"]"; +
"\"Run time\":\"1\","
+
"\"Start time\": \"2018-08-06 10:31:34.0\","
+
"\"End time\": \"2018-08-06 10:31:49.0\","
+
"\"Host\": \"192.168.xx.xx\","
+
"\"Notify group\" :\"4\"}]";
Alert alert = new Alert(); Alert alert = new Alert();
alert.setTitle("Mysql Exception"); alert.setTitle("Mysql Exception");
@ -196,8 +209,6 @@ public class EnterpriseWeChatUtilsTest {
} }
// @Test // @Test
// public void testSendSingleTeamWeChat() { // public void testSendSingleTeamWeChat() {
// try { // try {

23
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/AlertEvent.java

@ -0,0 +1,23 @@
/*
* 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.apache.dolphinscheduler.common.enums;
public enum AlertEvent {
SERVER_DOWN,TIME_OUT
}

23
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/AlertWarnLevel.java

@ -0,0 +1,23 @@
/*
* 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.apache.dolphinscheduler.common.enums;
public enum AlertWarnLevel {
MIDDLE,SERIOUS
}

58
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java

@ -17,22 +17,25 @@
package org.apache.dolphinscheduler.dao; package org.apache.dolphinscheduler.dao;
import org.apache.dolphinscheduler.common.enums.AlertEvent;
import org.apache.dolphinscheduler.common.enums.AlertStatus; import org.apache.dolphinscheduler.common.enums.AlertStatus;
import org.apache.dolphinscheduler.common.enums.AlertType; import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.enums.AlertWarnLevel;
import org.apache.dolphinscheduler.common.enums.ShowType; import org.apache.dolphinscheduler.common.enums.ShowType;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory; import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.ProcessAlertContent;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.ServerAlertContent;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.AlertMapper; import org.apache.dolphinscheduler.dao.mapper.AlertMapper;
import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper; import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -103,14 +106,12 @@ public class AlertDao extends AbstractBaseDao {
*/ */
public void sendServerStopedAlert(int alertgroupId, String host, String serverType) { public void sendServerStopedAlert(int alertgroupId, String host, String serverType) {
Alert alert = new Alert(); Alert alert = new Alert();
List<LinkedHashMap> serverStopList = new ArrayList<>(1); List<ServerAlertContent> serverAlertContents = new ArrayList<>(1);
LinkedHashMap<String, String> serverStopedMap = new LinkedHashMap(); ServerAlertContent serverStopAlertContent = ServerAlertContent.newBuilder().
serverStopedMap.put("type", serverType); type(serverType).host(host).event(AlertEvent.SERVER_DOWN).warningLevel(AlertWarnLevel.SERIOUS).
serverStopedMap.put("host", host); build();
serverStopedMap.put("event", "server down"); serverAlertContents.add(serverStopAlertContent);
serverStopedMap.put("warning level", "serious"); String content = JSONUtils.toJsonString(serverAlertContents);
serverStopList.add(serverStopedMap);
String content = JSONUtils.toJsonString(serverStopList);
alert.setTitle("Fault tolerance warning"); alert.setTitle("Fault tolerance warning");
saveTaskTimeoutAlert(alert, content, alertgroupId, null, null); saveTaskTimeoutAlert(alert, content, alertgroupId, null, null);
} }
@ -126,14 +127,15 @@ public class AlertDao extends AbstractBaseDao {
String receivers = processDefinition.getReceivers(); String receivers = processDefinition.getReceivers();
String receiversCc = processDefinition.getReceiversCc(); String receiversCc = processDefinition.getReceiversCc();
Alert alert = new Alert(); Alert alert = new Alert();
List<LinkedHashMap> processTimeoutList = new ArrayList<>(1); List<ProcessAlertContent> processAlertContentList = new ArrayList<>(1);
LinkedHashMap<String, String> processTimeoutMap = new LinkedHashMap(); ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder()
processTimeoutMap.put("id", String.valueOf(processInstance.getId())); .processId(processInstance.getId())
processTimeoutMap.put("name", processInstance.getName()); .processName(processInstance.getName())
processTimeoutMap.put("event", "timeout"); .event(AlertEvent.TIME_OUT)
processTimeoutMap.put("warnLevel", "middle"); .warningLevel(AlertWarnLevel.MIDDLE)
processTimeoutList.add(processTimeoutMap); .build();
String content = JSONUtils.toJsonString(processTimeoutList); processAlertContentList.add(processAlertContent);
String content = JSONUtils.toJsonString(processAlertContentList);
alert.setTitle("Process Timeout Warn"); alert.setTitle("Process Timeout Warn");
saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc); saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc);
} }
@ -169,16 +171,17 @@ public class AlertDao extends AbstractBaseDao {
public void sendTaskTimeoutAlert(int alertgroupId, String receivers, String receiversCc, int processInstanceId, public void sendTaskTimeoutAlert(int alertgroupId, String receivers, String receiversCc, int processInstanceId,
String processInstanceName, int taskId, String taskName) { String processInstanceName, int taskId, String taskName) {
Alert alert = new Alert(); Alert alert = new Alert();
List<LinkedHashMap> taskTimeoutList = new ArrayList<>(1); List<ProcessAlertContent> processAlertContentList = new ArrayList<>(1);
LinkedHashMap<String, String> taskTimeoutMap = new LinkedHashMap(); ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder()
taskTimeoutMap.put("process instance id", String.valueOf(processInstanceId)); .processId(processInstanceId)
taskTimeoutMap.put("process name", processInstanceName); .processName(processInstanceName)
taskTimeoutMap.put("task id", String.valueOf(taskId)); .taskId(taskId)
taskTimeoutMap.put("task name", taskName); .taskName(taskName)
taskTimeoutMap.put("event", "timeout"); .event(AlertEvent.TIME_OUT)
taskTimeoutMap.put("warnLevel", "middle"); .warningLevel(AlertWarnLevel.MIDDLE)
taskTimeoutList.add(taskTimeoutMap); .build();
String content = JSONUtils.toJsonString(taskTimeoutList); processAlertContentList.add(processAlertContent);
String content = JSONUtils.toJsonString(processAlertContentList);
alert.setTitle("Task Timeout Warn"); alert.setTitle("Task Timeout Warn");
saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc); saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc);
} }
@ -210,4 +213,5 @@ public class AlertDao extends AbstractBaseDao {
public AlertMapper getAlertMapper() { public AlertMapper getAlertMapper() {
return alertMapper; return alertMapper;
} }
} }

236
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/ProcessAlertContent.java

@ -0,0 +1,236 @@
/*
* 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.apache.dolphinscheduler.dao.entity;
import org.apache.dolphinscheduler.common.enums.AlertEvent;
import org.apache.dolphinscheduler.common.enums.AlertWarnLevel;
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.ExecutionStatus;
import org.apache.dolphinscheduler.common.enums.Flag;
import java.io.Serializable;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonInclude(Include.NON_NULL)
public class ProcessAlertContent implements Serializable {
@JsonProperty("processId")
private int processId;
@JsonProperty("processName")
private String processName;
@JsonProperty("processType")
private CommandType processType;
@JsonProperty("processState")
private ExecutionStatus processState;
@JsonProperty("recovery")
private Flag recovery;
@JsonProperty("runTimes")
private int runTimes;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JsonProperty("processStartTime")
private Date processStartTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JsonProperty("processEndTime")
private Date processEndTime;
@JsonProperty("processHost")
private String processHost;
@JsonProperty("taskId")
private int taskId;
@JsonProperty("taskName")
private String taskName;
@JsonProperty("event")
private AlertEvent event;
@JsonProperty("warnLevel")
private AlertWarnLevel warnLevel;
@JsonProperty("taskType")
private String taskType;
@JsonProperty("retryTimes")
private int retryTimes;
@JsonProperty("taskState")
private ExecutionStatus taskState;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JsonProperty("taskStartTime")
private Date taskStartTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JsonProperty("taskEndTime")
private Date taskEndTime;
@JsonProperty("taskHost")
private String taskHost;
@JsonProperty("logPath")
private String logPath;
private ProcessAlertContent(Builder builder) {
this.processId = builder.processId;
this.processName = builder.processName;
this.processType = builder.processType;
this.recovery = builder.recovery;
this.processState = builder.processState;
this.runTimes = builder.runTimes;
this.processStartTime = builder.processStartTime;
this.processEndTime = builder.processEndTime;
this.processHost = builder.processHost;
this.taskId = builder.taskId;
this.taskName = builder.taskName;
this.event = builder.event;
this.warnLevel = builder.warnLevel;
this.taskType = builder.taskType;
this.taskState = builder.taskState;
this.taskStartTime = builder.taskStartTime;
this.taskEndTime = builder.taskEndTime;
this.taskHost = builder.taskHost;
this.logPath = builder.logPath;
this.retryTimes = builder.retryTimes;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
private int processId;
private String processName;
private CommandType processType;
private Flag recovery;
private ExecutionStatus processState;
private int runTimes;
private Date processStartTime;
private Date processEndTime;
private String processHost;
private int taskId;
private String taskName;
private AlertEvent event;
private AlertWarnLevel warnLevel;
private String taskType;
private int retryTimes;
private ExecutionStatus taskState;
private Date taskStartTime;
private Date taskEndTime;
private String taskHost;
private String logPath;
public Builder processId(int processId) {
this.processId = processId;
return this;
}
public Builder processName(String processName) {
this.processName = processName;
return this;
}
public Builder processType(CommandType processType) {
this.processType = processType;
return this;
}
public Builder recovery(Flag recovery) {
this.recovery = recovery;
return this;
}
public Builder processState(ExecutionStatus processState) {
this.processState = processState;
return this;
}
public Builder runTimes(int runTimes) {
this.runTimes = runTimes;
return this;
}
public Builder processStartTime(Date processStartTime) {
this.processStartTime = processStartTime;
return this;
}
public Builder processEndTime(Date processEndTime) {
this.processEndTime = processEndTime;
return this;
}
public Builder processHost(String processHost) {
this.processHost = processHost;
return this;
}
public Builder taskId(int taskId) {
this.taskId = taskId;
return this;
}
public Builder taskName(String taskName) {
this.taskName = taskName;
return this;
}
public Builder event(AlertEvent event) {
this.event = event;
return this;
}
public Builder warningLevel(AlertWarnLevel warnLevel) {
this.warnLevel = warnLevel;
return this;
}
public Builder taskType(String taskType) {
this.taskType = taskType;
return this;
}
public Builder retryTimes(int retryTimes) {
this.retryTimes = retryTimes;
return this;
}
public Builder taskState(ExecutionStatus taskState) {
this.taskState = taskState;
return this;
}
public Builder taskStartTime(Date taskStartTime) {
this.taskStartTime = taskStartTime;
return this;
}
public Builder taskEndTime(Date taskEndTime) {
this.taskEndTime = taskEndTime;
return this;
}
public Builder taskHost(String taskHost) {
this.taskHost = taskHost;
return this;
}
public Builder logPath(String logPath) {
this.logPath = logPath;
return this;
}
public ProcessAlertContent build() {
return new ProcessAlertContent(this);
}
}
}

85
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/ServerAlertContent.java

@ -0,0 +1,85 @@
/*
* 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.apache.dolphinscheduler.dao.entity;
import org.apache.dolphinscheduler.common.enums.AlertEvent;
import org.apache.dolphinscheduler.common.enums.AlertWarnLevel;
import com.fasterxml.jackson.annotation.JsonProperty;
public class ServerAlertContent {
/**
* server type :master or worker
*/
@JsonProperty("type")
final String type;
@JsonProperty("host")
final String host;
@JsonProperty("event")
final AlertEvent event;
@JsonProperty("warningLevel")
final AlertWarnLevel warningLevel;
private ServerAlertContent(Builder builder) {
this.type = builder.type;
this.host = builder.host;
this.event = builder.event;
this.warningLevel = builder.warningLevel;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
private String type;
private String host;
private AlertEvent event;
private AlertWarnLevel warningLevel;
public Builder type(String type) {
this.type = type;
return this;
}
public Builder host(String host) {
this.host = host;
return this;
}
public Builder event(AlertEvent event) {
this.event = event;
return this;
}
public Builder warningLevel(AlertWarnLevel warningLevel) {
this.warningLevel = warningLevel;
return this;
}
public ServerAlertContent build() {
return new ServerAlertContent(this);
}
}
}

71
dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/AlertManager.java

@ -21,18 +21,17 @@ import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.ShowType; import org.apache.dolphinscheduler.common.enums.ShowType;
import org.apache.dolphinscheduler.common.enums.WarningType; import org.apache.dolphinscheduler.common.enums.WarningType;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.dao.DaoFactory; import org.apache.dolphinscheduler.dao.DaoFactory;
import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.ProcessAlertContent;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.TaskInstance; import org.apache.dolphinscheduler.dao.entity.TaskInstance;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -98,39 +97,40 @@ public class AlertManager {
String res = ""; String res = "";
if (processInstance.getState().typeIsSuccess()) { if (processInstance.getState().typeIsSuccess()) {
List<LinkedHashMap> successTaskList = new ArrayList<>(1); List<ProcessAlertContent> successTaskList = new ArrayList<>(1);
LinkedHashMap<String, String> successTaskMap = new LinkedHashMap(); ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder()
successTaskMap.put("id", String.valueOf(processInstance.getId())); .processId(processInstance.getId())
successTaskMap.put("name", processInstance.getName()); .processName(processInstance.getName())
successTaskMap.put("job type", getCommandCnName(processInstance.getCommandType())); .processType(processInstance.getCommandType())
successTaskMap.put("state", processInstance.getState().toString()); .processState(processInstance.getState())
successTaskMap.put("recovery", processInstance.getRecovery().toString()); .recovery(processInstance.getRecovery())
successTaskMap.put("run time", String.valueOf(processInstance.getRunTimes())); .runTimes(processInstance.getRunTimes())
successTaskMap.put("start time", DateUtils.dateToString(processInstance.getStartTime())); .processStartTime(processInstance.getStartTime())
successTaskMap.put("end time", DateUtils.dateToString(processInstance.getEndTime())); .processEndTime(processInstance.getEndTime())
successTaskMap.put("host", processInstance.getHost()); .processHost(processInstance.getHost())
successTaskList.add(successTaskMap); .build();
successTaskList.add(processAlertContent);
res = JSONUtils.toJsonString(successTaskList); res = JSONUtils.toJsonString(successTaskList);
} else if (processInstance.getState().typeIsFailure()) { } else if (processInstance.getState().typeIsFailure()) {
List<LinkedHashMap> failedTaskList = new ArrayList<>(); List<ProcessAlertContent> failedTaskList = new ArrayList<>();
for (TaskInstance task : taskInstances) { for (TaskInstance task : taskInstances) {
if (task.getState().typeIsSuccess()) { if (task.getState().typeIsSuccess()) {
continue; continue;
} }
LinkedHashMap<String, String> failedTaskMap = new LinkedHashMap(); ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder()
failedTaskMap.put("process instance id", String.valueOf(processInstance.getId())); .processId(processInstance.getId())
failedTaskMap.put("process instance name", processInstance.getName()); .processName(processInstance.getName())
failedTaskMap.put("task id", String.valueOf(task.getId())); .taskId(task.getId())
failedTaskMap.put("task name", task.getName()); .taskName(task.getName())
failedTaskMap.put("task type", task.getTaskType()); .taskType(task.getTaskType())
failedTaskMap.put("task state", task.getState().toString()); .taskState(task.getState())
failedTaskMap.put("task start time", DateUtils.dateToString(task.getStartTime())); .taskStartTime(task.getStartTime())
failedTaskMap.put("task end time", DateUtils.dateToString(task.getEndTime())); .taskEndTime(task.getEndTime())
failedTaskMap.put("host", task.getHost()); .taskHost(task.getHost())
failedTaskMap.put("log path", task.getLogPath()); .logPath(task.getLogPath())
failedTaskList.add(failedTaskMap); .build();
failedTaskList.add(processAlertContent);
} }
res = JSONUtils.toJsonString(failedTaskList); res = JSONUtils.toJsonString(failedTaskList);
} }
@ -147,15 +147,16 @@ public class AlertManager {
*/ */
private String getWorkerToleranceContent(ProcessInstance processInstance, List<TaskInstance> toleranceTaskList) { private String getWorkerToleranceContent(ProcessInstance processInstance, List<TaskInstance> toleranceTaskList) {
List<LinkedHashMap<String, String>> toleranceTaskInstanceList = new ArrayList<>(); List<ProcessAlertContent> toleranceTaskInstanceList = new ArrayList<>();
for (TaskInstance taskInstance : toleranceTaskList) { for (TaskInstance taskInstance : toleranceTaskList) {
LinkedHashMap<String, String> toleranceWorkerContentMap = new LinkedHashMap(); ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder()
toleranceWorkerContentMap.put("process name", processInstance.getName()); .processName(processInstance.getName())
toleranceWorkerContentMap.put("task name", taskInstance.getName()); .taskName(taskInstance.getName())
toleranceWorkerContentMap.put("host", taskInstance.getHost()); .taskHost(taskInstance.getHost())
toleranceWorkerContentMap.put("task retry times", String.valueOf(taskInstance.getRetryTimes())); .retryTimes(taskInstance.getRetryTimes())
toleranceTaskInstanceList.add(toleranceWorkerContentMap); .build();
toleranceTaskInstanceList.add(processAlertContent);
} }
return JSONUtils.toJsonString(toleranceTaskInstanceList); return JSONUtils.toJsonString(toleranceTaskInstanceList);
} }

Loading…
Cancel
Save