Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add md5 in alarmRule #810

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface MetricInfoService extends IService<MetricInfo> {

List<MetricInfoDTO> queryListByTenantProduct(String tenant, String workspace, String product);

List<MetricInfoDTO> queryListByRef(String tenant, String workspace, String product, String ref);

MetricInfoDTO queryByMetric(String tenant, String workspace, String metric);

Map<String, QueryRequest> querySpmList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ public List<MetricInfoDTO> queryListByTenantProduct(String tenant, String worksp
return metricInfoConverter.dosToDTOs(metricInfos);
}

@Override
public List<MetricInfoDTO> queryListByRef(String tenant, String workspace, String product,
String ref) {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("tenant", tenant);
columnMap.put("workspace", workspace);
columnMap.put("product", product);
columnMap.put("ref", ref);
columnMap.put("deleted", 0);
List<MetricInfo> metricInfos = listByMap(columnMap);
if (CollectionUtils.isEmpty(metricInfos)) {
return null;
}
return metricInfoConverter.dosToDTOs(metricInfos);
}

@Override
public MetricInfoDTO queryByMetric(String tenant, String workspace, String metric) {
Map<String, Object> columnMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class AlertRuleExtra {
public List<String> alertTags;
public Map<String, String> tagAlias;
public boolean isRecord;
public String md5;
public AlertSilenceConfig alertSilenceConfig;

public NotificationTemplate getDingTalkTemplate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public interface AlertRuleService extends IService<AlarmRule> {

AlarmRuleDTO queryById(Long id, String tenant, String workspace);

List<AlarmRuleDTO> queryBySourceType(String sourceType, String tenant, String workspace);

List<AlarmRuleDTO> queryBySourceId(Long sourceId);

MonitorPageResult<AlarmRuleDTO> getListByPage(MonitorPageRequest<AlarmRuleDTO> pageRequest);

List<AlarmRuleDTO> getListByKeyword(String keyword, String tenant, String workspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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.common.J;
import io.holoinsight.server.common.MD5Hash;
import io.holoinsight.server.home.biz.service.AlertBlockService;
import io.holoinsight.server.home.biz.service.AlertRuleService;
import io.holoinsight.server.home.common.service.RequestContextAdapter;
Expand All @@ -15,9 +17,11 @@
import io.holoinsight.server.home.dal.model.AlarmBlock;
import io.holoinsight.server.home.dal.model.AlarmRule;
import io.holoinsight.server.home.facade.AlarmRuleDTO;
import io.holoinsight.server.home.facade.AlertRuleExtra;
import io.holoinsight.server.home.facade.page.MonitorPageRequest;
import io.holoinsight.server.home.facade.page.MonitorPageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
Expand Down Expand Up @@ -45,6 +49,10 @@ public class AlertRuleServiceImpl extends ServiceImpl<AlarmRuleMapper, AlarmRule

@Override
public Long save(AlarmRuleDTO alarmRuleDTO) {
String md5 = MD5Hash.getMD5(J.toJson(alarmRuleDTO.getRule()));
AlertRuleExtra extra =
null != alarmRuleDTO.getExtra() ? alarmRuleDTO.getExtra() : new AlertRuleExtra();
extra.md5 = md5;
AlarmRule alarmRule = alarmRuleConverter.dtoToDO(alarmRuleDTO);
this.save(alarmRule);
EventBusHolder.post(alarmRuleConverter.doToDTO(alarmRule));
Expand All @@ -53,11 +61,24 @@ public Long save(AlarmRuleDTO alarmRuleDTO) {

@Override
public Boolean updateById(AlarmRuleDTO alarmRuleDTO) {

AlertRuleExtra extra =
null != alarmRuleDTO.getExtra() ? alarmRuleDTO.getExtra() : new AlertRuleExtra();
extra.md5 = genMd5(alarmRuleDTO);
AlarmRule alarmRule = alarmRuleConverter.dtoToDO(alarmRuleDTO);
EventBusHolder.post(alarmRuleDTO);
return this.updateById(alarmRule);
}

public String genMd5(AlarmRuleDTO alarmRuleDTO) {

AlarmRuleDTO alarmRuleDTO1 = new AlarmRuleDTO();
BeanUtils.copyProperties(alarmRuleDTO, alarmRuleDTO1, "id", "gmtCreate", "gmtModified",
"creator", "modifier", "status", "blockId", "tenant", "workspace", "sourceType", "sourceId",
"extra", "envType");
return MD5Hash.getMD5(J.toJson(alarmRuleDTO1));
}

@Override
public Boolean deleteById(Long id) {
AlarmRule alarmRule = getById(id);
Expand All @@ -82,6 +103,21 @@ public AlarmRuleDTO queryById(Long id, String tenant, String workspace) {
return alarmRuleDTO;
}

@Override
public List<AlarmRuleDTO> queryBySourceType(String sourceType, String tenant, String workspace) {
QueryWrapper<AlarmRule> wrapper = new QueryWrapper<>();
wrapper.eq("tenant", tenant);
wrapper.eq("workspace", workspace);
wrapper.eq("source_type", sourceType);
return alarmRuleConverter.dosToDTOs(this.baseMapper.selectList(wrapper));
}

@Override
public List<AlarmRuleDTO> queryBySourceId(Long sourceId) {
QueryWrapper<AlarmRule> wrapper = new QueryWrapper<>();
wrapper.eq("source_id", sourceId);
return alarmRuleConverter.dosToDTOs(this.baseMapper.selectList(wrapper));
}

@Override
public MonitorPageResult<AlarmRuleDTO> getListByPage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package io.holoinsight.server.home.web.controller;

import io.holoinsight.server.common.UtilMisc;
import io.holoinsight.server.common.dao.entity.dto.MetricInfoDTO;
import io.holoinsight.server.common.service.MetricInfoService;
import io.holoinsight.server.home.biz.plugin.core.LogPluginUtil;
import io.holoinsight.server.home.biz.service.AlarmMetricService;
import io.holoinsight.server.home.biz.service.CustomPluginService;
Expand Down Expand Up @@ -75,6 +77,9 @@ public class CustomPluginFacadeImpl extends BaseFacade {
@Autowired
private AlarmMetricService alarmMetricService;

@Autowired
private MetricInfoService metricInfoService;

@Autowired
private TenantInitService tenantInitService;

Expand Down Expand Up @@ -294,12 +299,32 @@ public void doManage() {
if (null == byId) {
throw new MonitorException(ResultCodeEnum.CANNOT_FIND_RECORD, "can not find record");
}

List<MetricInfoDTO> metricInfoDTOS = metricInfoService.queryListByRef(ms.getTenant(),
ms.getWorkspace(), "logmonitor", String.valueOf(byId.getId()));
if (!CollectionUtils.isEmpty(metricInfoDTOS)) {
boolean checkHasAlarmMetric = false;

for (MetricInfoDTO metricInfoDTO : metricInfoDTOS) {
List<AlarmMetric> alarmMetrics = alarmMetricService
.queryByMetric(metricInfoDTO.getMetricTable(), ms.getTenant(), ms.getWorkspace());
if (!CollectionUtils.isEmpty(alarmMetrics)) {
checkHasAlarmMetric = true;
break;
}
}

if (checkHasAlarmMetric) {
throw new MonitorException(ResultCodeEnum.CANNOT_FIND_RECORD,
"This log configuration is associated with the alarm rule configuration. Delete the alarm rule first");
}
}

customPluginService.deleteById(id);
JsonResult.createSuccessResult(result, null);
userOpLogService.append("custom_plugin", byId.getId(), OpType.DELETE,
RequestContext.getContext().mu.getLoginName(), ms.getTenant(), ms.getWorkspace(),
J.toJson(byId), null, null, "custom_plugin_delete");

}
});
return result;
Expand Down
Loading