网关重启适配
This commit is contained in:
parent
9bcdd78bae
commit
c51e335f9f
|
@ -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<String> querys = new ArrayList<>();
|
||||
List<CompletableFuture<List<?>>> futures = querys.stream().map(item -> {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
|
||||
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query, time);
|
||||
//查询重启记录时间节点
|
||||
LambdaQueryWrapper<ApiCountHistoryEntity> historyEntityLambdaQueryWrapper = new QueryWrapper<ApiCountHistoryEntity>().lambda()
|
||||
.select(ApiCountHistoryEntity::getUpdateTime)
|
||||
.ge(ApiCountHistoryEntity::getUpdateTime, new Date(start * 1000))
|
||||
.le(ApiCountHistoryEntity::getUpdateTime, new Date(end * 1000))
|
||||
.orderByAsc(ApiCountHistoryEntity::getUpdateTime);
|
||||
List<ApiCountHistoryEntity> apiCountHistoryEntities = apiCountHistoryDao.selectList(historyEntityLambdaQueryWrapper);
|
||||
List<Long> timePoint = apiCountHistoryEntities.stream()
|
||||
.map(item -> item.getUpdateTime().getTime()/1000)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
timePoint.add(0, start);
|
||||
timePoint.add(end);
|
||||
//根据时间拆分,按照时间段异步请求
|
||||
List<CompletableFuture<List<?>>> futures = new ArrayList<>();
|
||||
for (int i = 0; i < timePoint.size()-1; i++) {
|
||||
HashMap<String, Long> map = new HashMap<String, Long>();
|
||||
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<HashMap> 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,42 +145,32 @@ public class MonitorControllerV2 {
|
|||
return results;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}, executor);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
HashMap<String, Map<String, Object>> filterMap = new HashMap<String, Map<String, Object>>();
|
||||
}, executor));
|
||||
}
|
||||
//不同时间段的数据相加
|
||||
HashMap<Long, Map<String, Object>> filterMap = new HashMap<Long, Map<String, Object>>();
|
||||
for (CompletableFuture<List<?>> future : futures) {
|
||||
List<Map<String, Object>> list = (List<Map<String, Object>>) future.get(30, TimeUnit.SECONDS);
|
||||
list.forEach(item -> {
|
||||
// filterMap.containsKey(item.)
|
||||
Long groupInfo = (Long) item.get("groupInfo");
|
||||
if (filterMap.containsKey(groupInfo)) {
|
||||
Map<String, Object> 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<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query, time);
|
||||
HashMap body = entity.getBody();
|
||||
HashMap data = (HashMap) body.get("data");
|
||||
if (data != null){
|
||||
List<HashMap> result = (List) data.get("result");
|
||||
ArrayList<Map> results = new ArrayList<>();
|
||||
ArrayList<Long> 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<Map<String, Object>> results = filterMap.values().stream()
|
||||
.sorted(Comparator.comparingInt(item -> (int) ((Map) item).get("count"))
|
||||
.reversed())
|
||||
.limit(10)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
//数据聚合
|
||||
if (!results.isEmpty()) {
|
||||
LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda();
|
||||
queryWrapper
|
||||
|
@ -163,7 +179,7 @@ public class MonitorControllerV2 {
|
|||
ResourceEntity::getApiMethodType,
|
||||
ResourceEntity::getType,
|
||||
ResourceEntity::getApiUrl)
|
||||
.in(ResourceEntity::getId, abilityIds);
|
||||
.in(ResourceEntity::getId, filterMap.keySet());
|
||||
List<ResourceEntity> entities = resourceDao.selectList(queryWrapper);
|
||||
for (Map map : results) {
|
||||
Long groupInfo = (Long) map.get("groupInfo");
|
||||
|
@ -190,9 +206,7 @@ public class MonitorControllerV2 {
|
|||
}
|
||||
|
||||
return Result.success(results);
|
||||
}
|
||||
|
||||
return Result.success(Collections.emptyList());
|
||||
}
|
||||
|
||||
@GetMapping("/queryGroupByDepartment")
|
||||
|
|
Loading…
Reference in New Issue