Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Stop going through artifact documents when querying CouchDB for sever…
Browse files Browse the repository at this point in the history
…al runs (#263)

* Avoid loading artifact paths when getting several runs

Signed-off-by: Eamonn Mansour <[email protected]>

* Increase couchdb query results limit to 100 instead of 25

Signed-off-by: Eamonn Mansour <[email protected]>

* Move code to build runs queries into a separate function

Signed-off-by: Eamonn Mansour <[email protected]>

* Empty commit to kick off build

Signed-off-by: Eamonn Mansour <[email protected]>

* Add more search criteria to unit test

Signed-off-by: Eamonn Mansour <[email protected]>

---------

Signed-off-by: Eamonn Mansour <[email protected]>
  • Loading branch information
eamansour authored Aug 15, 2024
1 parent 6aa9f90 commit b1ea642
Show file tree
Hide file tree
Showing 3 changed files with 404 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class CouchdbDirectoryService implements IResultArchiveStoreDirectoryServ

private final CouchdbRasStore store;

private static final int COUCHDB_RESULTS_LIMIT_PER_QUERY = 100;

public CouchdbDirectoryService(CouchdbRasStore store, LogFactory logFactory, HttpRequestFactory requestFactory) {
this.store = store;
this.logFactory = logFactory;
Expand All @@ -85,10 +87,13 @@ public boolean isLocal() {
return false;
}

private Path getRunArtifactPath(TestStructureCouchdb ts) throws CouchdbRasException {

private CouchdbRasFileSystemProvider createFileSystemProvider() {
ResultArchiveStoreFileStore fileStore = new ResultArchiveStoreFileStore();
CouchdbRasFileSystemProvider runProvider = new CouchdbRasFileSystemProvider(fileStore, store, logFactory);
return new CouchdbRasFileSystemProvider(fileStore, store, logFactory);
}

private Path getRunArtifactPath(TestStructureCouchdb ts) throws CouchdbRasException {
CouchdbRasFileSystemProvider runProvider = createFileSystemProvider();
if (ts.getArtifactRecordIds() == null || ts.getArtifactRecordIds().isEmpty()) {
return runProvider.getRoot();
}
Expand Down Expand Up @@ -327,58 +332,10 @@ private CouchdbRunResult fetchRun(String id) throws ParseException, IOException,

HttpPost httpPost = requestFactory.getHttpPostRequest(store.getCouchdbUri() + "/galasa_run/_find");

JsonObject selector = new JsonObject();
JsonArray and = new JsonArray();
selector.add("$and", and);

for(IRasSearchCriteria searchCriteria : searchCriterias) {
if (searchCriteria instanceof RasSearchCriteriaRequestor) {
RasSearchCriteriaRequestor sRequestor = (RasSearchCriteriaRequestor) searchCriteria;

inArray(and, "requestor", sRequestor.getRequestors());
} else if (searchCriteria instanceof RasSearchCriteriaRunName) {
RasSearchCriteriaRunName sRunName = (RasSearchCriteriaRunName) searchCriteria;

inArray(and, "runName", sRunName.getRunNames());
} else if (searchCriteria instanceof RasSearchCriteriaQueuedFrom) {
RasSearchCriteriaQueuedFrom sFrom = (RasSearchCriteriaQueuedFrom) searchCriteria;

JsonObject criteria = new JsonObject();
JsonObject jFrom = new JsonObject();
jFrom.addProperty("$gte", sFrom.getFrom().toString());
criteria.add("queued", jFrom);
and.add(criteria);
} else if (searchCriteria instanceof RasSearchCriteriaQueuedTo) {
RasSearchCriteriaQueuedTo sTo = (RasSearchCriteriaQueuedTo) searchCriteria;

JsonObject criteria = new JsonObject();
JsonObject jTo = new JsonObject();
jTo.addProperty("$lt", sTo.getTo().toString());
criteria.add("queued", jTo);
and.add(criteria);
} else if (searchCriteria instanceof RasSearchCriteriaTestName) {
RasSearchCriteriaTestName sTestName = (RasSearchCriteriaTestName) searchCriteria;

inArray(and, "testName", sTestName.getTestNames());
} else if (searchCriteria instanceof RasSearchCriteriaBundle) {
RasSearchCriteriaBundle sBundle = (RasSearchCriteriaBundle) searchCriteria;

inArray(and, "bundle", sBundle.getBundles());
} else if (searchCriteria instanceof RasSearchCriteriaResult) {
RasSearchCriteriaResult sResult = (RasSearchCriteriaResult) searchCriteria;

inArray(and, "result", sResult.getResults());
} else if(searchCriteria instanceof RasSearchCriteriaStatus){
RasSearchCriteriaStatus sStatus = (RasSearchCriteriaStatus) searchCriteria;
inArray(and, "status", sStatus.getStatusesAsStrings());
} else {
throw new ResultArchiveStoreException("Unrecognised search criteria class " + searchCriteria.getClass().getName());
}
}

Find find = new Find();
find.selector = selector;
find.selector = buildGetRunsQuery(searchCriterias);
find.execution_stats = true;
find.limit = COUCHDB_RESULTS_LIMIT_PER_QUERY;

while (true) {
String requestContent = store.getGson().toJson(find);
Expand Down Expand Up @@ -407,12 +364,12 @@ private CouchdbRunResult fetchRun(String id) throws ParseException, IOException,
}

for (TestStructureCouchdb ts : found.docs) {
Path runArtifactPath = getRunArtifactPath(ts);

// *** Add this run to the results
CouchdbRunResult cdbrr = new CouchdbRunResult(store, ts, runArtifactPath);
if (ts.isValid()) {
runs.add(cdbrr);
// Don't load the artifacts for the found runs, just set a root location for artifacts
CouchdbRasFileSystemProvider runProvider = createFileSystemProvider();

// Add this run to the results
runs.add(new CouchdbRunResult(store, ts, runProvider.getRoot()));
}
}

Expand Down Expand Up @@ -501,6 +458,58 @@ private String getRevision(String databaseName, String id) throws ResultArchiveS
}
}

private JsonObject buildGetRunsQuery(IRasSearchCriteria... searchCriterias) throws ResultArchiveStoreException {
JsonObject selector = new JsonObject();
JsonArray and = new JsonArray();
selector.add("$and", and);

for(IRasSearchCriteria searchCriteria : searchCriterias) {
if (searchCriteria instanceof RasSearchCriteriaRequestor) {
RasSearchCriteriaRequestor sRequestor = (RasSearchCriteriaRequestor) searchCriteria;

inArray(and, "requestor", sRequestor.getRequestors());
} else if (searchCriteria instanceof RasSearchCriteriaRunName) {
RasSearchCriteriaRunName sRunName = (RasSearchCriteriaRunName) searchCriteria;

inArray(and, "runName", sRunName.getRunNames());
} else if (searchCriteria instanceof RasSearchCriteriaQueuedFrom) {
RasSearchCriteriaQueuedFrom sFrom = (RasSearchCriteriaQueuedFrom) searchCriteria;

JsonObject criteria = new JsonObject();
JsonObject jFrom = new JsonObject();
jFrom.addProperty("$gte", sFrom.getFrom().toString());
criteria.add("queued", jFrom);
and.add(criteria);
} else if (searchCriteria instanceof RasSearchCriteriaQueuedTo) {
RasSearchCriteriaQueuedTo sTo = (RasSearchCriteriaQueuedTo) searchCriteria;

JsonObject criteria = new JsonObject();
JsonObject jTo = new JsonObject();
jTo.addProperty("$lt", sTo.getTo().toString());
criteria.add("queued", jTo);
and.add(criteria);
} else if (searchCriteria instanceof RasSearchCriteriaTestName) {
RasSearchCriteriaTestName sTestName = (RasSearchCriteriaTestName) searchCriteria;

inArray(and, "testName", sTestName.getTestNames());
} else if (searchCriteria instanceof RasSearchCriteriaBundle) {
RasSearchCriteriaBundle sBundle = (RasSearchCriteriaBundle) searchCriteria;

inArray(and, "bundle", sBundle.getBundles());
} else if (searchCriteria instanceof RasSearchCriteriaResult) {
RasSearchCriteriaResult sResult = (RasSearchCriteriaResult) searchCriteria;

inArray(and, "result", sResult.getResults());
} else if(searchCriteria instanceof RasSearchCriteriaStatus) {
RasSearchCriteriaStatus sStatus = (RasSearchCriteriaStatus) searchCriteria;
inArray(and, "status", sStatus.getStatusesAsStrings());
} else {
throw new ResultArchiveStoreException("Unrecognised search criteria class " + searchCriteria.getClass().getName());
}
}
return selector;
}

private void inArray(JsonArray and, String field, String[] inArray) {
if (inArray == null || inArray.length < 1) {
return;
Expand Down
Loading

0 comments on commit b1ea642

Please sign in to comment.