From 3dc4bdc4dd0d5bfa483f2f57a2d1e872f22f0eb5 Mon Sep 17 00:00:00 2001 From: "K.O" Date: Tue, 12 Jan 2021 00:18:21 -0600 Subject: [PATCH] [DS-4396][feat] Add project information to email alert message (#4413) * [DS-4396][feat] Add project information to email alert message - build find project and user sql to ProjectMapper - add project information to alert db. * [DS-4396][style] change style --- .../dao/entity/ProcessAlertContent.java | 28 ++++++++++++++++++- .../dao/mapper/ProjectMapper.java | 15 ++++++++-- .../dao/mapper/ProjectMapper.xml | 12 ++++++++ .../master/runner/MasterExecThread.java | 4 ++- .../server/utils/AlertManager.java | 17 ++++++++--- .../server/master/AlertManagerTest.java | 10 +++++-- .../service/process/ProcessService.java | 10 +++++++ 7 files changed, 86 insertions(+), 10 deletions(-) diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/ProcessAlertContent.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/ProcessAlertContent.java index 71058f4af8..4f46140c21 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/ProcessAlertContent.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/ProcessAlertContent.java @@ -33,6 +33,12 @@ import com.fasterxml.jackson.annotation.JsonProperty; @JsonInclude(Include.NON_NULL) public class ProcessAlertContent implements Serializable { + @JsonProperty("projectId") + private int projectId; + @JsonProperty("projectName") + private String projectName; + @JsonProperty("owner") + private String owner; @JsonProperty("processId") private int processId; @JsonProperty("processName") @@ -79,6 +85,9 @@ public class ProcessAlertContent implements Serializable { private String logPath; private ProcessAlertContent(Builder builder) { + this.projectId = builder.projectId; + this.projectName = builder.projectName; + this.owner = builder.owner; this.processId = builder.processId; this.processName = builder.processName; this.processType = builder.processType; @@ -107,7 +116,9 @@ public class ProcessAlertContent implements Serializable { } public static class Builder { - + private int projectId; + private String projectName; + private String owner; private int processId; private String processName; private CommandType processType; @@ -129,6 +140,21 @@ public class ProcessAlertContent implements Serializable { private String taskHost; private String logPath; + public Builder projectId(int projectId) { + this.projectId = projectId; + return this; + } + + public Builder projectName(String projectName) { + this.projectName = projectName; + return this; + } + + public Builder owner(String owner) { + this.owner = owner; + return this; + } + public Builder processId(int processId) { this.processId = processId; return this; diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java index b24fd2ede4..872d53fd92 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java @@ -14,15 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.dolphinscheduler.dao.mapper; import org.apache.dolphinscheduler.dao.entity.Project; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.baomidou.mybatisplus.core.metadata.IPage; +import org.apache.dolphinscheduler.dao.entity.ProjectUser; + import org.apache.ibatis.annotations.Param; import java.util.List; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; + /** * project mapper interface */ @@ -81,4 +85,11 @@ public interface ProjectMapper extends BaseMapper { */ List queryProjectCreatedAndAuthorizedByUserId(@Param("userId") int userId); + /** + * query project name and user name by processInstanceId. + * @param processInstanceId processInstanceId + * @return projectName and userName + */ + ProjectUser queryProjectWithUserByProcessInstanceId(@Param("processInstanceId") int processInstanceId); + } diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml index 64c5b6cfc6..0ec03846d0 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml @@ -96,4 +96,16 @@ (select project_id from t_ds_relation_project_user where user_id=#{userId} union select id as project_id from t_ds_project where user_id=#{userId}) + + diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java index f5946d5614..3b113b6536 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/runner/MasterExecThread.java @@ -47,6 +47,7 @@ import org.apache.dolphinscheduler.common.utils.ParameterUtils; import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.common.utils.VarPoolUtils; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; +import org.apache.dolphinscheduler.dao.entity.ProjectUser; import org.apache.dolphinscheduler.dao.entity.Schedule; import org.apache.dolphinscheduler.dao.entity.TaskInstance; import org.apache.dolphinscheduler.dao.utils.DagHelper; @@ -371,7 +372,8 @@ public class MasterExecThread implements Runnable { processService.createRecoveryWaitingThreadCommand(null, processInstance); } List taskInstances = processService.findValidTaskListByProcessId(processInstance.getId()); - alertManager.sendAlertProcessInstance(processInstance, taskInstances); + ProjectUser projectUser = processService.queryProjectWithUserByProcessInstanceId(processInstance.getId()); + alertManager.sendAlertProcessInstance(processInstance, taskInstances, projectUser); } /** diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/AlertManager.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/AlertManager.java index 6b8d926c0f..6cfbc5b650 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/AlertManager.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/AlertManager.java @@ -29,6 +29,7 @@ import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.ProcessAlertContent; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; +import org.apache.dolphinscheduler.dao.entity.ProjectUser; import org.apache.dolphinscheduler.dao.entity.TaskInstance; import java.util.ArrayList; @@ -94,12 +95,16 @@ public class AlertManager { * @return process instance format content */ public String getContentProcessInstance(ProcessInstance processInstance, - List taskInstances) { + List taskInstances, + ProjectUser projectUser) { String res = ""; if (processInstance.getState().typeIsSuccess()) { List successTaskList = new ArrayList<>(1); ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder() + .projectId(projectUser.getProjectId()) + .projectName(projectUser.getProjectName()) + .owner(projectUser.getUserName()) .processId(processInstance.getId()) .processName(processInstance.getName()) .processType(processInstance.getCommandType()) @@ -120,6 +125,9 @@ public class AlertManager { continue; } ProcessAlertContent processAlertContent = ProcessAlertContent.newBuilder() + .projectId(projectUser.getProjectId()) + .projectName(projectUser.getProjectName()) + .owner(projectUser.getUserName()) .processId(processInstance.getId()) .processName(processInstance.getName()) .taskId(task.getId()) @@ -196,9 +204,10 @@ public class AlertManager { * @param taskInstances task instance list */ public void sendAlertProcessInstance(ProcessInstance processInstance, - List taskInstances) { + List taskInstances, + ProjectUser projectUser) { - if(Flag.YES == processInstance.getIsSubProcess()){ + if (Flag.YES == processInstance.getIsSubProcess()) { return; } boolean sendWarnning = false; @@ -231,7 +240,7 @@ public class AlertManager { alert.setTitle(cmdName + " " + success); ShowType showType = processInstance.getState().typeIsSuccess() ? ShowType.TEXT : ShowType.TABLE; alert.setShowType(showType); - String content = getContentProcessInstance(processInstance, taskInstances); + String content = getContentProcessInstance(processInstance, taskInstances, projectUser); alert.setContent(content); alert.setAlertType(AlertType.EMAIL); alert.setAlertGroupId(processInstance.getWarningGroupId()); diff --git a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/AlertManagerTest.java b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/AlertManagerTest.java index 598b3306c3..58fea5e5f5 100644 --- a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/AlertManagerTest.java +++ b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/AlertManagerTest.java @@ -19,9 +19,11 @@ package org.apache.dolphinscheduler.server.master; import org.apache.dolphinscheduler.common.enums.ExecutionStatus; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; +import org.apache.dolphinscheduler.dao.entity.ProjectUser; import org.apache.dolphinscheduler.dao.entity.TaskInstance; import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper; +import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.TaskInstanceMapper; import org.apache.dolphinscheduler.server.utils.AlertManager; import org.junit.Ignore; @@ -51,6 +53,9 @@ public class AlertManagerTest { @Autowired TaskInstanceMapper taskInstanceMapper; + @Autowired + ProjectMapper projectMapper; + AlertManager alertManager; /** @@ -90,7 +95,6 @@ public class AlertManagerTest { ProcessDefinition processDefinition = processDefinitionMapper.selectById(47); processInstance.setProcessDefinition(processDefinition); - // fault task instance TaskInstance toleranceTask1 = taskInstanceMapper.selectById(5038); toleranceTask1.setState(ExecutionStatus.FAILURE); @@ -101,7 +105,9 @@ public class AlertManagerTest { toleranceTaskList.add(toleranceTask1); toleranceTaskList.add(toleranceTask2); - alertManager.sendAlertProcessInstance(processInstance, toleranceTaskList); + ProjectUser projectUser = projectMapper.queryProjectWithUserByProcessInstanceId(processInstance.getId()); + + alertManager.sendAlertProcessInstance(processInstance, toleranceTaskList, projectUser); } } diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java index d77eb79ba3..aa70ed225b 100644 --- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java @@ -56,6 +56,7 @@ import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.ProcessInstanceMap; import org.apache.dolphinscheduler.dao.entity.Project; +import org.apache.dolphinscheduler.dao.entity.ProjectUser; import org.apache.dolphinscheduler.dao.entity.Resource; import org.apache.dolphinscheduler.dao.entity.Schedule; import org.apache.dolphinscheduler.dao.entity.TaskInstance; @@ -1859,6 +1860,15 @@ public class ProcessService { return queue; } + /** + * query project name and user name by processInstanceId. + * @param processInstanceId processInstanceId + * @return projectName and userName + */ + public ProjectUser queryProjectWithUserByProcessInstanceId(int processInstanceId) { + return projectMapper.queryProjectWithUserByProcessInstanceId(processInstanceId); + } + /** * get task worker group *