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

* Change the content of alert to an array

* Increase unit test coverage

* Modify the code format

* Modify the code format

* Modify judgment statement

* Deleting invalid References

* Modify the previous code implementation and improve the test data

* Modify the code format

* Move the location of the Content conversion

* Remove useless code

* Change the code format

* Modify unit tests

* Modify unit tests

* Modify unit tests

* Changing the location of the code

* Modify the code

Co-authored-by: fanwanlong <fanwanlong@kezaihui.com>
Co-authored-by: 范万龙 <fanwanlong@fanwanlongs-MBP.lan>
This commit is contained in:
jackfanwan 2022-08-12 09:57:27 +08:00 committed by zhuangchong
parent 70ddb11636
commit b709cd9a12
2 changed files with 24 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import org.apache.dolphinscheduler.plugin.alert.email.EmailConstants;
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;
@ -35,6 +36,11 @@ import org.slf4j.LoggerFactory;
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 {
@ -113,6 +119,16 @@ public class DefaultHTMLTemplate implements AlertTemplate {
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) {

View File

@ -46,6 +46,14 @@ public class DefaultHTMLTemplateTest {
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() {