From 69f29b1a02a266888c9ee5c2d26b68dfc6b5f3a9 Mon Sep 17 00:00:00 2001 From: masaimu Date: Wed, 19 Jul 2023 16:49:32 +0800 Subject: [PATCH 1/2] feat(home): alert subscribe across tenants (#565) --- .../alert/plugin/GetSubscriptionHandler.java | 12 ++++--- .../alert/service/calculate/BaseFunction.java | 6 +++- .../common/service/RequestContextAdapter.java | 3 +- .../service/RequestContextAdapterImpl.java | 11 +++++- .../biz/service/AlertSubscribeService.java | 5 +-- .../impl/AlarmHistoryDetailServiceImpl.java | 4 +-- .../service/impl/AlarmHistoryServiceImpl.java | 5 ++- .../impl/AlertDingDingRobotServiceImpl.java | 15 +++++--- .../service/impl/AlertGroupServiceImpl.java | 35 +++++++++++-------- .../service/impl/AlertRuleServiceImpl.java | 6 ++-- .../impl/AlertSubscribeServiceImpl.java | 16 ++++++--- .../AlarmDingDingRobotFacadeImpl.java | 13 ++++--- .../web/controller/AlarmGroupFacadeImpl.java | 14 +++++--- .../web/controller/AlarmRuleFacadeImpl.java | 33 +++++++++-------- .../controller/AlarmSubscribeFacadeImpl.java | 34 +++++++++--------- .../controller/AlarmRuleFacadeImplTest.java | 2 +- 16 files changed, 131 insertions(+), 83 deletions(-) diff --git a/server/home/home-alert/src/main/java/io/holoinsight/server/home/alert/plugin/GetSubscriptionHandler.java b/server/home/home-alert/src/main/java/io/holoinsight/server/home/alert/plugin/GetSubscriptionHandler.java index c9be0103b..d0376c4a2 100644 --- a/server/home/home-alert/src/main/java/io/holoinsight/server/home/alert/plugin/GetSubscriptionHandler.java +++ b/server/home/home-alert/src/main/java/io/holoinsight/server/home/alert/plugin/GetSubscriptionHandler.java @@ -12,6 +12,7 @@ import io.holoinsight.server.home.alert.model.event.WebhookInfo; import io.holoinsight.server.home.alert.service.converter.DoConvert; import io.holoinsight.server.home.alert.service.event.AlertHandlerExecutor; +import io.holoinsight.server.home.common.service.RequestContextAdapter; import io.holoinsight.server.home.dal.mapper.AlarmBlockMapper; import io.holoinsight.server.home.dal.mapper.AlarmDingDingRobotMapper; import io.holoinsight.server.home.dal.mapper.AlarmGroupMapper; @@ -27,6 +28,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -39,7 +41,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -71,6 +72,9 @@ public class GetSubscriptionHandler implements AlertHandlerExecutor { @Resource private AlarmBlockMapper alarmBlockDOMapper; + @Autowired + private RequestContextAdapter requestContextAdapter; + @Override public void handle(List alertNotifies) { try { @@ -147,7 +151,8 @@ public void handle(List alertNotifies) { QueryWrapper alertSubscribeQueryWrapper = new QueryWrapper<>(); alertSubscribeQueryWrapper.eq("unique_id", alertNotify.getUniqueId()); alertSubscribeQueryWrapper.eq("status", (byte) 1); - alertSubscribeQueryWrapper.eq("tenant", alertNotify.getTenant()); + requestContextAdapter.queryWrapperTenantAdapt(alertSubscribeQueryWrapper, null, + alertNotify.getWorkspace()); addGlobalWebhook(alertNotify, alertWebhookMap); List alertSubscribeList = alarmSubscribeDOMapper.selectList(alertSubscribeQueryWrapper); @@ -175,7 +180,6 @@ public void handle(List alertNotifies) { if (isNotifyGroup(alertSubscribe.getGroupId())) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("id", alertSubscribe.getGroupId()); - wrapper.eq("tenant", alertNotify.getTenant()); AlarmGroup alarmGroup = alarmGroupDOMapper.selectOne(wrapper); Map> map = G.get().fromJson(alarmGroup.getGroupInfo(), Map.class); @@ -197,7 +201,6 @@ public void handle(List alertNotifies) { if (isNotifyGroup(alertSubscribe.getGroupId())) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("id", alertSubscribe.getGroupId()); - wrapper.eq("tenant", alertNotify.getTenant()); AlarmGroup alarmGroup = alarmGroupDOMapper.selectOne(wrapper); if (StringUtils.isNotBlank(alarmGroup.getGroupInfo())) { Map> map = @@ -235,7 +238,6 @@ public void handle(List alertNotifies) { if (!CollectionUtils.isEmpty(dingDingGroupIdList)) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.in("id", new ArrayList<>(dingDingGroupIdList)); - wrapper.eq("tenant", alertNotify.getTenant()); List alertDingDingRobotList = alarmDingDingRobotDOMapper.selectList(wrapper); List dingdingUrls = new ArrayList<>(); diff --git a/server/home/home-alert/src/main/java/io/holoinsight/server/home/alert/service/calculate/BaseFunction.java b/server/home/home-alert/src/main/java/io/holoinsight/server/home/alert/service/calculate/BaseFunction.java index 3e689eb43..088d92ef3 100644 --- a/server/home/home-alert/src/main/java/io/holoinsight/server/home/alert/service/calculate/BaseFunction.java +++ b/server/home/home-alert/src/main/java/io/holoinsight/server/home/alert/service/calculate/BaseFunction.java @@ -48,7 +48,11 @@ protected TriggerResult doInvoke(DataResult dataResult, FunctionConfigParam func long delta = getDelta(functionConfigParam.getPeriodType()); Map points = dataResult.getPoints(); - result.setCurrentValue(points.get(functionConfigParam.getPeriod())); + Double currentValue = points.get(functionConfigParam.getPeriod()); + if (currentValue == null && functionConfigParam.isZeroFill()) { + currentValue = 0d; + } + result.setCurrentValue(currentValue); long duration = functionConfigParam.getDuration(); for (long i = 0; i < duration; i++) { long time = functionConfigParam.getPeriod() - i * 60000L; diff --git a/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/RequestContextAdapter.java b/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/RequestContextAdapter.java index ec077e1e9..76485db5d 100644 --- a/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/RequestContextAdapter.java +++ b/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/RequestContextAdapter.java @@ -18,6 +18,7 @@ public interface RequestContextAdapter { QueryProto.PqlRangeRequest requestAdapte(QueryProto.PqlRangeRequest request); - void queryWrapperTenantAdapte(QueryWrapper queryWrapper, String tenant, String workspace); + void queryWrapperTenantAdapt(QueryWrapper queryWrapper, String tenant, String workspace); + String getWorkspace(boolean cross); } diff --git a/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/RequestContextAdapterImpl.java b/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/RequestContextAdapterImpl.java index a2de90cc7..cf8a5d08a 100644 --- a/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/RequestContextAdapterImpl.java +++ b/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/RequestContextAdapterImpl.java @@ -4,6 +4,9 @@ package io.holoinsight.server.home.common.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import io.holoinsight.server.home.common.util.scope.MonitorCookieUtil; +import io.holoinsight.server.home.common.util.scope.MonitorScope; +import io.holoinsight.server.home.common.util.scope.RequestContext; import io.holoinsight.server.query.grpc.QueryProto; import org.apache.commons.lang3.StringUtils; @@ -25,7 +28,7 @@ public QueryProto.PqlRangeRequest requestAdapte(QueryProto.PqlRangeRequest reque } @Override - public void queryWrapperTenantAdapte(QueryWrapper queryWrapper, String tenant, + public void queryWrapperTenantAdapt(QueryWrapper queryWrapper, String tenant, String workspace) { if (queryWrapper != null) { if (StringUtils.isNotBlank(tenant)) { @@ -36,4 +39,10 @@ public void queryWrapperTenantAdapte(QueryWrapper queryWrapper, String te } } } + + @Override + public String getWorkspace(boolean cross) { + MonitorScope ms = RequestContext.getContext().ms; + return ms.getWorkspace(); + } } diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/AlertSubscribeService.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/AlertSubscribeService.java index ee2b84b27..0c216675b 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/AlertSubscribeService.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/AlertSubscribeService.java @@ -3,6 +3,7 @@ */ package io.holoinsight.server.home.biz.service; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import io.holoinsight.server.home.dal.model.AlarmSubscribe; import io.holoinsight.server.home.dal.model.dto.AlarmSubscribeDTO; @@ -21,9 +22,9 @@ public interface AlertSubscribeService extends IService { Boolean saveDataBatch(AlarmSubscribeDTO alarmSubscribeDTO, String creator, String tenant, String workspace); - AlarmSubscribeDTO queryByUniqueId(Map columnMap); + AlarmSubscribeDTO queryByUniqueId(QueryWrapper queryWrapper, String uniqueId); - List queryByMap(Map columnMap); + List queryByMap(QueryWrapper queryWrapper); Long save(AlarmSubscribeInfo alarmSubscribeInfo); diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlarmHistoryDetailServiceImpl.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlarmHistoryDetailServiceImpl.java index 2af81a468..ebb6a19c1 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlarmHistoryDetailServiceImpl.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlarmHistoryDetailServiceImpl.java @@ -57,7 +57,7 @@ public MonitorPageResult getListByPage( wrapper.eq("alarm_time", alarmHistoryDetail.getAlarmTime()); } - this.requestContextAdapter.queryWrapperTenantAdapte(wrapper, + this.requestContextAdapter.queryWrapperTenantAdapt(wrapper, alarmHistoryDetail.getTenant().trim(), alarmHistoryDetail.getWorkspace()); if (null != pageRequest.getFrom()) { @@ -105,7 +105,7 @@ public List> count(MonitorPageRequest if (StringUtils.isNotBlank(target.getUniqueId())) { queryWrapper.eq("unique_id", target.getUniqueId()); } - this.requestContextAdapter.queryWrapperTenantAdapte(queryWrapper, target.getTenant(), + this.requestContextAdapter.queryWrapperTenantAdapt(queryWrapper, target.getTenant(), target.getWorkspace()); queryWrapper.between("gmt_create", diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlarmHistoryServiceImpl.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlarmHistoryServiceImpl.java index 2e8820cd4..0f32a2249 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlarmHistoryServiceImpl.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlarmHistoryServiceImpl.java @@ -15,7 +15,6 @@ 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.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -38,7 +37,7 @@ public class AlarmHistoryServiceImpl extends ServiceImpl wrapper = new QueryWrapper<>(); - this.requestContextAdapter.queryWrapperTenantAdapte(wrapper, tenant, workspace); + this.requestContextAdapter.queryWrapperTenantAdapt(wrapper, tenant, workspace); wrapper.eq("id", id); wrapper.last("LIMIT 1"); @@ -61,7 +60,7 @@ public MonitorPageResult getListByPage( wrapper.eq("id", alarmHistory.getId()); } - this.requestContextAdapter.queryWrapperTenantAdapte(wrapper, alarmHistory.getTenant(), + this.requestContextAdapter.queryWrapperTenantAdapt(wrapper, alarmHistory.getTenant(), alarmHistory.getWorkspace()); if (null != alarmHistory.getUniqueId()) { diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertDingDingRobotServiceImpl.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertDingDingRobotServiceImpl.java index f72b2349d..e9eeed864 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertDingDingRobotServiceImpl.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertDingDingRobotServiceImpl.java @@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import io.holoinsight.server.home.biz.service.AlertDingDingRobotService; +import io.holoinsight.server.home.common.service.RequestContextAdapter; import io.holoinsight.server.home.common.util.StringUtil; import io.holoinsight.server.home.dal.converter.AlarmDingDingRobotConverter; import io.holoinsight.server.home.dal.mapper.AlarmDingDingRobotMapper; @@ -14,6 +15,7 @@ import io.holoinsight.server.home.dal.model.dto.AlarmDingDingRobotDTO; import io.holoinsight.server.home.facade.page.MonitorPageRequest; import io.holoinsight.server.home.facade.page.MonitorPageResult; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -31,6 +33,9 @@ public class AlertDingDingRobotServiceImpl extends @Resource private AlarmDingDingRobotConverter alarmDingDingRobotConverter; + @Autowired + private RequestContextAdapter requestContextAdapter; + @Override public Long save(AlarmDingDingRobotDTO alarmDingDingRobotDTO) { AlarmDingDingRobot alarmDingDingRobot = @@ -49,7 +54,8 @@ public Boolean updateById(AlarmDingDingRobotDTO alarmDingDingRobotDTO) { @Override public AlarmDingDingRobotDTO queryById(Long id, String tenant) { QueryWrapper wrapper = new QueryWrapper<>(); - wrapper.eq("tenant", tenant); + requestContextAdapter.queryWrapperTenantAdapt(wrapper, tenant, + requestContextAdapter.getWorkspace(true)); wrapper.eq("id", id); wrapper.last("LIMIT 1"); AlarmDingDingRobot alarmDingDingRobot = this.getOne(wrapper); @@ -68,14 +74,13 @@ public MonitorPageResult getListByPage( AlarmDingDingRobot alarmDingDingRobot = alarmDingDingRobotConverter.dtoToDO(pageRequest.getTarget()); + this.requestContextAdapter.queryWrapperTenantAdapt(wrapper, alarmDingDingRobot.getTenant(), + alarmDingDingRobot.getWorkspace()); + if (null != alarmDingDingRobot.getId()) { wrapper.eq("id", alarmDingDingRobot.getId()); } - if (StringUtil.isNotBlank(alarmDingDingRobot.getTenant())) { - wrapper.eq("tenant", alarmDingDingRobot.getTenant().trim()); - } - if (StringUtil.isNotBlank(alarmDingDingRobot.getGroupName())) { wrapper.like("group_name", alarmDingDingRobot.getGroupName().trim()); } diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertGroupServiceImpl.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertGroupServiceImpl.java index 7a4629052..2711cb72e 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertGroupServiceImpl.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertGroupServiceImpl.java @@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import io.holoinsight.server.home.biz.service.AlertGroupService; +import io.holoinsight.server.home.common.service.RequestContextAdapter; import io.holoinsight.server.home.dal.converter.AlarmGroupConverter; import io.holoinsight.server.home.dal.mapper.AlarmGroupMapper; import io.holoinsight.server.home.dal.model.AlarmGroup; @@ -14,6 +15,7 @@ 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.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -26,6 +28,9 @@ public class AlertGroupServiceImpl extends ServiceImpl wrapper = new QueryWrapper<>(); - wrapper.eq("tenant", tenant); + requestContextAdapter.queryWrapperTenantAdapt(wrapper, tenant, + requestContextAdapter.getWorkspace(true)); wrapper.eq("id", id); wrapper.last("LIMIT 1"); AlarmGroup alarmGroup = this.getOne(wrapper); @@ -67,29 +73,28 @@ public MonitorPageResult getListByPage( QueryWrapper wrapper = new QueryWrapper<>(); - AlarmGroup alarmHistory = alarmGroupConverter.dtoToDO(pageRequest.getTarget()); + AlarmGroup alarmGroup = alarmGroupConverter.dtoToDO(pageRequest.getTarget()); - if (null != alarmHistory.getId()) { - wrapper.eq("id", alarmHistory.getId()); - } + this.requestContextAdapter.queryWrapperTenantAdapt(wrapper, alarmGroup.getTenant(), + alarmGroup.getWorkspace()); - if (StringUtils.isNotBlank(alarmHistory.getTenant())) { - wrapper.eq("tenant", alarmHistory.getTenant().trim()); + if (null != alarmGroup.getId()) { + wrapper.eq("id", alarmGroup.getId()); } - if (null != alarmHistory.getEnvType()) { - wrapper.eq("env_type", alarmHistory.getEnvType()); + if (null != alarmGroup.getEnvType()) { + wrapper.eq("env_type", alarmGroup.getEnvType()); } - if (StringUtils.isNotBlank(alarmHistory.getGroupName())) { - wrapper.like("group_name", alarmHistory.getGroupName().trim()); + if (StringUtils.isNotBlank(alarmGroup.getGroupName())) { + wrapper.like("group_name", alarmGroup.getGroupName().trim()); } - if (StringUtils.isNotBlank(alarmHistory.getCreator())) { - wrapper.like("creator", alarmHistory.getCreator().trim()); + if (StringUtils.isNotBlank(alarmGroup.getCreator())) { + wrapper.like("creator", alarmGroup.getCreator().trim()); } - if (StringUtils.isNotBlank(alarmHistory.getModifier())) { - wrapper.like("modifier", alarmHistory.getModifier().trim()); + if (StringUtils.isNotBlank(alarmGroup.getModifier())) { + wrapper.like("modifier", alarmGroup.getModifier().trim()); } wrapper.orderByDesc("id"); diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertRuleServiceImpl.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertRuleServiceImpl.java index 94c9b9f69..899f032b7 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertRuleServiceImpl.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertRuleServiceImpl.java @@ -74,7 +74,7 @@ public Boolean deleteById(Long id) { @Override public AlarmRuleDTO queryById(Long id, String tenant, String workspace) { QueryWrapper wrapper = new QueryWrapper<>(); - this.requestContextAdapter.queryWrapperTenantAdapte(wrapper, tenant, workspace); + this.requestContextAdapter.queryWrapperTenantAdapt(wrapper, tenant, workspace); wrapper.eq("id", id); wrapper.last("LIMIT 1"); @@ -100,7 +100,7 @@ public MonitorPageResult getListByPage( wrapper.eq("id", alarmRule.getId()); } - this.requestContextAdapter.queryWrapperTenantAdapte(wrapper, alarmRule.getTenant(), + this.requestContextAdapter.queryWrapperTenantAdapt(wrapper, alarmRule.getTenant(), alarmRule.getWorkspace()); if (null != alarmRule.getStatus()) { @@ -223,7 +223,7 @@ protected void setAlarmContent(AlarmRuleDTO alarmRuleDTO) { @Override public List getListByKeyword(String keyword, String tenant, String workspace) { QueryWrapper wrapper = new QueryWrapper<>(); - this.requestContextAdapter.queryWrapperTenantAdapte(wrapper, tenant, workspace); + this.requestContextAdapter.queryWrapperTenantAdapt(wrapper, tenant, workspace); wrapper.like("id", keyword).or().like("rule_name", keyword); wrapper.last("LIMIT 10"); diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertSubscribeServiceImpl.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertSubscribeServiceImpl.java index a1ed0a641..4ef085346 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertSubscribeServiceImpl.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/AlertSubscribeServiceImpl.java @@ -3,6 +3,7 @@ */ package io.holoinsight.server.home.biz.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import io.holoinsight.server.home.biz.service.AlertSubscribeService; import io.holoinsight.server.home.dal.converter.AlarmSubscribeConverter; @@ -34,6 +35,9 @@ public class AlertSubscribeServiceImpl extends ServiceImpl columnMap) { + public AlarmSubscribeDTO queryByUniqueId(QueryWrapper queryWrapper, + String uniqueId) { AlarmSubscribeDTO alarmSubscribeDTO = new AlarmSubscribeDTO(); - alarmSubscribeDTO.setUniqueId((String) columnMap.get("unique_id")); - List list = this.listByMap(columnMap); + alarmSubscribeDTO.setUniqueId(uniqueId); + List list = this.alarmSubscribeMapper.selectList(queryWrapper); if (!CollectionUtils.isEmpty(list)) { alarmSubscribeDTO.setEnvType(list.get(0).getEnvType()); } @@ -92,8 +97,9 @@ public AlarmSubscribeDTO queryByUniqueId(Map columnMap) { } @Override - public List queryByMap(Map columnMap) { - return alarmSubscribeConverter.dosToDTOs(this.listByMap(columnMap)); + public List queryByMap(QueryWrapper queryWrapper) { + List list = this.alarmSubscribeMapper.selectList(queryWrapper); + return alarmSubscribeConverter.dosToDTOs(list); } @Override diff --git a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmDingDingRobotFacadeImpl.java b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmDingDingRobotFacadeImpl.java index 1a35f7f50..9ebddb306 100644 --- a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmDingDingRobotFacadeImpl.java +++ b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmDingDingRobotFacadeImpl.java @@ -5,6 +5,7 @@ import io.holoinsight.server.home.biz.service.AlertDingDingRobotService; import io.holoinsight.server.home.biz.service.UserOpLogService; +import io.holoinsight.server.home.common.service.RequestContextAdapter; import io.holoinsight.server.home.common.util.MonitorException; import io.holoinsight.server.home.common.util.scope.AuthTargetType; import io.holoinsight.server.home.common.util.scope.MonitorScope; @@ -49,6 +50,9 @@ public class AlarmDingDingRobotFacadeImpl extends BaseFacade { @Autowired private UserOpLogService userOpLogService; + @Autowired + private RequestContextAdapter requestContextAdapter; + @PostMapping("/create") @ResponseBody @MonitorScopeAuth(targetType = AuthTargetType.TENANT, needPower = PowerConstants.EDIT) @@ -72,6 +76,7 @@ public void doManage() { if (null != ms && !StringUtils.isEmpty(ms.tenant)) { alarmDingDingRobotDTO.setTenant(ms.tenant); } + alarmDingDingRobotDTO.setWorkspace(requestContextAdapter.getWorkspace(true)); alarmDingDingRobotDTO.setGmtCreate(new Date()); alarmDingDingRobotDTO.setGmtModified(new Date()); Long id = alarmDingDingRobotService.save(alarmDingDingRobotDTO); @@ -121,9 +126,7 @@ public void doManage() { alarmDingDingRobotDTO.setModifier(mu.getLoginName()); } MonitorScope ms = RequestContext.getContext().ms; - if (null != ms && !StringUtils.isEmpty(ms.tenant)) { - alarmDingDingRobotDTO.setTenant(ms.tenant); - } + alarmDingDingRobotDTO.setGmtModified(new Date()); boolean save = alarmDingDingRobotService.updateById(alarmDingDingRobotDTO); @@ -177,7 +180,8 @@ public void doManage() { boolean rtn = false; AlarmDingDingRobotDTO alarmDingDingRobot = alarmDingDingRobotService.queryById(id, ms.getTenant()); - if (alarmDingDingRobot != null) { + if (alarmDingDingRobot != null + && StringUtils.equals(alarmDingDingRobot.getTenant(), ms.getTenant())) { rtn = alarmDingDingRobotService.removeById(id); } @@ -209,6 +213,7 @@ public void doManage() { if (null != ms && !StringUtils.isEmpty(ms.tenant)) { pageRequest.getTarget().setTenant(ms.tenant); } + pageRequest.getTarget().setWorkspace(requestContextAdapter.getWorkspace(true)); JsonResult.createSuccessResult(result, alarmDingDingRobotService.getListByPage(pageRequest)); } diff --git a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmGroupFacadeImpl.java b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmGroupFacadeImpl.java index e75132031..6dc53ef91 100644 --- a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmGroupFacadeImpl.java +++ b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmGroupFacadeImpl.java @@ -6,8 +6,10 @@ import io.holoinsight.server.home.biz.service.AlertGroupService; import io.holoinsight.server.home.biz.service.UserOpLogService; import io.holoinsight.server.home.biz.ula.ULAFacade; +import io.holoinsight.server.home.common.service.RequestContextAdapter; import io.holoinsight.server.home.common.util.MonitorException; import io.holoinsight.server.home.common.util.scope.AuthTargetType; +import io.holoinsight.server.home.common.util.scope.MonitorCookieUtil; import io.holoinsight.server.home.common.util.scope.MonitorScope; import io.holoinsight.server.home.common.util.scope.MonitorUser; import io.holoinsight.server.home.common.util.scope.PowerConstants; @@ -58,6 +60,8 @@ public class AlarmGroupFacadeImpl extends BaseFacade { @Autowired private ULAFacade ulaFacade; + @Autowired + private RequestContextAdapter requestContextAdapter; @PostMapping("/pageQuery") @ResponseBody @@ -73,10 +77,9 @@ public void checkParameter() { @Override public void doManage() { - MonitorScope ms = RequestContext.getContext().ms; - if (null != ms && !StringUtils.isEmpty(ms.tenant)) { - pageRequest.getTarget().setTenant(ms.tenant); - } + String tenant = MonitorCookieUtil.getTenantOrException(); + pageRequest.getTarget().setTenant(tenant); + pageRequest.getTarget().setWorkspace(requestContextAdapter.getWorkspace(true)); JsonResult.createSuccessResult(result, alarmGroupService.getListByPage(pageRequest)); } }); @@ -118,6 +121,7 @@ public void doManage() { if (null != ms && !StringUtils.isEmpty(ms.tenant)) { alarmGroup.setTenant(ms.tenant); } + alarmGroup.setWorkspace(requestContextAdapter.getWorkspace(true)); alarmGroup.setGmtCreate(new Date()); alarmGroup.setGmtModified(new Date()); Long rtn = alarmGroupService.save(alarmGroup); @@ -231,7 +235,7 @@ public void doManage() { MonitorScope ms = RequestContext.getContext().ms; boolean rtn = false; AlarmGroupDTO alarmGroup = alarmGroupService.queryById(id, ms.getTenant()); - if (alarmGroup != null) { + if (alarmGroup != null && StringUtils.equals(alarmGroup.getTenant(), ms.getTenant())) { rtn = alarmGroupService.removeById(id); } diff --git a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmRuleFacadeImpl.java b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmRuleFacadeImpl.java index 3a536e1bb..e581b3874 100644 --- a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmRuleFacadeImpl.java +++ b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmRuleFacadeImpl.java @@ -13,6 +13,7 @@ import io.holoinsight.server.home.biz.service.AlertRuleService; import io.holoinsight.server.home.biz.service.AlertSubscribeService; import io.holoinsight.server.home.biz.service.UserOpLogService; +import io.holoinsight.server.home.common.service.RequestContextAdapter; import io.holoinsight.server.home.common.util.MonitorException; import io.holoinsight.server.home.common.util.scope.AuthTargetType; import io.holoinsight.server.home.common.util.scope.MonitorCookieUtil; @@ -23,6 +24,7 @@ import io.holoinsight.server.home.dal.converter.AlarmRuleConverter; import io.holoinsight.server.home.dal.mapper.CustomPluginMapper; import io.holoinsight.server.home.dal.model.AlarmRule; +import io.holoinsight.server.home.dal.model.AlarmSubscribe; import io.holoinsight.server.home.dal.model.CustomPlugin; import io.holoinsight.server.home.dal.model.OpType; import io.holoinsight.server.home.dal.model.dto.AlarmGroupDTO; @@ -90,6 +92,9 @@ public class AlarmRuleFacadeImpl extends BaseFacade { @Resource protected AlarmRuleConverter alarmRuleConverter; + @Autowired + private RequestContextAdapter requestContextAdapter; + @Value("${holoinsight.home.domain}") private String domain; @@ -420,13 +425,13 @@ protected List getRuleListByGroup(boolean myself) { List alarmRuleDTOS = new ArrayList<>(); for (AlarmGroupDTO alarmGroupDTO : listByUserLike) { - Map conditions = new HashMap<>(); - conditions.put("group_id", alarmGroupDTO.getId()); - conditions.put("tenant", MonitorCookieUtil.getTenantOrException()); - if (null != ms && !StringUtils.isEmpty(ms.workspace)) { - conditions.put("workspace", ms.getWorkspace()); - } - List alarmSubscribeInfos = alarmSubscribeService.queryByMap(conditions); + QueryWrapper alarmSubscribeQueryWrapper = new QueryWrapper<>(); + alarmSubscribeQueryWrapper.eq("group_id", alarmGroupDTO.getId()); + + requestContextAdapter.queryWrapperTenantAdapt(alarmSubscribeQueryWrapper, + MonitorCookieUtil.getTenantOrException(), requestContextAdapter.getWorkspace(false)); + List alarmSubscribeInfos = + alarmSubscribeService.queryByMap(alarmSubscribeQueryWrapper); if (CollectionUtils.isEmpty(alarmSubscribeInfos)) { continue; @@ -465,13 +470,13 @@ protected List getRuleListByGroup(boolean myself) { protected List getRuleListBySubscribe(boolean myself) { MonitorScope ms = RequestContext.getContext().ms; String userId = RequestContext.getContext().mu.getUserId(); - Map conditions = new HashMap<>(); - conditions.put("subscriber", userId); - conditions.put("tenant", MonitorCookieUtil.getTenantOrException()); - if (null != ms && !StringUtils.isEmpty(ms.workspace)) { - conditions.put("workspace", ms.getWorkspace()); - } - List alarmSubscribeInfos = alarmSubscribeService.queryByMap(conditions); + QueryWrapper alarmSubscribeQueryWrapper = new QueryWrapper<>(); + alarmSubscribeQueryWrapper.eq("subscriber", userId); + + requestContextAdapter.queryWrapperTenantAdapt(alarmSubscribeQueryWrapper, + MonitorCookieUtil.getTenantOrException(), requestContextAdapter.getWorkspace(false)); + List alarmSubscribeInfos = + alarmSubscribeService.queryByMap(alarmSubscribeQueryWrapper); if (CollectionUtils.isEmpty(alarmSubscribeInfos)) return null; diff --git a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmSubscribeFacadeImpl.java b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmSubscribeFacadeImpl.java index 79dee2c98..4f4229ec2 100644 --- a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmSubscribeFacadeImpl.java +++ b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/AlarmSubscribeFacadeImpl.java @@ -4,11 +4,12 @@ package io.holoinsight.server.home.web.controller; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Set; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import io.holoinsight.server.home.common.service.RequestContextAdapter; +import io.holoinsight.server.home.dal.model.AlarmSubscribe; import io.holoinsight.server.home.dal.model.dto.AlarmSubscribeInfo; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -49,6 +50,9 @@ public class AlarmSubscribeFacadeImpl extends BaseFacade { @Autowired private ULAFacade ulaFacade; + @Autowired + private RequestContextAdapter requestContextAdapter; + @GetMapping(value = "/queryByUniqueId/{uniqueId}") @MonitorScopeAuth(targetType = AuthTargetType.TENANT, needPower = PowerConstants.VIEW) public JsonResult queryByUniqueId(@PathVariable("uniqueId") String uniqueId) { @@ -62,13 +66,12 @@ public void checkParameter() { @Override public void doManage() { MonitorScope ms = RequestContext.getContext().ms; - Map conditions = new HashMap<>(); - conditions.put("unique_id", uniqueId); - conditions.put("tenant", MonitorCookieUtil.getTenantOrException()); - if (StringUtils.isNotBlank(ms.getWorkspace())) { - conditions.put("workspace", ms.getWorkspace()); - } - JsonResult.createSuccessResult(result, alarmSubscribeService.queryByUniqueId(conditions)); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("unique_id", uniqueId); + requestContextAdapter.queryWrapperTenantAdapt(queryWrapper, ms.getTenant(), + ms.getWorkspace()); + JsonResult.createSuccessResult(result, + alarmSubscribeService.queryByUniqueId(queryWrapper, uniqueId)); } }); return result; @@ -130,13 +133,12 @@ public void checkParameter() { @Override public void doManage() { MonitorScope ms = RequestContext.getContext().ms; - Map conditions = new HashMap<>(); - conditions.put("unique_id", uniqueId); - conditions.put("tenant", MonitorCookieUtil.getTenantOrException()); - if (StringUtils.isNotBlank(ms.getWorkspace())) { - conditions.put("workspace", ms.getWorkspace()); - } - AlarmSubscribeDTO alarmSubscribeDTO = alarmSubscribeService.queryByUniqueId(conditions); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("unique_id", uniqueId); + requestContextAdapter.queryWrapperTenantAdapt(queryWrapper, ms.getTenant(), + ms.getWorkspace()); + AlarmSubscribeDTO alarmSubscribeDTO = + alarmSubscribeService.queryByUniqueId(queryWrapper, uniqueId); if (null == alarmSubscribeDTO || CollectionUtils.isEmpty(alarmSubscribeDTO.getAlarmSubscribe())) diff --git a/server/home/home-web/src/test/java/io/holoinsight/server/home/web/controller/AlarmRuleFacadeImplTest.java b/server/home/home-web/src/test/java/io/holoinsight/server/home/web/controller/AlarmRuleFacadeImplTest.java index d4d2bde93..2ea0cc788 100644 --- a/server/home/home-web/src/test/java/io/holoinsight/server/home/web/controller/AlarmRuleFacadeImplTest.java +++ b/server/home/home-web/src/test/java/io/holoinsight/server/home/web/controller/AlarmRuleFacadeImplTest.java @@ -67,7 +67,7 @@ public void setUp() throws Exception { Mockito.when(facade.alarmGroupService.getListByUserLike("test_userId", "test_tenant")) .thenReturn(Collections.singletonList(alarmGroupDTO)); - Mockito.when(facade.alarmSubscribeService.queryByMap(Mockito.anyMap())) + Mockito.when(facade.alarmSubscribeService.queryByMap(Mockito.any())) .thenReturn(Collections.singletonList(alarmSubscribeInfo)); Mockito.when(facade.alarmRuleService.list(Mockito.any())) .thenReturn(Collections.singletonList(alarmRule)); From dd07d8fcb6ea2db85c23c66433ed3f6235a2fed7 Mon Sep 17 00:00:00 2001 From: jsy Date: Thu, 20 Jul 2023 13:25:39 +0800 Subject: [PATCH 2/2] refactor(home): add conditons when query tagvalues (#566) --- .../common/service/MetricInfoService.java | 2 ++ .../common/service/MetricInfoServiceImpl.java | 16 ++++++++++++++ .../impl/DefaultTenantInitServiceImpl.java | 3 ++- .../home/web/controller/QueryFacadeImpl.java | 8 +++++++ .../home/web/controller/SearchFacadeImpl.java | 22 ++++++++++++++++--- .../web/controller/model/TagQueryRequest.java | 4 ++++ 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/server/common/common-service/src/main/java/io/holoinsight/server/common/service/MetricInfoService.java b/server/common/common-service/src/main/java/io/holoinsight/server/common/service/MetricInfoService.java index a9114e03a..85818b039 100644 --- a/server/common/common-service/src/main/java/io/holoinsight/server/common/service/MetricInfoService.java +++ b/server/common/common-service/src/main/java/io/holoinsight/server/common/service/MetricInfoService.java @@ -30,4 +30,6 @@ public interface MetricInfoService extends IService { Map querySpmList(); + List getListByKeyword(String keyword, String tenant, String workspace); + } diff --git a/server/common/common-service/src/main/java/io/holoinsight/server/common/service/MetricInfoServiceImpl.java b/server/common/common-service/src/main/java/io/holoinsight/server/common/service/MetricInfoServiceImpl.java index 0db997f22..a657e8d3d 100644 --- a/server/common/common-service/src/main/java/io/holoinsight/server/common/service/MetricInfoServiceImpl.java +++ b/server/common/common-service/src/main/java/io/holoinsight/server/common/service/MetricInfoServiceImpl.java @@ -4,6 +4,7 @@ package io.holoinsight.server.common.service; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import io.holoinsight.server.common.dao.converter.MetricInfoConverter; import io.holoinsight.server.common.dao.entity.MetricInfo; @@ -148,4 +149,19 @@ public Map querySpmList() { }); return map; } + + @Override + public List getListByKeyword(String keyword, String tenant, String workspace) { + QueryWrapper wrapper = new QueryWrapper<>(); + if (StringUtils.isNotBlank(tenant) || "-".equalsIgnoreCase(tenant)) { + wrapper.eq("tenant", tenant); + } + if (StringUtils.isNotBlank(workspace) || "-".equalsIgnoreCase(workspace)) { + wrapper.eq("workspace", workspace); + } + + wrapper.like("metric_table", keyword); + wrapper.last("LIMIT 10"); + return metricInfoConverter.dosToDTOs(baseMapper.selectList(wrapper)); + } } diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/DefaultTenantInitServiceImpl.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/DefaultTenantInitServiceImpl.java index 7fb062f2f..25870da2f 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/DefaultTenantInitServiceImpl.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/DefaultTenantInitServiceImpl.java @@ -11,6 +11,7 @@ import io.holoinsight.server.home.dal.model.dto.CloudMonitorRange; import io.holoinsight.server.query.grpc.QueryProto.QueryFilter; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -62,7 +63,7 @@ public Map getTenantWorkspaceMetaConditions(String workspace) { @Override public List getTenantFilters(String workspace) { - return null; + return new ArrayList<>(); } @Override diff --git a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/QueryFacadeImpl.java b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/QueryFacadeImpl.java index 2cf9fe303..a3050f564 100644 --- a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/QueryFacadeImpl.java +++ b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/QueryFacadeImpl.java @@ -298,6 +298,14 @@ public void doManage() { List tenantFilters = tenantInitService.getTenantFilters(ms.getWorkspace()); + + if (!CollectionUtils.isEmpty(tagQueryRequest.getConditions())) { + tagQueryRequest.getConditions().forEach((k, v) -> { + tenantFilters.add(QueryProto.QueryFilter.newBuilder().setName(k).setType("literal_or") + .setValue(v).build()); + }); + } + if (!CollectionUtils.isEmpty(tenantFilters)) { builder.addAllFilters(tenantFilters); } diff --git a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/SearchFacadeImpl.java b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/SearchFacadeImpl.java index 1d6788374..0cc20a230 100644 --- a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/SearchFacadeImpl.java +++ b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/SearchFacadeImpl.java @@ -3,6 +3,8 @@ */ package io.holoinsight.server.home.web.controller; +import io.holoinsight.server.common.dao.entity.dto.MetricInfoDTO; +import io.holoinsight.server.common.service.MetricInfoService; import io.holoinsight.server.home.biz.service.AlertRuleService; import io.holoinsight.server.home.biz.service.CustomPluginService; import io.holoinsight.server.home.biz.service.DashboardService; @@ -35,6 +37,7 @@ import java.util.Map; import java.util.concurrent.Future; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * @@ -60,6 +63,9 @@ public class SearchFacadeImpl extends BaseFacade { @Autowired private AlertRuleService alarmRuleService; + @Autowired + private MetricInfoService metricInfoService; + @Autowired private DataClientService dataClientService; @@ -89,7 +95,6 @@ public void doManage() { List ret = new ArrayList<>(); List> futures = new ArrayList<>(); futures.add(queryThreadPool.submit(() -> { - log.info(">>>>>, " + tenant + ", ..... , " + workspace); return searchLogEntity(req.keyword, tenant, workspace); })); @@ -113,6 +118,10 @@ public void doManage() { return searchAlarmEntity(req.keyword, tenant, workspace); })); + futures.add(queryThreadPool.submit(() -> { + return searchLogMetricEntity(req.keyword, tenant, workspace); + })); + // 多线程 for (Future future : futures) { try { @@ -208,8 +217,6 @@ public SearchKeywordRet searchDashboardEntity(String keyword, String tenant, Str public SearchKeywordRet searchInfraEntity(String keyword, String tenant, String workspace) { QueryExample queryExample = new QueryExample(); - // queryExample.getParams().put("ip", - // Pattern.compile(String.format("^.*%s.*$", keyword), Pattern.CASE_INSENSITIVE)); queryExample.getParams().put("hostname", Pattern.compile(String.format("^.*%s.*$", keyword), Pattern.CASE_INSENSITIVE)); if (StringUtils.isNotBlank(workspace)) { @@ -245,6 +252,15 @@ public SearchKeywordRet searchAlarmEntity(String keyword, String tenant, String return genSearchResult("alarm", alarmRuleService.getListByKeyword(keyword, tenant, workspace)); } + public SearchKeywordRet searchLogMetricEntity(String keyword, String tenant, String workspace) { + List listByKeyword = + metricInfoService.getListByKeyword(keyword, tenant, workspace); + List logList = + listByKeyword.stream().filter(item -> item.getProduct().equalsIgnoreCase("logmonitor")) + .collect(Collectors.toList()); + return genSearchResult("logMetric", logList); + } + public SearchKeywordRet genSearchResult(String type, List datas) { SearchKeywordRet skr = new SearchKeywordRet(); skr.count = datas.size(); diff --git a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/model/TagQueryRequest.java b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/model/TagQueryRequest.java index 4a573fff1..a405f361d 100644 --- a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/model/TagQueryRequest.java +++ b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/model/TagQueryRequest.java @@ -5,6 +5,8 @@ import lombok.Data; +import java.util.Map; + /** * @author wangsiyuan * @date 2022/8/16 7:00 下午 @@ -15,4 +17,6 @@ public class TagQueryRequest { private String metric; private String key; + + private Map conditions; }