Skip to content

Commit

Permalink
[fix-10961][alert server]Change the content of alert to an array (apa…
Browse files Browse the repository at this point in the history
…che#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>
  • Loading branch information
3 people authored and xdu-chenrj committed Oct 13, 2022
1 parent 90f34ac commit b5034c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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

0 comments on commit b5034c4

Please sign in to comment.