From c51e335f9fe2f00a7f57a2cae97c73357e04db7d Mon Sep 17 00:00:00 2001 From: huangweixiong Date: Mon, 22 Aug 2022 11:11:02 +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 | 150 ++++++++++-------- 1 file changed, 82 insertions(+), 68 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 2521d151..fbe61a17 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 @@ -3,6 +3,8 @@ package io.renren.modules.gateway.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import io.renren.modules.gateway.dao.ApiCountHistoryDao; +import io.renren.modules.gateway.entity.ApiCountHistoryEntity; import io.renren.modules.gateway.service.MonitorServiceV2; import io.renren.modules.monitor.entity.Result; import io.renren.modules.resource.dao.AttrDao; @@ -37,6 +39,7 @@ import java.net.URLConnection; import java.util.*; import java.util.concurrent.*; import java.util.stream.Collectors; +import java.util.stream.Stream; @RestController @Api(tags = "网关统计") @@ -67,16 +70,38 @@ public class MonitorControllerV2 { @Autowired private MonitorServiceV2 monitorServiceV2; + @Autowired + private ApiCountHistoryDao apiCountHistoryDao; + @GetMapping("/queryGroupByAbility") @ApiOperation("统计数据按能力归集") - public Result queryGroupByAbility(String query, String time) throws InterruptedException, ExecutionException, TimeoutException { + public Result queryGroupByAbility(Long start, Long end) throws InterruptedException, ExecutionException, TimeoutException { String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}&time={time}"; - List querys = new ArrayList<>(); - List>> futures = querys.stream().map(item -> { - return CompletableFuture.supplyAsync(() -> { - ResponseEntity entity = restTemplate.getForEntity(url, HashMap.class, query, time); + //查询重启记录时间节点 + LambdaQueryWrapper historyEntityLambdaQueryWrapper = new QueryWrapper().lambda() + .select(ApiCountHistoryEntity::getUpdateTime) + .ge(ApiCountHistoryEntity::getUpdateTime, new Date(start * 1000)) + .le(ApiCountHistoryEntity::getUpdateTime, new Date(end * 1000)) + .orderByAsc(ApiCountHistoryEntity::getUpdateTime); + List apiCountHistoryEntities = apiCountHistoryDao.selectList(historyEntityLambdaQueryWrapper); + List timePoint = apiCountHistoryEntities.stream() + .map(item -> item.getUpdateTime().getTime()/1000) + .distinct() + .collect(Collectors.toList()); + timePoint.add(0, start); + timePoint.add(end); + //根据时间拆分,按照时间段异步请求 + List>> futures = new ArrayList<>(); + for (int i = 0; i < timePoint.size()-1; i++) { + 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); + futures.add(CompletableFuture.supplyAsync(() -> { + + ResponseEntity entity = restTemplate.getForEntity(url, HashMap.class, query, endTime); /** 接口数据示例 * { * "status": "success", @@ -108,6 +133,7 @@ public class MonitorControllerV2 { continue; } try { + metric.put("count", Double.valueOf((String) value.get(1)).intValue()); Long groupInfo = Long.valueOf((String) metric.get("groupInfo")); metric.put("groupInfo", groupInfo); results.add(metric); @@ -119,80 +145,68 @@ public class MonitorControllerV2 { return results; } return Collections.emptyList(); - }, executor); - }).collect(Collectors.toList()); - - HashMap> filterMap = new HashMap>(); + }, executor)); + } + //不同时间段的数据相加 + HashMap> filterMap = new HashMap>(); for (CompletableFuture> future : futures) { List> list = (List>) future.get(30, TimeUnit.SECONDS); list.forEach(item -> { -// filterMap.containsKey(item.) + Long groupInfo = (Long) item.get("groupInfo"); + if (filterMap.containsKey(groupInfo)) { + Map objectMap = filterMap.get(groupInfo); + Integer count = (Integer) objectMap.get("count"); + Integer itemCount = (Integer) item.get("count"); + objectMap.put("count", count + itemCount); + }else { + filterMap.put(groupInfo, item); + } }); } - ResponseEntity entity = restTemplate.getForEntity(url, HashMap.class, query, time); - HashMap body = entity.getBody(); - HashMap data = (HashMap) body.get("data"); - if (data != null){ - List result = (List) data.get("result"); - ArrayList results = new ArrayList<>(); - ArrayList abilityIds = new ArrayList<>(); - for (HashMap hashMap : result) { - Map metric = (Map) hashMap.get("metric"); - if (metric != null && metric.get("groupInfo") != null){ - List value = (List) hashMap.get("value"); - if (value.size() == 2){ - metric.put("count", value.get(1)); - try{ - Long groupInfo = Long.valueOf((String) metric.get("groupInfo")); - abilityIds.add(groupInfo); - metric.put("groupInfo", groupInfo); - results.add(metric); - }catch (Exception e) { - //忽略 - } + //重新排序 + List> results = filterMap.values().stream() + .sorted(Comparator.comparingInt(item -> (int) ((Map) item).get("count")) + .reversed()) + .limit(10) + .collect(Collectors.toList()); + //数据聚合 + if (!results.isEmpty()) { + LambdaQueryWrapper queryWrapper = new QueryWrapper().lambda(); + queryWrapper + .select(ResourceEntity::getId, + ResourceEntity::getName, + ResourceEntity::getApiMethodType, + ResourceEntity::getType, + ResourceEntity::getApiUrl) + .in(ResourceEntity::getId, filterMap.keySet()); + List entities = resourceDao.selectList(queryWrapper); + for (Map map : results) { + Long groupInfo = (Long) map.get("groupInfo"); + for (ResourceEntity resourceEntity : entities) { + if (groupInfo != null && groupInfo.equals(resourceEntity.getId())) { + map.put("name", resourceEntity.getName()); + map.put("ApiMethodType", resourceEntity.getApiMethodType()); + map.put("type", resourceEntity.getType()); + map.put("apiUrl", resourceEntity.getApiUrl()); + LambdaQueryWrapper attrQueryWrapper = new LambdaQueryWrapper<>(); + attrQueryWrapper.select(AttrEntity::getAttrType,AttrEntity::getAttrValue) + .eq(AttrEntity::getDataResourceId, groupInfo) + .eq(AttrEntity::getAttrType,"服务商名") + .eq(AttrEntity::getDelFlag, 0); + AttrEntity attrEntity = attrDao.selectOne(attrQueryWrapper); + if (attrEntity.getAttrValue() != null) { + map.put("privider", attrEntity.getAttrValue()); + } + entities.remove(resourceEntity); + break; } } } - - if (!results.isEmpty()) { - LambdaQueryWrapper queryWrapper = new QueryWrapper().lambda(); - queryWrapper - .select(ResourceEntity::getId, - ResourceEntity::getName, - ResourceEntity::getApiMethodType, - ResourceEntity::getType, - ResourceEntity::getApiUrl) - .in(ResourceEntity::getId, abilityIds); - List entities = resourceDao.selectList(queryWrapper); - for (Map map : results) { - Long groupInfo = (Long) map.get("groupInfo"); - for (ResourceEntity resourceEntity : entities) { - if (groupInfo != null && groupInfo.equals(resourceEntity.getId())) { - map.put("name", resourceEntity.getName()); - map.put("ApiMethodType", resourceEntity.getApiMethodType()); - map.put("type", resourceEntity.getType()); - map.put("apiUrl", resourceEntity.getApiUrl()); - LambdaQueryWrapper attrQueryWrapper = new LambdaQueryWrapper<>(); - attrQueryWrapper.select(AttrEntity::getAttrType,AttrEntity::getAttrValue) - .eq(AttrEntity::getDataResourceId,groupInfo) - .eq(AttrEntity::getAttrType,"服务商名") - .eq(AttrEntity::getDelFlag, 0); - AttrEntity attrEntity = attrDao.selectOne(attrQueryWrapper); - if (attrEntity.getAttrValue() != null) { - map.put("privider", attrEntity.getAttrValue()); - } - entities.remove(resourceEntity); - break; - } - } - } - } - - return Result.success(results); } - return Result.success(Collections.emptyList()); + return Result.success(results); + } @GetMapping("/queryGroupByDepartment")