diff --git a/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorController.java b/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorController.java index a136cc11..2df9b4b6 100644 --- a/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorController.java +++ b/renren-admin/src/main/java/io/renren/modules/gateway/controller/MonitorController.java @@ -1,8 +1,6 @@ package io.renren.modules.gateway.controller; -import cn.hutool.core.net.URLEncoder; -import cn.hutool.core.util.URLUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.renren.modules.gateway.service.MonitorServiceV2; @@ -15,19 +13,16 @@ import io.renren.modules.sys.dao.SysDeptDao; import io.renren.modules.sys.entity.SysDeptEntity; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import lombok.extern.log4j.Log4j; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.*; -import org.springframework.http.converter.ByteArrayHttpMessageConverter; -import org.springframework.stereotype.Controller; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.HttpMessageConverterExtractor; -import org.springframework.web.client.RequestCallback; import org.springframework.web.client.RestTemplate; import javax.servlet.ServletOutputStream; @@ -66,14 +61,14 @@ public class MonitorController { @Autowired private MonitorServiceV2 monitorServiceV2; -// @RequestMapping("/metrics/**") + // @RequestMapping("/metrics/**") void forward(HttpServletRequest request, HttpServletResponse response) throws IOException { //类似nginx匹配前缀 String location = "/juapi"; String requestURI = request.getRequestURI(); - requestURI = requestURI.replace( context_path,""); - String url = gatewayDomain + location + requestURI ; + requestURI = requestURI.replace(context_path, ""); + String url = gatewayDomain + location + requestURI; if (StringUtils.isNotBlank(request.getQueryString())) { url = url + "?" + request.getQueryString(); } @@ -81,7 +76,7 @@ public class MonitorController { HttpHeaders headers = new HttpHeaders(); Enumeration headerNames = request.getHeaderNames(); - while (headerNames.hasMoreElements()){ + while (headerNames.hasMoreElements()) { String element = headerNames.nextElement(); headers.add(element, request.getHeader(element)); } @@ -89,17 +84,17 @@ public class MonitorController { InputStream in = request.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = 1024; - byte tmp [] = new byte[len]; - int i ; - while((i=in.read(tmp, 0, len))>0){ + byte tmp[] = new byte[len]; + int i; + while ((i = in.read(tmp, 0, len)) > 0) { baos.write(tmp, 0, i); } HttpEntity httpEntity = null; - if (baos.size() > 0){ + if (baos.size() > 0) { httpEntity = new HttpEntity(baos.toByteArray(), headers); - }else { + } else { httpEntity = new HttpEntity(headers); } @@ -118,13 +113,13 @@ public class MonitorController { @GetMapping("/metrics/**") @ApiOperation("前端访问图片请求转发") - public void ford(HttpServletRequest request, HttpServletResponse response){ + public void ford(HttpServletRequest request, HttpServletResponse response) { String location = "/juapi"; String requestURI = request.getRequestURI(); - requestURI = requestURI.replace( context_path,""); - String fordUrl = gatewayDomain + location + requestURI ; + requestURI = requestURI.replace(context_path, ""); + String fordUrl = gatewayDomain + location + requestURI; if (StringUtils.isNotBlank(request.getQueryString())) { fordUrl = fordUrl + "?" + request.getQueryString(); } @@ -157,34 +152,34 @@ public class MonitorController { len = inputStream.read(buffer); } inputStream.close(); - }catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); } } @GetMapping("/gateway-monitor/queryGroupByAbility") @ApiOperation("统计数据按能力归集") - public Result queryGroupByAbility(String query, String time){ + public Result queryGroupByAbility(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(); HashMap data = (HashMap) body.get("data"); - if (data != null){ + 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){ + if (metric != null && metric.get("groupInfo") != null) { List value = (List) hashMap.get("value"); - if (value.size() == 2){ + if (value.size() == 2) { metric.put("count", value.get(1)); - try{ + try { Long groupInfo = Long.valueOf((String) metric.get("groupInfo")); abilityIds.add(groupInfo); metric.put("groupInfo", groupInfo); results.add(metric); - }catch (Exception e) { + } catch (Exception e) { //忽略 } @@ -211,9 +206,9 @@ public class MonitorController { 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,"服务商名") + 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) { @@ -234,27 +229,27 @@ public class MonitorController { @GetMapping("/gateway-monitor/queryGroupByDepartment") @ApiOperation("统计数据按部门归集") - public Result queryGroupByDepartment(String query, String time){ + 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(); HashMap data = (HashMap) body.get("data"); - if (data != null){ + 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("deptInfo") != null){ + if (metric != null && metric.get("deptInfo") != null) { List value = (List) hashMap.get("value"); - if (value.size() == 2){ + if (value.size() == 2) { metric.put("count", value.get(1)); try { Long groupInfo = Long.valueOf((String) metric.get("deptInfo")); abilityIds.add(groupInfo); metric.put("deptInfo", groupInfo); results.add(metric); - }catch (Exception e) { + } catch (Exception e) { //忽略 } @@ -264,7 +259,7 @@ public class MonitorController { if (!results.isEmpty()) { LambdaQueryWrapper queryWrapper = new QueryWrapper().lambda(); - queryWrapper.select(SysDeptEntity::getName,SysDeptEntity::getId) + queryWrapper.select(SysDeptEntity::getName, SysDeptEntity::getId) .in(SysDeptEntity::getId, abilityIds); List entities = sysDeptDao.selectList(queryWrapper); for (Map map : results) { @@ -287,7 +282,7 @@ public class MonitorController { @GetMapping("/gateway-monitor/queryGroupByDeptInRange") @ApiOperation("统计数据按部门显示趋势") - public Result queryGroupByDeptInRange(String query, String start, String end, String step){ + public Result queryGroupByDeptInRange(String query, String start, String end, String step) { String url = gatewayDomain + "/juapi/metrics/api/v1/query_range?query={query}" + "&start=" + start + "&end=" + end + @@ -296,34 +291,32 @@ public class MonitorController { ResponseEntity entity = restTemplate.getForEntity(url, HashMap.class, query); HashMap body = entity.getBody(); HashMap data = (HashMap) body.get("data"); - if (data != null){ + if (data != null) { List result = (List) data.get("result"); ArrayList results = new ArrayList<>(result.size()); for (HashMap hashMap : result) { Map metric = (Map) hashMap.get("metric"); - if (metric != null && metric.get("deptInfo") != null){ - try{ + if (metric != null && metric.get("deptInfo") != null) { + try { Long filterId = Long.valueOf((String) metric.get("deptInfo")); SysDeptEntity sysDeptEntity = sysDeptDao.selectById(filterId); - if (sysDeptEntity != null && StringUtils.isNotBlank(sysDeptEntity.getName())){ + if (sysDeptEntity != null && StringUtils.isNotBlank(sysDeptEntity.getName())) { metric.put("name", sysDeptEntity.getName()); results.add(hashMap); } - }catch (Exception e){ - log.warn("数据异常忽略", e); + } catch (Exception e) { + log.warn("数据异常忽略:{}", e.getMessage()); } - } } return Result.success(results); } - return Result.success(Collections.emptyList()); } @GetMapping("/gateway-monitor/queryGroupCount") @ApiOperation("查询总api数量") - public Result queryGroupCount(){ + public Result queryGroupCount() { LambdaQueryWrapper queryWrapper = new QueryWrapper().lambda(); queryWrapper.isNotNull(ResourceEntity::getGroupId).eq(ResourceEntity::getDelFlag, 0); Integer selectCount = resourceDao.selectCount(queryWrapper); @@ -332,23 +325,23 @@ public class MonitorController { @GetMapping("/gateway-monitor/getCallCount") @ApiOperation("查询总api调用总量") - public Result getCallCount(){ + public Result getCallCount() { Long callCount = monitorServiceV2.getCallCount(); return Result.success(callCount); } @GetMapping("/gateway-monitor/test/get") - public Result testGet(){ + public Result testGet() { return Result.success(null); } @GetMapping("/gateway-monitor/test/404") - public void test404(HttpServletResponse response){ + public void test404(HttpServletResponse response) { response.setStatus(404); } @GetMapping("/gateway-monitor/test/500") - public void test500(HttpServletResponse response){ + public void test500(HttpServletResponse response) { response.setStatus(500); }