Commit 3ff5d3f3 authored by wenmo's avatar wenmo

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

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