You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by ke...@apache.org on 2022/09/19 00:55:45 UTC

[dolphinscheduler] branch 3.0.1-prepare updated: [fix-10961][alert server]Change the content of alert to an array (#11033)

This is an automated email from the ASF dual-hosted git repository.

kerwin pushed a commit to branch 3.0.1-prepare
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/3.0.1-prepare by this push:
     new b709cd9a12 [fix-10961][alert server]Change the content of alert to an array (#11033)
b709cd9a12 is described below

commit b709cd9a12bf0dc87cc19af3a2ee712bfb818799
Author: jackfanwan <61...@users.noreply.github.com>
AuthorDate: Fri Aug 12 09:57:27 2022 +0800

    [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 <fa...@kezaihui.com>
    Co-authored-by: 范万龙 <fa...@fanwanlongs-MBP.lan>
---
 .../plugin/alert/email/template/DefaultHTMLTemplate.java | 16 ++++++++++++++++
 .../alert/email/template/DefaultHTMLTemplateTest.java    |  8 ++++++++
 2 files changed, 24 insertions(+)

diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplate.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplate.java
index 433cfda3f7..4167e77235 100644
--- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplate.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplate.java
@@ -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) {
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplateTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplateTest.java
index bc357cd34a..37fe97857c 100644
--- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplateTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/template/DefaultHTMLTemplateTest.java
@@ -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() {