From 7fb34392fffb0f274f10b38140fc267016b8ba50 Mon Sep 17 00:00:00 2001 From: masaimu Date: Mon, 11 Mar 2024 12:03:17 +0800 Subject: [PATCH] fix: query schema (#815) --- .../server/home/biz/common/MetaDictKey.java | 1 + .../home/web/controller/QueryFacadeImpl.java | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/common/MetaDictKey.java b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/common/MetaDictKey.java index b0da6d64b..0ce92ab57 100644 --- a/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/common/MetaDictKey.java +++ b/server/home/home-service/src/main/java/io/holoinsight/server/home/biz/common/MetaDictKey.java @@ -58,4 +58,5 @@ public class MetaDictKey { public static final String METERING_HOLOINSIGHT_SUBMIT_OPEN = "metering_holoinsight_submit_open"; public static final String INTEGRATION_LOCAL_PRODUCT = "integration_local_product"; public static final String NETWORK_MODE = "networkMode"; + public static final String SCHEMA_METRIC_TABLE_PREFIX = "schema_metric_table_prefix"; } 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 b70d11259..940d218a0 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 @@ -3,6 +3,7 @@ */ package io.holoinsight.server.home.web.controller; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.holoinsight.server.common.J; import io.holoinsight.server.common.JsonResult; import com.google.protobuf.InvalidProtocolBufferException; @@ -11,6 +12,7 @@ import io.holoinsight.server.common.LatchWork; import io.holoinsight.server.common.UtilMisc; import io.holoinsight.server.common.dao.entity.MetricInfo; +import io.holoinsight.server.common.dao.mapper.MetricInfoMapper; import io.holoinsight.server.common.service.SuperCacheService; import io.holoinsight.server.common.threadpool.CommonThreadPools; import io.holoinsight.server.home.biz.common.MetaDictUtil; @@ -62,6 +64,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; +import javax.annotation.Resource; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -73,6 +76,11 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import static io.holoinsight.server.home.biz.common.MetaDictKey.SCHEMA_METRIC_TABLE_PREFIX; +import static io.holoinsight.server.home.biz.common.MetaDictType.GLOBAL_CONFIG; +import static io.holoinsight.server.home.task.MetricCrawlerConstant.GLOBAL_TENANT; +import static io.holoinsight.server.home.task.MetricCrawlerConstant.GLOBAL_WORKSPACE; + @RestController @RequestMapping("/webapi/v1/query") @TokenUrls("/webapi/v1/query") @@ -96,6 +104,8 @@ public class QueryFacadeImpl extends BaseFacade { @Autowired private ParameterSecurityService parameterSecurityService; + @Resource + private MetricInfoMapper metricInfoMapper; @PostMapping @MonitorScopeAuth(targetType = AuthTargetType.TENANT, needPower = PowerConstants.VIEW) @@ -272,6 +282,17 @@ public void checkParameter() {} @Override public void doManage() { + if (useGlobalMetricInfo(metric)) { + MetricInfo metricInfo = getGlobalMetricInfo(metric); + if (metricInfo != null) { + List tags = getTagsFromMetricInfo(metricInfo); + KeyResult keyResult = new KeyResult(); + keyResult.setMetric(metric); + keyResult.setTags(tags); + result.setData(keyResult); + return; + } + } MonitorScope ms = RequestContext.getContext().ms; Builder builder = Datasource.newBuilder().setMetric(metric) .setStart(System.currentTimeMillis() - 60000 * 60 * 5) @@ -305,6 +326,40 @@ public void doManage() { return result; } + private List getTagsFromMetricInfo(MetricInfo metricInfo) { + String tags = metricInfo.getTags(); + if (StringUtils.isBlank(tags)) { + return Collections.emptyList(); + } + return J.toList(tags); + } + + private MetricInfo getGlobalMetricInfo(String metric) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("tenant", GLOBAL_TENANT); + queryWrapper.eq("workspace", GLOBAL_WORKSPACE); + queryWrapper.eq("metric_table", metric); + List metricInfos = metricInfoMapper.selectList(queryWrapper); + if (CollectionUtils.isEmpty(metricInfos)) { + return null; + } + return metricInfos.get(0); + } + + private boolean useGlobalMetricInfo(String metric) { + List schemaMetricTablePrefix = + MetaDictUtil.getList(GLOBAL_CONFIG, SCHEMA_METRIC_TABLE_PREFIX); + if (CollectionUtils.isEmpty(schemaMetricTablePrefix)) { + return false; + } + for (String prefix : schemaMetricTablePrefix) { + if (metric.startsWith(prefix)) { + return true; + } + } + return false; + } + /** * 模糊匹配查询 *