Skip to content

Commit

Permalink
feat: add caseTag
Browse files Browse the repository at this point in the history
  • Loading branch information
rchen9 committed Dec 11, 2023
1 parent bba96ac commit 8118a48
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import com.arextest.web.core.repository.ReportPlanStatisticRepository;
import com.arextest.web.core.repository.mongo.util.MongoHelper;
import com.arextest.web.model.contract.contracts.QueryPlanStatisticsRequestType;
import com.arextest.web.model.dao.mongodb.ModelBase;
import com.arextest.web.model.dao.mongodb.ReportPlanStatisticCollection;
import com.arextest.web.model.dao.mongodb.ReportPlanStatisticCollection.Fields;
import com.arextest.web.model.dto.LatestDailySuccessPlanIdDto;
import com.arextest.web.model.dto.ReportPlanStatisticDto;
import com.arextest.web.model.enums.ReplayStatusType;
import com.arextest.web.model.mapper.PlanMapper;
import com.mongodb.BasicDBObject;
import com.mongodb.client.result.DeleteResult;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -46,39 +46,16 @@
@Component
public class ReportPlanStatisticRepositoryImpl implements ReportPlanStatisticRepository {

private static final String PLAN_ID = "planId";
private static final String PLAN_NAME = "planName";
private static final String STATUS = "status";
private static final String APP_ID = "appId";
private static final String APP_NAME = "appName";
private static final String CREATOR = "creator";
private static final String TARGET_IMAGE_ID = "targetImageId";
private static final String TARGET_IMAGE_NAME = "targetImageName";
private static final String CASE_SOURCE_TYPE = "caseSourceType";
private static final String SOURCE_ENV = "sourceEnv";
private static final String TARGET_ENV = "targetEnv";
private static final String SOURCE_HOST = "sourceHost";
private static final String TARGET_HOST = "targetHost";
private static final String CORE_VERSION = "coreVersion";
private static final String EXT_VERSION = "extVersion";
private static final String CASE_RECORD_VERSION = "caseRecordVersion";
private static final String REPLAY_START_TIME = "replayStartTime";
private static final String REPLAY_END_TIME = "replayEndTime";
private static final String CASE_START_TIME = "caseStartTime";
private static final String CASE_END_TIME = "caseEndTime";
private static final String DATA_CHANGE_CREATE_TIME = "dataChangeCreateTime";
private static final String TOTAL_CASE_COUNT = "totalCaseCount";
private static final String DATE_TIME = "dateTime";
private static final String CUSTOM_TAGS = "customTags";
private static final String ERROR_MESSAGE = "errorMessage";

@Resource
private MongoTemplate mongoTemplate;

@Override
public List<ReportPlanStatisticDto> findByDataCreateTimeBetween(Date startTime, Date endTime) {
Query query = new Query();
Criteria criteria = Criteria.where(DATA_CHANGE_CREATE_TIME).gte(startTime).lte(endTime);
Criteria criteria = Criteria.where(ModelBase.Fields.dataChangeCreateTime).gte(startTime)
.lte(endTime);
query.addCriteria(criteria);
List<ReportPlanStatisticCollection> result = mongoTemplate.find(query,
ReportPlanStatisticCollection.class);
Expand All @@ -91,7 +68,7 @@ public ReportPlanStatisticDto findByPlanId(String planId) {
return null;
}
ReportPlanStatisticCollection result =
mongoTemplate.findOne(Query.query(Criteria.where(PLAN_ID).is(planId)),
mongoTemplate.findOne(Query.query(Criteria.where(Fields.planId).is(planId)),
ReportPlanStatisticCollection.class);
return PlanMapper.INSTANCE.dtoFromDao(result);
}
Expand All @@ -103,74 +80,78 @@ public boolean findAndModifyBaseInfo(ReportPlanStatisticDto result) {
}

Update update = MongoHelper.getUpdate();
update.setOnInsert(DATA_CHANGE_CREATE_TIME, System.currentTimeMillis());
update.setOnInsert(ModelBase.Fields.dataChangeCreateTime, System.currentTimeMillis());

if (result.getCaseStartTime() != null) {
update.set(CASE_START_TIME, result.getCaseStartTime());
update.set(Fields.caseStartTime, result.getCaseStartTime());
}
if (result.getCaseEndTime() != null) {
update.set(CASE_END_TIME, result.getCaseEndTime());
update.set(Fields.caseEndTime, result.getCaseEndTime());
}
if (!StringUtils.isEmpty(result.getPlanName())) {
update.set(PLAN_NAME, result.getPlanName());
update.set(Fields.planName, result.getPlanName());
}
if (result.getStatus() != null) {
update.set(STATUS, result.getStatus());
update.set(Fields.status, result.getStatus());
}
if (!StringUtils.isEmpty(result.getAppId())) {
update.set(APP_ID, result.getAppId());
update.set(Fields.appId, result.getAppId());
}
if (!StringUtils.isEmpty(result.getAppName())) {
update.set(APP_NAME, result.getAppName());
update.set(Fields.appName, result.getAppName());
}
if (!StringUtils.isEmpty(result.getCreator())) {
update.set(CREATOR, result.getCreator());
update.set(Fields.creator, result.getCreator());
}
if (result.getTargetImageId() != null) {
update.set(TARGET_IMAGE_ID, result.getTargetImageId());
update.set(Fields.targetImageId, result.getTargetImageId());
}
if (!StringUtils.isEmpty(result.getTargetImageName())) {
update.set(TARGET_IMAGE_NAME, result.getTargetImageName());
update.set(Fields.targetImageName, result.getTargetImageName());
}
if (result.getCaseSourceType() != null) {
update.set(CASE_SOURCE_TYPE, result.getCaseSourceType());
update.set(Fields.caseSourceType, result.getCaseSourceType());
}
if (!StringUtils.isEmpty(result.getSourceEnv())) {
update.set(SOURCE_ENV, result.getSourceEnv());
update.set(Fields.sourceEnv, result.getSourceEnv());
}
if (!StringUtils.isEmpty(result.getTargetEnv())) {
update.set(TARGET_ENV, result.getTargetEnv());
update.set(Fields.targetEnv, result.getTargetEnv());
}
if (!StringUtils.isEmpty(result.getSourceHost())) {
update.set(SOURCE_HOST, result.getSourceHost());
update.set(Fields.sourceHost, result.getSourceHost());
}
if (!StringUtils.isEmpty(result.getTargetHost())) {
update.set(TARGET_HOST, result.getTargetHost());
update.set(Fields.targetHost, result.getTargetHost());
}
if (!StringUtils.isEmpty(result.getCoreVersion())) {
update.set(CORE_VERSION, result.getCoreVersion());
update.set(Fields.coreVersion, result.getCoreVersion());
}
if (!StringUtils.isEmpty(result.getExtVersion())) {
update.set(EXT_VERSION, result.getExtVersion());
update.set(Fields.extVersion, result.getExtVersion());
}
if (!StringUtils.isEmpty(result.getCaseRecordVersion())) {
update.set(CASE_RECORD_VERSION, result.getCaseRecordVersion());
update.set(Fields.caseRecordVersion, result.getCaseRecordVersion());
}
if (result.getTotalCaseCount() != null) {
update.set(TOTAL_CASE_COUNT, result.getTotalCaseCount());
update.set(Fields.totalCaseCount, result.getTotalCaseCount());
}
if (result.getReplayStartTime() != null) {
update.set(REPLAY_START_TIME, result.getReplayStartTime());
update.set(Fields.replayStartTime, result.getReplayStartTime());
}
if (result.getReplayEndTime() != null) {
update.set(REPLAY_END_TIME, result.getReplayEndTime());
update.set(Fields.replayEndTime, result.getReplayEndTime());
}
if (MapUtils.isNotEmpty(result.getCustomTags())) {
update.set(CUSTOM_TAGS, result.getCustomTags());
update.set(Fields.customTags, result.getCustomTags());
}
if (MapUtils.isNotEmpty(result.getCaseTags())) {
update.set(Fields.caseTags, result.getCaseTags());
}

ReportPlanStatisticCollection dao =
mongoTemplate.findAndModify(Query.query(Criteria.where(PLAN_ID).is(result.getPlanId())),
mongoTemplate.findAndModify(
Query.query(Criteria.where(Fields.planId).is(result.getPlanId())),
update,
FindAndModifyOptions.options().returnNew(true).upsert(true),
ReportPlanStatisticCollection.class);
Expand All @@ -189,7 +170,7 @@ public Pair<List<ReportPlanStatisticDto>, Long> pageQueryPlanStatistic(

Pageable pageable =
PageRequest.of(request.getPageIndex() - 1, request.getPageSize(),
Sort.by(Sort.Direction.DESC, PLAN_ID));
Sort.by(Sort.Direction.DESC, Fields.planId));
query.with(pageable);

List<ReportPlanStatisticCollection> daos = mongoTemplate.find(query,
Expand All @@ -207,7 +188,7 @@ public Long findReplayCount() {

@Override
public Long findReplayCountByAppId(String appId) {
Query query = Query.query(Criteria.where(APP_ID).is(appId));
Query query = Query.query(Criteria.where(Fields.appId).is(appId));
return mongoTemplate.count(query, ReportPlanStatisticCollection.class);
}

Expand All @@ -233,12 +214,14 @@ public List<ReportPlanStatisticDto> findLatestSuccessPlanId(String rangeField, L
SortOperation sortOperation = Aggregation.sort(sort);
operations.add(sortOperation);

GroupOperation groupOperation = Aggregation.group(APP_ID, APP_NAME).first(PLAN_ID).as(PLAN_ID)
.first(DATA_CHANGE_CREATE_TIME).as(DATA_CHANGE_CREATE_TIME);
GroupOperation groupOperation = Aggregation.group(Fields.appId, Fields.appName)
.first(Fields.planId).as(Fields.planId)
.first(ModelBase.Fields.dataChangeCreateTime).as(ModelBase.Fields.dataChangeCreateTime);
operations.add(groupOperation);

ProjectionOperation projectionOperation =
Aggregation.project(PLAN_ID, DATA_CHANGE_CREATE_TIME, APP_ID, APP_NAME);
Aggregation.project(Fields.planId, ModelBase.Fields.dataChangeCreateTime, Fields.appId,
Fields.appName);
operations.add(projectionOperation);
AggregationResults<BasicDBObject> aggregate = mongoTemplate.aggregate(
Aggregation.newAggregation(operations),
Expand Down Expand Up @@ -275,7 +258,8 @@ public List<LatestDailySuccessPlanIdDto> findLatestDailySuccessPlanId(String ran
SortOperation sortOperation = Aggregation.sort(sort);
operations.add(sortOperation);

ProjectionOperation projectionOperation = Aggregation.project(PLAN_ID, DATA_CHANGE_CREATE_TIME,
ProjectionOperation projectionOperation = Aggregation.project(Fields.planId,
ModelBase.Fields.dataChangeCreateTime,
groupField)
.and(DateOperators.DateToString.dateOf(new AggregationExpression() {
@Override
Expand All @@ -289,7 +273,8 @@ public Document toDocument(AggregationOperationContext aggregationOperationConte
GroupOperation groupOperation =
Aggregation.group(DATE_TIME, groupField).first(groupField).as(groupField).first(DATE_TIME)
.as(DATE_TIME)
.first(PLAN_ID).as(PLAN_ID).first(DATA_CHANGE_CREATE_TIME).as(DATA_CHANGE_CREATE_TIME);
.first(Fields.planId).as(Fields.planId).first(ModelBase.Fields.dataChangeCreateTime)
.as(ModelBase.Fields.dataChangeCreateTime);
operations.add(groupOperation);

AggregationResults<BasicDBObject> aggregate = mongoTemplate.aggregate(
Expand All @@ -308,40 +293,41 @@ public ReportPlanStatisticDto changePlanStatus(String planId, Integer status,
}
Update update = MongoHelper.getUpdate();
if (status != null) {
update.set(STATUS, status);
update.set(Fields.status, status);
}
if (totalCaseCount != null) {
update.set(TOTAL_CASE_COUNT, totalCaseCount);
update.set(Fields.totalCaseCount, totalCaseCount);
}
if (errorMessage != null) {
update.set(ERROR_MESSAGE, errorMessage);
update.set(Fields.errorMessage, errorMessage);
}
if (!rerun) {
update.set(REPLAY_END_TIME, System.currentTimeMillis());
update.set(Fields.replayEndTime, System.currentTimeMillis());
}
if (update.getUpdateObject().keySet().size() == 0) {
if (update.getUpdateObject().keySet().isEmpty()) {
return null;
}
ReportPlanStatisticCollection plan =
mongoTemplate.findAndModify(Query.query(Criteria.where(PLAN_ID).is(planId)), update,
mongoTemplate.findAndModify(Query.query(Criteria.where(Fields.planId).is(planId)), update,
FindAndModifyOptions.options().returnNew(true).upsert(true),
ReportPlanStatisticCollection.class);
return PlanMapper.INSTANCE.dtoFromDao(plan);
}

@Override
public boolean deletePlan(String planId) {
Query query = Query.query(Criteria.where(PLAN_ID).is(planId));
Query query = Query.query(Criteria.where(Fields.planId).is(planId));
DeleteResult deleteResult = mongoTemplate.remove(query, ReportPlanStatisticCollection.class);
return deleteResult.getDeletedCount() > 0;
}

@Override
public boolean removeErrorMsg(String planId) {
Query query = Query.query(Criteria.where(PLAN_ID).is(planId));
Query query = Query.query(Criteria.where(Fields.planId).is(planId));
Update update = MongoHelper.getUpdate();
update.set(ERROR_MESSAGE, null);
return mongoTemplate.updateMulti(query, update, ReportPlanStatisticCollection.class).getMatchedCount() > 0;
update.set(Fields.errorMessage, null);
return mongoTemplate.updateMulti(query, update, ReportPlanStatisticCollection.class)
.getMatchedCount() > 0;
}

private Query fillFilterConditions(QueryPlanStatisticsRequestType request) {
Expand All @@ -350,15 +336,15 @@ private Query fillFilterConditions(QueryPlanStatisticsRequestType request) {
return query;
}
if (request.getPlanId() != null) {
query.addCriteria(Criteria.where(PLAN_ID).is(request.getPlanId()));
query.addCriteria(Criteria.where(Fields.planId).is(request.getPlanId()));
}

if (!StringUtils.isEmpty(request.getAppId())) {
query.addCriteria(Criteria.where(APP_ID).is(request.getAppId()));
query.addCriteria(Criteria.where(Fields.appId).is(request.getAppId()));
}

if (!StringUtils.isEmpty(request.getImageId())) {
query.addCriteria(Criteria.where(TARGET_IMAGE_ID).is(request.getImageId()));
query.addCriteria(Criteria.where(Fields.targetImageId).is(request.getImageId()));
}

return query;
Expand All @@ -371,10 +357,11 @@ private ReportPlanStatisticDto covertToReportPlanStatisticDto(BasicDBObject basi
if (basicDBObject == null) {
return reportPlanStatisticDto;
}
reportPlanStatisticDto.setAppId(basicDBObject.getString(APP_ID));
reportPlanStatisticDto.setAppName(basicDBObject.getString(APP_NAME));
reportPlanStatisticDto.setPlanId(basicDBObject.getString(PLAN_ID));
reportPlanStatisticDto.setDataChangeCreateTime(basicDBObject.getLong(DATA_CHANGE_CREATE_TIME));
reportPlanStatisticDto.setAppId(basicDBObject.getString(Fields.appId));
reportPlanStatisticDto.setAppName(basicDBObject.getString(Fields.appName));
reportPlanStatisticDto.setPlanId(basicDBObject.getString(Fields.planId));
reportPlanStatisticDto.setDataChangeCreateTime(
basicDBObject.getLong(ModelBase.Fields.dataChangeCreateTime));
return reportPlanStatisticDto;
}

Expand All @@ -385,10 +372,10 @@ private LatestDailySuccessPlanIdDto covertToLatestDailySuccessPlanIdDto(
return latestDailySuccessPlanIdDto;
}
latestDailySuccessPlanIdDto.setDateTime(basicDBObject.getString(DATE_TIME));
latestDailySuccessPlanIdDto.setAppId(basicDBObject.getString(APP_ID));
latestDailySuccessPlanIdDto.setPlanId(basicDBObject.getString(PLAN_ID));
latestDailySuccessPlanIdDto.setAppId(basicDBObject.getString(Fields.appId));
latestDailySuccessPlanIdDto.setPlanId(basicDBObject.getString(Fields.planId));
latestDailySuccessPlanIdDto.setDataChangeCreateTime(
basicDBObject.getLong(DATA_CHANGE_CREATE_TIME));
basicDBObject.getLong(ModelBase.Fields.dataChangeCreateTime));
return latestDailySuccessPlanIdDto;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static class CaseSourceEnvironment {

private Integer caseSourceType;

private Map<String, Object> caseTags;

private Long caseStartTime;

private Long caseEndTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public class ReportPlanStatisticCollection extends ModelBase {
private String targetImageId;
private String targetImageName;

private Integer caseSourceType;
private String sourceEnv;
private String targetEnv;
private String sourceHost;
private String targetHost;

private String coreVersion;

private String extVersion;

private Integer caseSourceType;
private String caseRecordVersion;
private Map<String, Object> caseTags;
private Long caseStartTime;
private Long caseEndTime;

private Long replayStartTime;
private Long replayEndTime;
private Long caseStartTime;
private Long caseEndTime;

private Integer totalCaseCount;
private String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class ReportPlanStatisticDto {
private String targetImageId;
private String targetImageName;

private Integer caseSourceType;
private String sourceEnv;
private String targetEnv;
private String sourceHost;
Expand All @@ -33,6 +32,10 @@ public class ReportPlanStatisticDto {

private Long replayEndTime;

private Integer caseSourceType;

private Map<String, Object> caseTags;

private Long caseStartTime;

private Long caseEndTime;
Expand Down
Loading

0 comments on commit 8118a48

Please sign in to comment.