Commit e8e68a14 authored by 李欣峰's avatar 李欣峰

<dev>

1.联调修改BUG<210930>
parent 296de82f
......@@ -175,7 +175,7 @@ public interface ApiApplication {
* @param inParam
* @return
*/
InvokeResult executeApi(String apiName, Map<String, String> inParam);
InvokeResult executeApi(String apiName, Map<String, String> inParam, String username);
/**
* 获取所有系统用户列表
......
......@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.zaxxer.hikari.HikariDataSource;
import com.zorkdata.apiservice.application.application.ApiApplication;
import com.zorkdata.apiservice.application.cache.DataSourceCache;
......@@ -20,12 +21,14 @@ import com.zorkdata.apiservice.domain.domain.*;
import com.zorkdata.apiservice.domain.dto.*;
import com.zorkdata.apiservice.domain.listdto.WhiteListDTO;
import com.zorkdata.apiservice.domain.repository.ApiAuthRepository;
import com.zorkdata.apiservice.domain.repository.ApiCallRepository;
import com.zorkdata.apiservice.domain.repository.ApiGroupRepository;
import com.zorkdata.apiservice.domain.repository.ApiRepository;
import com.zorkdata.dddlib.core.sdk.InvokeResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
......@@ -44,6 +47,7 @@ import javax.persistence.criteria.Predicate;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.*;
......@@ -102,6 +106,9 @@ public class ApiServiceApplicationImpl implements ApiApplication {
@Autowired
ApiAuthRepository apiAuthRepository;
@Autowired
ApiCallRepository apiCallRepository;
@Autowired
@Qualifier(value = "remoteRestTemplate")
RestTemplate restTemplate;
......@@ -458,8 +465,18 @@ public class ApiServiceApplicationImpl implements ApiApplication {
apiViewDTO.setDateSource(dataSource.getName());
}
apiViewDTO.setCreatedBy(item.getCreatedBy());
apiViewDTO.setLastCall24(10);
apiViewDTO.setTotalCall(15);
//24小时调用量
Integer apiId = item.getId();
Date endTime = new Date();
long time = endTime.getTime();
long day = 24 * 60 * 60 * 1000;
Date startTime = new Date(time - day);
Integer count24 = apiRepository.findLastCall24ByApiCall(apiId, startTime, endTime);
apiViewDTO.setLastCall24(count24);
//调用量
Integer countByApiCall = apiRepository.findCountByApiCall(apiId);
apiViewDTO.setTotalCall(countByApiCall);
apiViewDTO.setSouceWay(item.getSourceWay());
apiViewDTO.setCreatedTime(item.getCreatedTime());
apiViewDTO.setCreatedBy(item.getCreatedBy());
......@@ -494,7 +511,7 @@ public class ApiServiceApplicationImpl implements ApiApplication {
Pageable pageable = null;
Specification<Api> specification = null;
try {
String sortFieldValue = StrUtil.isNotBlank(sortField) ? sortField : "createdTime";
String sortFieldValue = StrUtil.isNotBlank(sortField) ? sortField : "updatedTime";
Sort.Direction sortDirection = Sort.Direction.DESC;
if (ORDER_ASC.equalsIgnoreCase(sortType)) {
sortDirection = Sort.Direction.ASC;
......@@ -517,7 +534,7 @@ public class ApiServiceApplicationImpl implements ApiApplication {
String format2 = simpleDateFormat.format(date2);
Date start = simpleDateFormat.parse(format);
Date end = simpleDateFormat.parse(format2);
Predicate keywordPredicate = criteriaBuilder.and(criteriaBuilder.between(root.get("createdTime"), start, end));
Predicate keywordPredicate = criteriaBuilder.and(criteriaBuilder.between(root.get("updatedTime"), start, end));
predicates.add(keywordPredicate);
} catch (ParseException e) {
log.error("时间转换失败" + e.getMessage());
......@@ -542,7 +559,9 @@ public class ApiServiceApplicationImpl implements ApiApplication {
apiDtO.setName(item.getName());
apiDtO.setDescription(item.getDescription());
apiDtO.setCreatedBy(item.getCreatedBy());
apiDtO.setTotalCall(0);
Integer apiId = apiDtO.getId();
Integer countByApiCall = apiRepository.findCountByApiCall(apiId);
apiDtO.setTotalCall(countByApiCall);
apiDtO.setCreatedBy(item.getCreatedBy());
apiDtO.setUpdatedTime(item.getUpdatedTime());
apiDtO.setRequestType(item.getRequestType());
......@@ -587,7 +606,9 @@ public class ApiServiceApplicationImpl implements ApiApplication {
apiDtO.setName(item.getName());
apiDtO.setDescription(item.getDescription());
apiDtO.setCreatedBy(item.getCreatedBy());
apiDtO.setTotalCall(0);
Integer apiId = apiDtO.getId();
Integer countByApiCall = apiRepository.findCountByApiCall(apiId);
apiDtO.setTotalCall(countByApiCall);
apiDtO.setCreatedBy(item.getCreatedBy());
apiDtO.setUpdatedTime(item.getUpdatedTime());
apiDtO.setRequestType(item.getRequestType());
......@@ -604,7 +625,7 @@ public class ApiServiceApplicationImpl implements ApiApplication {
invokeResult = InvokeResult.success(queryResult, "查询市场api成功!");
return invokeResult;
} else {
Integer count = apiRepository.listMyAuthTwoCount(keyword,userid);
Integer count = apiRepository.listMyAuthTwoCount(keyword, userid);
apis = apiRepository.listMyAuthTwo(keyword, sortType, sortField, pageNo - 1, pageSize, userid);
List<ApiDTO> apiDTOS = new ArrayList<>();
apis.forEach(item -> {
......@@ -615,7 +636,9 @@ public class ApiServiceApplicationImpl implements ApiApplication {
apiDtO.setName(item.getName());
apiDtO.setDescription(item.getDescription());
apiDtO.setCreatedBy(item.getCreatedBy());
apiDtO.setTotalCall(0);
Integer apiId = apiDtO.getId();
Integer countByApiCall = apiRepository.findCountByApiCall(apiId);
apiDtO.setTotalCall(countByApiCall);
apiDtO.setCreatedBy(item.getCreatedBy());
apiDtO.setUpdatedTime(item.getUpdatedTime());
apiDtO.setRequestType(item.getRequestType());
......@@ -723,7 +746,9 @@ public class ApiServiceApplicationImpl implements ApiApplication {
}
@Override
public InvokeResult executeApi(String apiName, Map<String, String> inParam) {
public InvokeResult executeApi(String apiName, Map<String, String> inParam, String username) {
//开始时间
Long startTime = System.currentTimeMillis();
InvokeResult invokeResult = null;
Api api = apiRepository.findByName(apiName);
//0未发布 , 1发布
......@@ -755,6 +780,24 @@ public class ApiServiceApplicationImpl implements ApiApplication {
} else {
invokeResult = executeCommonApi(api, inParam);
}
Long endTime = System.currentTimeMillis();
Long duration = endTime - startTime;
ApiCall apiCall = new ApiCall();
apiCall.setApiId(api.getId());
apiCall.setCallTime(new DateTime(startTime));
apiCall.setDuration(duration);
String resquest = new Gson().toJson(inParam);
apiCall.setRequestParam(resquest);
String result = new Gson().toJson(invokeResult);
apiCall.setResult(result);
apiCall.setCaller(username);
if (invokeResult.getCode().equals(0)) {
apiCall.setSuccess(true);
} else {
apiCall.setSuccess(false);
}
apiCallRepository.save(apiCall);
return invokeResult;
}
......@@ -1251,7 +1294,7 @@ public class ApiServiceApplicationImpl implements ApiApplication {
break;
case DELETE:
restTemplate.delete(uir, httpEntity);
invokeResult = InvokeResult.success("接口DELETE请求成功!","{'message':'接口DELETE请求成功!'}");
invokeResult = InvokeResult.success("接口DELETE请求成功!", "{'message':'接口DELETE请求成功!'}");
break;
default:
invokeResult = InvokeResult.fail();
......
......@@ -27,7 +27,7 @@ public class ApiCall {
* 接口id
*/
@Column(name = "api_id")
private Long apiId;
private Integer apiId;
/**
* 调用时间
*/
......@@ -37,7 +37,7 @@ public class ApiCall {
* 耗时
*/
@Column(name = "duration")
private String duration;
private Long duration;
/**
* 请求参数
*/
......@@ -57,32 +57,12 @@ public class ApiCall {
* 是否调用成功
*/
@Column(name = "success")
private Integer success;
private Boolean success;
/**
* 错误类型
*/
@Column(name = "error_type")
private String errorType;
/**
* 创建者
*/
@Column(name = "created_by")
private String createdBy;
/**
* 创建时间
*/
@Column(name = "created_time")
private Date createdTime;
/**
* 更新者
*/
@Column(name = "updated_by")
private String updatedBy;
/**
* 更新时间
*/
@Column(name = "updated_time")
private Date updatedTime;
}
......@@ -2,6 +2,10 @@ package com.zorkdata.apiservice.domain.dto;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;
/**
......@@ -12,14 +16,16 @@ import java.util.Date;
@Data
public class ApiCallDTO {
/**
* 主键ID
*/
private Integer id;
/**
* 接口id
*/
private Long apiId;
private Integer apiId;
/**
* 调用时间
*/
......@@ -27,7 +33,7 @@ public class ApiCallDTO {
/**
* 耗时
*/
private String duration;
private Long duration;
/**
* 请求参数
*/
......@@ -48,22 +54,6 @@ public class ApiCallDTO {
* 错误类型
*/
private String errorType;
/**
* 创建者
*/
private String createdBy;
/**
* 创建时间
*/
private Date createdTime;
/**
* 更新者
*/
private String updatedBy;
/**
* 更新时间
*/
private Date updatedTime;
}
package com.zorkdata.apiservice.domain.repository;
import com.zorkdata.apiservice.domain.domain.Api;
import com.zorkdata.apiservice.domain.domain.ApiCall;
import com.zorkdata.dddlib.domain.EntityRepository;
import org.springframework.stereotype.Repository;
/**
* @title: ApiCallRepository
* @Author Linxinfeng
* @Date: 2021-09-2021/9/30 030-10:43
*/
@Repository
public interface ApiCallRepository extends EntityRepository<ApiCall, Integer> {
}
package com.zorkdata.apiservice.domain.repository;
import cn.hutool.core.date.DateTime;
import com.zorkdata.apiservice.domain.domain.Api;
import com.zorkdata.apiservice.domain.domain.ApiAuth;
import com.zorkdata.apiservice.domain.domain.User;
......@@ -53,7 +54,7 @@ public interface ApiRepository extends EntityRepository<Api, Integer> {
* @param end
* @return
*/
@Query(value = " select a.* from `api_auth` ap left join api a on a.id = ap.api_id where (a.name like CONCAT ('%',?1,'%') or a.description like CONCAT ('%',?1,'%') or a.created_by like CONCAT ('%',?1,'%')) and ap.user_id = ?6 and a.created_time between ?7 and ?8 order by ?2 ?3 limit ?4,?5 ", nativeQuery = true)
@Query(value = " select a.* from `api_auth` ap left join api a on a.id = ap.api_id where (a.name like CONCAT ('%',?1,'%') or a.description like CONCAT ('%',?1,'%') or a.created_by like CONCAT ('%',?1,'%')) and ap.user_id = ?6 and a.updated_time between ?7 and ?8 order by ?2 ?3 limit ?4,?5 ", nativeQuery = true)
List<Api> listMyAuth(String keyword, String sortType, String sortField, Integer pageNo, Integer pageSize, Integer userid, String start, String end);
/**
......@@ -65,7 +66,7 @@ public interface ApiRepository extends EntityRepository<Api, Integer> {
* @param end
* @return
*/
@Query(value = " select count(*) from `api_auth` ap left join api a on a.id = ap.api_id where (a.name like CONCAT ('%',?1,'%') or a.description like CONCAT ('%',?1,'%') or a.created_by like CONCAT ('%',?1,'%')) and ap.user_id = ?2 and a.created_time between ?3 and ?4 ", nativeQuery = true)
@Query(value = " select count(*) from `api_auth` ap left join api a on a.id = ap.api_id where (a.name like CONCAT ('%',?1,'%') or a.description like CONCAT ('%',?1,'%') or a.created_by like CONCAT ('%',?1,'%')) and ap.user_id = ?2 and a.updated_time between ?3 and ?4 ", nativeQuery = true)
Integer listMyAuthCount(String keyword, Integer userid, String start, String end);
/**
......@@ -153,4 +154,17 @@ public interface ApiRepository extends EntityRepository<Api, Integer> {
*/
@Query(value = "select white_list from api where id = ?1 ", nativeQuery = true)
String findWhiteList(Integer apiId);
/**
* 根据apiId查询api_Call数量
*
* @param id
* @return
*/
@Query(value = "select count(*) from api_call where api_id = ?1 ", nativeQuery = true)
Integer findCountByApiCall(Integer id);
@Query(value = "select count(*) from api_call where api_id = ?1 and call_time >= ?2 and call_time <= ?3", nativeQuery = true)
Integer findLastCall24ByApiCall(Integer apiId, Date startTime, Date endTime);
}
......@@ -174,7 +174,7 @@ public interface ApiFacade {
* @param inParam
* @return
*/
InvokeResult<Void> executeApi(String apiName, Map<String, String> inParam);
InvokeResult<Void> executeApi(String apiName, Map<String, String> inParam,String username);
/**
* 获取Mysql数据类型
......
......@@ -225,10 +225,10 @@ public class ApiServicueFacadeImpl implements ApiFacade {
}
@Override
public InvokeResult executeApi(String apiName, Map<String, String> inParam) {
public InvokeResult executeApi(String apiName, Map<String, String> inParam, String username) {
InvokeResult invokeResult;
try {
InvokeResult apiResult = apiApplication.executeApi(apiName, inParam);
InvokeResult apiResult = apiApplication.executeApi(apiName, inParam, username);
return apiResult;
} catch (Exception e) {
log.error("执行Api失败", e);
......
......@@ -270,7 +270,9 @@ public class ApiServicueController extends BaseController {
@Inner(value = false)
@PostMapping("/executeApi/{apiName}")
public InvokeResult executeApi(@PathVariable String apiName, @RequestBody(required = false) Map<String, String> inParam) {
return apiFacade.executeApi(apiName, inParam);
BkUser user = super.getUser();
String username = user.getUsername();
return apiFacade.executeApi(apiName, inParam, username);
}
@Inner(value = false)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment