Parcourir la source

[Improvement][Api Module]split alert group list-paging interface (#5941)

* [Improvement][Api Module]split alert group list-paging interface
Shukun Zhang il y a 3 ans
Parent
commit
75f15df361

+ 23 - 0
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java

@@ -20,6 +20,7 @@ package org.apache.dolphinscheduler.api.controller;
 import static org.apache.dolphinscheduler.api.enums.Status.CREATE_ALERT_GROUP_ERROR;
 import static org.apache.dolphinscheduler.api.enums.Status.DELETE_ALERT_GROUP_ERROR;
 import static org.apache.dolphinscheduler.api.enums.Status.LIST_PAGING_ALERT_GROUP_ERROR;
+import static org.apache.dolphinscheduler.api.enums.Status.QUERY_ALERT_GROUP_ERROR;
 import static org.apache.dolphinscheduler.api.enums.Status.QUERY_ALL_ALERTGROUP_ERROR;
 import static org.apache.dolphinscheduler.api.enums.Status.UPDATE_ALERT_GROUP_ERROR;
 
@@ -139,6 +140,28 @@ public class AlertGroupController extends BaseController {
         searchVal = ParameterUtils.handleEscapes(searchVal);
         return alertGroupService.listPaging(loginUser, searchVal, pageNo, pageSize);
     }
+    /**
+     * check alarm group detail by Id
+     *
+     * @param loginUser login user
+     * @param id alert group id
+     * @return one alert group
+     */
+
+    @ApiOperation(value = "queryAlertGroupById", notes = "QUERY_ALERT_GROUP_BY_ID_NOTES")
+    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "ALERT_GROUP_ID", dataType = "Int", example = "1")
+    })
+    @PostMapping(value = "/query")
+    @ResponseStatus(HttpStatus.OK)
+    @ApiException(QUERY_ALERT_GROUP_ERROR)
+    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
+    public Result queryAlertGroupById(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
+                                      @RequestParam("id") Integer id) {
+
+        Map<String, Object> result = alertGroupService.queryAlertGroupById(loginUser, id);
+        return returnDataList(result);
+    }
+
 
     /**
      * updateProcessInstance alert group

+ 1 - 0
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java

@@ -211,6 +211,7 @@ public enum Status {
     WORKER_ADDRESS_INVALID(10177, "worker address {0} invalid", "worker地址[{0}]无效"),
     QUERY_WORKER_ADDRESS_LIST_FAIL(10178, "query worker address list fail ", "查询worker地址列表失败"),
     TRANSFORM_PROJECT_OWNERSHIP(10179, "Please transform project ownership [{0}]", "请先转移项目所有权[{0}]"),
+    QUERY_ALERT_GROUP_ERROR(10180, "query alert group error", "查询告警组错误"),
 
     UDF_FUNCTION_NOT_EXIST(20001, "UDF function not found", "UDF函数不存在"),
     UDF_FUNCTION_EXISTS(20002, "UDF function already exists", "UDF函数已存在"),

+ 8 - 0
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java

@@ -34,6 +34,14 @@ public interface AlertGroupService {
      */
     Map<String, Object> queryAlertgroup();
 
