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

Commit

Permalink
Move code to build runs queries into a separate function
Browse files Browse the repository at this point in the history
Signed-off-by: Eamonn Mansour <[email protected]>
  • Loading branch information
eamansour committed Aug 14, 2024
1 parent 73e7faa commit d65ed22
Showing 1 changed file with 53 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,57 +332,8 @@ 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;

Expand Down Expand Up @@ -507,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

0 comments on commit d65ed22

Please sign in to comment.