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 {
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 WEB_HOOK = "webhook";
......@@ -18,7 +22,7 @@ public final class DingTalkConstants {
static final String SECRET = "secret";
static final String MSG_TYPE = "msgType";
static final String MSG_TYPE = "msgtype";
static final String AT_MOBILES = "atMobiles";
......@@ -34,8 +38,5 @@ public final class DingTalkConstants {
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;
import com.dlink.alert.AlertResult;
import com.dlink.alert.AlertSendResponse;
import com.dlink.alert.ShowType;
import com.dlink.assertion.Asserts;
import com.dlink.utils.JSONUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
......@@ -26,10 +28,7 @@ import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
/**
* DingTalkSender
......@@ -123,13 +122,13 @@ public class DingTalkSender {
private String generateMsgJson(String title, String content) {
if (Asserts.isNullString(msgType)) {
msgType = DingTalkConstants.MSG_TYPE_TEXT;
msgType = ShowType.TEXT.getValue();
}
Map<String, Object> items = new HashMap<>();
items.put("msgtype", msgType);
Map<String, Object> text = new HashMap<>();
items.put(msgType, text);
if (DingTalkConstants.MSG_TYPE_MARKDOWN.equals(msgType)) {
if (ShowType.TABLE.getValue().equals(msgType)) {
generateMarkdownMsg(title, content, text);
} else {
generateTextMsg(title, content, text);
......@@ -139,18 +138,17 @@ public class DingTalkSender {
}
private void generateTextMsg(String title, String content, Map<String, Object> text) {
StringBuilder builder = new StringBuilder(title);
builder.append("\n");
builder.append(content);
StringBuilder builder = new StringBuilder();
if (Asserts.isNotNullString(keyword)) {
builder.append(" ");
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) {
StringBuilder builder = new StringBuilder(content);
StringBuilder builder = new StringBuilder("# ");
if (Asserts.isNotNullString(keyword)) {
builder.append(" ");
builder.append(keyword);
......@@ -170,8 +168,41 @@ public class DingTalkSender {
builder.append(" ");
});
}
String txt = genrateResultMsg(title, content, builder);
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() {
......
......@@ -3,6 +3,7 @@ package com.dlink.alert.dingtalk;
import com.dlink.alert.Alert;
import com.dlink.alert.AlertConfig;
import com.dlink.alert.AlertResult;
import com.dlink.alert.ShowType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
......@@ -18,14 +19,32 @@ import java.util.Map;
**/
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
public void initDingTalkConfig() {
config.put(DingTalkConstants.KEYWORD, "keyword");
config.put(DingTalkConstants.KEYWORD, "Dlinky-Fink 钉钉告警测试");
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.PASSWORD, "password");
......@@ -34,10 +53,20 @@ public class DingTalkTest {
}
@Test
public void sendTest(){
AlertConfig config = AlertConfig.build("test", "DingTalk", DingTalkTest.config);
public void sendMarkDownMsgTest(){
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);
AlertResult result = alert.send("hello word", "UTF-8");
Assert.assertEquals(false, result.getSuccess());
AlertResult result = alert.send("Dlinky钉钉告警测试", contentTest);
Assert.assertEquals(true, result.getSuccess());
}
}
......@@ -155,7 +155,7 @@ public class WeChatSender {
Map.Entry<String, Object> entry = iterator.next();
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);
}
contents.append(t);
......@@ -179,7 +179,7 @@ public class WeChatSender {
Set<Map.Entry<String, Object>> entries = mapItems.entrySet();
for (Map.Entry<String, Object> entry : entries) {
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);
}
......
......@@ -72,8 +72,8 @@ public class Table implements Serializable, Comparable<Table> {
public String getFlinkTableSql(String catalogName, String flinkConfig) {
StringBuilder sb = new StringBuilder("DROP TABLE IF EXISTS ");
String fullSchemaName = catalogName + "." + schema + "." + name;
sb.append(fullSchemaName + ";\n");
sb.append("CREATE TABLE IF NOT EXISTS " + fullSchemaName + " (\n");
sb.append(name + ";\n");
sb.append("CREATE TABLE IF NOT EXISTS " + name + " (\n");
List<String> pks = new ArrayList<>();
for (int i = 0; i < columns.size(); i++) {
String type = columns.get(i).getJavaType().getFlinkType();
......
import React, {useEffect, useState} from 'react';
import {Form, Button, Input, Modal, Divider,Switch} from 'antd';
import React, {useState} from 'react';
import {Button, Divider, Form, Input, Modal, Radio, Switch} from 'antd';
import {AlertInstanceTableListItem} from "@/pages/AlertInstance/data";
import {buildJSONData, getJSONData} from "@/pages/AlertInstance/function";
import {ALERT_TYPE} from "@/pages/AlertInstance/conf";
......@@ -109,7 +109,7 @@ const DingTalkForm: React.FC<AlertInstanceFormProps> = (props) => {
name="isAtAll"
label="@所有人">
<Switch checkedChildren="启用" unCheckedChildren="禁用"
defaultChecked={formVals.IsAtAll}/>
defaultChecked={formVals.isAtAll}/>
</Form.Item>
<Form.Item
name="enabled"
......@@ -117,6 +117,16 @@ const DingTalkForm: React.FC<AlertInstanceFormProps> = (props) => {
<Switch checkedChildren="启用" unCheckedChildren="禁用"
defaultChecked={formVals.enabled}/>
</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) => {
<Form.Item
name="showType"
label="展示方式"
rules={[{required: true, message: '请输入展示方式!'}]}
rules={[{required: true, message: '请选择展示方式!'}]}
>
<Radio.Group >
<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