Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix-10961][alert server]Change the content of alert to an array #11033

Merged
merged 17 commits into from
Aug 12, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import org.apache.dolphinscheduler.spi.utils.StringUtils;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -35,6 +36,11 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONTokener;

public class DefaultHTMLTemplate implements AlertTemplate {

Expand Down Expand Up @@ -113,6 +119,16 @@ private String getTableTypeMessage(String content, boolean showAll) {
private String getTextTypeMessage(String content) {

if (StringUtils.isNotEmpty(content)) {
// Converts an object type to an array type to prevent subsequent conversions from reporting errors
try {
Object contentObject = new JSONTokener(content).nextValue();
if (!(contentObject instanceof JSONArray)) {
ObjectNode jsonNodes = JSONUtils.parseObject(content);
content = JSONUtils.toJsonString(Collections.singletonList(jsonNodes));
}
} catch (JSONException e) {
logger.error("alert content is null");
}
ArrayNode list = JSONUtils.parseArray(content);
StringBuilder contents = new StringBuilder(100);
for (JsonNode jsonNode : list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public void testGetMessageFromTemplate() {
String textTypeMessage = template.getMessageFromTemplate(list2String(), ShowType.TEXT, true);

assertEquals(textTypeMessage, generateMockTextTypeResultByHand());

String mapjson = "{\"taskInstanceId\":94,\"taskName\":\"000\",\"taskType\":\"DATA_QUALITY\","
+ "\"processDefinitionId\":0,\"processInstanceId\":58,\"state\":\"RUNNING_EXECUTION\","
+ "\"startTime\":\"2022-07-17 16:00:32\",\"host\":\"192.168.18.182:1234\","
+ "\"logPath\":\"/Users/mac/学习/dolphinscheduler/dolphinscheduler/logs/20220717/6222644042400_1-58-94.log\"}";
textTypeMessage = template.getMessageFromTemplate(mapjson, ShowType.TEXT, true);
String result = textTypeMessage;
assertEquals(textTypeMessage, result);
}

private String list2String() {
Expand Down