Skip to content

Commit

Permalink
Feat/rolling expiration extension (#139)
Browse files Browse the repository at this point in the history
* feat: extend mockers expiration

* feat: extend mockers expiration

* chore: update pom ver

* chore: update pom ver
  • Loading branch information
QizhengMo authored Dec 26, 2023
1 parent b3235b5 commit 1084b8c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.arextest.model.mock.MockCategoryType;
import com.arextest.model.mock.Mocker;
import java.util.Date;
import java.util.List;

/**
Expand All @@ -16,6 +17,15 @@ public interface RepositoryWriter<T extends Mocker> {

long removeBy(MockCategoryType categoryType, String recordId);

/**
* To extend all mockers of the given category type and record id to the given expire time.
* @param categoryType the category type of the mockers to be extended
* @param recordId the record id of the mockers to be extended
* @param expireTime the expiration time to be extended to
* @return document count of the mockers extended
*/
long extendExpirationTo(MockCategoryType categoryType, String recordId, Date expireTime);

boolean update(T value);

long removeByAppId(MockCategoryType categoryType, String appId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ public long removeBy(MockCategoryType categoryType, String recordId) {
return deleteResult.getDeletedCount();
}

@Override
public long extendExpirationTo(MockCategoryType categoryType, String recordId, Date expireTime) {
MongoCollection<AREXMocker> collectionSource = createOrGetCollection(categoryType);
return collectionSource.updateMany(buildRecordIdFilter(categoryType, recordId),
Updates.set(EXPIRATION_TIME_COLUMN_NAME, expireTime)).getModifiedCount();
}
@Override
public long removeByAppId(MockCategoryType categoryType, String appId) {
MongoCollection<AREXMocker> collectionSource = createOrGetCollection(categoryType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@AllArgsConstructor
public class ScenePoolProviderImpl extends AbstractScenePoolProvider {
private final String providerName;
private static final long EXPIRATION_DAYS = 14L;

@Override
public String getProviderName() {
return providerName;
Expand All @@ -31,7 +33,7 @@ public void upsertOne(Scene scene) {
Bson filter = Filters.and(Filters.eq(Fields.appId, scene.getAppId()),
Filters.eq(Fields.sceneKey, scene.getSceneKey()));

Date expire = Date.from(LocalDateTime.now().plusDays(30).atZone(ZoneId.systemDefault()).toInstant());
Date expire = Date.from(LocalDateTime.now().plusDays(EXPIRATION_DAYS).atZone(ZoneId.systemDefault()).toInstant());
Bson update = Updates.combine(
Updates.set(Fields.appId, scene.getAppId()),
Updates.set(Fields.sceneKey, scene.getSceneKey()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
import com.arextest.model.mock.Mocker;
import com.arextest.storage.repository.RepositoryProvider;
import com.arextest.storage.repository.RepositoryProviderFactory;
import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collections;
import java.util.Comparator;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -206,6 +212,23 @@ public boolean removeByRecordId(String providerName, String recordId) {
return true;
}

public boolean extendMockerExpirationByRecordId(String providerName, String recordId, Long extensionDays) {
RepositoryProvider<?> repositoryWriter = providerFactory.findProvider(providerName);
if (repositoryWriter == null) {
LOGGER.warn("Could not found provider for {}", providerName);
return false;
}
long updateCount = 0;
for (MockCategoryType categoryType : providerFactory.getCategoryTypes()) {
updateCount += repositoryWriter.extendExpirationTo(categoryType, recordId,
Date.from(LocalDateTime.now().plusDays(extensionDays).atZone(
ZoneId.systemDefault()).toInstant()));
}
LOGGER.info("extendMockerExpirationByRecordId updated {} mockers for recordId: {}",
updateCount, recordId);
return updateCount > 0;
}

private List<Mocker> createTargetList(List<AREXMocker> srcMockers, String targetRecordId) {
List<Mocker> targetList = null;
long now = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private void transferEntry(AREXMocker coverageMocker, String incomingCaseId) {
private class RecordTask implements Runnable {
private final ScenePoolProvider scenePoolProvider;
private final AREXMocker coverageMocker;
private static final long EXPIRATION_EXTENSION_DAYS = 14L;
@Override
public void run() {
try {
Expand All @@ -180,13 +181,16 @@ public void run() {
LOGGER.info("CoverageMockerHandler received existing case, recordId: {}, pathKey: {}",
coverageMocker.getRecordId(), coverageMocker.getOperationName());
} else {
// new scene: extend mocker expiration and insert scene
Scene scene = new Scene();
scene.setSceneKey(sceneKey);
scene.setAppId(appId);
scene.setRecordId(recordId);
scene.setExecutionPath(executionPath);

scenePoolProvider.upsertOne(scene);
mockSourceEditionService.extendMockerExpirationByRecordId(ProviderNames.DEFAULT,
coverageMocker.getRecordId(), EXPIRATION_EXTENSION_DAYS);
LOGGER.info("CoverageMockerHandler received new case, recordId: {}, pathKey: {}",
coverageMocker.getRecordId(), coverageMocker.getOperationName());
}
Expand Down

0 comments on commit 1084b8c

Please sign in to comment.