Browse Source

[Improvement][alert-spi]plugin instance only saves the main information

plugin instance only saves the main information

when users need to display all the complete information (usually UI display), then do the conversion.
pull/3/MERGE
CalvinKirs 4 years ago
parent
commit
243ad386d0
  1. 7
      dolphinscheduler-alert-plugin/dolphinscheduler-alert-dingtalk/src/main/java/org/apache/dolphinscheduler/plugin/alert/dingtalk/DingTalkAlertChannel.java
  2. 6
      dolphinscheduler-alert-plugin/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/EmailAlertChannel.java
  3. 6
      dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannel.java
  4. 7
      dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
  5. 12
      dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java
  6. 6
      dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannel.java
  7. 62
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java
  8. 8
      dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/alert/AlertResult.java
  9. 4
      dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/params/base/PluginParams.java

7
dolphinscheduler-alert-plugin/dolphinscheduler-alert-dingtalk/src/main/java/org/apache/dolphinscheduler/plugin/alert/dingtalk/DingTalkAlertChannel.java

@ -21,7 +21,7 @@ import org.apache.dolphinscheduler.spi.alert.AlertChannel;
import org.apache.dolphinscheduler.spi.alert.AlertData; import org.apache.dolphinscheduler.spi.alert.AlertData;
import org.apache.dolphinscheduler.spi.alert.AlertInfo; import org.apache.dolphinscheduler.spi.alert.AlertInfo;
import org.apache.dolphinscheduler.spi.alert.AlertResult; import org.apache.dolphinscheduler.spi.alert.AlertResult;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import java.util.Map; import java.util.Map;
@ -35,7 +35,10 @@ public class DingTalkAlertChannel implements AlertChannel {
AlertData alertData = alertInfo.getAlertData(); AlertData alertData = alertInfo.getAlertData();
String alertParams = alertInfo.getAlertParams(); String alertParams = alertInfo.getAlertParams();
Map<String, String> paramsMap = PluginParamsTransfer.getPluginParamsMap(alertParams); Map paramsMap = JSONUtils.parseObject(alertParams,Map.class);
if(null==paramsMap){
return new AlertResult("false","ding talk params is null");
}
return new DingTalkSender(paramsMap).sendDingTalkMsg(alertData.getTitle(), alertData.getContent()); return new DingTalkSender(paramsMap).sendDingTalkMsg(alertData.getTitle(), alertData.getContent());
} }
} }

6
dolphinscheduler-alert-plugin/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/EmailAlertChannel.java

@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.spi.alert.AlertData;
import org.apache.dolphinscheduler.spi.alert.AlertInfo; import org.apache.dolphinscheduler.spi.alert.AlertInfo;
import org.apache.dolphinscheduler.spi.alert.AlertResult; import org.apache.dolphinscheduler.spi.alert.AlertResult;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import java.util.Map; import java.util.Map;
@ -39,7 +40,10 @@ public class EmailAlertChannel implements AlertChannel {
AlertData alert = info.getAlertData(); AlertData alert = info.getAlertData();
String alertParams = info.getAlertParams(); String alertParams = info.getAlertParams();
Map<String, String> paramsMap = PluginParamsTransfer.getPluginParamsMap(alertParams); Map paramsMap = JSONUtils.parseObject(alertParams,Map.class);
if(null==paramsMap){
return new AlertResult("false","mail params is null");
}
MailSender mailSender = new MailSender(paramsMap); MailSender mailSender = new MailSender(paramsMap);
AlertResult alertResult = mailSender.sendMails(alert.getTitle(), alert.getContent()); AlertResult alertResult = mailSender.sendMails(alert.getTitle(), alert.getContent());

6
dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannel.java

@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.spi.alert.AlertData;
import org.apache.dolphinscheduler.spi.alert.AlertInfo; import org.apache.dolphinscheduler.spi.alert.AlertInfo;
import org.apache.dolphinscheduler.spi.alert.AlertResult; import org.apache.dolphinscheduler.spi.alert.AlertResult;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import java.util.Map; import java.util.Map;
@ -34,7 +35,10 @@ public class HttpAlertChannel implements AlertChannel {
AlertData alertData = alertInfo.getAlertData(); AlertData alertData = alertInfo.getAlertData();
String alertParams = alertInfo.getAlertParams(); String alertParams = alertInfo.getAlertParams();
Map<String, String> paramsMap = PluginParamsTransfer.getPluginParamsMap(alertParams); Map paramsMap = JSONUtils.parseObject(alertParams,Map.class);
if(null==paramsMap){
return new AlertResult("false","http params is null");
}
return new HttpSender(paramsMap).send(alertData.getContent()); return new HttpSender(paramsMap).send(alertData.getContent());
} }

