diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java index 92450f8eec..227775d62c 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java +++ b/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 result = alertGroupService.queryAlertGroupById(loginUser, id); + return returnDataList(result); + } + /** * updateProcessInstance alert group diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java index 8372a69355..4c7d25efca 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java +++ b/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函数已存在"), diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java index 9d016aca3f..5e25696f00 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java @@ -34,6 +34,14 @@ public interface AlertGroupService { */ Map queryAlertgroup(); + /** + * query alert group by id + * + * @param loginUser login user + * @param id alert group id + * @return one alert group + */ + Map queryAlertGroupById(User loginUser, Integer id); /** * paging query alarm group list * diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java index dcee5feb56..5fa4d7059e 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java +++ b/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 queryAlertGroupById(User loginUser, Integer id) { + Map 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 page = new Page<>(pageNo, pageSize); - IPage alertGroupIPage = alertGroupMapper.queryAlertGroupPage( - page, searchVal); - PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); - pageInfo.setTotal((int) alertGroupIPage.getTotal()); - pageInfo.setTotalList(alertGroupIPage.getRecords()); + Page page = new Page<>(pageNo, pageSize); + IPage alertGroupVoIPage = alertGroupMapper.queryAlertGroupVo(page, searchVal); + PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); + + pageInfo.setTotal((int) alertGroupVoIPage.getTotal()); + pageInfo.setTotalList(alertGroupVoIPage.getRecords()); result.setData(pageInfo); + putMsg(result, Status.SUCCESS); return result; } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AlertGroupControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AlertGroupControllerTest.java index 1c1eec9238..6075b16dd7 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AlertGroupControllerTest.java +++ b/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 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 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 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 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()); } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java index 3a78b37e9e..eea323e6f6 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java +++ b/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 page = new Page<>(1, 10); + IPage 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 pageInfo = (PageInfo) result.getData(); + PageInfo pageInfo = (PageInfo) result.getData(); Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getTotalList())); } @@ -216,4 +217,23 @@ public class AlertGroupServiceTest { return alertGroup; } + /** + * get AlertGroupVo list + */ + private List getAlertGroupVoList() { + List 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; + } + } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java index b8f4188fc7..72eac71441 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java +++ b/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 { * @return */ String queryAlertGroupInstanceIdsById(@Param("alertGroupId") int alertGroupId); + + /** + * query alertGroupVo page list + * @param page page + * @param groupName groupName + * @return IPage: include alert group id and group_name + */ + IPage queryAlertGroupVo(Page page, + @Param("groupName") String groupName); } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/vo/AlertGroupVo.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/vo/AlertGroupVo.java new file mode 100644 index 0000000000..e970c8b2ca --- /dev/null +++ b/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; + } + +} diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml index 8a7d3a57e8..77611d8ebd 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml @@ -32,6 +32,15 @@ order by update_time desc + - \ No newline at end of file +