From 55e3b4a44e2786ccbbc42a39922b7ecb2b0493a6 Mon Sep 17 00:00:00 2001 From: huangweixiong Date: Wed, 24 Aug 2022 15:00:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E5=85=B3=E9=87=8D=E5=90=AF=E9=80=82?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MonitorControllerV2.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorControllerV2.java b/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorControllerV2.java index fbe61a17..c9c2eccf 100644 --- a/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorControllerV2.java +++ b/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorControllerV2.java @@ -1,6 +1,7 @@ package io.renren.modules.gateway.controller; +import cn.hutool.core.io.FileUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.renren.modules.gateway.dao.ApiCountHistoryDao; @@ -32,6 +33,7 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -73,10 +75,7 @@ public class MonitorControllerV2 { @Autowired private ApiCountHistoryDao apiCountHistoryDao; - @GetMapping("/queryGroupByAbility") - @ApiOperation("统计数据按能力归集") - public Result queryGroupByAbility(Long start, Long end) throws InterruptedException, ExecutionException, TimeoutException { - + public List queryMetricCount(String queryFormat, Long start, Long end, String metricElement, int limit) throws InterruptedException, ExecutionException, TimeoutException { String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}&time={time}"; //查询重启记录时间节点 @@ -98,7 +97,7 @@ public class MonitorControllerV2 { HashMap map = new HashMap(); Long startTime = timePoint.get(i); Long endTime = timePoint.get(i+1); - String query = String.format("topk(10, sum(label_replace(increase(apigateway_http_status[%ds:1s]), \"groupInfo\", \"$2\", \"matched_uri\", \"/juapi/(.*?)/(.*?)/.*\")) by (groupInfo))", endTime - startTime); + String query = String.format(queryFormat, endTime - startTime); futures.add(CompletableFuture.supplyAsync(() -> { ResponseEntity entity = restTemplate.getForEntity(url, HashMap.class, query, endTime); @@ -129,13 +128,13 @@ public class MonitorControllerV2 { for (HashMap hashMap : result) { Map metric = (Map) hashMap.get("metric"); List value = (List) hashMap.get("value"); - if (metric == null || metric.get("groupInfo") == null || value == null || value.size() != 2) { + if (metric == null || metric.get(metricElement) == null || value == null || value.size() != 2) { continue; } try { metric.put("count", Double.valueOf((String) value.get(1)).intValue()); - Long groupInfo = Long.valueOf((String) metric.get("groupInfo")); - metric.put("groupInfo", groupInfo); + Long groupInfo = Long.valueOf((String) metric.get(metricElement)); + metric.put(metricElement, groupInfo); results.add(metric); } catch (Exception e) { //忽略 @@ -152,7 +151,7 @@ public class MonitorControllerV2 { for (CompletableFuture> future : futures) { List> list = (List>) future.get(30, TimeUnit.SECONDS); list.forEach(item -> { - Long groupInfo = (Long) item.get("groupInfo"); + Long groupInfo = (Long) item.get(metricElement); if (filterMap.containsKey(groupInfo)) { Map objectMap = filterMap.get(groupInfo); Integer count = (Integer) objectMap.get("count"); @@ -167,11 +166,22 @@ public class MonitorControllerV2 { List> results = filterMap.values().stream() .sorted(Comparator.comparingInt(item -> (int) ((Map) item).get("count")) .reversed()) - .limit(10) + .limit(limit) .collect(Collectors.toList()); + return results; + } + + @GetMapping("/queryGroupByAbility") + @ApiOperation("统计数据按能力归集") + public Result queryGroupByAbility(Long start, Long end) throws InterruptedException, ExecutionException, TimeoutException { + + String queryFormat = "topk(10, sum(label_replace(increase(apigateway_http_status[%ds:1s]), \"groupInfo\", \"$2\", \"matched_uri\", \"/juapi/(.*?)/(.*?)/.*\")) by (groupInfo))"; + List> results = queryMetricCount(queryFormat, start, end, "groupInfo", 10); + //数据聚合 if (!results.isEmpty()) { + List ids = results.stream().map(item -> item.get("groupInfo")).collect(Collectors.toList()); LambdaQueryWrapper queryWrapper = new QueryWrapper().lambda(); queryWrapper .select(ResourceEntity::getId, @@ -179,7 +189,7 @@ public class MonitorControllerV2 { ResourceEntity::getApiMethodType, ResourceEntity::getType, ResourceEntity::getApiUrl) - .in(ResourceEntity::getId, filterMap.keySet()); + .in(ResourceEntity::getId, ids); List entities = resourceDao.selectList(queryWrapper); for (Map map : results) { Long groupInfo = (Long) map.get("groupInfo"); @@ -212,6 +222,7 @@ public class MonitorControllerV2 { @GetMapping("/queryGroupByDepartment") @ApiOperation("统计数据按部门归集") public Result queryGroupByDepartment(String query, String time){ + /** ================*/ String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}" + "&time=" + time; ResponseEntity entity = restTemplate.getForEntity(url, HashMap.class, query); HashMap body = entity.getBody();