Search in sources :

Example 11 with SearchRequest

use of org.pmiops.workbench.model.SearchRequest in project workbench by all-of-us.

the class CohortBuilderControllerTest method countSubjectsDemoAge.

@Test
public void countSubjectsDemoAge() throws Exception {
    LocalDate birthdate = LocalDate.of(1980, 8, 01);
    LocalDate now = LocalDate.now();
    Integer age = Period.between(birthdate, now).getYears();
    Criteria demoAge = createDemoCriteria("DEMO", "AGE", null);
    SearchParameter demo = createSearchParameter(demoAge, null);
    demo.attribute(new Attribute().operator("=").operands(Arrays.asList(age.toString())));
    SearchRequest searchRequests = createSearchRequests(demoAge.getType(), Arrays.asList(demo));
    assertParticipants(controller.countParticipants(cdrVersion.getCdrVersionId(), searchRequests), 1);
}
Also used : SearchRequest(org.pmiops.workbench.model.SearchRequest) Attribute(org.pmiops.workbench.model.Attribute) Criteria(org.pmiops.workbench.cdr.model.Criteria) SearchParameter(org.pmiops.workbench.model.SearchParameter) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Example 12 with SearchRequest

use of org.pmiops.workbench.model.SearchRequest in project workbench by all-of-us.

the class CohortBuilderControllerTest method countSubjectsICD10ConditionOccurrenceChild.

@Test
public void countSubjectsICD10ConditionOccurrenceChild() throws Exception {
    SearchParameter icd10 = createSearchParameter(icd10ConditionChild, "A09");
    SearchRequest searchRequest = createSearchRequests(icd10ConditionChild.getType(), Arrays.asList(icd10));
    assertParticipants(controller.countParticipants(cdrVersion.getCdrVersionId(), searchRequest), 1);
}
Also used : SearchRequest(org.pmiops.workbench.model.SearchRequest) SearchParameter(org.pmiops.workbench.model.SearchParameter) Test(org.junit.Test)

Example 13 with SearchRequest

use of org.pmiops.workbench.model.SearchRequest in project workbench by all-of-us.

the class CohortBuilderControllerTest method countSubjectsICD9MeasurementParent.

@Test
public void countSubjectsICD9MeasurementParent() throws Exception {
    SearchParameter icd9 = createSearchParameter(icd9MeasurementParent, "003");
    SearchRequest searchRequest = createSearchRequests(icd9MeasurementParent.getType(), Arrays.asList(icd9));
    assertParticipants(controller.countParticipants(cdrVersion.getCdrVersionId(), searchRequest), 1);
}
Also used : SearchRequest(org.pmiops.workbench.model.SearchRequest) SearchParameter(org.pmiops.workbench.model.SearchParameter) Test(org.junit.Test)

Example 14 with SearchRequest

use of org.pmiops.workbench.model.SearchRequest in project workbench by all-of-us.

the class CohortBuilderControllerTest method createSearchRequests.

private SearchRequest createSearchRequests(String type, List<SearchParameter> parameters) {
    final SearchGroupItem searchGroupItem = new SearchGroupItem().type(type).searchParameters(parameters);
    final SearchGroup searchGroup = new SearchGroup().addItemsItem(searchGroupItem);
    List<SearchGroup> groups = new ArrayList<>();
    groups.add(searchGroup);
    return new SearchRequest().includes(groups);
}
Also used : SearchRequest(org.pmiops.workbench.model.SearchRequest) SearchGroup(org.pmiops.workbench.model.SearchGroup) ArrayList(java.util.ArrayList) SearchGroupItem(org.pmiops.workbench.model.SearchGroupItem)

Example 15 with SearchRequest

use of org.pmiops.workbench.model.SearchRequest in project workbench by all-of-us.

the class ParticipantCounter method buildQuery.

public QueryJobConfiguration buildQuery(ParticipantCriteria participantCriteria, String sqlTemplate, String endSql, String mainTable, Map<String, QueryParameterValue> params) {
    SearchRequest request = participantCriteria.getSearchRequest();
    StringBuilder queryBuilder = new StringBuilder(sqlTemplate.replace("${mainTable}", mainTable));
    if (request == null) {
        queryBuilder.append(PERSON_ID_WHITELIST_TEMPLATE.replace("${mainTable}", mainTable));
        params.put(PERSON_ID_WHITELIST_PARAM, QueryParameterValue.array(participantCriteria.getParticipantIdsToInclude().toArray(new Long[0]), Long.class));
    } else {
        domainLookupService.findCodesForEmptyDomains(request.getIncludes());
        domainLookupService.findCodesForEmptyDomains(request.getExcludes());
        if (request.getIncludes().isEmpty() && request.getExcludes().isEmpty()) {
            throw new BadRequestException("Invalid SearchRequest: includes[] and excludes[] cannot both be empty");
        }
        // build query for included search groups
        StringJoiner joiner = buildQuery(request.getIncludes(), mainTable, params, false);
        // if includes is empty then don't add the excludes clause
        if (joiner.toString().isEmpty()) {
            joiner.merge(buildQuery(request.getExcludes(), mainTable, params, false));
        } else {
            joiner.merge(buildQuery(request.getExcludes(), mainTable, params, true));
        }
        Set<Long> participantIdsToExclude = participantCriteria.getParticipantIdsToExclude();
        if (!participantIdsToExclude.isEmpty()) {
            joiner.add(PERSON_ID_BLACKLIST_TEMPLATE.replace("${mainTable}", mainTable));
            params.put(PERSON_ID_BLACKLIST_PARAM, QueryParameterValue.array(participantIdsToExclude.toArray(new Long[0]), Long.class));
        }
        queryBuilder.append(joiner.toString());
    }
    queryBuilder.append(endSql.replace("${mainTable}", mainTable));
    return QueryJobConfiguration.newBuilder(queryBuilder.toString()).setNamedParameters(params).setUseLegacySql(false).build();
}
Also used : SearchRequest(org.pmiops.workbench.model.SearchRequest) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) StringJoiner(java.util.StringJoiner)

Aggregations

SearchRequest (org.pmiops.workbench.model.SearchRequest)31 Test (org.junit.Test)27 SearchParameter (org.pmiops.workbench.model.SearchParameter)27 SearchGroup (org.pmiops.workbench.model.SearchGroup)8 SearchGroupItem (org.pmiops.workbench.model.SearchGroupItem)8 Criteria (org.pmiops.workbench.cdr.model.Criteria)6 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)4 LocalDate (java.time.LocalDate)3 Attribute (org.pmiops.workbench.model.Attribute)3 ArrayList (java.util.ArrayList)2 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)2 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 StringJoiner (java.util.StringJoiner)1 CdrVersion (org.pmiops.workbench.db.model.CdrVersion)1 CohortReview (org.pmiops.workbench.db.model.CohortReview)1 Workspace (org.pmiops.workbench.db.model.Workspace)1 NotFoundException (org.pmiops.workbench.exceptions.NotFoundException)1 MaterializeCohortResponse (org.pmiops.workbench.model.MaterializeCohortResponse)1