Skip to content

Commit

Permalink
perf: 定时任务详情数据加载方式优化 #3284
Browse files Browse the repository at this point in the history
1.列表接口去除变量信息;
2.详情接口已存在:/web/scope/{scopeType}/{scopeId}/cron/job/{cronJobId}。
  • Loading branch information
jsonwan committed Nov 14, 2024
1 parent d6e673b commit 0b48e2e
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Data
Expand Down Expand Up @@ -65,9 +63,6 @@ public class ServiceCronJobDTO {
@ApiModelProperty("单次执行的指定执行时间戳")
private Long executeTime;

@ApiModelProperty("变量信息")
private List<ServiceCronJobVariableDTO> variableValue;

@ApiModelProperty("上次执行结果 0 - 未执行 1 - 成功 2 - 失败")
private Integer lastExecuteStatus;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,15 @@ void listCronJobByCondition() {
baseSearchCondition.setOrderField("last_modify_user");
baseSearchCondition.setCreator(CRON_JOB_1.getCreator());
PageData<CronJobInfoDTO> cronJobInfoPageData =
cronJobDAO.listPageCronJobsByCondition(cronJobCondition, baseSearchCondition);
cronJobDAO.listPageCronJobsWithoutVarsByCondition(cronJobCondition, baseSearchCondition);
assertThat(cronJobInfoPageData.getStart()).isEqualTo(0);
assertThat(cronJobInfoPageData.getPageSize()).isEqualTo(2);
assertThat(cronJobInfoPageData.getTotal()).isEqualTo(4);
assertThat(cronJobInfoPageData.getData()).contains(CRON_JOB_5);
assertThat(cronJobInfoPageData.getData()).contains(CRON_JOB_1);

baseSearchCondition.setStart(2);
cronJobInfoPageData = cronJobDAO.listPageCronJobsByCondition(cronJobCondition, baseSearchCondition);
cronJobInfoPageData = cronJobDAO.listPageCronJobsWithoutVarsByCondition(cronJobCondition, baseSearchCondition);
assertThat(cronJobInfoPageData.getStart()).isEqualTo(2);
assertThat(cronJobInfoPageData.getPageSize()).isEqualTo(2);
assertThat(cronJobInfoPageData.getTotal()).isEqualTo(4);
Expand All @@ -433,7 +433,7 @@ void listCronJobByCondition() {
baseSearchCondition.setStart(0);

baseSearchCondition.setOrder(1);
cronJobInfoPageData = cronJobDAO.listPageCronJobsByCondition(cronJobCondition, baseSearchCondition);
cronJobInfoPageData = cronJobDAO.listPageCronJobsWithoutVarsByCondition(cronJobCondition, baseSearchCondition);
assertThat(cronJobInfoPageData.getStart()).isEqualTo(0);
assertThat(cronJobInfoPageData.getPageSize()).isEqualTo(2);
assertThat(cronJobInfoPageData.getTotal()).isEqualTo(4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public EsbResp<List<EsbCronInfoResponse>> getCronList(String username,
request.getLastModifyTimeEnd(), "yyyy-MM-dd", ChronoUnit.SECONDS, ZoneId.systemDefault()));
}
PageData<CronJobInfoDTO> cronJobInfoPageData =
cronJobService.listPageCronJobInfos(cronJobCondition, baseSearchCondition);
cronJobService.listPageCronJobInfosWithoutVars(cronJobCondition, baseSearchCondition);
return EsbResp.buildSuccessResp(cronJobInfoPageData.getData().stream()
.map(CronJobInfoDTO::toEsbCronInfo).collect(Collectors.toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public EsbResp<EsbPageDataV3<EsbCronInfoV3DTO>> getCronListUsingPost(String user
baseSearchCondition.setLastModifyTimeStart(request.getLastModifyTimeStart());
baseSearchCondition.setLastModifyTimeEnd(request.getLastModifyTimeEnd());
}
PageData<CronJobInfoDTO> cronJobInfoPageData = cronJobService.listPageCronJobInfos(cronJobCondition,
PageData<CronJobInfoDTO> cronJobInfoPageData = cronJobService.listPageCronJobInfosWithoutVars(cronJobCondition,
baseSearchCondition);
List<EsbCronInfoV3DTO> cronInfoV3ResponseData = cronJobInfoPageData.getData().stream()
.peek(cronJobInfoDTO -> cronJobInfoDTO.setVariableValue(null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected SearchInstanceResponseDTO searchInstanceResp(

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

return IamRespUtil.getSearchInstanceRespFromPageData(cronJobInfoPageData, this::convert);
}
Expand All @@ -114,7 +114,7 @@ protected ListInstanceResponseDTO listInstanceResp(
BaseSearchCondition baseSearchCondition = basicQueryCond.getRight();

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

return IamRespUtil.getListInstanceRespFromPageData(cronJobInfoPageData, this::convert);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public InternalResponse<List<ServiceCronJobDTO>> listCronJobs(Long appId, Boolea
BaseSearchCondition baseSearchCondition = new BaseSearchCondition();
baseSearchCondition.setStart(0);
baseSearchCondition.setLength(Integer.MAX_VALUE);
List<CronJobInfoDTO> cronJobDTOList = cronJobService.listPageCronJobInfos(cronJobInfoDTO,
List<CronJobInfoDTO> cronJobDTOList = cronJobService.listPageCronJobInfosWithoutVars(cronJobInfoDTO,
baseSearchCondition).getData();
List<ServiceCronJobDTO> resultData = cronJobDTOList.stream()
.map(CronJobInfoDTO::toServiceCronJobDTO).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Response<PageData<CronJobVO>> listCronJobs(String username,
}
baseSearchCondition.setOrder(order);
PageData<CronJobInfoDTO> cronJobInfoPageData =
cronJobService.listPageCronJobInfos(cronJobCondition, baseSearchCondition);
cronJobService.listPageCronJobInfosWithoutVars(cronJobCondition, baseSearchCondition);
List<CronJobVO> resultCronJobs = new ArrayList<>();
cronJobInfoPageData.getData().forEach(cronJobInfo -> resultCronJobs.add(CronJobInfoDTO.toVO(cronJobInfo)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public interface CronJobDAO {
List<CronJobWithVarsDTO> listBasicCronJobWithHostVars(List<Long> appIdList, int start, int limit);

/**
* 根据业务 ID 列表批量查询定时任务信息
* 根据业务 ID 列表批量查询定时任务信息(不含变量)
*
* @param cronJobCondition 查询参数
* @param baseSearchCondition 分页信息
* @return 分页的定时任务信息列表
*/
PageData<CronJobInfoDTO> listPageCronJobsByCondition(CronJobInfoDTO cronJobCondition,
BaseSearchCondition baseSearchCondition);
PageData<CronJobInfoDTO> listPageCronJobsWithoutVarsByCondition(CronJobInfoDTO cronJobCondition,
BaseSearchCondition baseSearchCondition);

/**
* 根据定时任务 ID 查询定时任务信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.OrderField;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Record21;
import org.jooq.Record3;
Expand Down Expand Up @@ -117,8 +118,8 @@ public List<CronJobWithVarsDTO> listBasicCronJobWithHostVars(List<Long> appIdLis
}

@Override
public PageData<CronJobInfoDTO> listPageCronJobsByCondition(CronJobInfoDTO cronJobCondition,
BaseSearchCondition baseSearchCondition) {
public PageData<CronJobInfoDTO> listPageCronJobsWithoutVarsByCondition(CronJobInfoDTO cronJobCondition,
BaseSearchCondition baseSearchCondition) {
List<Condition> conditions = buildConditionList(cronJobCondition, baseSearchCondition);
long templateCount = getPageCronJobCount(conditions);
List<OrderField<?>> orderFields = new ArrayList<>();
Expand All @@ -143,17 +144,35 @@ public PageData<CronJobInfoDTO> listPageCronJobsByCondition(CronJobInfoDTO cronJ
int start = baseSearchCondition.getStartOrDefault(0);
int length = baseSearchCondition.getLengthOrDefault(10);

Result<Record21<ULong, ULong, String, String, ULong, ULong, String, ULong, String, ULong, String, UByte, UByte,
UByte, ULong, String, ULong, ULong, ULong, String, String>> records =
context
.select(TABLE.ID, TABLE.APP_ID, TABLE.NAME, TABLE.CREATOR, TABLE.TASK_TEMPLATE_ID,
TABLE.TASK_PLAN_ID, TABLE.SCRIPT_ID, TABLE.SCRIPT_VERSION_ID, TABLE.CRON_EXPRESSION,
TABLE.EXECUTE_TIME, TABLE.VARIABLE_VALUE, TABLE.LAST_EXECUTE_STATUS, TABLE.IS_ENABLE,
TABLE.IS_DELETED, TABLE.CREATE_TIME, TABLE.LAST_MODIFY_USER, TABLE.LAST_MODIFY_TIME,
TABLE.END_TIME, TABLE.NOTIFY_OFFSET, TABLE.NOTIFY_USER, TABLE.NOTIFY_CHANNEL)
.from(TABLE).where(conditions).orderBy(orderFields).limit(start, length).fetch();
Result<?> records =
context.select(
TABLE.ID,
TABLE.APP_ID,
TABLE.NAME,
TABLE.CREATOR,
TABLE.TASK_TEMPLATE_ID,
TABLE.TASK_PLAN_ID,
TABLE.SCRIPT_ID,
TABLE.SCRIPT_VERSION_ID,
TABLE.CRON_EXPRESSION,
TABLE.EXECUTE_TIME,
TABLE.LAST_EXECUTE_STATUS,
TABLE.IS_ENABLE,
TABLE.IS_DELETED,
TABLE.CREATE_TIME,
TABLE.LAST_MODIFY_USER,
TABLE.LAST_MODIFY_TIME,
TABLE.END_TIME,
TABLE.NOTIFY_OFFSET,
TABLE.NOTIFY_USER,
TABLE.NOTIFY_CHANNEL
).from(TABLE)
.where(conditions)
.orderBy(orderFields)
.limit(start, length)
.fetch();
List<CronJobInfoDTO> cronJobInfoList = new ArrayList<>();
if (records.size() >= 1) {
if (!records.isEmpty()) {
records.map(record -> cronJobInfoList.add(convertToCronJobDTO(record)));
}
PageData<CronJobInfoDTO> cronJobInfoPageData = new PageData<>();
Expand Down Expand Up @@ -568,8 +587,7 @@ public List<Long> listCronJobIds(CronJobInfoDTO cronJobInfoDTO) {
return cronJobIds.stream().map(ULong::longValue).collect(Collectors.toList());
}

private CronJobInfoDTO convertToCronJobDTO(Record21<ULong, ULong, String, String, ULong, ULong, String, ULong,
String, ULong, String, UByte, UByte, UByte, ULong, String, ULong, ULong, ULong, String, String> record) {
private CronJobInfoDTO convertToCronJobDTO(Record record) {
if (record == null) {
return null;
}
Expand All @@ -590,7 +608,9 @@ private CronJobInfoDTO convertToCronJobDTO(Record21<ULong, ULong, String, String
}
cronJobInfoDTO.setCronExpression(record.get(TABLE.CRON_EXPRESSION));
cronJobInfoDTO.setExecuteTime(DbUtils.convertJooqLongValue(record.get(TABLE.EXECUTE_TIME)));
cronJobInfoDTO.decryptAndSetVariableValue(record.get(TABLE.VARIABLE_VALUE));
if (record.indexOf(TABLE.VARIABLE_VALUE) != -1) {
cronJobInfoDTO.decryptAndSetVariableValue(record.get(TABLE.VARIABLE_VALUE));
}
standardizeDynamicGroupId(cronJobInfoDTO.getVariableValue());
cronJobInfoDTO.setLastExecuteStatus(record.get(TABLE.LAST_EXECUTE_STATUS).intValue());
cronJobInfoDTO.setEnable(record.get(TABLE.IS_ENABLE).intValue() == 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,6 @@ public ServiceCronJobDTO toServiceCronJobDTO() {
cronJob.setScriptVersionId(scriptVersionId);
cronJob.setCronExpression(cronExpression);
cronJob.setExecuteTime(executeTime);
if (CollectionUtils.isNotEmpty(variableValue)) {
cronJob.setVariableValue(variableValue.stream().map(CronJobVariableDTO::toServiceCronJobVariableDTO)
.collect(Collectors.toList()));
} else {
cronJob.setVariableValue(Collections.emptyList());
}
cronJob.setLastExecuteStatus(lastExecuteStatus);
cronJob.setEnable(enable);
cronJob.setLastModifyUser(lastModifyUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.crontab.model.CronJobVariableVO;
import com.tencent.bk.job.crontab.model.inner.ServerDTO;
import com.tencent.bk.job.crontab.model.inner.ServiceCronJobVariableDTO;
import com.tencent.bk.job.execute.model.inner.ServiceTaskVariable;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down Expand Up @@ -146,16 +145,6 @@ public static EsbGlobalVarV3DTO toEsbGlobalVarV3(CronJobVariableDTO cronJobVaria
return esbGlobalVar;
}

public ServiceCronJobVariableDTO toServiceCronJobVariableDTO() {
ServiceCronJobVariableDTO variable = new ServiceCronJobVariableDTO();
variable.setId(id);
variable.setName(name);
variable.setType(type);
variable.setValue(value);
variable.setServer(server);
return variable;
}

@SuppressWarnings("MethodDoesntCallSuperMethod")
@Override
public CronJobVariableDTO clone() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
*/
public interface CronJobService {
/**
* 分页查询定时任务列表
* 分页查询定时任务列表(不含变量)
*
* @param cronJobCondition 查询条件
* @param baseSearchCondition 搜索参数
* @return 分页后的定时任务列表
*/
PageData<CronJobInfoDTO> listPageCronJobInfos(CronJobInfoDTO cronJobCondition,
BaseSearchCondition baseSearchCondition);
PageData<CronJobInfoDTO> listPageCronJobInfosWithoutVars(CronJobInfoDTO cronJobCondition,
BaseSearchCondition baseSearchCondition);

/**
* 根据 ID 查询定时任务信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ private static String getNotifyJobName(long appId, long cronJobId) {
}

@Override
public PageData<CronJobInfoDTO> listPageCronJobInfos(CronJobInfoDTO cronJobCondition,
BaseSearchCondition baseSearchCondition) {
return cronJobDAO.listPageCronJobsByCondition(cronJobCondition, baseSearchCondition);
public PageData<CronJobInfoDTO> listPageCronJobInfosWithoutVars(CronJobInfoDTO cronJobCondition,
BaseSearchCondition baseSearchCondition) {
return cronJobDAO.listPageCronJobsWithoutVarsByCondition(cronJobCondition, baseSearchCondition);
}

@Override
Expand Down Expand Up @@ -279,7 +279,7 @@ public CronJobInfoDTO updateCronJobInfo(String username, CronJobInfoDTO cronJobI
return updateCron;
}

private void authExecuteTask(CronJobInfoDTO cronJobInfo){
private void authExecuteTask(CronJobInfoDTO cronJobInfo) {
try {
List<ServiceTaskVariable> taskVariables = null;
if (CollectionUtils.isNotEmpty(cronJobInfo.getVariableValue())) {
Expand Down

0 comments on commit 0b48e2e

Please sign in to comment.