Unverified Commit df862a44 authored by aiwenmo's avatar aiwenmo Committed by GitHub

Merge pull request #175 from zhu-mingye/dev

添加钉钉告警的MarkDown和Text消息类型
parents 0bef2d49 508a6a99
...@@ -10,6 +10,10 @@ public final class DingTalkConstants { ...@@ -10,6 +10,10 @@ public final class DingTalkConstants {
static final String TYPE = "DingTalk"; static final String TYPE = "DingTalk";
static final String MARKDOWN_QUOTE = "- ";
static final String MARKDOWN_ENTER = "\n";
static final String PROXY_ENABLE = "isEnableProxy"; static final String PROXY_ENABLE = "isEnableProxy";
static final String WEB_HOOK = "webhook"; static final String WEB_HOOK = "webhook";
...@@ -18,7 +22,7 @@ public final class DingTalkConstants { ...@@ -18,7 +22,7 @@ public final class DingTalkConstants {
static final String SECRET = "secret"; static final String SECRET = "secret";
static final String MSG_TYPE = "msgType"; static final String MSG_TYPE = "msgtype";
static final String AT_MOBILES = "atMobiles"; static final String AT_MOBILES = "atMobiles";
...@@ -34,8 +38,5 @@ public final class DingTalkConstants { ...@@ -34,8 +38,5 @@ public final class DingTalkConstants {
static final String PASSWORD = "password"; static final String PASSWORD = "password";
static final String MSG_TYPE_TEXT = "text";
static final String MSG_TYPE_MARKDOWN = "markdown";
} }
...@@ -2,9 +2,11 @@ package com.dlink.alert.dingtalk; ...@@ -2,9 +2,11 @@ package com.dlink.alert.dingtalk;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
import com.dlink.alert.AlertSendResponse; import com.dlink.alert.AlertSendResponse;
import com.dlink.alert.ShowType;
import com.dlink.assertion.Asserts; import com.dlink.assertion.Asserts;
import com.dlink.utils.JSONUtil; import com.dlink.utils.JSONUtil;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
...@@ -26,10 +28,7 @@ import javax.crypto.spec.SecretKeySpec; ...@@ -26,10 +28,7 @@ import javax.crypto.spec.SecretKeySpec;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/** /**
* DingTalkSender * DingTalkSender
...@@ -123,13 +122,13 @@ public class DingTalkSender { ...@@ -123,13 +122,13 @@ public class DingTalkSender {
private String generateMsgJson(String title, String content) { private String generateMsgJson(String title, String content) {
if (Asserts.isNullString(msgType)) { if (Asserts.isNullString(msgType)) {
msgType = DingTalkConstants.MSG_TYPE_TEXT; msgType = ShowType.TEXT.getValue();
} }
Map<String, Object> items = new HashMap<>(); Map<String, Object> items = new HashMap<>();
items.put("msgtype", msgType); items.put("msgtype", msgType);
Map<String, Object> text = new HashMap<>(); Map<String, Object> text = new HashMap<>();
items.put(msgType, text); items.put(msgType, text);
if (DingTalkConstants.MSG_TYPE_MARKDOWN.equals(msgType)) { if (ShowType.TABLE.getValue().equals(msgType)) {
generateMarkdownMsg(title, content, text); generateMarkdownMsg(title, content, text);
} else { } else {
generateTextMsg(title, content, text); generateTextMsg(title, content, text);
...@@ -139,18 +138,17 @@ public class DingTalkSender { ...@@ -139,18 +138,17 @@ public class DingTalkSender {
} }
private void generateTextMsg(String title, String content, Map<String, Object> text) { private void generateTextMsg(String title, String content, Map<String, Object> text) {
StringBuilder builder = new StringBuilder(title); StringBuilder builder = new StringBuilder();
builder.append("\n");
builder.append(content);
if (Asserts.isNotNullString(keyword)) { if (Asserts.isNotNullString(keyword)) {
builder.append(" ");
builder.append(keyword); builder.append(keyword);
builder.append("\n");
} }
text.put("content", builder.toString()); String txt = genrateResultMsg(title, content, builder);
text.put("content", txt);
} }
private void generateMarkdownMsg(String title, String content, Map<String, Object> text) { private void generateMarkdownMsg(String title, String content, Map<String, Object> text) {
StringBuilder builder = new StringBuilder(content); StringBuilder builder = new StringBuilder("# ");
if (Asserts.isNotNullString(keyword)) { if (Asserts.isNotNullString(keyword)) {
builder.append(" "); builder.append(" ");
builder.append(keyword); builder.append(keyword);
...@@ -170,8 +168,41 @@ public class DingTalkSender { ...@@ -170,8 +168,41 @@ public class DingTalkSender {
builder.append(" "); builder.append(" ");
}); });
} }
String txt = genrateResultMsg(title, content, builder);
text.put("title", title); text.put("title", title);
text.put("text", builder.toString()); text.put("text", txt);
}
/**
* 公共生成 markdown 和 text 消息
* @param title 标题
* @param content 内容
* @param builder 拼接字符串
* @return
*/
private String genrateResultMsg(String title, String content, StringBuilder builder) {
List<LinkedHashMap> mapSendResultItemsList = JSONUtil.toList(content, LinkedHashMap.class);
if (null == mapSendResultItemsList || mapSendResultItemsList.isEmpty()) {
logger.error("itemsList is null");
throw new RuntimeException("itemsList is null");
}
for (LinkedHashMap mapItems : mapSendResultItemsList) {
Set<Map.Entry<String, Object>> entries = mapItems.entrySet();
Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
StringBuilder t = new StringBuilder(String.format("`%s`%s", title, DingTalkConstants.MARKDOWN_ENTER));
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
t.append(DingTalkConstants.MARKDOWN_QUOTE);
t.append(entry.getKey()).append(":").append(entry.getValue());
t.append(DingTalkConstants.MARKDOWN_ENTER);
}
builder.append(t);
}
byte[] byt = StringUtils.getBytesUtf8(builder.toString());
String txt = StringUtils.newStringUtf8(byt);
return txt;
} }
private String generateSignedUrl() { private String generateSignedUrl() {
......
...@@ -3,6 +3,7 @@ package com.dlink.alert.dingtalk; ...@@ -3,6 +3,7 @@ package com.dlink.alert.dingtalk;
import com.dlink.alert.Alert; import com.dlink.alert.Alert;
import com.dlink.alert.AlertConfig; import com.dlink.alert.AlertConfig;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
import com.dlink.alert.ShowType;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -18,14 +19,32 @@ import java.util.Map; ...@@ -18,14 +19,32 @@ import java.util.Map;
**/ **/
public class DingTalkTest { public class DingTalkTest {
private static final Map<String, String> config = new HashMap<>(); private static Map<String, String> config = new HashMap<>();
private String contentTest = "[{\"id\":\"70\","
+
"\"name\":\"UserBehavior-0--1193959466\","
+
"\"Job name\":\"Start workflow\","
+
"\"State\":\"SUCCESS\","
+
"\"Recovery\":\"NO\","
+
"\"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\"}]";
@Before @Before
public void initDingTalkConfig() { public void initDingTalkConfig() {
config.put(DingTalkConstants.KEYWORD, "keyword"); config.put(DingTalkConstants.KEYWORD, "Dlinky-Fink 钉钉告警测试");
config.put(DingTalkConstants.WEB_HOOK, "url"); config.put(DingTalkConstants.WEB_HOOK, "url");
config.put(DingTalkConstants.MSG_TYPE, DingTalkConstants.MSG_TYPE_MARKDOWN); config.put(DingTalkConstants.MSG_TYPE, ShowType.TABLE.getValue());
config.put(DingTalkConstants.PROXY_ENABLE, "false"); config.put(DingTalkConstants.PROXY_ENABLE, "false");
config.put(DingTalkConstants.PASSWORD, "password"); config.put(DingTalkConstants.PASSWORD, "password");
...@@ -34,10 +53,20 @@ public class DingTalkTest { ...@@ -34,10 +53,20 @@ public class DingTalkTest {
} }
@Test @Test
public void sendTest(){ public void sendMarkDownMsgTest(){
AlertConfig config = AlertConfig.build("test", "DingTalk", DingTalkTest.config); AlertConfig config = AlertConfig.build("MarkDownTest", "DingTalk", DingTalkTest.config);
Alert alert = Alert.build(config);
AlertResult result = alert.send("Dlinky钉钉告警测试", contentTest);
Assert.assertEquals(true, result.getSuccess());
}
@Test
public void sendTextMsgTest(){
config.put(DingTalkConstants.MSG_TYPE, ShowType.TEXT.getValue());
AlertConfig config = AlertConfig.build("TextMsgTest", "DingTalk", DingTalkTest.config);
Alert alert = Alert.build(config); Alert alert = Alert.build(config);
AlertResult result = alert.send("hello word", "UTF-8"); AlertResult result = alert.send("Dlinky钉钉告警测试", contentTest);
Assert.assertEquals(false, result.getSuccess()); Assert.assertEquals(true, result.getSuccess());
} }
} }
...@@ -155,7 +155,7 @@ public class WeChatSender { ...@@ -155,7 +155,7 @@ public class WeChatSender {
Map.Entry<String, Object> entry = iterator.next(); Map.Entry<String, Object> entry = iterator.next();
t.append(WeChatConstants.MARKDOWN_QUOTE); t.append(WeChatConstants.MARKDOWN_QUOTE);
t.append(entry.getKey()).append(":").append(entry.getValue()); t.append(entry.getKey()).append("").append(entry.getValue());
t.append(WeChatConstants.MARKDOWN_ENTER); t.append(WeChatConstants.MARKDOWN_ENTER);
} }
contents.append(t); contents.append(t);
...@@ -179,7 +179,7 @@ public class WeChatSender { ...@@ -179,7 +179,7 @@ public class WeChatSender {
Set<Map.Entry<String, Object>> entries = mapItems.entrySet(); Set<Map.Entry<String, Object>> entries = mapItems.entrySet();
for (Map.Entry<String, Object> entry : entries) { for (Map.Entry<String, Object> entry : entries) {
contents.append(WeChatConstants.MARKDOWN_QUOTE); contents.append(WeChatConstants.MARKDOWN_QUOTE);
contents.append(entry.getKey()).append(":").append(entry.getValue()); contents.append(entry.getKey()).append("").append(entry.getValue());
contents.append(WeChatConstants.MARKDOWN_ENTER); contents.append(WeChatConstants.MARKDOWN_ENTER);
} }
......
...@@ -72,8 +72,8 @@ public class Table implements Serializable, Comparable<Table> { ...@@ -72,8 +72,8 @@ public class Table implements Serializable, Comparable<Table> {
public String getFlinkTableSql(String catalogName, String flinkConfig) { public String getFlinkTableSql(String catalogName, String flinkConfig) {
StringBuilder sb = new StringBuilder("DROP TABLE IF EXISTS "); StringBuilder sb = new StringBuilder("DROP TABLE IF EXISTS ");
String fullSchemaName = catalogName + "." + schema + "." + name; String fullSchemaName = catalogName + "." + schema + "." + name;
sb.append(fullSchemaName + ";\n"); sb.append(name + ";\n");
sb.append("CREATE TABLE IF NOT EXISTS " + fullSchemaName + " (\n"); sb.append("CREATE TABLE IF NOT EXISTS " + name + " (\n");
List<String> pks = new ArrayList<>(); List<String> pks = new ArrayList<>();
for (int i = 0; i < columns.size(); i++) { for (int i = 0; i < columns.size(); i++) {
String type = columns.get(i).getJavaType().getFlinkType(); String type = columns.get(i).getJavaType().getFlinkType();
......
import React, {useEffect, useState} from 'react'; import React, {useState} from 'react';
import {Form, Button, Input, Modal, Divider,Switch} from 'antd'; import {Button, Divider, Form, Input, Modal, Radio, Switch} from 'antd';
import {AlertInstanceTableListItem} from "@/pages/AlertInstance/data"; import {AlertInstanceTableListItem} from "@/pages/AlertInstance/data";
import {buildJSONData, getJSONData} from "@/pages/AlertInstance/function"; import {buildJSONData, getJSONData} from "@/pages/AlertInstance/function";
import {ALERT_TYPE} from "@/pages/AlertInstance/conf"; import {ALERT_TYPE} from "@/pages/AlertInstance/conf";
...@@ -109,7 +109,7 @@ const DingTalkForm: React.FC<AlertInstanceFormProps> = (props) => { ...@@ -109,7 +109,7 @@ const DingTalkForm: React.FC<AlertInstanceFormProps> = (props) => {
name="isAtAll" name="isAtAll"
label="@所有人"> label="@所有人">
<Switch checkedChildren="启用" unCheckedChildren="禁用" <Switch checkedChildren="启用" unCheckedChildren="禁用"
defaultChecked={formVals.IsAtAll}/> defaultChecked={formVals.isAtAll}/>
</Form.Item> </Form.Item>
<Form.Item <Form.Item
name="enabled" name="enabled"
...@@ -117,6 +117,16 @@ const DingTalkForm: React.FC<AlertInstanceFormProps> = (props) => { ...@@ -117,6 +117,16 @@ const DingTalkForm: React.FC<AlertInstanceFormProps> = (props) => {
<Switch checkedChildren="启用" unCheckedChildren="禁用" <Switch checkedChildren="启用" unCheckedChildren="禁用"
defaultChecked={formVals.enabled}/> defaultChecked={formVals.enabled}/>
</Form.Item> </Form.Item>
<Form.Item
name="msgtype"
label="展示方式"
rules={[{required: true, message: '请选择展示方式!'}]}
>
<Radio.Group >
<Radio value='markdown'>MarkDown</Radio>
<Radio value='text'>文本</Radio>
</Radio.Group>
</Form.Item>
</> </>
); );
}; };
......
...@@ -102,7 +102,7 @@ const WeChatForm: React.FC<AlertInstanceFormProps> = (props) => { ...@@ -102,7 +102,7 @@ const WeChatForm: React.FC<AlertInstanceFormProps> = (props) => {
<Form.Item <Form.Item
name="showType" name="showType"
label="展示方式" label="展示方式"
rules={[{required: true, message: '请输入展示方式!'}]} rules={[{required: true, message: '请选择展示方式!'}]}
> >
<Radio.Group > <Radio.Group >
<Radio value='markdown'>MarkDown</Radio> <Radio value='markdown'>MarkDown</Radio>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment