Merge branch 'master' into docker_package

This commit is contained in:
wangliwen 2022-08-11 11:31:35 +08:00
commit 60e20399c0
6 changed files with 271 additions and 16 deletions

View File

@ -112,9 +112,8 @@ public class TbFuseServiceImpl extends CrudServiceImpl<TbFuseDao, TbFuseEntity,
} }
params.put("userId", SecurityUser.getUserId()); params.put("userId", SecurityUser.getUserId());
List<TbFuseDTO> dtoList = baseDao.getFuseDTOList(params); List<TbFuseDTO> dtoList = baseDao.getFuseDTOList(params);
Map<String, Map<String, Object>> dataResourceList=getDataResource();
dtoList.forEach(dto -> { dtoList.forEach(dto -> {
dto.setFuseResourceList(getFuseResourceByFuseId(dto.getId(), dataResourceList)); dto.setFuseResourceList(getFuseResourceByFuseId(dto.getId()));
dto.setFuseAttrList(getAttrByFuseId(dto.getId())); dto.setFuseAttrList(getAttrByFuseId(dto.getId()));
}); });
List<TbFuseDTO> result = dtoList.stream().skip((curPage - 1) * limit).limit(limit).collect(Collectors.toList()); List<TbFuseDTO> result = dtoList.stream().skip((curPage - 1) * limit).limit(limit).collect(Collectors.toList());
@ -130,7 +129,7 @@ public class TbFuseServiceImpl extends CrudServiceImpl<TbFuseDao, TbFuseEntity,
TbFuseDTO fuseDTO = new TbFuseDTO(); TbFuseDTO fuseDTO = new TbFuseDTO();
BeanUtils.copyProperties(fuseEntity, fuseDTO); BeanUtils.copyProperties(fuseEntity, fuseDTO);
fuseDTO.setFuseAttrList(getAttrByFuseId(id)); fuseDTO.setFuseAttrList(getAttrByFuseId(id));
fuseDTO.setFuseResourceList(getFuseResourceByFuseId(id, getDataResource())); fuseDTO.setFuseResourceList(getFuseResourceByFuseId(id));
setCollection(fuseDTO); setCollection(fuseDTO);
return fuseDTO; return fuseDTO;
} }
@ -175,17 +174,22 @@ public class TbFuseServiceImpl extends CrudServiceImpl<TbFuseDao, TbFuseEntity,
return result; return result;
} }
private List<TbFuseResourceDTO> getFuseResourceByFuseId(Long fuseId, Map<String, Map<String, Object>> dataResourceMap) { private List<TbFuseResourceDTO> getFuseResourceByFuseId(Long fuseId) {
Map<String, Map<String, Object>> dataResourceMap = new HashMap<>();
List<TbFuseResourceDTO> result = new ArrayList(); List<TbFuseResourceDTO> result = new ArrayList();
QueryWrapper wrapper = new QueryWrapper(); QueryWrapper wrapper = new QueryWrapper();
wrapper.eq("fuse_id", fuseId); wrapper.eq("fuse_id", fuseId);
List<TbFuseResourceEntity> list = fuseResourceDao.selectList(wrapper); List<TbFuseResourceEntity> list = fuseResourceDao.selectList(wrapper);
list.stream().filter(index -> index.getType() != null).collect(Collectors.groupingBy(TbFuseResourceEntity::getType)).forEach((type, value) -> { Map<String, List<TbFuseResourceEntity>> resourceMap = list.stream().filter(index -> index.getType() != null).collect(Collectors.groupingBy(TbFuseResourceEntity::getType));
if (resourceMap.containsKey("数据资源")) {
dataResourceMap.putAll(getDataResource());
}
resourceMap.forEach((type, value) -> {
if ("组件服务".equals(type)) { if ("组件服务".equals(type)) {
result.addAll(value.stream().map(attr -> { result.addAll(value.stream().map(attr -> {
TbFuseResourceDTO dto = new TbFuseResourceDTO(); TbFuseResourceDTO dto = new TbFuseResourceDTO();
BeanUtils.copyProperties(attr, dto); BeanUtils.copyProperties(attr, dto);
ResourceDTO resourceDTO=resourceService.get(dto.getResourceId()); ResourceDTO resourceDTO = resourceService.get(dto.getResourceId());
resourceDTO.setDeptName(sysDeptService.get(resourceDTO.getDeptId()).getName()); resourceDTO.setDeptName(sysDeptService.get(resourceDTO.getDeptId()).getName());
dto.setResource(resourceDTO); dto.setResource(resourceDTO);
return dto; return dto;

View File

@ -0,0 +1,241 @@
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.service.MonitorServiceV2;
import io.renren.modules.monitor.entity.Result;
import io.renren.modules.resource.dao.AttrDao;
import io.renren.modules.resource.dao.ResourceDao;
import io.renren.modules.resource.entity.AttrEntity;
import io.renren.modules.resource.entity.ResourceEntity;
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.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.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.RestTemplate;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
@RestController
@Api(tags = "网关统计")
@Log4j2
@RequestMapping("/gateway-monitor/v2")
public class MonitorControllerV2 {
@Autowired
private RestTemplate restTemplate;
@Value("${hisense.gateway.url}")
private String gatewayDomain;
@Value("${server.servlet.context-path}")
private String context_path;
@Autowired
private ResourceDao resourceDao;
@Autowired
private AttrDao attrDao;
@Autowired
private SysDeptDao sysDeptDao;
@Autowired
private MonitorServiceV2 monitorServiceV2;
@GetMapping("/queryGroupByAbility")
@ApiOperation("统计数据按能力归集")
public Result queryGroupByAbility(String query, String time){
String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}" + "&time=" + time;
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query);
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) {
//忽略
}
}
}
}
if (!results.isEmpty()) {
LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda();
queryWrapper
.select(ResourceEntity::getId,
ResourceEntity::getName,
ResourceEntity::getApiMethodType,
ResourceEntity::getType,
ResourceEntity::getApiUrl)
.in(ResourceEntity::getId, abilityIds);
List<ResourceEntity> 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<AttrEntity> 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());
}
@GetMapping("/queryGroupByDepartment")
@ApiOperation("统计数据按部门归集")
public Result queryGroupByDepartment(String query, String time){
String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}" + "&time=" + time;
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query);
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("deptInfo") != 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("deptInfo"));
abilityIds.add(groupInfo);
metric.put("deptInfo", groupInfo);
results.add(metric);
}catch (Exception e) {
//忽略
}
}
}
}
if (!results.isEmpty()) {
LambdaQueryWrapper<SysDeptEntity> queryWrapper = new QueryWrapper<SysDeptEntity>().lambda();
queryWrapper.select(SysDeptEntity::getName,SysDeptEntity::getId)
.in(SysDeptEntity::getId, abilityIds);
List<SysDeptEntity> entities = sysDeptDao.selectList(queryWrapper);
for (Map map : results) {
Long deptInfo = (Long) map.get("deptInfo");
for (SysDeptEntity sysDeptEntity : entities) {
if (deptInfo != null && deptInfo.equals(sysDeptEntity.getId())) {
map.put("name", sysDeptEntity.getName());
entities.remove(sysDeptEntity);
break;
}
}
}
}
return Result.success(results);
}
return Result.success(Collections.emptyList());
}
@GetMapping("/queryGroupByDeptInRange")
@ApiOperation("统计数据按部门显示趋势")
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 +
"&step=" + step;
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query);
HashMap body = entity.getBody();
HashMap data = (HashMap) body.get("data");
if (data != null){
List<HashMap> result = (List) data.get("result");
ArrayList<HashMap> results = new ArrayList<>(result.size());
for (HashMap hashMap : result) {
Map metric = (Map) hashMap.get("metric");
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())){
metric.put("name", sysDeptEntity.getName());
results.add(hashMap);
}
}catch (Exception e){
log.warn("数据异常忽略", e);
}
}
}
return Result.success(results);
}
return Result.success(Collections.emptyList());
}
@GetMapping("/queryGroupCount")
@ApiOperation("查询总api数量")
public Result queryGroupCount(){
LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda();
queryWrapper.isNotNull(ResourceEntity::getGroupId).eq(ResourceEntity::getDelFlag, 0);
Integer selectCount = resourceDao.selectCount(queryWrapper);
return Result.success(selectCount);
}
@GetMapping("/getCallCount")
@ApiOperation("查询总api调用总量")
public Result getCallCount(){
Long callCount = monitorServiceV2.getCallCount();
return Result.success(callCount);
}
}

View File

@ -153,6 +153,8 @@ public class ApiGatewayService {
} }
} }
deleteGroup(groupId);
} }
} }

View File

@ -263,6 +263,20 @@ public class ResourceController {
return new Result().ok(dto.getId() == null ? "" : dto.getId()); return new Result().ok(dto.getId() == null ? "" : dto.getId());
} }
@PostMapping("/registerApi2Gateway")
public Result registerApi2Gateway(@RequestParam String source) {
try {
apiGatewayService.resetApiGroup(source);
apiGatewayService.registerApi2Gateway(source);
}catch (Exception exception){
//注册失败忽略简单记录一下
logger.error("挂接网关注册失败", exception);
return new Result().error(exception.getMessage());
}
return new Result().ok(null);
}
@PostMapping("/importResource") @PostMapping("/importResource")
@ApiOperation("导入") @ApiOperation("导入")
@LogOperation("导入") @LogOperation("导入")

View File

@ -250,7 +250,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
if (StringUtils.isNotBlank(attrEntity.getAttrValue())) { if (StringUtils.isNotBlank(attrEntity.getAttrValue())) {
String[] ids = attrEntity.getAttrValue().split(","); String[] ids = attrEntity.getAttrValue().split(",");
if (ids.length != 0) { if (ids.length != 0) {
List<TbDataResourceRelEntity> resourceRels = new ArrayList<>();
for (String keyid : ids) { for (String keyid : ids) {
TbDataResourceRelEntity resourceRel = new TbDataResourceRelEntity(); TbDataResourceRelEntity resourceRel = new TbDataResourceRelEntity();
resourceRel.setKeyId(Long.valueOf(keyid)); resourceRel.setKeyId(Long.valueOf(keyid));
@ -264,7 +264,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
if (StringUtils.isNotBlank(attrEntity.getAttrValue())) { if (StringUtils.isNotBlank(attrEntity.getAttrValue())) {
String[] ids = attrEntity.getAttrValue().split(","); String[] ids = attrEntity.getAttrValue().split(",");
if (ids.length != 0) { if (ids.length != 0) {
List<TbDataResourceRelEntity> resourceRels = new ArrayList<>();
for (String keyid : ids) { for (String keyid : ids) {
TbDataResourceRelEntity resourceRel = new TbDataResourceRelEntity(); TbDataResourceRelEntity resourceRel = new TbDataResourceRelEntity();
resourceRel.setKeyId(resourceID); resourceRel.setKeyId(resourceID);
@ -311,7 +311,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
if (StringUtils.isNotBlank(attrEntity.getAttrValue())) { if (StringUtils.isNotBlank(attrEntity.getAttrValue())) {
String[] ids = attrEntity.getAttrValue().split(","); String[] ids = attrEntity.getAttrValue().split(",");
if (ids.length != 0) { if (ids.length != 0) {
List<TbDataResourceRelEntity> resourceRels = new ArrayList<>();
for (String keyid : ids) { for (String keyid : ids) {
TbDataResourceRelEntity resourceRel = new TbDataResourceRelEntity(); TbDataResourceRelEntity resourceRel = new TbDataResourceRelEntity();
resourceRel.setKeyId(Long.valueOf(keyid)); resourceRel.setKeyId(Long.valueOf(keyid));
@ -408,7 +408,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
if (response.isSuccessful()) { if (response.isSuccessful()) {
String body = response.body().string(); String body = response.body().string();
JSONObject jsonObject_ = JSON.parseObject(body); JSONObject jsonObject_ = JSON.parseObject(body);
logger.info("西海岸接口返回:{}" + body, url); logger.info("西海岸接口{}返回:{}" , url, body);
if (jsonObject_.containsKey("data")) { if (jsonObject_.containsKey("data")) {
if (jsonObject_.getJSONObject("data").containsKey("list")) { if (jsonObject_.getJSONObject("data").containsKey("list")) {
resultPage.setTotal(jsonObject_.getJSONObject("data").getLongValue("total")); resultPage.setTotal(jsonObject_.getJSONObject("data").getLongValue("total"));

View File

@ -43,7 +43,6 @@ public class ApiGatewayServiceTest {
resourceEntities.forEach(item -> { resourceEntities.forEach(item -> {
String id = String.valueOf(item.getId()); String id = String.valueOf(item.getId());
apiGatewayService.resetApiGroup(id); apiGatewayService.resetApiGroup(id);
apiGatewayService.deleteGroup(id);
apiGatewayService.registerApi2Gateway(id); apiGatewayService.registerApi2Gateway(id);
}); });
@ -53,11 +52,6 @@ public class ApiGatewayServiceTest {
public void registerAPI(){ public void registerAPI(){
String id = "1522550194544123907"; String id = "1522550194544123907";
apiGatewayService.resetApiGroup(id); apiGatewayService.resetApiGroup(id);
try{
apiGatewayService.deleteGroup(id);
}catch (Exception e){
e.printStackTrace();
}
apiGatewayService.registerApi2Gateway(id); apiGatewayService.registerApi2Gateway(id);
} }