Skip to content

Commit 029b183

Browse files
Merge pull request #70 from uc-cdis/fix_cohort_listing
Fix cohort listing and improve existing warning on startup
2 parents ba01e7b + 9a9f744 commit 029b183

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

models/cohortdata.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ func (h CohortData) ValidateObservationData(observationConceptIdsToCheck []int64
169169
log.Printf("INFO: no issues found in observation table of data source %d.", source.SourceId)
170170
} else {
171171
log.Printf("WARNING: !!! found a total of %d `person` records with duplicated `observation` entries for one or more concepts "+
172-
"where this is not expected (in data source=%d). These are the entries found: %v !!!",
173-
len(personConceptAndCount), source.SourceId, personConceptAndCount)
172+
"where this is not expected (in data source=%d).",
173+
len(personConceptAndCount), source.SourceId)
174174
countIssues += len(personConceptAndCount)
175175
}
176176
}

models/cohortdefinition.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package models
33
import (
44
"fmt"
55

6+
"log"
7+
68
"github.com/uc-cdis/cohort-middleware/db"
79
"github.com/uc-cdis/cohort-middleware/utils"
810
)
@@ -80,11 +82,20 @@ func (h CohortDefinition) GetAllCohortDefinitionsAndStatsOrderBySizeDesc(sourceI
8082
meta_result := query.Scan(&cohortDefinitionStats)
8183

8284
// add name details:
85+
finalList := []*CohortDefinitionStats{}
8386
for _, cohortDefinitionStat := range cohortDefinitionStats {
8487
var cohortDefinition, _ = h.GetCohortDefinitionById(cohortDefinitionStat.Id)
85-
cohortDefinitionStat.Name = cohortDefinition.Name
88+
if cohortDefinition == nil {
89+
// unexpected issue: cohortDefinition not found. Warn and skip:
90+
log.Printf("WARNING: found a cohort of size %d with missing cohort_definition record",
91+
cohortDefinitionStat.CohortSize)
92+
continue
93+
} else {
94+
cohortDefinitionStat.Name = cohortDefinition.Name
95+
finalList = append(finalList, cohortDefinitionStat)
96+
}
8697
}
87-
return cohortDefinitionStats, meta_result.Error
98+
return finalList, meta_result.Error
8899
}
89100

90101
func (h CohortDefinition) GetCohortName(cohortId int) (string, error) {

tests/models_tests/models_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models_tests
22

33
import (
4+
"fmt"
45
"log"
56
"os"
67
"strings"
@@ -609,6 +610,34 @@ func TestGetAllCohortDefinitionsAndStatsOrderBySizeDesc(t *testing.T) {
609610
}
610611
}
611612

613+
// Tests whether the code deals correctly with the (error) situation where
614+
// the `cohort_definition` and `cohort` tables are not in sync (more specifically
615+
// the situation where a cohort still exists in `cohort` table but not in `cohort_definition`).
616+
func TestGetAllCohortDefinitionsAndStatsOrderBySizeDescWhenCohortDefinitionIsMissing(t *testing.T) {
617+
setUp(t)
618+
cohortDefinitions, _ := cohortDefinitionModel.GetAllCohortDefinitionsAndStatsOrderBySizeDesc(testSourceId)
619+
if len(cohortDefinitions) != len(allCohortDefinitions) {
620+
t.Errorf("Found %d", len(cohortDefinitions))
621+
}
622+
623+
// remove one cohort_definition record and verify that the list is now indeed smaller:
624+
firstCohort := cohortDefinitions[0]
625+
tests.ExecAtlasSQLString(fmt.Sprintf("delete from %s.cohort_definition where id = %d",
626+
db.GetAtlasDB().Schema, firstCohort.Id))
627+
cohortDefinitions, _ = cohortDefinitionModel.GetAllCohortDefinitionsAndStatsOrderBySizeDesc(testSourceId)
628+
if len(cohortDefinitions) != len(allCohortDefinitions)-1 {
629+
t.Errorf("Number of cohor_definition records expected to be %d, found %d",
630+
len(allCohortDefinitions)-1, len(cohortDefinitions))
631+
}
632+
// restore:
633+
tests.ExecAtlasSQLString(fmt.Sprintf("insert into %s.cohort_definition (id,name,description) "+
634+
"values (%d, '%s', '%s')",
635+
db.GetAtlasDB().Schema,
636+
firstCohort.Id,
637+
firstCohort.Name,
638+
firstCohort.Name))
639+
}
640+
612641
func TestGetCohortName(t *testing.T) {
613642
setUp(t)
614643
allCohortDefinitions, _ := cohortDefinitionModel.GetAllCohortDefinitions()

tests/testutils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func ExecSQLScript(sqlFilePath string, sourceId int) {
5555
}
5656

5757
func ExecAtlasSQLString(sqlString string) {
58-
ExecSQLScript(sqlString, -1)
58+
ExecSQLString(sqlString, -1)
5959
}
6060

6161
func ExecSQLString(sqlString string, sourceId int) {

0 commit comments

Comments
 (0)