Unverified Commit 2f47610f authored by aiwenmo's avatar aiwenmo Committed by GitHub

[Fix-347] [alert] Fix to the latest configuration take effect during …

[Fix-347] [alert] Fix to the latest configuration take effect during …
parents 574c7403 3ff5d3f3
......@@ -7,7 +7,9 @@ import com.dlink.common.result.Result;
import com.dlink.model.AlertInstance;
import com.dlink.service.AlertInstanceService;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
......@@ -93,14 +95,11 @@ public class AlertInstanceController {
*/
@PostMapping("/sendTest")
public Result sendTest(@RequestBody AlertInstance alertInstance) throws Exception {
AlertResult alertResult = alertInstanceService.getAlerTesttResult(alertInstance);
AlertResult alertResult = alertInstanceService.testAlert(alertInstance);
if (alertResult.getSuccess()) {
return Result.succeed("发送成功");
}else {
} else {
return Result.failed("发送失败");
}
}
}
......@@ -16,5 +16,5 @@ public interface AlertInstanceService extends ISuperService<AlertInstance> {
List<AlertInstance> listEnabledAll();
AlertResult getAlerTesttResult(AlertInstance alertInstance);
AlertResult testAlert(AlertInstance alertInstance);
}
......@@ -7,6 +7,7 @@ import com.dlink.mapper.AlertInstanceMapper;
import com.dlink.model.AlertInstance;
import com.dlink.service.AlertInstanceService;
import com.dlink.utils.JSONUtil;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
......@@ -28,27 +29,19 @@ public class AlertInstanceServiceImpl extends SuperServiceImpl<AlertInstanceMapp
}
@Override
public AlertResult getAlerTesttResult(AlertInstance alertInstance) {
AlertConfig alertConfig =null;
Alert alert = null;
if (!AlertPool.exist(alertInstance.getName())) {
alertConfig = AlertConfig.build(alertInstance.getName(), alertInstance.getType(), JSONUtil.toMap(alertInstance.getParams()));
alert = Alert.build(alertConfig);
AlertPool.push(alertInstance.getName(), alert);
}else {
alert = AlertPool.get(alertInstance.getName());
}
public AlertResult testAlert(AlertInstance alertInstance) {
AlertConfig alertConfig = AlertConfig.build(alertInstance.getName(), alertInstance.getType(), JSONUtil.toMap(alertInstance.getParams()));
Alert alert = Alert.buildTest(alertConfig);
String currentDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
String testSendMsg = "[{\"type\":\"Flink 实时监控\"," +
"\"time\":\""+currentDateTime+"\"," +
"\"id\":\""+ UUID.randomUUID() +"\"," +
"\"name\":\"此信息仅用于测试告警信息是否发送正常 ! 请忽略此信息!\"," +
"\"status\":\"Test\"," +
"\"content\" :\""+ UUID.randomUUID() +"\"}]";
"\"time\":\"" + currentDateTime + "\"," +
"\"id\":\"" + UUID.randomUUID() + "\"," +
"\"name\":\"此信息仅用于测试告警信息是否发送正常 ! 请忽略此信息!\"," +
"\"status\":\"Test\"," +
"\"content\" :\"" + UUID.randomUUID() + "\"}]";
List<AlertMsg> lists = JSONUtil.toList(testSendMsg, AlertMsg.class);
String title = "任务【测试任务】:" + alertInstance.getType() + " 报警 !";
String content = JSONUtil.toJsonString(lists);
AlertResult alertResult = alert.send(title, content);
return alertResult;
return alert.send(title, content);
}
}
package com.dlink.alert;
import com.dlink.assertion.Asserts;
import sun.misc.Service;
import java.util.Iterator;
......@@ -40,6 +41,14 @@ public interface Alert {
return driver;
}
static Alert buildTest(AlertConfig config) {
Optional<Alert> optionalDriver = Alert.get(config);
if (!optionalDriver.isPresent()) {
throw new AlertException("不支持报警组件类型【" + config.getType() + "】,请在 lib 下添加扩展依赖");
}
return optionalDriver.get();
}
Alert setConfig(AlertConfig config);
default boolean canHandle(String type) {
......
......@@ -42,7 +42,7 @@ const DingTalkForm: React.FC<AlertInstanceFormProps> = (props) => {
const sendTestForm = async () => {
const fieldsValue = await form.validateFields();
setFormVals(buildJSONData(formVals,fieldsValue));
handleTest({...formVals, ...fieldsValue});
handleTest(buildJSONData(formVals,fieldsValue));
};
const submitForm = async () => {
......@@ -143,7 +143,7 @@ const DingTalkForm: React.FC<AlertInstanceFormProps> = (props) => {
return (
<>
<Button onClick={() => handleModalVisible(false)}>取消</Button>
<Button type="primary" danger htmlType="button" onClick={sendTestForm}>测试</Button>
<Button type="primary" onClick={() => sendTestForm()}>测试</Button>
<Button type="primary" onClick={() => submitForm()}>
完成
</Button>
......
......@@ -41,8 +41,8 @@ const FeiShuForm: React.FC<AlertInstanceFormProps> = (props) => {
const sendTestForm = async () => {
const fieldsValue = await form.validateFields();
setFormVals({...formVals, ...fieldsValue});
handleTest({...formVals, ...fieldsValue});
setFormVals(buildJSONData(formVals,fieldsValue));
handleTest(buildJSONData(formVals,fieldsValue));
};
const submitForm = async () => {
......@@ -181,7 +181,7 @@ const FeiShuForm: React.FC<AlertInstanceFormProps> = (props) => {
return (
<>
<Button onClick={() => handleModalVisible(false)}>取消</Button>
<Button type="primary" danger htmlType="button" onClick={sendTestForm}>测试</Button>
<Button type="primary" onClick={() => sendTestForm()}>测试</Button>
<Button type="primary" onClick={() => submitForm()}>
完成
</Button>
......
......@@ -47,8 +47,8 @@ const FeiShuForm: React.FC<AlertInstanceFormProps> = (props) => {
const sendTestForm = async () => {
const fieldsValue = await form.getFieldsValue();
setFormVals({...formVals, ...fieldsValue});
handleTest({...formVals, ...fieldsValue});
setFormVals(buildJSONData(formVals,fieldsValue));
handleTest(buildJSONData(formVals,fieldsValue));
};
const renderContent = (vals) => {
......@@ -152,7 +152,7 @@ const FeiShuForm: React.FC<AlertInstanceFormProps> = (props) => {
return (
<>
<Button onClick={() => handleModalVisible(false)}>取消</Button>
<Button type="primary" danger htmlType="button" onClick={sendTestForm}>测试</Button>
<Button type="primary" onClick={() => sendTestForm()}>测试</Button>
<Button type="primary" onClick={() => submitForm()}>
完成
</Button>
......
......@@ -40,8 +40,8 @@ const WeChatForm: React.FC<AlertInstanceFormProps> = (props) => {
};
const sendTestForm = async () => {
const fieldsValue = await form.validateFields();
setFormVals({...formVals, ...fieldsValue});
handleTest({...formVals, ...fieldsValue});
setFormVals(buildJSONData(formVals,fieldsValue));
handleTest(buildJSONData(formVals,fieldsValue));
};
const submitForm = async () => {
......@@ -185,7 +185,7 @@ const WeChatForm: React.FC<AlertInstanceFormProps> = (props) => {
return (
<>
<Button onClick={() => handleModalVisible(false)}>取消</Button>
<Button type="primary" danger htmlType="button" onClick={sendTestForm}>测试</Button>
<Button type="primary" onClick={() => sendTestForm()}>测试</Button>
<Button type="primary" onClick={() => submitForm()}>
完成
</Button>
......
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