fix(UI 自动化): 删除调试类型报告截图同时也删除报告数据

This commit is contained in:
junhong 2022-06-21 14:06:06 +08:00 committed by John Bro
parent 898c8e7985
commit f4c7542189
4 changed files with 56 additions and 7 deletions

View File

@ -1,5 +1,5 @@
package io.metersphere.api.dto.automation;
public enum ExecuteType {
Saved, Completed, Debug, Marge, Deleted
Saved, Completed, Debug, Marge
}

View File

@ -31,7 +31,7 @@ import io.metersphere.service.SystemParameterService;
import io.metersphere.service.UserService;
import io.metersphere.track.dto.PlanReportCaseDTO;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.xpack.ui.service.UiScenarioReportStructureService;
import io.metersphere.xmind.utils.FileUtil;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
@ -44,6 +44,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.*;
@ -91,8 +92,6 @@ public class ApiScenarioReportService {
private UiReportServiceProxy uiReportServiceProxy;
@Resource
private ExtApiScenarioReportResultMapper extApiScenarioReportResultMapper;
@Resource
private UiScenarioReportStructureService uiScenarioReportStructureService;
public void saveResult(ResultDTO dto) {
// 报告详情内容
@ -638,7 +637,7 @@ public class ApiScenarioReportService {
// UI 类型报告需要删除报告产生的截图
List<String> ids = new ArrayList<>();
ids.add(request.getId());
uiScenarioReportStructureService.cleanUpReport(ids);
this.cleanUpUiReportImg(ids);
}
// 补充逻辑如果是集成报告则把零时报告全部删除
@ -761,7 +760,7 @@ public class ApiScenarioReportService {
if(!BooleanUtils.isNotTrue(reportRequest.getIsUi())){
// UI 类型报告需要删除报告产生的截图
uiScenarioReportStructureService.cleanUpReport(ids);
this.cleanUpUiReportImg(ids);
}
//处理最后剩余的数据
@ -1006,4 +1005,14 @@ public class ApiScenarioReportService {
public RequestResult selectReportContent(String stepId) {
return apiScenarioReportStructureService.selectReportContent(stepId);
}
public void cleanUpUiReportImg(List<String> ids){
if(ids != null && CollectionUtils.isNotEmpty(ids)){
for(String id : ids){
if(FileUtil.deleteDir(new File(FileUtils.UI_IMAGE_DIR + "/" + id))){
LogUtil.info("删除 UI 报告截图成功,报告 ID 为 " + id);
}
}
}
}
}

View File

@ -56,7 +56,7 @@ public class CleanUpReportJob extends MsScheduleJob {
this.doCleanUp(projectService::cleanUpLoadReport, config.getCleanLoadReportExpr());
}
// 定时删除 UI 调试模式生成的截图
uiScenarioReportStructureService.cleanUpReport(null);
projectService.cleanUpUiReportImg();
} catch (Exception e) {
LogUtil.error("clean up report error.");
LogUtil.error(e.getMessage(), e);

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.metersphere.api.dto.DeleteAPITestRequest;
import io.metersphere.api.dto.QueryAPITestRequest;
import io.metersphere.api.dto.automation.ExecuteType;
import io.metersphere.api.service.*;
import io.metersphere.api.tcp.TCPPool;
import io.metersphere.base.domain.*;
@ -37,6 +38,7 @@ import io.metersphere.track.service.TestCaseService;
import io.metersphere.track.service.TestPlanProjectService;
import io.metersphere.track.service.TestPlanReportService;
import io.metersphere.track.service.TestPlanService;
import io.metersphere.xmind.utils.FileUtil;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
@ -47,6 +49,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@ -114,6 +117,14 @@ public class ProjectService {
ExtModuleNodeMapper extModuleNodeMapper;
@Resource
ApiModuleMapper apiModuleMapper;
@Resource
private ApiScenarioReportMapper apiScenarioReportMapper;
@Resource
private ApiScenarioReportDetailMapper apiScenarioReportDetailMapper;
@Resource
private ApiScenarioReportStructureMapper apiScenarioReportStructureMapper;
@Resource
private ApiScenarioReportResultMapper apiScenarioReportResultMapper;
public Project addProject(AddProjectRequest project) {
if (StringUtils.isBlank(project.getName())) {
@ -1016,6 +1027,35 @@ public class ProjectService {
performanceReportService.cleanUpReport(time, projectId);
}
// 删除 UI 报告产生的截图
public void cleanUpUiReportImg(){
// 属于定时任务删除调试报告情况
// 获取昨天的当前时间
Date backupTime = org.apache.commons.lang3.time.DateUtils.addDays(new Date(), -1);
// 清理类型为 UI 报告类型且时间为昨天之前的 UI 调试类型报告截图
ApiScenarioReportExample example = new ApiScenarioReportExample();
example.createCriteria().andCreateTimeLessThan(backupTime.getTime()).andReportTypeEqualTo(ReportTypeConstants.UI_INDEPENDENT.name())
.andExecuteTypeEqualTo(ExecuteType.Debug.name());
List<ApiScenarioReport> apiScenarioReports = apiScenarioReportMapper.selectByExample(example);
// 删除调试报告的截图
for(ApiScenarioReport apiScenarioReport : apiScenarioReports){
if(FileUtil.deleteDir(new File(FileUtils.UI_IMAGE_DIR + "/" + apiScenarioReport.getId()))){
LogUtil.info("删除 UI 调试报告截图成功,报告 ID 为 " + apiScenarioReport.getId());
// 删除调试报告
ApiScenarioReportResultExample resultExample = new ApiScenarioReportResultExample();
resultExample.createCriteria().andReportIdEqualTo(apiScenarioReport.getId());
ApiScenarioReportStructureExample structureExample = new ApiScenarioReportStructureExample();
structureExample.createCriteria().andReportIdEqualTo(apiScenarioReport.getId());
apiScenarioReportDetailMapper.deleteByPrimaryKey(apiScenarioReport.getId());
apiScenarioReportResultMapper.deleteByExample(resultExample);
apiScenarioReportStructureMapper.deleteByExample(structureExample);
apiScenarioReportMapper.deleteByPrimaryKey(apiScenarioReport.getId());
}
}
}
public void checkProjectIsRepeatable(String projectId) {
Project project = this.getProjectById(projectId);
if (project == null) {