Skip to content

Commit 8b91efe

Browse files
committed
feat: update RetrieveStatsForCohortIdAndConceptId and remove deprecated method
1 parent 4826d19 commit 8b91efe

File tree

5 files changed

+16
-71
lines changed

5 files changed

+16
-71
lines changed

controllers/cohortdata.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,15 @@ func (u CohortDataController) RetrieveHistogramForCohortIdAndConceptId(c *gin.Co
6565
}
6666

6767
func (u CohortDataController) RetrieveStatsForCohortIdAndConceptId(c *gin.Context) {
68-
sourceIdStr := c.Param("sourceid")
69-
log.Printf("Querying source: %s", sourceIdStr)
70-
cohortIdStr := c.Param("cohortid")
71-
log.Printf("Querying cohort for cohort definition id: %s", cohortIdStr)
72-
conceptIdStr := c.Param("conceptid")
73-
if sourceIdStr == "" || cohortIdStr == "" || conceptIdStr == "" {
74-
c.JSON(http.StatusBadRequest, gin.H{"message": "bad request"})
68+
sourceId, cohortId, conceptIdsAndCohortPairs, err := utils.ParseSourceIdAndCohortIdAndVariablesAsSingleList(c)
69+
if err != nil {
70+
c.JSON(http.StatusBadRequest, gin.H{"message": "bad request", "error": err.Error()})
7571
c.Abort()
7672
return
7773
}
7874

79-
filterConceptIdsAndValues, cohortPairs, _ := utils.ParseConceptDefsAndDichotomousDefs(c)
80-
81-
sourceId, _ := strconv.Atoi(sourceIdStr)
82-
cohortId, _ := strconv.Atoi(cohortIdStr)
83-
conceptId, _ := strconv.ParseInt(conceptIdStr, 10, 64)
75+
// parse cohortPairs separately as well, so we can validate permissions
76+
_, cohortPairs := utils.GetConceptDefsAndValuesAndCohortPairsAsSeparateLists(conceptIdsAndCohortPairs)
8477

8578
validAccessRequest := u.teamProjectAuthz.TeamProjectValidation(c, []int{cohortId}, cohortPairs)
8679
if !validAccessRequest {
@@ -90,7 +83,7 @@ func (u CohortDataController) RetrieveStatsForCohortIdAndConceptId(c *gin.Contex
9083
return
9184
}
9285

93-
cohortData, err := u.cohortDataModel.RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsAndCohortPairs(sourceId, cohortId, conceptId, filterConceptIdsAndValues, cohortPairs)
86+
cohortData, err := u.cohortDataModel.RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs(sourceId, cohortId, conceptIdsAndCohortPairs)
9487
if err != nil {
9588
c.JSON(http.StatusInternalServerError, gin.H{"message": "Error retrieving concept details", "error": err.Error()})
9689
c.Abort()
@@ -101,8 +94,14 @@ func (u CohortDataController) RetrieveStatsForCohortIdAndConceptId(c *gin.Contex
10194
for _, personData := range cohortData {
10295
conceptValues = append(conceptValues, float64(*personData.ConceptValueAsNumber))
10396
}
97+
conceptToStat, errGetLast := utils.CheckAndGetLastCustomConceptVariableDef(conceptIdsAndCohortPairs)
98+
if errGetLast != nil {
99+
c.JSON(http.StatusInternalServerError, gin.H{"message": "Error: last variable should be of numeric type", "error": errGetLast.Error()})
100+
c.Abort()
101+
return
102+
}
104103

105-
statsData := utils.GenerateStatsData(cohortId, conceptId, conceptValues)
104+
statsData := utils.GenerateStatsData(cohortId, conceptToStat.ConceptId, conceptValues)
106105

107106
c.JSON(http.StatusOK, gin.H{"statsData": statsData})
108107
}

models/cohortdata.go

-22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type CohortDataI interface {
1212
RetrieveDataBySourceIdAndCohortIdAndConceptIdsOrderedByPersonId(sourceId int, cohortDefinitionId int, conceptIds []int64) ([]*PersonConceptAndValue, error)
1313
RetrieveCohortOverlapStats(sourceId int, caseCohortId int, controlCohortId int, otherFilterConceptIds []int64, filterCohortPairs []utils.CustomDichotomousVariableDef) (CohortOverlapStats, error)
1414
RetrieveDataByOriginalCohortAndNewCohort(sourceId int, originalCohortDefinitionId int, cohortDefinitionId int) ([]*PersonIdAndCohort, error)
15-
RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsAndCohortPairs(sourceId int, cohortDefinitionId int, histogramConceptId int64, filterConceptIdsAndValues []utils.CustomConceptVariableDef, filterCohortPairs []utils.CustomDichotomousVariableDef) ([]*PersonConceptAndValue, error)
1615
RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs(sourceId int, cohortDefinitionId int, filterConceptDefsAndCohortPairs []interface{}) ([]*PersonConceptAndValue, error)
1716
RetrieveBarGraphDataBySourceIdAndCohortIdAndConceptIds(sourceId int, conceptId int64) ([]*NominalGroupData, error)
1817
RetrieveHistogramDataBySourceIdAndConceptId(sourceId int, histogramConceptId int64) ([]*PersonConceptAndValue, error)
@@ -134,27 +133,6 @@ func (h CohortData) RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlus
134133
return cohortData, err
135134
}
136135

137-
// DEPRECATED - USE RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs instead.
138-
func (h CohortData) RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsAndCohortPairs(sourceId int, cohortDefinitionId int, histogramConceptId int64, filterConceptDefs []utils.CustomConceptVariableDef, filterCohortPairs []utils.CustomDichotomousVariableDef) ([]*PersonConceptAndValue, error) {
139-
var dataSourceModel = new(Source)
140-
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
141-
resultsDataSource := dataSourceModel.GetDataSource(sourceId, Results)
142-
143-
// get the observations for the subjects and the concepts, to build up the data rows to return:
144-
var cohortData []*PersonConceptAndValue
145-
query := QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, cohortDefinitionId, "unionAndIntersect").
146-
Select("distinct(observation.person_id), observation.observation_concept_id as concept_id, observation.value_as_number as concept_value_as_number").
147-
Joins("INNER JOIN "+omopDataSource.Schema+".observation_continuous as observation"+omopDataSource.GetViewDirective()+" ON unionAndIntersect.subject_id = observation.person_id").
148-
Where("observation.observation_concept_id = ?", histogramConceptId).
149-
Where("observation.value_as_number is not null")
150-
151-
query = QueryFilterByConceptDefsHelper(query, sourceId, filterConceptDefs, omopDataSource, resultsDataSource.Schema, "unionAndIntersect")
152-
query, cancel := utils.AddTimeoutToQuery(query)
153-
defer cancel()
154-
meta_result := query.Scan(&cohortData)
155-
return cohortData, meta_result.Error
156-
}
157-
158136
func (h CohortData) RetrieveHistogramDataBySourceIdAndConceptId(sourceId int, histogramConceptId int64) ([]*PersonConceptAndValue, error) {
159137
var dataSourceModel = new(Source)
160138
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)

server/router.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewRouter() *gin.Engine {
5454
authorized.POST("/cohort-data/by-source-id/:sourceid/by-cohort-definition-id/:cohortid", cohortData.RetrieveDataBySourceIdAndCohortIdAndVariables)
5555

5656
// cohort data statistics
57-
authorized.POST("/cohort-stats/by-source-id/:sourceid/by-cohort-definition-id/:cohortid/by-concept-id/:conceptid", cohortData.RetrieveStatsForCohortIdAndConceptId)
57+
authorized.POST("/cohort-stats/by-source-id/:sourceid/by-cohort-definition-id/:cohortid", cohortData.RetrieveStatsForCohortIdAndConceptId)
5858

5959
// histogram endpoint
6060
authorized.POST("/histogram/by-source-id/:sourceid/by-cohort-definition-id/:cohortid", cohortData.RetrieveHistogramForCohortIdAndConceptId)

tests/controllers_tests/controllers_test.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ func (h dummyCohortDataModel) RetrieveDataBySourceIdAndCohortIdAndConceptIdsOrde
7575
return cohortData, nil
7676
}
7777

78-
func (h dummyCohortDataModel) RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsAndCohortPairs(sourceId int, cohortDefinitionId int, histogramConceptId int64, filterConceptIds []utils.CustomConceptVariableDef, filterCohortPairs []utils.CustomDichotomousVariableDef) ([]*models.PersonConceptAndValue, error) {
79-
80-
cohortData := []*models.PersonConceptAndValue{}
81-
return cohortData, nil
82-
}
83-
8478
func (h dummyCohortDataModel) RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs(sourceId int, cohortDefinitionId int, filterConceptDefsAndCohortPairs []interface{}) ([]*models.PersonConceptAndValue, error) {
8579
cohortData := []*models.PersonConceptAndValue{}
8680
return cohortData, nil
@@ -1246,13 +1240,12 @@ func TestRetrieveStatsForCohortIdAndConceptIdWithCorrectParams(t *testing.T) {
12461240
requestContext := new(gin.Context)
12471241
requestContext.Params = append(requestContext.Params, gin.Param{Key: "sourceid", Value: strconv.Itoa(tests.GetTestSourceId())})
12481242
requestContext.Params = append(requestContext.Params, gin.Param{Key: "cohortid", Value: "4"})
1249-
requestContext.Params = append(requestContext.Params, gin.Param{Key: "conceptid", Value: "2000006885"})
12501243
requestContext.Writer = new(tests.CustomResponseWriter)
12511244
requestContext.Request = new(http.Request)
1252-
requestBody := "{\"variables\":[{\"variable_type\": \"concept\", \"concept_id\": 2000000324},{\"variable_type\": \"custom_dichotomous\", \"cohort_ids\": [1, 3]}]}"
1245+
requestBody := "{\"variables\":[{\"variable_type\": \"concept\", \"concept_id\": 2000000324},{\"variable_type\": \"custom_dichotomous\", \"cohort_ids\": [1, 3]},{\"variable_type\": \"concept\", \"concept_id\": 2000006885}]}"
12531246
requestContext.Request.Body = io.NopCloser(strings.NewReader(requestBody))
12541247
cohortDataController.RetrieveStatsForCohortIdAndConceptId(requestContext)
1255-
// Params above are correct, so request should NOT abort:
1248+
// Params above are correct, with the concept of interest as last variable, so request should NOT abort:
12561249
if requestContext.IsAborted() {
12571250
t.Errorf("Did not expect this request to abort")
12581251
}

tests/models_tests/models_test.go

-25
Original file line numberDiff line numberDiff line change
@@ -768,31 +768,6 @@ func TestGetCohortDefinitionByName(t *testing.T) {
768768
}
769769
}
770770

771-
func TestRetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsAndCohortPairs(t *testing.T) {
772-
setUp(t)
773-
filterConceptIdsAndValues := []utils.CustomConceptVariableDef{}
774-
filterCohortPairs := []utils.CustomDichotomousVariableDef{}
775-
data, _ := cohortDataModel.RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsAndCohortPairs(testSourceId, largestCohort.Id, histogramConceptId, filterConceptIdsAndValues, filterCohortPairs)
776-
// everyone in the largestCohort has the histogramConceptId, but one person has NULL in the value_as_number:
777-
if len(data) != largestCohort.CohortSize-1 {
778-
t.Errorf("expected %d histogram data but got %d", largestCohort.CohortSize, len(data))
779-
}
780-
781-
// now filter on the extendedCopyOfSecondLargestCohort
782-
filterCohortPairs = []utils.CustomDichotomousVariableDef{
783-
{
784-
CohortDefinitionId1: smallestCohort.Id,
785-
CohortDefinitionId2: extendedCopyOfSecondLargestCohort.Id,
786-
ProvidedName: "test"},
787-
}
788-
// then we expect histogram data for the overlapping population only (which is 5 for extendedCopyOfSecondLargestCohort and largestCohort):
789-
data, _ = cohortDataModel.RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsAndCohortPairs(testSourceId, largestCohort.Id, histogramConceptId, filterConceptIdsAndValues, filterCohortPairs)
790-
if len(data) != 5 {
791-
t.Errorf("expected 5 histogram data but got %d", len(data))
792-
}
793-
794-
}
795-
796771
func TestRetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs(t *testing.T) {
797772
setUp(t)
798773
filterConceptDefsPlusCohortPairs := []interface{}{

0 commit comments

Comments
 (0)