From d2988369255d2cbe7156438015516242bf69de80 Mon Sep 17 00:00:00 2001 From: "saimu.msm" Date: Tue, 5 Mar 2024 16:36:48 +0800 Subject: [PATCH] alert rule template fix page overview --- .../HoloinsightHomeConfiguration.java | 3 +- .../common/service/RequestContextAdapter.java | 2 + .../service/RequestContextAdapterImpl.java | 5 ++ .../service/query/QueryDetailResponse.java | 45 ++++++++++ .../service/impl/AlertRuleServiceImpl.java | 82 +++++++++++++++---- .../service/impl/UserOpLogServiceImpl.java | 1 - .../controller/UserFavoriteFacadeImpl.java | 9 ++ 7 files changed, 128 insertions(+), 19 deletions(-) diff --git a/server/home/home-boot/src/main/java/io/holoinsight/server/home/bootstrap/HoloinsightHomeConfiguration.java b/server/home/home-boot/src/main/java/io/holoinsight/server/home/bootstrap/HoloinsightHomeConfiguration.java index d062d6e75..b16407a2f 100644 --- a/server/home/home-boot/src/main/java/io/holoinsight/server/home/bootstrap/HoloinsightHomeConfiguration.java +++ b/server/home/home-boot/src/main/java/io/holoinsight/server/home/bootstrap/HoloinsightHomeConfiguration.java @@ -20,6 +20,7 @@ import io.holoinsight.server.home.biz.plugin.MarketplaceProductHandler; import io.holoinsight.server.home.biz.plugin.MetricInfoCheckService; import io.holoinsight.server.home.biz.plugin.MetricInfoCheckServiceImpl; +import io.holoinsight.server.home.biz.service.AlertRuleService; import io.holoinsight.server.home.biz.service.EnvironmentService; import io.holoinsight.server.home.biz.service.TenantInitService; import io.holoinsight.server.home.biz.service.UserinfoVerificationService; @@ -113,7 +114,7 @@ public ProductCtlService productCtlService() { } @Bean - public AlertRuleServiceImpl alertRuleService() { + public AlertRuleService alertRuleService() { return new AlertRuleServiceImpl(); } 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 14ef09a56..d7849bf9d 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 @@ -25,4 +25,6 @@ public interface RequestContextAdapter { void queryWrapperWorkspaceAdapt(QueryWrapper queryWrapper, String workspace); String getWorkspace(boolean cross); + + String getLoginName(); } 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 78241a594..eeb224631 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 @@ -61,4 +61,9 @@ public String getWorkspace(boolean cross) { MonitorScope ms = RequestContext.getContext().ms; return ms.getWorkspace(); } + + @Override + public String getLoginName() { + return StringUtils.EMPTY; + } } diff --git a/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/query/QueryDetailResponse.java b/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/query/QueryDetailResponse.java index d55c03917..06e889563 100644 --- a/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/query/QueryDetailResponse.java +++ b/server/home/home-common/src/main/java/io/holoinsight/server/home/common/service/query/QueryDetailResponse.java @@ -4,6 +4,7 @@ package io.holoinsight.server.home.common.service.query; import lombok.Data; +import org.apache.commons.lang3.StringUtils; import org.springframework.util.CollectionUtils; import java.util.ArrayList; @@ -54,5 +55,49 @@ public void distinct() { } this.values = distinctValues; } + + public void sort(String order, String... priorities) { + if (CollectionUtils.isEmpty(headers) || CollectionUtils.isEmpty(values)) { + return; + } + int index = -1; + for (String priority : priorities) { + for (int i = 0; i < headers.size(); i++) { + if (StringUtils.equals(headers.get(i), priority) && validData(i)) { + index = i; + break; + } + } + } + if (index < 0) { + return; + } + int finalIndex = index; + values.sort((o1, o2) -> { + Double d1 = Double.parseDouble(String.valueOf(o1[finalIndex])); + Double d2 = Double.parseDouble(String.valueOf(o2[finalIndex])); + if (StringUtils.equalsIgnoreCase(order, "asc")) { + return d1.compareTo(d2); + } else { + return d2.compareTo(d1); + } + }); + } + + private boolean validData(int i) { + for (Object[] value : values) { + if (value.length <= i) { + return false; + } + if (!(value[i] instanceof Number)) { + return false; + } + Double d = ((Number) value[i]).doubleValue(); + if (!d.equals(0d)) { + return true; + } + } + return false; + } } } 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 bb506b8b6..f6f208ed3 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 @@ -18,14 +18,21 @@ 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.Rule; import io.holoinsight.server.home.facade.page.MonitorPageRequest; import io.holoinsight.server.home.facade.page.MonitorPageResult; +import io.holoinsight.server.home.facade.trigger.DataSource; +import io.holoinsight.server.home.facade.trigger.Filter; +import io.holoinsight.server.home.facade.trigger.Trigger; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; @@ -35,6 +42,7 @@ * @author wangsiyuan * @date 2022/4/1 10:44 上午 */ +@Slf4j public class AlertRuleServiceImpl extends ServiceImpl implements AlertRuleService { @@ -49,10 +57,7 @@ public class AlertRuleServiceImpl extends ServiceImpl getListByKeyword(String keyword, String tenant, String wrapper.last("LIMIT 10"); return alarmRuleConverter.dosToDTOs(baseMapper.selectList(wrapper)); } + + public static void genMd5(AlarmRuleDTO alarmRuleDTO) { + + AlarmRuleDTO md5Rule = new AlarmRuleDTO(); + BeanUtils.copyProperties(alarmRuleDTO, md5Rule, "id", "gmtCreate", "gmtModified", "creator", + "modifier", "status", "isMerge", "mergeType", "noticeType", "alarmContent", "tenant", + "workspace", "sourceType", "sourceId", "envType", "alertNotificationTemplateId"); + if (md5Rule.getExtra() != null) { + md5Rule.getExtra().md5 = StringUtils.EMPTY; + } + if (!CollectionUtils.isEmpty(md5Rule.getRule())) { + Rule rule = J.fromJson(J.toJson(md5Rule.getRule()), Rule.class); + if (!CollectionUtils.isEmpty(rule.getTriggers())) { + for (Trigger trigger : rule.getTriggers()) { + markMetric(trigger, alarmRuleDTO.getSourceType()); + } + } + md5Rule.setRule(J.toMap(J.toJson(rule))); + } + String md5 = MD5Hash.getMD5(J.toJson(md5Rule)); + log.info("generate md5 {} for rule {}", md5, J.toJson(md5Rule)); + if (alarmRuleDTO.getExtra() == null) { + alarmRuleDTO.setExtra(new AlertRuleExtra()); + } + alarmRuleDTO.getExtra().md5 = md5; + } + + private static void markMetric(Trigger trigger, String sourceType) { + if (CollectionUtils.isEmpty(trigger.getDatasources())) { + return; + } + if (StringUtils.isEmpty(sourceType) || !sourceType.startsWith("template")) { + return; + } + for (DataSource dataSource : trigger.getDatasources()) { + if (StringUtils.isEmpty(dataSource.getMetric())) { + continue; + } + String[] metricArr = dataSource.getMetric().split("_"); + String suffix = metricArr[metricArr.length - 1]; + if (StringUtils.isNumeric(suffix) || "${appId}".equals(suffix)) { + String[] newMetricArr = new String[metricArr.length - 1]; + System.arraycopy(metricArr, 0, newMetricArr, 0, metricArr.length - 1); + dataSource.setMetric(String.join("_", Arrays.asList(newMetricArr))); + } + List filters = dataSource.getFilters(); + if (!CollectionUtils.isEmpty(filters)) { + for (Filter filter : filters) { + if (StringUtils.equals(filter.getName(), "appId")) { + filter.setValue(StringUtils.EMPTY); + } + } + } + } + } } diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/UserOpLogServiceImpl.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/UserOpLogServiceImpl.java index 4df60230e..a599b6112 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/UserOpLogServiceImpl.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/service/impl/UserOpLogServiceImpl.java @@ -95,7 +95,6 @@ public MonitorPageResult getListByPage( wrapper.orderByDesc("gmt_create"); - wrapper.select(UserOpLog.class, info -> !info.getColumn().equals("creator")); Page page = new Page<>(userOpLogRequest.getPageNum(), userOpLogRequest.getPageSize()); diff --git a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/UserFavoriteFacadeImpl.java b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/UserFavoriteFacadeImpl.java index aaa71f0a5..0e4fd63f3 100644 --- a/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/UserFavoriteFacadeImpl.java +++ b/server/home/home-web/src/main/java/io/holoinsight/server/home/web/controller/UserFavoriteFacadeImpl.java @@ -10,6 +10,7 @@ import io.holoinsight.server.home.biz.service.TenantInitService; import io.holoinsight.server.home.biz.service.UserFavoriteService; import io.holoinsight.server.home.biz.service.UserOpLogService; +import io.holoinsight.server.home.common.service.RequestContextAdapter; import io.holoinsight.server.home.common.service.SpringContext; import io.holoinsight.server.home.common.util.MonitorException; import io.holoinsight.server.home.common.util.ResultCodeEnum; @@ -92,6 +93,9 @@ public class UserFavoriteFacadeImpl extends BaseFacade { @Autowired private ParameterSecurityService parameterSecurityService; + @Autowired + private RequestContextAdapter requestContextAdapter; + @PostMapping("/create") @ResponseBody @MonitorScopeAuth(targetType = AuthTargetType.TENANT, needPower = PowerConstants.EDIT) @@ -323,6 +327,11 @@ public void doManage() { if (null != ms && !StringUtil.isBlank(ms.workspace)) { userFavoriteRequest.getTarget().setWorkspace(ms.workspace); } + if (StringUtils.isEmpty(userFavoriteRequest.getTarget().getUserLoginName())) { + if (StringUtils.isNotEmpty(requestContextAdapter.getLoginName())) { + userFavoriteRequest.getTarget().setUserLoginName(requestContextAdapter.getLoginName()); + } + } JsonResult.createSuccessResult(result, userFavoriteService.getListByPage(userFavoriteRequest)); }