Skip to content

Commit

Permalink
tags also deleted when the monitor is deleted (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceilzcx authored Aug 8, 2023
1 parent d46b922 commit 35c6567
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.dromara.hertzbeat.manager.service;

import org.dromara.hertzbeat.common.entity.manager.Monitor;
import org.dromara.hertzbeat.common.entity.manager.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -30,7 +31,6 @@
* 标签服务
*
* @author tom
*
*/
public interface TagService {

Expand Down Expand Up @@ -72,4 +72,10 @@ public interface TagService {
*/
List<Tag> listTag(Set<Long> ids);

/**
* remove monitor system tags
*
* @param monitor monitor
*/
void deleteMonitorSystemTags(Monitor monitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.dromara.hertzbeat.manager.service.AppService;
import org.dromara.hertzbeat.manager.service.ImExportService;
import org.dromara.hertzbeat.manager.service.MonitorService;
import org.dromara.hertzbeat.manager.service.TagService;
import org.dromara.hertzbeat.manager.support.exception.MonitorDatabaseException;
import org.dromara.hertzbeat.manager.support.exception.MonitorDetectException;
import org.dromara.hertzbeat.manager.support.exception.MonitorMetricsException;
Expand Down Expand Up @@ -81,6 +82,9 @@ public class MonitorServiceImpl implements MonitorService {

@Autowired
private AppService appService;

@Autowired
private TagService tagService;

@Autowired
private CollectJobScheduling collectJobScheduling;
Expand Down Expand Up @@ -546,6 +550,8 @@ public void deleteMonitor(long id) throws RuntimeException {
if (monitorOptional.isPresent()) {
Monitor monitor = monitorOptional.get();
monitorDao.deleteById(id);
// delete tag 删除监控对应的标签
tagService.deleteMonitorSystemTags(monitor);
paramDao.deleteParamsByMonitorId(id);
tagMonitorBindDao.deleteTagMonitorBindsByMonitorId(id);
alertDefineBindDao.deleteAlertDefineMonitorBindsByMonitorIdEquals(id);
Expand All @@ -565,6 +571,8 @@ public void deleteMonitors(Set<Long> ids) throws RuntimeException {
tagMonitorBindDao.deleteTagMonitorBindsByMonitorIdIn(monitorIds);
alertDefineBindDao.deleteAlertDefineMonitorBindsByMonitorIdIn(monitorIds);
for (Monitor monitor : monitors) {
// delete tag 删除监控对应的标签
tagService.deleteMonitorSystemTags(monitor);
collectJobScheduling.cancelAsyncCollectJob(monitor.getJobId());
calculateAlarm.triggeredAlertMap.remove(String.valueOf(monitor.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.dromara.hertzbeat.manager.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.dromara.hertzbeat.common.entity.manager.Monitor;
import org.dromara.hertzbeat.common.entity.manager.Tag;
import org.dromara.hertzbeat.manager.dao.TagDao;
import org.dromara.hertzbeat.manager.service.TagService;
Expand All @@ -32,6 +34,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author tom
Expand Down Expand Up @@ -75,5 +78,12 @@ public List<Tag> listTag(Set<Long> ids) {
return tagDao.findByIdIn(ids);
}

@Override
public void deleteMonitorSystemTags(Monitor monitor) {
if (CollectionUtils.isNotEmpty(monitor.getTags())) {
List<Tag> tags = monitor.getTags().stream().filter(tag -> tag.getType() == (byte) 0).collect(Collectors.toList());
tagDao.deleteAll(tags);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ class MonitorServiceTest {

@Mock
private AppService appService;


@Mock
private TagService tagService;

@Mock
private CollectJobScheduling collectJobScheduling;

Expand Down

0 comments on commit 35c6567

Please sign in to comment.