+    /**
+     * query alert group by id
+     *
+     * @param loginUser login user
+     * @param id alert group id
+     * @return one alert group
+     */
+    Map<String, Object> queryAlertGroupById(User loginUser, Integer id);
     /**
      * paging query alarm group list
      *

+ 35 - 6
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java

@@ -27,6 +27,7 @@ import org.apache.dolphinscheduler.common.utils.StringUtils;
 import org.apache.dolphinscheduler.dao.entity.AlertGroup;
 import org.apache.dolphinscheduler.dao.entity.User;
 import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper;
+import org.apache.dolphinscheduler.dao.vo.AlertGroupVo;
 
 import java.util.Date;
 import java.util.HashMap;
@@ -70,6 +71,33 @@ public class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup
         return result;
     }
 
+    /**
+     * query alert group by id
+     *
+     * @param loginUser login user
+     * @param id alert group id
+     * @return one alert group
+     */
+    @Override
+    public Map<String, Object> queryAlertGroupById(User loginUser, Integer id) {
+        Map<String, Object> result = new HashMap<>();
+        result.put(Constants.STATUS, false);
+
+        //only admin can operate
+        if (isNotAdmin(loginUser, result)) {
+            return result;
+        }
+        //check if exist
+        AlertGroup alertGroup = alertGroupMapper.selectById(id);
+        if (alertGroup == null) {
+            putMsg(result, Status.ALERT_GROUP_NOT_EXIST);
+            return result;
+        }
+        result.put("data", alertGroup);
+        putMsg(result, Status.SUCCESS);
+        return result;
+    }
+
     /**
      * paging query alarm group list
      *
@@ -88,13 +116,14 @@ public class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup
             return result;
         }
 
-        Page<AlertGroup> page = new Page<>(pageNo, pageSize);
-        IPage<AlertGroup> alertGroupIPage = alertGroupMapper.queryAlertGroupPage(
-                page, searchVal);
-        PageInfo<AlertGroup> pageInfo = new PageInfo<>(pageNo, pageSize);
-        pageInfo.setTotal((int) alertGroupIPage.getTotal());
-        pageInfo.setTotalList(alertGroupIPage.getRecords());
+        Page<AlertGroupVo> page = new Page<>(pageNo, pageSize);
+        IPage<AlertGroupVo> alertGroupVoIPage = alertGroupMapper.queryAlertGroupVo(page, searchVal);
+        PageInfo<AlertGroupVo> pageInfo = new PageInfo<>(pageNo, pageSize);
+
+        pageInfo.setTotal((int) alertGroupVoIPage.getTotal());
+        pageInfo.setTotalList(alertGroupVoIPage.getRecords());
         result.setData(pageInfo);
+
         putMsg(result, Status.SUCCESS);
         return result;
     }

+ 19 - 19
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AlertGroupControllerTest.java

@@ -49,6 +49,7 @@ public class AlertGroupControllerTest extends AbstractControllerTest {
         paramsMap.add("groupName","cxc test group name");
         paramsMap.add("groupType", AlertType.EMAIL.toString());
         paramsMap.add("description","cxc junit 测试告警描述");
+        paramsMap.add("alertInstanceIds", "");
         MvcResult mvcResult = mockMvc.perform(post("/alert-group/create")
                 .header("sessionId", sessionId)
                 .params(paramsMap))
@@ -93,40 +94,41 @@ public class AlertGroupControllerTest extends AbstractControllerTest {
     }
 
     @Test
-    public void testUpdateAlertgroup() throws Exception {
+    public void testQueryAlertGroupById() throws Exception {
         MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
         paramsMap.add("id","22");
-        paramsMap.add("groupName", "hd test group name");
-        paramsMap.add("groupType",AlertType.EMAIL.toString());
-        paramsMap.add("description","update alter group");
-        MvcResult mvcResult = mockMvc.perform(post("/alert-group/update")
+        MvcResult mvcResult = mockMvc.perform(post("/alert-group/query")
                 .header("sessionId", sessionId)
                 .params(paramsMap))
                 .andExpect(status().isOk())
                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                 .andReturn();
         Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
-        Assert.assertTrue(result != null && result.isSuccess());
+        Assert.assertTrue(result != null && result.isStatus(Status.ALERT_GROUP_NOT_EXIST));
         logger.info(mvcResult.getResponse().getContentAsString());
     }
 
     @Test
-    public void testVerifyGroupName() throws Exception {
+    public void testUpdateAlertgroup() throws Exception {
         MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
-        paramsMap.add("groupName","hd test group name");
-        MvcResult mvcResult = mockMvc.perform(get("/alert-group/verify-group-name")
+        paramsMap.add("id","22");
+        paramsMap.add("groupName", "cxc test group name");
+        paramsMap.add("groupType",AlertType.EMAIL.toString());
+        paramsMap.add("description","update alter group");
+        paramsMap.add("alertInstanceIds", "");
+        MvcResult mvcResult = mockMvc.perform(post("/alert-group/update")
                 .header("sessionId", sessionId)
                 .params(paramsMap))
                 .andExpect(status().isOk())
                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                 .andReturn();
         Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
-        Assert.assertTrue(result != null && result.isStatus(Status.ALERT_GROUP_EXIST));
+        Assert.assertTrue(result != null && result.isStatus(Status.ALERT_GROUP_NOT_EXIST));
         logger.info(mvcResult.getResponse().getContentAsString());
     }
 
     @Test
-    public void testVerifyGroupNameNotExit() throws Exception {
+    public void testVerifyGroupName() throws Exception {
         MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
         paramsMap.add("groupName","cxc test group name");
         MvcResult mvcResult = mockMvc.perform(get("/alert-group/verify-group-name")
@@ -136,24 +138,22 @@ public class AlertGroupControllerTest extends AbstractControllerTest {
                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                 .andReturn();
         Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
-        Assert.assertTrue(result != null && result.isSuccess());
+        Assert.assertTrue(result != null && result.isStatus(Status.ALERT_GROUP_EXIST));
         logger.info(mvcResult.getResponse().getContentAsString());
     }
 
     @Test
-    public void testGrantUser() throws Exception {
+    public void testVerifyGroupNameNotExit() throws Exception {
         MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
-        paramsMap.add("alertgroupId","2");
-        paramsMap.add("userIds","2");
-
-        MvcResult mvcResult = mockMvc.perform(post("/alert-group/grant-user")
+        paramsMap.add("groupName","cxc test group name");
+        MvcResult mvcResult = mockMvc.perform(get("/alert-group/verify-group-name")
                 .header("sessionId", sessionId)
                 .params(paramsMap))
                 .andExpect(status().isOk())
                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                 .andReturn();
         Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
-        Assert.assertTrue(result != null && result.isSuccess());
+        Assert.assertTrue(result != null && result.isStatus(Status.ALERT_GROUP_EXIST));
         logger.info(mvcResult.getResponse().getContentAsString());
     }
 
@@ -168,7 +168,7 @@ public class AlertGroupControllerTest extends AbstractControllerTest {
                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                 .andReturn();
         Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
-        Assert.assertTrue(result != null && result.isSuccess());
+        Assert.assertTrue(result != null && result.isStatus(Status.ALERT_GROUP_NOT_EXIST));
         logger.info(mvcResult.getResponse().getContentAsString());
     }
 }

+ 24 - 4
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java

@@ -30,6 +30,7 @@ import org.apache.dolphinscheduler.common.utils.CollectionUtils;
 import org.apache.dolphinscheduler.dao.entity.AlertGroup;
 import org.apache.dolphinscheduler.dao.entity.User;
 import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper;
+import org.apache.dolphinscheduler.dao.vo.AlertGroupVo;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -77,10 +78,10 @@ public class AlertGroupServiceTest {
 
     @Test
     public void testListPaging() {
-        IPage<AlertGroup> page = new Page<>(1, 10);
+        IPage<AlertGroupVo> page = new Page<>(1, 10);
         page.setTotal(1L);
-        page.setRecords(getList());
-        Mockito.when(alertGroupMapper.queryAlertGroupPage(any(Page.class), eq(groupName))).thenReturn(page);
+        page.setRecords(getAlertGroupVoList());
+        Mockito.when(alertGroupMapper.queryAlertGroupVo(any(Page.class), eq(groupName))).thenReturn(page);
         User user = new User();
         // no operate
         Result result = alertGroupService.listPaging(user, groupName, 1, 10);
@@ -90,7 +91,7 @@ public class AlertGroupServiceTest {
         user.setUserType(UserType.ADMIN_USER);
         result = alertGroupService.listPaging(user, groupName, 1, 10);
         logger.info(result.toString());
-        PageInfo<AlertGroup> pageInfo = (PageInfo<AlertGroup>) result.getData();
+        PageInfo<AlertGroupVo> pageInfo = (PageInfo<AlertGroupVo>) result.getData();
         Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getTotalList()));
 
     }
@@ -216,4 +217,23 @@ public class AlertGroupServiceTest {
         return alertGroup;
     }
 
+    /**
+     * get AlertGroupVo list
+     */
+    private List<AlertGroupVo> getAlertGroupVoList() {
+        List<AlertGroupVo> alertGroupVos = new ArrayList<>();
+        alertGroupVos.add(getAlertGroupVoEntity());
+        return alertGroupVos;
+    }
+
+    /**
+     * get AlertGroupVo entity
+     */
+    private AlertGroupVo getAlertGroupVoEntity() {
+        AlertGroupVo alertGroupVo = new AlertGroupVo();
+        alertGroupVo.setId(1);
+        alertGroupVo.setGroupName(groupName);
+        return alertGroupVo;
+    }
+
 }

