Search in sources :

Example 1 with MaterializeCohortResponse

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

the class CohortsController method materializeCohort.

@Override
public ResponseEntity<MaterializeCohortResponse> materializeCohort(String workspaceNamespace, String workspaceId, MaterializeCohortRequest request) {
    // This also enforces registered auth domain.
    workspaceService.enforceWorkspaceAccessLevel(workspaceNamespace, workspaceId, WorkspaceAccessLevel.READER);
    Workspace workspace = workspaceService.getRequired(workspaceNamespace, workspaceId);
    CdrVersion cdrVersion = workspace.getCdrVersion();
    CdrVersionContext.setCdrVersion(cdrVersion);
    if (request.getCdrVersionName() != null) {
        cdrVersion = cdrVersionDao.findByName(request.getCdrVersionName());
        if (cdrVersion == null) {
            throw new NotFoundException(String.format("Couldn't find CDR version with name %s", request.getCdrVersionName()));
        }
    }
    String cohortSpec;
    CohortReview cohortReview = null;
    if (request.getCohortName() != null) {
        org.pmiops.workbench.db.model.Cohort cohort = cohortDao.findCohortByNameAndWorkspaceId(request.getCohortName(), workspace.getWorkspaceId());
        if (cohort == null) {
            throw new NotFoundException(String.format("Couldn't find cohort with name %s in workspace %s/%s", request.getCohortName(), workspaceNamespace, workspaceId));
        }
        cohortReview = cohortReviewDao.findCohortReviewByCohortIdAndCdrVersionId(cohort.getCohortId(), cdrVersion.getCdrVersionId());
        cohortSpec = cohort.getCriteria();
    } else if (request.getCohortSpec() != null) {
        cohortSpec = request.getCohortSpec();
        if (request.getStatusFilter() != null) {
            throw new BadRequestException("statusFilter cannot be used with cohortSpec");
        }
    } else {
        throw new BadRequestException("Must specify either cohortName or cohortSpec");
    }
    Integer pageSize = request.getPageSize();
    if (pageSize == null || pageSize == 0) {
        request.setPageSize(DEFAULT_PAGE_SIZE);
    } else if (pageSize < 0) {
        throw new BadRequestException(String.format("Invalid page size: %s; must be between 1 and %d", pageSize, MAX_PAGE_SIZE));
    } else if (pageSize > MAX_PAGE_SIZE) {
        request.setPageSize(MAX_PAGE_SIZE);
    }
    SearchRequest searchRequest;
    try {
        searchRequest = new Gson().fromJson(cohortSpec, SearchRequest.class);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid cohort spec");
    }
    MaterializeCohortResponse response = cohortMaterializationService.materializeCohort(cohortReview, searchRequest, request);
    return ResponseEntity.ok(response);
}
Also used : CdrVersion(org.pmiops.workbench.db.model.CdrVersion) SearchRequest(org.pmiops.workbench.model.SearchRequest) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) Gson(com.google.gson.Gson) MaterializeCohortResponse(org.pmiops.workbench.model.MaterializeCohortResponse) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) CohortReview(org.pmiops.workbench.db.model.CohortReview) Workspace(org.pmiops.workbench.db.model.Workspace)

Example 2 with MaterializeCohortResponse

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

the class CohortMaterializationServiceTest method testMaterializeCohortPersonFieldSetPersonIdWithStringLessThanNullNonMatch.

@Test
public void testMaterializeCohortPersonFieldSetPersonIdWithStringLessThanNullNonMatch() {
    TableQuery tableQuery = new TableQuery();
    tableQuery.setTableName("person");
    tableQuery.setColumns(ImmutableList.of("person_id"));
    ColumnFilter filter = new ColumnFilter();
    filter.setColumnName("ethnicity_source_value");
    filter.setOperator(Operator.LESS_THAN);
    filter.setValue("esv");
    tableQuery.addFiltersItem(ImmutableList.of(filter));
    FieldSet fieldSet = new FieldSet();
    fieldSet.setTableQuery(tableQuery);
    MaterializeCohortResponse response = cohortMaterializationService.materializeCohort(null, SearchRequests.allGenders(), makeRequest(fieldSet, 1000));
    assertPersonIds(response);
    assertThat(response.getNextPageToken()).isNull();
}
Also used : FieldSet(org.pmiops.workbench.model.FieldSet) TableQuery(org.pmiops.workbench.model.TableQuery) ColumnFilter(org.pmiops.workbench.model.ColumnFilter) MaterializeCohortResponse(org.pmiops.workbench.model.MaterializeCohortResponse) BigQueryBaseTest(org.pmiops.workbench.api.BigQueryBaseTest) Test(org.junit.Test)