7
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java

@ -21,7 +21,7 @@ import org.apache.dolphinscheduler.spi.alert.AlertChannel;
import org.apache.dolphinscheduler.spi.alert.AlertData; import org.apache.dolphinscheduler.spi.alert.AlertData;
import org.apache.dolphinscheduler.spi.alert.AlertInfo; import org.apache.dolphinscheduler.spi.alert.AlertInfo;
import org.apache.dolphinscheduler.spi.alert.AlertResult; import org.apache.dolphinscheduler.spi.alert.AlertResult;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import java.util.Map; import java.util.Map;
@ -34,7 +34,10 @@ public class ScriptAlertChannel implements AlertChannel {
public AlertResult process(AlertInfo alertinfo) { public AlertResult process(AlertInfo alertinfo) {
AlertData alertData = alertinfo.getAlertData(); AlertData alertData = alertinfo.getAlertData();
String alertParams = alertinfo.getAlertParams(); String alertParams = alertinfo.getAlertParams();
Map<String, String> paramsMap = PluginParamsTransfer.getPluginParamsMap(alertParams); Map paramsMap = JSONUtils.parseObject(alertParams,Map.class);
if(null==paramsMap){
return new AlertResult("false","ding talk params is null");
}
return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle()); return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle());
} }
} }

12
dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java