+ 10 - 0
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java

@@ -18,6 +18,7 @@
 package org.apache.dolphinscheduler.dao.mapper;
 
 import org.apache.dolphinscheduler.dao.entity.AlertGroup;
+import org.apache.dolphinscheduler.dao.vo.AlertGroupVo;
 
 import org.apache.ibatis.annotations.Param;
 
@@ -82,4 +83,13 @@ public interface AlertGroupMapper extends BaseMapper<AlertGroup> {
      * @return
      */
     String queryAlertGroupInstanceIdsById(@Param("alertGroupId") int alertGroupId);
+
+    /**
+     * query alertGroupVo page list
+     * @param page page
+     * @param groupName groupName
+     * @return IPage<AlertGroupVo>: include alert group id and group_name
+     */
+    IPage<AlertGroupVo> queryAlertGroupVo(Page<AlertGroupVo> page,
+                                          @Param("groupName") String groupName);
 }

+ 50 - 0
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/vo/AlertGroupVo.java

@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dolphinscheduler.dao.vo;
+
+/**
+ * AlertGroupVo
+ */
+public class AlertGroupVo {
+
+    /**
+     * primary key
+     */
+    private int id;
+    /**
+     * group_name
+     */
+    private String groupName;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getGroupName() {
+        return groupName;
+    }
+
+    public void setGroupName(String groupName) {
+        this.groupName = groupName;
+    }
+
+}

+ 10 - 1
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml

@@ -32,6 +32,15 @@
         </if>
         order by update_time desc
     </select>
+    <select id="queryAlertGroupVo" resultType="org.apache.dolphinscheduler.dao.vo.AlertGroupVo">
+        select id, group_name
+        from t_ds_alertgroup
+        where 1 = 1
+        <if test="groupName != null and groupName != ''">
+            and group_name like concat('%', #{groupName}, '%')
+        </if>
+        order by update_time desc
+    </select>
     <select id="queryByGroupName" resultType="org.apache.dolphinscheduler.dao.entity.AlertGroup">
         select
         <include refid="baseSql"/>
@@ -70,4 +79,4 @@
         select alert_instance_ids  from t_ds_alertgroup
         where id = #{alertGroupId}
     </select>
-</mapper>
+</mapper>