Example 3 with MaterializeCohortResponse

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

the class CohortMaterializationServiceTest method testMaterializeCohortPersonFieldSetPersonIdWithNumberGreaterThanFilter.

@Test
public void testMaterializeCohortPersonFieldSetPersonIdWithNumberGreaterThanFilter() {
    TableQuery tableQuery = new TableQuery();
    tableQuery.setTableName("person");
    tableQuery.setColumns(ImmutableList.of("person_id"));
    ColumnFilter filter = new ColumnFilter();
    filter.setColumnName("person_id");
    filter.setOperator(Operator.GREATER_THAN);
    filter.setValueNumber(new BigDecimal(2L));
    tableQuery.addFiltersItem(ImmutableList.of(filter));
    FieldSet fieldSet = new FieldSet();
    fieldSet.setTableQuery(tableQuery);
    MaterializeCohortResponse response = cohortMaterializationService.materializeCohort(null, SearchRequests.allGenders(), makeRequest(fieldSet, 1000));
    assertPersonIds(response, 102246L);
    assertThat(response.getNextPageToken()).isNull();
}
Also used : FieldSet(org.pmiops.workbench.model.FieldSet) TableQuery(org.pmiops.workbench.model.TableQuery) ColumnFilter(org.pmiops.workbench.model.ColumnFilter) BigDecimal(java.math.BigDecimal) MaterializeCohortResponse(org.pmiops.workbench.model.MaterializeCohortResponse) BigQueryBaseTest(org.pmiops.workbench.api.BigQueryBaseTest) Test(org.junit.Test)

Example 4 with MaterializeCohortResponse

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

the class CohortMaterializationServiceTest method testMaterializeCohortWithReviewNotExcludedFilter.

@Test
public void testMaterializeCohortWithReviewNotExcludedFilter() {
    MaterializeCohortRequest request = makeRequest(2);
    request.setStatusFilter(ImmutableList.of(CohortStatus.NOT_REVIEWED, CohortStatus.INCLUDED, CohortStatus.NEEDS_FURTHER_REVIEW));
    MaterializeCohortResponse response = cohortMaterializationService.materializeCohort(cohortReview, SearchRequests.allGenders(), request);
    // With a not excluded status filter, ID 2 is not returned.
    assertPersonIds(response, 1L, 102246L);
    assertThat(response.getNextPageToken()).isNull();
}
Also used : MaterializeCohortRequest(org.pmiops.workbench.model.MaterializeCohortRequest) MaterializeCohortResponse(org.pmiops.workbench.model.MaterializeCohortResponse) BigQueryBaseTest(org.pmiops.workbench.api.BigQueryBaseTest) Test(org.junit.Test)

Example 5 with MaterializeCohortResponse

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

the class CohortMaterializationServiceTest method testMaterializeCohortWithReviewJustExcludedFilter.

@Test
public void testMaterializeCohortWithReviewJustExcludedFilter() {
    MaterializeCohortRequest request = makeRequest(2);
    request.setStatusFilter(ImmutableList.of(CohortStatus.EXCLUDED));
    MaterializeCohortResponse response = cohortMaterializationService.materializeCohort(cohortReview, SearchRequests.allGenders(), request);
    assertPersonIds(response, 2L);
    assertThat(response.getNextPageToken()).isNull();
}
Also used : MaterializeCohortRequest(org.pmiops.workbench.model.MaterializeCohortRequest) MaterializeCohortResponse(org.pmiops.workbench.model.MaterializeCohortResponse) BigQueryBaseTest(org.pmiops.workbench.api.BigQueryBaseTest) Test(org.junit.Test)

Aggregations

MaterializeCohortResponse (org.pmiops.workbench.model.MaterializeCohortResponse)56 Test (org.junit.Test)52 BigQueryBaseTest (org.pmiops.workbench.api.BigQueryBaseTest)48 FieldSet (org.pmiops.workbench.model.FieldSet)36 TableQuery (org.pmiops.workbench.model.TableQuery)35 ColumnFilter (org.pmiops.workbench.model.ColumnFilter)29 MaterializeCohortRequest (org.pmiops.workbench.model.MaterializeCohortRequest)17 BigDecimal (java.math.BigDecimal)8 Cohort (org.pmiops.workbench.model.Cohort)6 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)4 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)3 CohortReview (org.pmiops.workbench.db.model.CohortReview)2 CohortStatus (org.pmiops.workbench.model.CohortStatus)2 PaginationToken (org.pmiops.workbench.utils.PaginationToken)2 BigQueryException (com.google.cloud.bigquery.BigQueryException)1 FieldValue (com.google.cloud.bigquery.FieldValue)1 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)1 QueryResult (com.google.cloud.bigquery.QueryResult)1 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1