refactor(项目配置): 任务中心报告清理问题

This commit is contained in:
song-cc-rock 2024-11-29 18:36:30 +08:00 committed by Craftsman
parent 8bde662985
commit 143b4ba455
5 changed files with 137 additions and 97 deletions

View File

@ -0,0 +1,114 @@
package io.metersphere.api.service;
import io.metersphere.api.domain.*;
import io.metersphere.api.mapper.*;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Component
public class CleanupApiReportDetailServiceImpl {
@Resource
private ApiReportMapper apiReportMapper;
@Resource
private ApiReportStepMapper apiReportStepMapper;
@Resource
private ApiReportDetailMapper apiReportDetailMapper;
@Resource
private ApiReportLogMapper apiReportLogMapper;
@Resource
private ApiScenarioReportMapper apiScenarioReportMapper;
@Resource
private ApiScenarioReportStepMapper apiScenarioReportStepMapper;
@Resource
private ApiScenarioReportDetailMapper apiScenarioReportDetailMapper;
@Resource
private ApiScenarioReportLogMapper apiScenarioReportLogMapper;
@Resource
private ApiScenarioReportDetailBlobMapper apiScenarioReportDetailBlobMapper;
@Resource
private ApiReportRelateTaskMapper apiReportRelateTaskMapper;
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void cleanApiReportOrDetail(List<String> cleanIds) {
ApiReportExample reportExample = new ApiReportExample();
reportExample.createCriteria().andIdIn(cleanIds);
ApiReport report = new ApiReport();
report.setDeleted(true);
apiReportMapper.updateByExampleSelective(report, reportExample);
// 任务执行结果存在报告明细做保留
List<String> taskReportIds = getTaskReportIds(cleanIds);
cleanIds.removeAll(taskReportIds);
deleteApiReportDetail(cleanIds);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void cleanScenarioReportOrDetail(List<String> cleanIds) {
ApiScenarioReportExample reportExample = new ApiScenarioReportExample();
reportExample.createCriteria().andIdIn(cleanIds);
ApiScenarioReport report = new ApiScenarioReport();
report.setDeleted(true);
apiScenarioReportMapper.updateByExampleSelective(report, reportExample);
// 任务执行结果存在报告明细做保留
List<String> taskReportIds = getTaskReportIds(cleanIds);
cleanIds.removeAll(taskReportIds);
deleteScenarioReportDetail(cleanIds);
}
/**
* 获取任务报告ID
*
* @param reportIds 报告ID集合
* @return 任务报告ID集合
*/
private List<String> getTaskReportIds(List<String> reportIds) {
ApiReportRelateTaskExample example = new ApiReportRelateTaskExample();
example.createCriteria().andReportIdIn(reportIds);
List<ApiReportRelateTask> relateTasks = apiReportRelateTaskMapper.selectByExample(example);
return relateTasks.stream().map(ApiReportRelateTask::getReportId).toList();
}
/**
* 清理接口报告明细
* @param ids 报告ID集合
*/
private void deleteApiReportDetail(List<String> ids) {
if (CollectionUtils.isNotEmpty(ids)) {
ApiReportStepExample stepExample = new ApiReportStepExample();
stepExample.createCriteria().andReportIdIn(ids);
apiReportStepMapper.deleteByExample(stepExample);
ApiReportDetailExample detailExample = new ApiReportDetailExample();
detailExample.createCriteria().andReportIdIn(ids);
apiReportDetailMapper.deleteByExample(detailExample);
ApiReportLogExample logExample = new ApiReportLogExample();
logExample.createCriteria().andReportIdIn(ids);
apiReportLogMapper.deleteByExample(logExample);
}
}
/**
* 清理场景报告明细
* @param ids 报告ID集合
*/
private void deleteScenarioReportDetail(List<String> ids) {
if (CollectionUtils.isNotEmpty(ids)) {
ApiScenarioReportStepExample stepExample = new ApiScenarioReportStepExample();
stepExample.createCriteria().andReportIdIn(ids);
apiScenarioReportStepMapper.deleteByExample(stepExample);
ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample();
detailExample.createCriteria().andReportIdIn(ids);
apiScenarioReportDetailMapper.deleteByExample(detailExample);
ApiScenarioReportDetailBlobExample blobExample = new ApiScenarioReportDetailBlobExample();
blobExample.createCriteria().andReportIdIn(ids);
apiScenarioReportDetailBlobMapper.deleteByExample(blobExample);
ApiScenarioReportLogExample logExample = new ApiScenarioReportLogExample();
logExample.createCriteria().andReportIdIn(ids);
apiScenarioReportLogMapper.deleteByExample(logExample);
}
}
}

View File

@ -1,9 +1,10 @@
package io.metersphere.api.service;
import io.metersphere.api.domain.*;
import io.metersphere.api.mapper.*;
import io.metersphere.api.mapper.ExtApiReportMapper;
import io.metersphere.api.mapper.ExtApiScenarioReportMapper;
import io.metersphere.sdk.constants.ProjectApplicationType;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.system.service.BaseCleanUpReport;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
@ -19,108 +20,31 @@ import static io.metersphere.sdk.util.ShareUtil.getCleanDate;
@Transactional(rollbackFor = Exception.class)
public class CleanupApiReportServiceImpl implements BaseCleanUpReport {
@Resource
private ApiReportMapper apiReportMapper;
@Resource
private ExtApiReportMapper extApiReportMapper;
@Resource
private ApiReportStepMapper apiReportStepMapper;
@Resource
private ApiReportDetailMapper apiReportDetailMapper;
@Resource
private ApiReportLogMapper apiReportLogMapper;
@Resource
private ApiScenarioReportMapper apiScenarioReportMapper;
@Resource
private ExtApiScenarioReportMapper extApiScenarioReportMapper;
@Resource
private ApiScenarioReportStepMapper apiScenarioReportStepMapper;
@Resource
private ApiScenarioReportDetailMapper apiScenarioReportDetailMapper;
@Resource
private ApiScenarioReportLogMapper apiScenarioReportLogMapper;
@Resource
private ApiScenarioReportDetailBlobMapper apiScenarioReportDetailBlobMapper;
@Resource
private ApiReportRelateTaskMapper apiReportRelateTaskMapper;
private CleanupApiReportDetailServiceImpl cleanupApiReportDetailService;
@Override
public void cleanReport(Map<String, String> map, String projectId) {
LogUtils.info("清理当前项目[" + projectId + "]相关接口测试报告");
String expr = map.get(ProjectApplicationType.API.API_CLEAN_REPORT.name());
long timeMills = getCleanDate(expr);
int apiReportCount = extApiReportMapper.countApiReportByTime(timeMills, projectId);
while (apiReportCount > 0) {
List<String> ids = extApiReportMapper.selectApiReportByProjectIdAndTime(timeMills, projectId);
ApiReportExample reportExample = new ApiReportExample();
reportExample.createCriteria().andIdIn(ids);
ApiReport report = new ApiReport();
report.setDeleted(true);
apiReportMapper.updateByExampleSelective(report, reportExample);
// 任务执行结果存在报告明细做保留
List<String> taskReportIds = getTaskReportIds(ids);
ids.removeAll(taskReportIds);
if (CollectionUtils.isNotEmpty(ids)) {
deleteApiReport(ids);
}
apiReportCount = extApiReportMapper.countApiReportByTime(timeMills, projectId);
List<String> apiReportIds = extApiReportMapper.selectApiReportByProjectIdAndTime(timeMills, projectId);
List<String> scenarioReportIds = extApiScenarioReportMapper.selectApiReportByProjectIdAndTime(timeMills, projectId);
LogUtils.info("清理当前项目[" + projectId + "]相关接口测试报告, 共[" + (apiReportIds.size() + scenarioReportIds.size()) + "]条");
if (CollectionUtils.isNotEmpty(apiReportIds)) {
SubListUtils.dealForSubList(apiReportIds, 100, (subIds) -> {
cleanupApiReportDetailService.cleanApiReportOrDetail(subIds);
});
}
int scenarioReportCount = extApiScenarioReportMapper.countScenarioReportByTime(timeMills, projectId);
while (scenarioReportCount > 0) {
List<String> ids = extApiScenarioReportMapper.selectApiReportByProjectIdAndTime(timeMills, projectId);
ApiScenarioReportExample reportExample = new ApiScenarioReportExample();
reportExample.createCriteria().andIdIn(ids);
ApiScenarioReport report = new ApiScenarioReport();
report.setDeleted(true);
apiScenarioReportMapper.updateByExampleSelective(report, reportExample);
// 任务执行结果存在报告明细做保留
List<String> taskReportIds = getTaskReportIds(ids);
ids.removeAll(taskReportIds);
if (CollectionUtils.isNotEmpty(ids)) {
deleteScenarioReport(ids);
}
scenarioReportCount = extApiScenarioReportMapper.countScenarioReportByTime(timeMills, projectId);
if (CollectionUtils.isNotEmpty(scenarioReportIds)) {
SubListUtils.dealForSubList(scenarioReportIds, 100, (subIds) -> {
cleanupApiReportDetailService.cleanScenarioReportOrDetail(subIds);
});
}
LogUtils.info("清理当前项目[" + projectId + "]相关接口测试报告结束!");
}
/**
* 获取任务报告ID
*
* @param reportIds 报告ID集合
* @return 任务报告ID集合
*/
private List<String> getTaskReportIds(List<String> reportIds) {
ApiReportRelateTaskExample example = new ApiReportRelateTaskExample();
example.createCriteria().andReportIdIn(reportIds);
List<ApiReportRelateTask> relateTasks = apiReportRelateTaskMapper.selectByExample(example);
return relateTasks.stream().map(ApiReportRelateTask::getReportId).toList();
}
private void deleteApiReport(List<String> ids) {
ApiReportStepExample stepExample = new ApiReportStepExample();
stepExample.createCriteria().andReportIdIn(ids);
apiReportStepMapper.deleteByExample(stepExample);
ApiReportDetailExample detailExample = new ApiReportDetailExample();
detailExample.createCriteria().andReportIdIn(ids);
apiReportDetailMapper.deleteByExample(detailExample);
ApiReportLogExample logExample = new ApiReportLogExample();
logExample.createCriteria().andReportIdIn(ids);
apiReportLogMapper.deleteByExample(logExample);
}
private void deleteScenarioReport(List<String> ids) {
ApiScenarioReportStepExample stepExample = new ApiScenarioReportStepExample();
stepExample.createCriteria().andReportIdIn(ids);
apiScenarioReportStepMapper.deleteByExample(stepExample);
ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample();
detailExample.createCriteria().andReportIdIn(ids);
apiScenarioReportDetailMapper.deleteByExample(detailExample);
ApiScenarioReportDetailBlobExample blobExample = new ApiScenarioReportDetailBlobExample();
blobExample.createCriteria().andReportIdIn(ids);
apiScenarioReportDetailBlobMapper.deleteByExample(blobExample);
ApiScenarioReportLogExample logExample = new ApiScenarioReportLogExample();
logExample.createCriteria().andReportIdIn(ids);
apiScenarioReportLogMapper.deleteByExample(logExample);
}
}

View File

@ -51,6 +51,7 @@ public class CleanupTaskResultServiceImpl implements BaseCleanUpReport {
}
List<String> cleanTaskItemIds = execTaskItems.stream().map(ExecTaskItem::getId).toList();
List<String> cleanIds = ListUtils.union(cleanTaskIds, cleanTaskItemIds);
LogUtils.info("清理当前项目[" + projectId + "]任务中心执行结果, 共[" + cleanIds.size() + "]条");
if (CollectionUtils.isNotEmpty(cleanIds)) {
// 清理任务-报告关系表
SubListUtils.dealForSubList(cleanIds, 100, ids -> {
@ -59,6 +60,6 @@ public class CleanupTaskResultServiceImpl implements BaseCleanUpReport {
apiReportRelateTaskMapper.deleteByExample(example);
});
}
LogUtils.info("清理当前项目[" + projectId + "]任务中心执行结果结束");
LogUtils.info("清理当前项目[" + projectId + "]任务中心执行结果结束!");
}
}

View File

@ -49,6 +49,7 @@ public class CleanupTaskServiceImpl implements BaseCleanUpReport {
execTaskItems = execTaskItemMapper.selectByExample(itemExample);
}
List<String> cleanTaskItemIds = execTaskItems.stream().map(ExecTaskItem::getId).toList();
LogUtils.info("清理当前项目[" + projectId + "]即时任务, 共[" + (cleanTaskIds.size() + cleanTaskItemIds.size()) + "]条");
if (CollectionUtils.isNotEmpty(cleanTaskIds)) {
ExecTaskExample example = new ExecTaskExample();
example.createCriteria().andIdIn(cleanTaskIds);
@ -63,6 +64,6 @@ public class CleanupTaskServiceImpl implements BaseCleanUpReport {
execTaskItem.setDeleted(true);
execTaskItemMapper.updateByExampleSelective(execTaskItem, example);
}
LogUtils.info("清理当前项目[" + projectId + "]即时任务结束");
LogUtils.info("清理当前项目[" + projectId + "]即时任务结束!");
}
}

View File

@ -24,12 +24,12 @@ public class CleanupTestPlanReportServiceImpl implements BaseCleanUpReport {
@Override
public void cleanReport(Map<String, String> map, String projectId) {
LogUtils.info("清理当前项目[" + projectId + "]相关测试计划报告开始:");
LogUtils.info("清理当前项目[" + projectId + "]测试计划报告");
String expr = map.get(ProjectApplicationType.TEST_PLAN.TEST_PLAN_CLEAN_REPORT.name());
long timeMills = getCleanDate(expr);
List<String> ids = extTestPlanReportMapper.selectReportIdByProjectIdAndTime(timeMills, projectId);
LogUtils.info("清理当前项目[" + projectId + "]相关测试计划报告,共[" + ids.size() + "]条");
LogUtils.info("清理当前项目[" + projectId + "]测试计划报告, 共[" + ids.size() + "]条");
testPlanReportService.cleanAndDeleteReport(ids);
LogUtils.info("清理当前项目[" + projectId + "]相关测试计划报告结束!");
LogUtils.info("清理当前项目[" + projectId + "]测试计划报告结束!");
}
}