Skip to content

Commit

Permalink
feat: alarm history delete (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
masaimu authored Feb 8, 2024
1 parent b606139 commit 81c0b2d
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

-- ----------------------------
-- Table structure for alarm_history
-- ----------------------------
ALTER TABLE `alarm_history`
ADD COLUMN `deleted` TINYINT(4) NULL COMMENT '软删除标记';
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,6 @@ public class AlarmHistory {
*/
@Column(name = "app")
private String app;

private boolean deleted;
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ public class AlarmHistoryDTO {
private String envType;

private List<String> app;

private boolean deleted;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.home.biz.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import io.holoinsight.server.home.common.util.EventBusHolder;
import io.holoinsight.server.home.dal.mapper.AlarmHistoryDetailMapper;
import io.holoinsight.server.home.dal.model.AlarmHistoryDetail;
import io.holoinsight.server.home.facade.AlarmHistoryDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
* @author jsy1001de
* @version 1.0: AlarmRuleUpdateListener.java, Date: 2023-06-09 Time: 09:57
*/
@Component
@Slf4j
public class AlarmRuleHistoryUpdateListener {

@Resource
private AlarmHistoryDetailMapper detailMapper;

@PostConstruct
void register() {
EventBusHolder.register(this);
}

@Subscribe
@AllowConcurrentEvents
public void onEvent(AlarmHistoryDTO alarmHistoryDTO) {

if (alarmHistoryDTO.isDeleted()) {
deleteAlarmHistoryDetail(alarmHistoryDTO.getId(), alarmHistoryDTO.getUniqueId());
}
}

private void deleteAlarmHistoryDetail(Long alarmHistoryId, String uniqueId) {
QueryWrapper<AlarmHistoryDetail> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("unique_id", uniqueId);
queryWrapper.eq("history_id", alarmHistoryId);
int count = this.detailMapper.delete(queryWrapper);
log.info("delete alarm history detail size {}", count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

package io.holoinsight.server.home.biz.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.google.gson.reflect.TypeToken;
import io.holoinsight.server.common.J;
import io.holoinsight.server.home.biz.service.AlarmMetricService;
import io.holoinsight.server.home.common.util.EventBusHolder;
import io.holoinsight.server.home.dal.mapper.AlarmHistoryMapper;
import io.holoinsight.server.home.dal.model.AlarmHistory;
import io.holoinsight.server.home.dal.model.AlarmMetric;
import io.holoinsight.server.home.facade.AlarmRuleDTO;
import io.holoinsight.server.home.facade.Rule;
Expand All @@ -22,6 +25,8 @@
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
* @author jsy1001de
Expand All @@ -33,6 +38,8 @@ public class AlarmRuleUpdateListener {

@Autowired
private AlarmMetricService alarmMetricService;
@Resource
private AlarmHistoryMapper alarmHistoryMapper;

@PostConstruct
void register() {
Expand All @@ -43,6 +50,10 @@ void register() {
@AllowConcurrentEvents
public void onEvent(AlarmRuleDTO alarmRuleDTO) {

if (alarmRuleDTO.getStatus() == 0) {
deleteAlarmHistory(alarmRuleDTO.getId(), alarmRuleDTO.getRuleType());
}

if (CollectionUtils.isEmpty(alarmRuleDTO.getRule())) {
return;
}
Expand Down Expand Up @@ -80,4 +91,22 @@ public void onEvent(AlarmRuleDTO alarmRuleDTO) {
}
}
}

private void deleteAlarmHistory(Long alarmRuleId, String ruleType) {
QueryWrapper<AlarmHistory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("unique_id", String.join("_", ruleType, String.valueOf(alarmRuleId)));
List<AlarmHistory> alarmHistoryList = this.alarmHistoryMapper.selectList(queryWrapper);
if (CollectionUtils.isEmpty(alarmHistoryList)) {
return;
}
int count = 0;
for (AlarmHistory alarmHistory : alarmHistoryList) {
AlarmHistory deletingHistory = new AlarmHistory();
deletingHistory.setId(alarmHistory.getId());
deletingHistory.setDeleted(true);
count += this.alarmHistoryMapper.updateById(deletingHistory);
}
log.info("need mark deleting alert history size {}, real deleted size {}",
alarmHistoryList.size(), count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*/
package io.holoinsight.server.home.biz.service;

import com.baomidou.mybatisplus.extension.service.IService;
import io.holoinsight.server.home.dal.model.AlarmHistory;
import io.holoinsight.server.home.facade.AlarmHistoryDTO;
import io.holoinsight.server.home.facade.page.MonitorPageRequest;
import io.holoinsight.server.home.facade.page.MonitorPageResult;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;

Expand All @@ -23,4 +23,6 @@ public interface AlarmHistoryService extends IService<AlarmHistory> {
MonitorPageResult<AlarmHistoryDTO> getListByPage(MonitorPageRequest<AlarmHistoryDTO> pageRequest,
List<String> uniqueIds);

Boolean deleteById(Long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
*/
package io.holoinsight.server.home.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.holoinsight.server.home.biz.service.AlarmHistoryService;
import io.holoinsight.server.home.common.service.RequestContextAdapter;
import io.holoinsight.server.home.common.util.EventBusHolder;
import io.holoinsight.server.home.common.util.StringUtil;
import io.holoinsight.server.home.dal.converter.AlarmHistoryConverter;
import io.holoinsight.server.home.dal.mapper.AlarmHistoryMapper;
import io.holoinsight.server.home.dal.model.AlarmHistory;
import io.holoinsight.server.home.facade.AlarmHistoryDTO;
import io.holoinsight.server.home.facade.page.MonitorPageRequest;
import io.holoinsight.server.home.facade.page.MonitorPageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -45,6 +46,17 @@ public AlarmHistoryDTO queryById(Long id, String tenant, String workspace) {
return alarmHistoryConverter.doToDTO(alarmHistory);
}

@Override
public Boolean deleteById(Long id) {
AlarmHistory alarmHistory = getById(id);
if (null == alarmHistory) {
return true;
}
alarmHistory.setDeleted(true);
EventBusHolder.post(alarmHistoryConverter.doToDTO(alarmHistory));
return this.removeById(id);
}

@Override
public MonitorPageResult<AlarmHistoryDTO> getListByPage(
MonitorPageRequest<AlarmHistoryDTO> pageRequest, List<String> uniqueIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package io.holoinsight.server.home.web.controller;

import io.holoinsight.server.common.JsonResult;
import io.holoinsight.server.home.biz.service.AlarmHistoryService;
import io.holoinsight.server.home.common.util.scope.AuthTargetType;
import io.holoinsight.server.home.common.util.scope.MonitorScope;
Expand All @@ -12,11 +13,11 @@
import io.holoinsight.server.home.facade.page.MonitorPageRequest;
import io.holoinsight.server.home.facade.page.MonitorPageResult;
import io.holoinsight.server.home.web.common.ManageCallback;
import io.holoinsight.server.home.web.common.ParaCheckUtil;
import io.holoinsight.server.home.web.interceptor.MonitorScopeAuth;
import io.holoinsight.server.common.JsonResult;
import io.holoinsight.server.home.web.security.LevelAuthorizationAccess;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -37,17 +38,16 @@ public class AlarmHistoryFacadeImpl extends BaseFacade {
@Autowired
private AlarmHistoryService alarmHistoryService;


@LevelAuthorizationAccess(paramConfigs = {"PARAMETER" + ":$!id"},
levelAuthorizationCheckeClass = "io.holoinsight.server.home.web.security.custom.AlarmHistoryFacadeImplChecker")
@GetMapping("/query/{id}")
@ResponseBody
@MonitorScopeAuth(targetType = AuthTargetType.TENANT, needPower = PowerConstants.VIEW)
public JsonResult<AlarmHistoryDTO> queryById(@PathVariable("id") Long id) {
final JsonResult<AlarmHistoryDTO> result = new JsonResult<>();
facadeTemplate.manage(result, new ManageCallback() {
@Override
public void checkParameter() {
ParaCheckUtil.checkParaNotNull(id, "id");
}
public void checkParameter() {}

@Override
public void doManage() {
Expand All @@ -60,6 +60,36 @@ public void doManage() {
return result;
}

@LevelAuthorizationAccess(paramConfigs = {"PARAMETER" + ":$!id"},
levelAuthorizationCheckeClass = "io.holoinsight.server.home.web.security.custom.AlarmHistoryFacadeImplChecker")
@DeleteMapping("/delete/{id}")
@ResponseBody
@MonitorScopeAuth(targetType = AuthTargetType.TENANT, needPower = PowerConstants.VIEW)
public JsonResult<Boolean> deleteById(@PathVariable("id") Long id) {
final JsonResult<Boolean> result = new JsonResult<>();
facadeTemplate.manage(result, new ManageCallback() {
@Override
public void checkParameter() {}

@Override
public void doManage() {
MonitorScope ms = RequestContext.getContext().ms;
boolean rtn = false;
AlarmHistoryDTO alarmHistoryDTO =
alarmHistoryService.queryById(id, ms.getTenant(), ms.getWorkspace());
if (alarmHistoryDTO != null) {
rtn = alarmHistoryService.deleteById(id);
}

JsonResult.createSuccessResult(result, rtn);
}
});

return result;
}

@LevelAuthorizationAccess(paramConfigs = {"PARAMETER" + ":$!pageRequest"},
levelAuthorizationCheckeClass = "io.holoinsight.server.home.web.security.custom.AlarmHistoryFacadeImplChecker")
@PostMapping("/pageQuery")
@ResponseBody
@MonitorScopeAuth(targetType = AuthTargetType.TENANT, needPower = PowerConstants.VIEW)
Expand All @@ -68,9 +98,7 @@ public JsonResult<MonitorPageResult<AlarmHistoryDTO>> pageQuery(
final JsonResult<MonitorPageResult<AlarmHistoryDTO>> result = new JsonResult<>();
facadeTemplate.manage(result, new ManageCallback() {
@Override
public void checkParameter() {
ParaCheckUtil.checkParaNotNull(pageRequest.getTarget(), "target");
}
public void checkParameter() {}

@Override
public void doManage() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.web.security.custom;

import io.holoinsight.server.home.web.security.LevelAuthorizationCheck;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
* @author masaimu
* @version 2024-02-07 17:29:00
*/
@Slf4j
public abstract class AbstractResourceChecker implements LevelAuthorizationCheck {

protected boolean checkIdNotNull(List<String> parameters) {
if (CollectionUtils.isEmpty(parameters) || !StringUtils.isNumeric(parameters.get(0))) {
log.error("parameters {} is empty or is not numeric.", parameters);
return false;
}
return true;
}

protected boolean checkIdExists(List<String> parameters, String tenant, String workspace) {
if (!checkIdNotNull(parameters)) {
return false;
}
Long id = Long.parseLong(parameters.get(0));
return checkIdExists(id, tenant, workspace);
}

abstract boolean checkIdExists(Long id, String tenant, String workspace);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.holoinsight.server.home.dal.model.AlarmDingDingRobot;
import io.holoinsight.server.home.dal.model.dto.AlarmDingDingRobotDTO;
import io.holoinsight.server.home.facade.page.MonitorPageRequest;
import io.holoinsight.server.home.web.security.LevelAuthorizationCheck;
import io.holoinsight.server.home.web.security.LevelAuthorizationMetaData;
import io.holoinsight.server.home.web.security.ParameterSecurityService;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -35,7 +34,7 @@
*/
@Slf4j
@Service
public class AlarmDingDingRobotFacadeImplChecker implements LevelAuthorizationCheck {
public class AlarmDingDingRobotFacadeImplChecker extends AbstractResourceChecker {

@Autowired
private AlarmDingDingRobotMapper alarmDingDingRobotMapper;
Expand Down Expand Up @@ -74,13 +73,7 @@ private boolean checkParameters(String methodName, List<String> parameters, Stri
}
}

private boolean checkIdNotNull(List<String> parameters) {
if (CollectionUtils.isEmpty(parameters) || !StringUtils.isNumeric(parameters.get(0))) {
log.error("parameters {} is empty or is not numeric.", parameters);
return false;
}
return true;
}


private boolean checkPageRequest(String methodName, List<String> parameters, String tenant,
String workspace) {
Expand All @@ -107,13 +100,7 @@ private boolean checkPageRequest(String methodName, List<String> parameters, Str
return checkAlarmDingDingRobotDTO(methodName, target, tenant, workspace);
}

private boolean checkIdExists(List<String> parameters, String tenant, String workspace) {
if (!checkIdNotNull(parameters)) {
return false;
}
Long id = Long.parseLong(parameters.get(0));
return checkIdExists(id, tenant, workspace);
}


private boolean checkAlarmDingDingRobotDTO(String methodName, List<String> parameters,
String tenant, String workspace) {
Expand Down Expand Up @@ -206,7 +193,8 @@ private boolean checkUserIds(String extra) {
return true;
}

private boolean checkIdExists(Long id, String tenant, String workspace) {
@Override
boolean checkIdExists(Long id, String tenant, String workspace) {
QueryWrapper<AlarmDingDingRobot> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", id);
queryWrapper.eq("tenant", tenant);
Expand Down
Loading

0 comments on commit 81c0b2d

Please sign in to comment.