diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/TagService.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/TagService.java index 8aa694b7426..dae0b4da0c1 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/TagService.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/TagService.java @@ -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; @@ -30,7 +31,6 @@ * 标签服务 * * @author tom - * */ public interface TagService { @@ -72,4 +72,10 @@ public interface TagService { */ List listTag(Set ids); + /** + * remove monitor system tags + * + * @param monitor monitor + */ + void deleteMonitorSystemTags(Monitor monitor); } diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java index 2f715ccc968..498001052d5 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -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; @@ -81,6 +82,9 @@ public class MonitorServiceImpl implements MonitorService { @Autowired private AppService appService; + + @Autowired + private TagService tagService; @Autowired private CollectJobScheduling collectJobScheduling; @@ -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); @@ -565,6 +571,8 @@ public void deleteMonitors(Set 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())); } diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/TagServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/TagServiceImpl.java index 80671644514..ffa6497d236 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/TagServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/TagServiceImpl.java @@ -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; @@ -32,6 +34,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; /** * @author tom @@ -75,5 +78,12 @@ public List listTag(Set ids) { return tagDao.findByIdIn(ids); } + @Override + public void deleteMonitorSystemTags(Monitor monitor) { + if (CollectionUtils.isNotEmpty(monitor.getTags())) { + List tags = monitor.getTags().stream().filter(tag -> tag.getType() == (byte) 0).collect(Collectors.toList()); + tagDao.deleteAll(tags); + } + } } diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/service/MonitorServiceTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/service/MonitorServiceTest.java index bfb3201aab7..72623532b9f 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/service/MonitorServiceTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/service/MonitorServiceTest.java @@ -76,7 +76,10 @@ class MonitorServiceTest { @Mock private AppService appService; - + + @Mock + private TagService tagService; + @Mock private CollectJobScheduling collectJobScheduling;