Skip to content

Commit

Permalink
perf: 权限中心获取作业模板/执行方案、脚本、账号等资源的搜索接口 TencentBlueKing#180
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonwan committed Sep 3, 2021
1 parent dfc69ad commit b858725
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.tencent.bk.job.common.iam.aspect;

import com.tencent.bk.job.common.util.json.JsonUtils;
import com.tencent.bk.sdk.iam.dto.callback.request.CallbackRequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class IamCallbackAspect {

@Autowired
public IamCallbackAspect() {
}

@Pointcut("execution (* com.tencent.bk.job.*.api.iam.impl.*.callback(..))")
public void processCallbackRequest() {
}

private void logRequest(CallbackRequestDTO callbackRequest) {
if (log.isDebugEnabled()) {
log.debug("callbackRequest={}", JsonUtils.toJson(callbackRequest));
} else {
log.info(
"Received iam callbackRequest:[{}|{}|{}|{}]",
callbackRequest.getMethod().getMethod(),
callbackRequest.getType(),
callbackRequest.getFilter(),
callbackRequest.getPage()
);
}
}

@Around("processCallbackRequest()")
public Object logBeforeProcessCallbackRequest(ProceedingJoinPoint pjp) throws Throwable {
Object[] args = pjp.getArgs();
if (args.length < 1) {
log.warn("unexpected ProceedingJoinPoint, please check");
return pjp.proceed();
}
CallbackRequestDTO callbackRequest = (CallbackRequestDTO) args[0];
logRequest(callbackRequest);
return pjp.proceed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ public interface IamCallbackController {
* @return 权限中心回调响应
*/
@PostMapping("/job")
CallbackBaseResponseDTO cronJobCallback(@RequestBody CallbackRequestDTO callbackRequest);
CallbackBaseResponseDTO callback(@RequestBody CallbackRequestDTO callbackRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.tencent.bk.sdk.iam.dto.callback.response.ListInstanceResponseDTO;
import com.tencent.bk.sdk.iam.dto.callback.response.SearchInstanceResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -61,6 +62,17 @@ public IamCallbackControllerImpl(CronJobService cronJobService) {
this.cronJobService = cronJobService;
}

private Pair<CronJobInfoDTO, BaseSearchCondition> getBasicQueryCondition(CallbackRequestDTO callbackRequest) {
IamSearchCondition searchCondition = IamSearchCondition.fromReq(callbackRequest);
BaseSearchCondition baseSearchCondition = new BaseSearchCondition();
baseSearchCondition.setStart(searchCondition.getStart().intValue());
baseSearchCondition.setLength(searchCondition.getLength().intValue());

CronJobInfoDTO cronJobQuery = new CronJobInfoDTO();
cronJobQuery.setAppId(searchCondition.getAppIdList().get(0));
return Pair.of(cronJobQuery, baseSearchCondition);
}

private InstanceInfoDTO convert(CronJobInfoDTO cronJobInfo) {
InstanceInfoDTO instanceInfo = new InstanceInfoDTO();
instanceInfo.setId(String.valueOf(cronJobInfo.getId()));
Expand All @@ -71,64 +83,38 @@ private InstanceInfoDTO convert(CronJobInfoDTO cronJobInfo) {
private SearchInstanceResponseDTO searchInstanceResp(
CallbackRequestDTO callbackRequest
) {
log.info(
"{}|{}|{}|{}",
callbackRequest.getMethod().getMethod(),
callbackRequest.getType(),
callbackRequest.getFilter(),
callbackRequest.getPage()
);
IamSearchCondition searchCondition = IamSearchCondition.fromReq(callbackRequest);
CronJobInfoDTO condition = new CronJobInfoDTO();
BaseSearchCondition baseSearchCondition = new BaseSearchCondition();
PageData<CronJobInfoDTO> cronJobInfoPageData;
condition.setAppId(searchCondition.getAppIdList().get(0));
condition.setName(callbackRequest.getFilter().getKeyword());
Pair<CronJobInfoDTO, BaseSearchCondition> basicQueryCond = getBasicQueryCondition(callbackRequest);
CronJobInfoDTO cronJobQuery = basicQueryCond.getLeft();
BaseSearchCondition baseSearchCondition = basicQueryCond.getRight();

baseSearchCondition.setStart(searchCondition.getStart().intValue());
baseSearchCondition.setLength(searchCondition.getLength().intValue());

cronJobInfoPageData =
cronJobService.listPageCronJobInfos(condition, baseSearchCondition);
cronJobQuery.setName(callbackRequest.getFilter().getKeyword());
PageData<CronJobInfoDTO> cronJobInfoPageData =
cronJobService.listPageCronJobInfos(cronJobQuery, baseSearchCondition);

return IamRespUtil.getSearchInstanceRespFromPageData(cronJobInfoPageData, this::convert);
}

private ListInstanceResponseDTO listInstanceResp(
CallbackRequestDTO callbackRequest
) {
log.info(
"{}|{}|{}|{}",
callbackRequest.getMethod().getMethod(),
callbackRequest.getType(),
callbackRequest.getFilter(),
callbackRequest.getPage()
);
IamSearchCondition searchCondition = IamSearchCondition.fromReq(callbackRequest);
CronJobInfoDTO condition = new CronJobInfoDTO();
condition.setAppId(searchCondition.getAppIdList().get(0));
BaseSearchCondition baseSearchCondition = new BaseSearchCondition();
baseSearchCondition.setStart(searchCondition.getStart().intValue());
baseSearchCondition.setLength(searchCondition.getLength().intValue());
Pair<CronJobInfoDTO, BaseSearchCondition> basicQueryCond = getBasicQueryCondition(callbackRequest);
CronJobInfoDTO cronJobQuery = basicQueryCond.getLeft();
BaseSearchCondition baseSearchCondition = basicQueryCond.getRight();

PageData<CronJobInfoDTO> cronJobInfoPageData =
cronJobService.listPageCronJobInfos(condition, baseSearchCondition);
cronJobService.listPageCronJobInfos(cronJobQuery, baseSearchCondition);

return IamRespUtil.getListInstanceRespFromPageData(cronJobInfoPageData, this::convert);
}

@Override
public CallbackBaseResponseDTO cronJobCallback(CallbackRequestDTO callbackRequest) {
log.debug("Receive iam callback|{}", callbackRequest);
public CallbackBaseResponseDTO callback(CallbackRequestDTO callbackRequest) {
CallbackBaseResponseDTO response;
switch (callbackRequest.getMethod()) {
case LIST_INSTANCE:
response = listInstanceResp(callbackRequest);
break;
case FETCH_INSTANCE_INFO:
log.debug("Fetch instance info request!|{}|{}|{}", callbackRequest.getType(),
callbackRequest.getFilter(), callbackRequest.getPage());

IamSearchCondition searchCondition = IamSearchCondition.fromReq(callbackRequest);
List<Object> instanceAttributeInfoList = new ArrayList<>();
for (String instanceId : searchCondition.getIdList()) {
Expand All @@ -150,20 +136,14 @@ public CallbackBaseResponseDTO cronJobCallback(CallbackRequestDTO callbackReques
response = fetchInstanceInfoResponse;
break;
case LIST_ATTRIBUTE:
log.debug("List attribute request!|{}|{}|{}", callbackRequest.getType(), callbackRequest.getFilter(),
callbackRequest.getPage());
response = new ListAttributeResponseDTO();
response.setCode(0L);
break;
case LIST_ATTRIBUTE_VALUE:
log.debug("List attribute value request!|{}|{}|{}", callbackRequest.getType(),
callbackRequest.getFilter(), callbackRequest.getPage());
response = new ListAttributeValueResponseDTO();
response.setCode(0L);
break;
case LIST_INSTANCE_BY_POLICY:
log.debug("List instance by policy request!|{}|{}|{}", callbackRequest.getType(),
callbackRequest.getFilter(), callbackRequest.getPage());
response = new ListInstanceByPolicyResponseDTO();
response.setCode(0L);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@

package com.tencent.bk.job.file_gateway.api.iam;

import com.tencent.bk.job.common.iam.util.IamRespUtil;
import com.tencent.bk.job.common.model.PageData;
import com.tencent.bk.job.file_gateway.model.dto.FileSourceDTO;
import com.tencent.bk.job.file_gateway.service.FileSourceService;
import com.tencent.bk.sdk.iam.dto.callback.request.CallbackRequestDTO;
import com.tencent.bk.sdk.iam.dto.callback.request.IamSearchCondition;
import com.tencent.bk.sdk.iam.dto.callback.response.*;
import com.tencent.bk.sdk.iam.dto.callback.response.CallbackBaseResponseDTO;
import com.tencent.bk.sdk.iam.dto.callback.response.FetchInstanceInfoResponseDTO;
import com.tencent.bk.sdk.iam.dto.callback.response.InstanceInfoDTO;
import com.tencent.bk.sdk.iam.dto.callback.response.ListAttributeResponseDTO;
import com.tencent.bk.sdk.iam.dto.callback.response.ListAttributeValueResponseDTO;
import com.tencent.bk.sdk.iam.dto.callback.response.ListInstanceByPolicyResponseDTO;
import com.tencent.bk.sdk.iam.dto.callback.response.ListInstanceResponseDTO;
import com.tencent.bk.sdk.iam.dto.callback.response.SearchInstanceResponseDTO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -48,47 +58,111 @@ public IamFileSourceCallbackResourceImpl(FileSourceService fileSourceService) {
this.fileSourceService = fileSourceService;
}

@Data
static class FileSourceSearchCondition {
List<Long> appIdList;
List<String> idStrList;
List<Integer> fileSourceIdList;
int start;
int length;
String keyword;

public FileSourceSearchCondition(
List<Long> appIdList,
List<String> idStrList,
List<Integer> fileSourceIdList,
int start,
int length,
String keyword
) {
this.appIdList = appIdList;
this.idStrList = idStrList;
this.fileSourceIdList = fileSourceIdList;
this.start = start;
this.length = length;
this.keyword = keyword;
}
}

private FileSourceSearchCondition getSearchCondition(CallbackRequestDTO callbackRequest) {
IamSearchCondition searchCondition = IamSearchCondition.fromReq(callbackRequest);
// 文件源列表实现
List<Long> appIdList = searchCondition.getAppIdList();
List<String> idStrList = searchCondition.getIdList();
List<Integer> fileSourceIdList = null;
if (idStrList != null) {
fileSourceIdList = idStrList.parallelStream().map(Integer::parseInt).collect(Collectors.toList());
}

int start = searchCondition.getStart().intValue();
int length = searchCondition.getLength().intValue();

String keyword = callbackRequest.getFilter().getKeyword();
return new FileSourceSearchCondition(
appIdList, idStrList, fileSourceIdList, start, length, keyword
);
}

private ListInstanceResponseDTO listInstanceResp(CallbackRequestDTO callbackRequest) {
FileSourceSearchCondition searchCondition = getSearchCondition(callbackRequest);

List<FileSourceDTO> fileSourceDTOList = fileSourceService.listWorkTableFileSource(
searchCondition.getAppIdList(),
searchCondition.getFileSourceIdList(),
searchCondition.getStart(),
searchCondition.getLength()
);
Long totalCount = fileSourceService.countWorkTableFileSource(
searchCondition.getAppIdList(), searchCondition.getFileSourceIdList()
).longValue();
PageData<FileSourceDTO> fileSourceDTOPageData = new PageData<>(
searchCondition.getStart(),
searchCondition.getLength(),
totalCount,
fileSourceDTOList
);
return IamRespUtil.getListInstanceRespFromPageData(fileSourceDTOPageData, this::convert);
}

private InstanceInfoDTO convert(FileSourceDTO fileSourceDTO) {
InstanceInfoDTO tmpInstanceInfo = new InstanceInfoDTO();
tmpInstanceInfo.setId(fileSourceDTO.getId().toString());
tmpInstanceInfo.setDisplayName(fileSourceDTO.getAlias());
return tmpInstanceInfo;
}

private SearchInstanceResponseDTO searchInstanceResp(CallbackRequestDTO callbackRequest) {
FileSourceSearchCondition searchCondition = getSearchCondition(callbackRequest);

List<FileSourceDTO> fileSourceDTOList = fileSourceService.listWorkTableFileSource(
searchCondition.getAppIdList().get(0),
null,
searchCondition.getKeyword(),
searchCondition.getStart(),
searchCondition.getLength()
);

Long totalCount = fileSourceService.countWorkTableFileSource(
searchCondition.getAppIdList(), searchCondition.getFileSourceIdList()
).longValue();
PageData<FileSourceDTO> fileSourceDTOPageData = new PageData<>(
searchCondition.getStart(),
searchCondition.getLength(),
totalCount,
fileSourceDTOList
);
return IamRespUtil.getSearchInstanceRespFromPageData(fileSourceDTOPageData, this::convert);
}

@Override
public CallbackBaseResponseDTO callback(CallbackRequestDTO callbackRequest) {
log.debug("Receive iam callback|{}", callbackRequest);
CallbackBaseResponseDTO response;
IamSearchCondition searchCondition = IamSearchCondition.fromReq(callbackRequest);
switch (callbackRequest.getMethod()) {
case LIST_INSTANCE:
log.debug("List instance request!|{}|{}|{}", callbackRequest.getType(), callbackRequest.getFilter(),
callbackRequest.getPage());
// 文件源列表实现
List<Long> appIdList = searchCondition.getAppIdList();
List<String> idStrList = searchCondition.getIdList();
List<Integer> fileSourceIdList = null;
if (idStrList != null) {
fileSourceIdList = idStrList.parallelStream().map(Integer::parseInt).collect(Collectors.toList());
}
List<FileSourceDTO> fileSourceDTOList = fileSourceService.listWorkTableFileSource(
appIdList,
fileSourceIdList,
searchCondition.getStart().intValue(),
searchCondition.getLength().intValue());
Long totalCount = fileSourceService.countWorkTableFileSource(appIdList, fileSourceIdList).longValue();
List<InstanceInfoDTO> instanceInfoList = fileSourceDTOList.parallelStream().map(fileSourceDTO -> {
InstanceInfoDTO tmpInstanceInfo = new InstanceInfoDTO();
tmpInstanceInfo.setId(fileSourceDTO.getId().toString());
tmpInstanceInfo.setDisplayName(fileSourceDTO.getAlias());
return tmpInstanceInfo;
}).collect(Collectors.toList());

ListInstanceResponseDTO instanceResponse = new ListInstanceResponseDTO();
instanceResponse.setCode(0L);
BaseDataResponseDTO<InstanceInfoDTO> baseDataResponse = new BaseDataResponseDTO<>();
baseDataResponse.setResult(instanceInfoList);
baseDataResponse.setCount(totalCount);
instanceResponse.setData(baseDataResponse);
response = instanceResponse;
response = listInstanceResp(callbackRequest);
break;
case FETCH_INSTANCE_INFO:
log.debug("Fetch instance info request!|{}|{}|{}", callbackRequest.getType(),
callbackRequest.getFilter(), callbackRequest.getPage());

List<Object> instanceAttributeInfoList = new ArrayList<>();
for (String instanceId : searchCondition.getIdList()) {
try {
Expand All @@ -115,23 +189,20 @@ public CallbackBaseResponseDTO callback(CallbackRequestDTO callbackRequest) {
response = fetchInstanceInfoResponse;
break;
case LIST_ATTRIBUTE:
log.debug("List attribute request!|{}|{}|{}", callbackRequest.getType(), callbackRequest.getFilter(),
callbackRequest.getPage());
response = new ListAttributeResponseDTO();
response.setCode(0L);
break;
case LIST_ATTRIBUTE_VALUE:
log.debug("List attribute value request!|{}|{}|{}", callbackRequest.getType(),
callbackRequest.getFilter(), callbackRequest.getPage());
response = new ListAttributeValueResponseDTO();
response.setCode(0L);
break;
case LIST_INSTANCE_BY_POLICY:
log.debug("List instance by policy request!|{}|{}|{}", callbackRequest.getType(),
callbackRequest.getFilter(), callbackRequest.getPage());
response = new ListInstanceByPolicyResponseDTO();
response.setCode(0L);
break;
case SEARCH_INSTANCE:
response = searchInstanceResp(callbackRequest);
break;
default:
log.error("Unknown callback method!|{}|{}|{}|{}", callbackRequest.getMethod(),
callbackRequest.getType(), callbackRequest.getFilter(), callbackRequest.getPage());
Expand Down
Loading

0 comments on commit b858725

Please sign in to comment.