Skip to content

Commit

Permalink
Complete HTTP alert (DataLinkDC#2711)
Browse files Browse the repository at this point in the history
* fix log error

* Complete HTTP alert

* Complete HTTP alert

* fix comment

* fix i81n

* fix http test

* formate code

* formate code
  • Loading branch information
gaoyan1998 authored Dec 21, 2023
1 parent 90c8b61 commit cd51137
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ public Result<List<AlertInstance>> listEnabledAll() {
paramType = "body",
required = true,
dataTypeClass = AlertInstanceDTO.class)
public Result<Void> sendAlertMsgTest(@RequestBody AlertInstanceDTO alertInstanceDTO) {
public Result<String> sendAlertMsgTest(@RequestBody AlertInstanceDTO alertInstanceDTO) {
AlertResult alertResult = alertInstanceService.testAlert(alertInstanceDTO);
if (alertResult.getSuccess()) {
return Result.succeed(Status.SEND_TEST_SUCCESS);
return Result.succeed(alertResult.getMessage(), Status.SEND_TEST_SUCCESS);
} else {
return Result.failed(Status.SEND_TEST_FAILED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class AlertBaseConstant {
public static final String AT_USERS = "atUsers";
public static final String PASSWORD = "password";

public static final String ALERT_TEMPLATE_TITLE = "Job Alert Test Title";
public static final String ALERT_TEMPLATE_MSG = "\n- **Job Name:** <font color='gray'>Test Job</font>\n"
public static final String ALERT_TEMPLATE_TITLE = "Dinky Job Alert Test Title";
public static final String ALERT_TEMPLATE_MSG = "\n- **Job Name:** <font color='gray'>Dinky Test Job</font>\n"
+ "- **Job Status:** <font color='red'>FAILED</font>\n"
+ "- **Alert Time:** 2023-01-01 12:00:00\n"
+ "- **Start Time:** 2023-01-01 12:00:00\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,11 @@
import org.dinky.alert.AbstractAlert;
import org.dinky.alert.AlertResult;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* DingTalkAlert
* Http Alert
*
* @since 2022/2/23 19:28
*/
public class HttpAlert extends AbstractAlert {
private static final Logger log = LoggerFactory.getLogger(HttpAlert.class);

@Override
public String getType() {
Expand All @@ -43,7 +36,6 @@ public String getType() {
@Override
public AlertResult send(String title, String content) {
HttpSender sender = new HttpSender(getConfig().getParam());
Map<String, Object> templateParams = sender.buildTemplateParams(title, content);
return sender.send(templateParams);
return sender.send(title, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

package org.dinky.alert.http;

/** DingTalkConstants */
/** Http Constants */
public class HttpConstants {
public static final String TYPE = "Http";
public static final String ALERT_TEMPLATE_TITLE = "title";
public static final String ALERT_TEMPLATE_CONTENT = "content";

public static final String REQUEST_TYPE_POST = "POST";
public static final String REQUEST_TYPE_GET = "GET";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,119 +26,85 @@

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.TextUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
* DingTalkSender
* Http Sender
*/
public class HttpSender {
private static final Logger logger = LoggerFactory.getLogger(HttpSender.class);

private final HttpParams httpParams;

private HttpRequestBase httpRequest;

HttpSender(Map<String, Object> config) {
this.httpParams = JSONUtil.toBean(JSONUtil.toJsonStr(config), HttpParams.class);
Asserts.checkNotNull(httpParams, "httpParams is null");
}

/**
* build template params
*
* @param title
* @param content
* @return
*/
public Map<String, Object> buildTemplateParams(String title, String content) {
Map<String, Object> params = new HashMap<>();
params.put(HttpConstants.ALERT_TEMPLATE_TITLE, title);
params.put(HttpConstants.ALERT_TEMPLATE_CONTENT, content);
return params;
}

/**
* send msg of main
*
* @param content: send msg content
* @return AlertResult
*/
public AlertResult send(Map<String, Object> templateParams) {
public AlertResult send(String title, String content) {
AlertResult alertResult = new AlertResult();

try {
createHttpRequest(templateParams);
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}

if (httpParams.getMethod() == null) {
alertResult.setSuccess(false);
alertResult.setMessage("Request types are not supported");
return alertResult;
}

try {
createHttpRequest(title, content);
String resp = this.getResponseString(httpRequest);
alertResult.setSuccess(true);
alertResult.setMessage(resp);
} catch (Exception e) {
logger.error("send http alert msg exception : {}", e.getMessage());
logger.error("send http alert msg failed", e);
alertResult.setSuccess(false);
alertResult.setMessage("send http request alert fail.");
}

return alertResult;
}

private void createHttpRequest(Map<String, Object> templateParams)
throws MalformedURLException, URISyntaxException {
private void createHttpRequest(String title, String content) {
if (HttpConstants.REQUEST_TYPE_POST.equals(httpParams.getMethod())) {
httpRequest = new HttpPost(httpParams.getUrl());
buildRequestHeader();
buildMsgToRequestBody(templateParams);
buildMsgToRequestBody(title, content);
} else if (HttpConstants.REQUEST_TYPE_GET.equals(httpParams.getMethod())) {
buildMsgToUrl(templateParams);
URL unencodeUrl = new URL(httpParams.getUrl());
URI uri = new URI(
unencodeUrl.getProtocol(),
unencodeUrl.getHost(),
unencodeUrl.getPath(),
unencodeUrl.getQuery(),
null);

httpRequest = new HttpGet(uri);
buildRequestHeader();
// TODO Get implementation
}
}

public String getResponseString(HttpRequestBase httpRequest) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, HttpConstants.DEFAULT_CHARSET);
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
CloseableHttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, HttpConstants.DEFAULT_CHARSET);
}
}

/**
Expand Down Expand Up @@ -170,19 +136,19 @@ private void buildRequestHeader() {
/**
* set body params
*/
private void buildMsgToRequestBody(Map<String, Object> templateParams) {
private void buildMsgToRequestBody(String title, String content) {
try {
JSONObject jsonObject = JSONUtil.createObj();
templateParams.forEach(jsonObject::set);
if (CollUtil.isNotEmpty(httpParams.getBody())) {
httpParams.getBody().forEach(configItem -> {
jsonObject.set(configItem.getKey(), configItem.getValue());
});
JSONObject body = JSONUtil.parseObj(httpParams.getBody());
if (TextUtils.isEmpty(httpParams.getTitleFiled())) {
content = StrFormatter.format("{}\n{}", title, content);
} else {
body.putByPath(httpParams.getTitleFiled(), title);
}
StringEntity entity = new StringEntity(JSONUtil.toJsonStr(jsonObject), HttpConstants.DEFAULT_CHARSET);
body.putByPath(httpParams.getContentFiled(), content);
StringEntity entity = new StringEntity(JSONUtil.toJsonStr(body), HttpConstants.DEFAULT_CHARSET);
((HttpPost) httpRequest).setEntity(entity);
} catch (Exception e) {
logger.error("send http alert msg exception : {}", e.getMessage());
logger.error("send http alert msg exception", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.dinky.data.ext.ConfigItem;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;
Expand All @@ -35,8 +34,10 @@
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
public class HttpParams {

private String url = "";
private String method = "";
private List<ConfigItem> headers = new ArrayList<>();
private List<ConfigItem> body = new ArrayList<>();
private String url;
private String method;
private String contentFiled;
private String titleFiled;
private List<ConfigItem> headers;
private String body;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,12 @@ public void initConfig() {
ConfigItem configItem = new ConfigItem("Content-Type", "application/json");
httpParams.setHeaders(Arrays.asList(configItem));

ConfigItem msgType = new ConfigItem("msgtype", "markdown");
ConfigItem title = new ConfigItem("title", AlertBaseConstant.ALERT_TEMPLATE_TITLE);
ConfigItem content = new ConfigItem("content", AlertBaseConstant.ALERT_TEMPLATE_MSG);
ConfigItem markdown = new ConfigItem("markdown", JSONUtil.toJsonStr(new HashMap<String, Object>() {
{
put("title", AlertBaseConstant.ALERT_TEMPLATE_TITLE);
put("text", AlertBaseConstant.ALERT_TEMPLATE_MSG);
}
}));

Arrays.asList(msgType, content, title, markdown).forEach(item -> {
httpParams.getBody().add(item);
});
httpParams.setBody(" {\n" + " \"msgtype\": \"markdown\",\n"
+ " \"markdown\": {\n"
+ " \"title\": \"http 测试\",\n"
+ " \"text\": \"\"\n"
+ " }\n"
+ "}");

httpConfig = JsonUtils.toMap(JSONUtil.toJsonStr(httpParams), String.class, Object.class);
}
Expand Down
9 changes: 9 additions & 0 deletions dinky-assembly/src/main/assembly/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@
<include>dinky-alert-email-${project.version}.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.parent.basedir}/dinky-alert/dinky-alert-http/target
</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>dinky-alert-http-${project.version}.jar</include>
</includes>
</fileSet>

<fileSet>
<directory>${project.parent.basedir}/dinky-alert/dinky-alert-feishu/target
</directory>
Expand Down
5 changes: 5 additions & 0 deletions dinky-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<artifactId>dinky-alert-email</artifactId>
<scope>${scope.runtime}</scope>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-alert-http</artifactId>
<scope>${scope.runtime}</scope>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-metadata-mysql</artifactId>
Expand Down
6 changes: 5 additions & 1 deletion dinky-web/src/components/Flink/FlinkDag/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ const FlinkDag = (props: DagProps) => {
{
key: '1',
label: 'Detail',
children: <div style={{whiteSpace:"pre"}}>{(currentSelect?.getData().description)?.replaceAll("<br/>","\n")}</div>
children: (
<div style={{ whiteSpace: 'pre' }}>
{currentSelect?.getData().description?.replaceAll('<br/>', '\n')}
</div>
)
},
{
key: '2',
Expand Down
6 changes: 6 additions & 0 deletions dinky-web/src/locales/en-US/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,12 @@ export default {
'rc.ai.http.methodPleaseHolder': 'Please select the request method',
'rc.ai.http.headers': 'Request headers',
'rc.ai.http.body': 'Request body',
'rc.ai.http.contentFiled': 'Content fields',
'rc.ai.http.contentFiled.help':
"In HTTP requests, the fields in the request body are replaced with 'Alarm Message', and if there are multiple layers of nested fields, use a JSON path expression, such as text.markdown.content",
'rc.ai.http.titleFiled': 'Title field',
'rc.ai.http.titleFiled.help':
"In HTTP requests, the field in the request body is replaced with 'header content', if there are multiple layers of nested fields, please use a JSON path expression, such as markdown.title, if not, the title is concatenated in the content field by default",
'rc.alert.template.create': 'Create Template',
'rc.alert.template.modify': 'Modify Template',
'rc.alert.template.new': 'Create an alert template',
Expand Down
6 changes: 6 additions & 0 deletions dinky-web/src/locales/zh-CN/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,12 @@ export default {
'rc.ai.http.methodPleaseHolder': '请选择请求方法',
'rc.ai.http.headers': '请求头',
'rc.ai.http.body': '请求体',
'rc.ai.http.contentFiled': '内容字段',
'rc.ai.http.contentFiled.help':
'在http请求中,请求体(body)内被替换为`告警消息`的字段,如果涉及多层嵌套字段,请使用json path表达式,例如 text.markdown.content',
'rc.ai.http.titleFiled': '标题字段',
'rc.ai.http.titleFiled.help':
'在http请求中,请求体(body)内被替换为`标题内容`的字段,如果涉及多层嵌套字段,请使用json path表达式,例如 markdown.title,如果不填写,则标题默认拼接在内容字段中',
'rc.alert.template.create': '创建模板',
'rc.alert.template.modify': '修改模板',
'rc.alert.template.new': '新建告警模板',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import { TabsPageType, TaskDataType } from '@/pages/DataStudio/model';
import { JOB_LIFE_CYCLE, JOB_STATUS } from '@/pages/DevOps/constants';
import { DIALECT } from '@/services/constants';
import { EnvironmentOutlined } from '@ant-design/icons';

/**
* @description: 生成面包屑
Expand Down
Loading

0 comments on commit cd51137

Please sign in to comment.