@ -18,9 +18,11 @@
package org.apache.dolphinscheduler.plugin.alert.script; package org.apache.dolphinscheduler.plugin.alert.script;
import org.apache.dolphinscheduler.spi.alert.AlertChannel; import org.apache.dolphinscheduler.spi.alert.AlertChannel;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
import org.apache.dolphinscheduler.spi.params.base.PluginParams; import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.utils.JSONUtils; import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import java.util.HashMap;
import java.util.List; import java.util.List;
import org.junit.Assert; import org.junit.Assert;
@ -35,8 +37,16 @@ public class ScriptAlertChannelFactoryTest {
public void testGetParams() { public void testGetParams() {
ScriptAlertChannelFactory scriptAlertChannelFactory = new ScriptAlertChannelFactory(); ScriptAlertChannelFactory scriptAlertChannelFactory = new ScriptAlertChannelFactory();
List<PluginParams> params = scriptAlertChannelFactory.getParams(); List<PluginParams> params = scriptAlertChannelFactory.getParams();
JSONUtils.toJsonString(params);
String pluginParamsMapString= JSONUtils.toJsonString(PluginParamsTransfer.getPluginParamsMap(JSONUtils.toJsonString(params)));
HashMap paramsMap= JSONUtils.parseObject(pluginParamsMapString,HashMap.class);
System.out.println(paramsMap.get("path"));
Assert.assertEquals(3, params.size()); Assert.assertEquals(3, params.size());
List<PluginParams> paramss= JSONUtils.toList(JSONUtils.toJsonString(params),PluginParams.class);
System.out.println(PluginParamsTransfer.getPluginParamsMap(JSONUtils.toJsonString(params)));
System.out.println(paramss.get(0).getName());
System.out.println(paramss.get(0).getName());
} }
@Test @Test

6
dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannel.java

@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.spi.alert.AlertData;
import org.apache.dolphinscheduler.spi.alert.AlertInfo; import org.apache.dolphinscheduler.spi.alert.AlertInfo;
import org.apache.dolphinscheduler.spi.alert.AlertResult; import org.apache.dolphinscheduler.spi.alert.AlertResult;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import java.util.Map; import java.util.Map;
@ -34,7 +35,10 @@ public class WeChatAlertChannel implements AlertChannel {
public AlertResult process(AlertInfo info) { public AlertResult process(AlertInfo info) {
AlertData alertData = info.getAlertData(); AlertData alertData = info.getAlertData();
String alertParams = info.getAlertParams(); String alertParams = info.getAlertParams();
Map<String, String> paramsMap = PluginParamsTransfer.getPluginParamsMap(alertParams); Map paramsMap = JSONUtils.parseObject(alertParams,Map.class);
if(null==paramsMap){
return new AlertResult("false","we chat params is null");
}
return new WeChatSender(paramsMap).sendEnterpriseWeChat(alertData.getTitle(), alertData.getContent()); return new WeChatSender(paramsMap).sendEnterpriseWeChat(alertData.getTitle(), alertData.getContent());
} }

62
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java

@ -24,16 +24,20 @@ import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.vo.AlertPluginInstanceVO; import org.apache.dolphinscheduler.api.vo.AlertPluginInstanceVO;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance; import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance;
import org.apache.dolphinscheduler.dao.entity.PluginDefine; import org.apache.dolphinscheduler.dao.entity.PluginDefine;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.AlertPluginInstanceMapper; import org.apache.dolphinscheduler.dao.mapper.AlertPluginInstanceMapper;
import org.apache.dolphinscheduler.dao.mapper.PluginDefineMapper; import org.apache.dolphinscheduler.dao.mapper.PluginDefineMapper;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -67,7 +71,8 @@ public class AlertPluginInstanceServiceImpl extends BaseService implements Alert
@Override @Override
public Map<String, Object> create(User loginUser, int pluginDefineId, String instanceName, String pluginInstanceParams) { public Map<String, Object> create(User loginUser, int pluginDefineId, String instanceName, String pluginInstanceParams) {
AlertPluginInstance alertPluginInstance = new AlertPluginInstance(); AlertPluginInstance alertPluginInstance = new AlertPluginInstance();
alertPluginInstance.setPluginInstanceParams(pluginInstanceParams); String paramsMapJson = parsePluginParamsMap(pluginInstanceParams);
alertPluginInstance.setPluginInstanceParams(paramsMapJson);
alertPluginInstance.setInstanceName(instanceName); alertPluginInstance.setInstanceName(instanceName);
alertPluginInstance.setPluginDefineId(pluginDefineId); alertPluginInstance.setPluginDefineId(pluginDefineId);
@ -82,7 +87,9 @@ public class AlertPluginInstanceServiceImpl extends BaseService implements Alert
if (i > 0) { if (i > 0) {
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result;
} }
putMsg(result, Status.SAVE_ERROR);
return result; return result;
} }
@ -98,7 +105,8 @@ public class AlertPluginInstanceServiceImpl extends BaseService implements Alert
public Map<String, Object> update(User loginUser, int pluginInstanceId, String instanceName, String pluginInstanceParams) { public Map<String, Object> update(User loginUser, int pluginInstanceId, String instanceName, String pluginInstanceParams) {
AlertPluginInstance alertPluginInstance = new AlertPluginInstance(); AlertPluginInstance alertPluginInstance = new AlertPluginInstance();
alertPluginInstance.setPluginInstanceParams(pluginInstanceParams); String paramsMapJson = parsePluginParamsMap(pluginInstanceParams);
alertPluginInstance.setPluginInstanceParams(paramsMapJson);
alertPluginInstance.setInstanceName(instanceName); alertPluginInstance.setInstanceName(instanceName);
alertPluginInstance.setId(pluginInstanceId); alertPluginInstance.setId(pluginInstanceId);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
@ -106,8 +114,9 @@ public class AlertPluginInstanceServiceImpl extends BaseService implements Alert
if (i > 0) { if (i > 0) {
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result;
} }
putMsg(result, Status.SAVE_ERROR);
return result; return result;
} }
@ -188,21 +197,62 @@ public class AlertPluginInstanceServiceImpl extends BaseService implements Alert
if (CollectionUtils.isEmpty(pluginDefineList)) { if (CollectionUtils.isEmpty(pluginDefineList)) {
return null; return null;
} }
Map<Integer, String> pluginDefineMap = pluginDefineList.stream().collect(Collectors.toMap(PluginDefine::getId, PluginDefine::getPluginName)); Map<Integer, PluginDefine> pluginDefineMap = pluginDefineList.stream().collect(Collectors.toMap(PluginDefine::getId, Function.identity()));
List<AlertPluginInstanceVO> alertPluginInstanceVOS = new ArrayList<>(); List<AlertPluginInstanceVO> alertPluginInstanceVOS = new ArrayList<>();
alertPluginInstances.forEach(alertPluginInstance -> { alertPluginInstances.forEach(alertPluginInstance -> {
AlertPluginInstanceVO alertPluginInstanceVO = new AlertPluginInstanceVO(); AlertPluginInstanceVO alertPluginInstanceVO = new AlertPluginInstanceVO();
alertPluginInstanceVO.setAlertPluginName(pluginDefineMap.get(alertPluginInstance.getPluginDefineId()));
alertPluginInstanceVO.setCreateTime(alertPluginInstance.getCreateTime()); alertPluginInstanceVO.setCreateTime(alertPluginInstance.getCreateTime());
alertPluginInstanceVO.setUpdateTime(alertPluginInstance.getUpdateTime()); alertPluginInstanceVO.setUpdateTime(alertPluginInstance.getUpdateTime());
alertPluginInstanceVO.setPluginDefineId(alertPluginInstance.getPluginDefineId()); alertPluginInstanceVO.setPluginDefineId(alertPluginInstance.getPluginDefineId());
alertPluginInstanceVO.setInstanceName(alertPluginInstance.getInstanceName()); alertPluginInstanceVO.setInstanceName(alertPluginInstance.getInstanceName());
alertPluginInstanceVO.setId(alertPluginInstance.getId()); alertPluginInstanceVO.setId(alertPluginInstance.getId());
PluginDefine pluginDefine = pluginDefineMap.get(alertPluginInstance.getPluginDefineId());
//FIXME When the user removes the plug-in, this will happen. At this time, maybe we should add a new field to indicate that the plug-in has expired?
if (null == pluginDefine) {
return;
}
alertPluginInstanceVO.setAlertPluginName(pluginDefine.getPluginName());
//todo List pages do not recommend returning this parameter //todo List pages do not recommend returning this parameter
alertPluginInstanceVO.setPluginInstanceParams(alertPluginInstance.getPluginInstanceParams()); String pluginParamsMapString = alertPluginInstance.getPluginInstanceParams();
String uiPluginParams=parseToPluginUiParams(pluginParamsMapString,pluginDefine.getPluginParams());
alertPluginInstanceVO.setPluginInstanceParams(uiPluginParams);
alertPluginInstanceVOS.add(alertPluginInstanceVO); alertPluginInstanceVOS.add(alertPluginInstanceVO);
}); });
return alertPluginInstanceVOS; return alertPluginInstanceVOS;
} }
/**
* Get the parameters actually needed by the plugin
*
* @param pluginParams Complete parameters(include ui)
* @return k, v(json string)
*/
private String parsePluginParamsMap(String pluginParams) {
Map<String, String> paramsMap = PluginParamsTransfer.getPluginParamsMap(pluginParams);
return JSONUtils.toJsonString(paramsMap);
}
/**
* parseToPluginUiParams
*
* @param pluginParamsMapString k-v data
* @param pluginUiParams Complete parameters(include ui)
* @return Complete parameters list(include ui)
*/
private String parseToPluginUiParams(String pluginParamsMapString, String pluginUiParams) {
//todo npe
HashMap paramsMap = JSONUtils.parseObject(pluginParamsMapString, HashMap.class);
assert paramsMap != null;
List<PluginParams> pluginParamsList = JSONUtils.toList(pluginUiParams, PluginParams.class);
List<PluginParams> newPluginParamsList = new ArrayList<>(pluginParamsList.size());
pluginParamsList.forEach(pluginParams -> {
pluginParams.setValue(paramsMap.get(pluginParams.getName()));
newPluginParamsList.add(pluginParams);
});
return JSONUtils.toJsonString(newPluginParamsList);
}
} }

8
dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/alert/AlertResult.java

@ -38,4 +38,12 @@ public class AlertResult {
public void setMessage(String message) { public void setMessage(String message) {
this.message = message; this.message = message;
} }
public AlertResult(String status, String message) {
this.status = status;
this.message = message;
}
public AlertResult() {
}
} }

4
dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/params/base/PluginParams.java

@ -165,6 +165,10 @@ public class PluginParams {
public List<Validate> getValidateList() { public List<Validate> getValidateList() {
return validateList; return validateList;
} }
public void setValue(Object value) {
this.value = value;
}
} }

Loading…
Cancel
Save