Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error logging #112

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions models/cohortdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ type OrdinalGroupData struct {
// TODO - name this function as such
func (h CohortData) RetrieveDataByOriginalCohortAndNewCohort(sourceId int, originalCohortDefinitionId int, cohortDefinitionId int) ([]*PersonIdAndCohort, error) {
var dataSourceModel = new(Source)
resultsDataSource := dataSourceModel.GetDataSource(sourceId, Results)
resultsDataSource, err := dataSourceModel.GetDataSource(sourceId, Results)
if err != nil {
return nil, err
}
var personData []*PersonIdAndCohort

query := resultsDataSource.Db.Model(&Cohort{}).
Expand All @@ -76,9 +79,15 @@ func (h CohortData) RetrieveDataByOriginalCohortAndNewCohort(sourceId int, origi
func (h CohortData) RetrieveDataBySourceIdAndCohortIdAndConceptIdsOrderedByPersonId(sourceId int, cohortDefinitionId int, conceptIds []int64) ([]*PersonConceptAndValue, error) {
log.Printf(">> Using inner join impl. for large cohorts")
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
omopDataSource, err := dataSourceModel.GetDataSource(sourceId, Omop)
if err != nil {
return nil, err
}

resultsDataSource := dataSourceModel.GetDataSource(sourceId, Results)
resultsDataSource, err := dataSourceModel.GetDataSource(sourceId, Results)
if err != nil {
return nil, err
}

// get the observations for the subjects and the concepts, to build up the data rows to return:
var cohortData []*PersonConceptAndValue
Expand All @@ -98,8 +107,14 @@ func (h CohortData) RetrieveDataBySourceIdAndCohortIdAndConceptIdsOrderedByPerso

func (h CohortData) RetrieveHistogramDataBySourceIdAndCohortIdAndConceptIdsAndCohortPairs(sourceId int, cohortDefinitionId int, histogramConceptId int64, filterConceptIds []int64, filterCohortPairs []utils.CustomDichotomousVariableDef) ([]*PersonConceptAndValue, error) {
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
resultsDataSource := dataSourceModel.GetDataSource(sourceId, Results)
omopDataSource, err := dataSourceModel.GetDataSource(sourceId, Omop)
if err != nil {
return nil, err
}
resultsDataSource, err := dataSourceModel.GetDataSource(sourceId, Results)
if err != nil {
return nil, err
}

// get the observations for the subjects and the concepts, to build up the data rows to return:
var cohortData []*PersonConceptAndValue
Expand All @@ -118,7 +133,10 @@ func (h CohortData) RetrieveHistogramDataBySourceIdAndCohortIdAndConceptIdsAndCo

func (h CohortData) RetrieveBarGraphDataBySourceIdAndCohortIdAndConceptIds(sourceId int, cohortDefinitionId int, conceptId int64) ([]*OrdinalGroupData, error) {
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
omopDataSource, err := dataSourceModel.GetDataSource(sourceId, Omop)
if err != nil {
return nil, err
}

// get the observations for the subjects and the concepts, to build up the data rows to return:
var cohortData []*OrdinalGroupData
Expand All @@ -142,8 +160,14 @@ func (h CohortData) RetrieveCohortOverlapStats(sourceId int, caseCohortId int, c
filterConceptIds []int64, filterCohortPairs []utils.CustomDichotomousVariableDef) (CohortOverlapStats, error) {

var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
resultsDataSource := dataSourceModel.GetDataSource(sourceId, Results)
omopDataSource, err := dataSourceModel.GetDataSource(sourceId, Omop)
if err != nil {
return CohortOverlapStats{}, err
}
resultsDataSource, err := dataSourceModel.GetDataSource(sourceId, Results)
if err != nil {
return CohortOverlapStats{}, err
}

var cohortOverlapStats CohortOverlapStats
query := QueryFilterByCohortPairsHelper(filterCohortPairs, resultsDataSource, caseCohortId, "case_cohort_unionedAndIntersectedWithFilters").
Expand Down Expand Up @@ -179,7 +203,10 @@ func (h CohortData) ValidateObservationData(observationConceptIdsToCheck []int64
countIssues := 0
for _, source := range sources {
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(source.SourceId, Omop)
omopDataSource, err := dataSourceModel.GetDataSource(source.SourceId, Omop)
if err != nil {
return -1, err
}

log.Printf("INFO: checking if no duplicate data is found for concept ids %v in `observation` table of data source %d...",
observationConceptIdsToCheck, source.SourceId)
Expand Down
5 changes: 4 additions & 1 deletion models/cohortdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ func (h CohortDefinition) GetAllCohortDefinitionsAndStatsOrderBySizeDesc(sourceI

// Connect to source db and gather stats:
var dataSourceModel = new(Source)
resultsDataSource := dataSourceModel.GetDataSource(sourceId, Results)
resultsDataSource, err := dataSourceModel.GetDataSource(sourceId, Results)
if err != nil {
return nil, err
}
var cohortDefinitionStats []*CohortDefinitionStats
query := resultsDataSource.Db.Model(&Cohort{}).
Select("cohort_definition_id as id, '' as name, count(*) as cohort_size").
Expand Down
36 changes: 27 additions & 9 deletions models/concept.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ type Observation struct {

func (h Concept) RetriveAllBySourceId(sourceId int) ([]*Concept, error) {
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
omopDataSource, err := dataSourceModel.GetDataSource(sourceId, Omop)
if err != nil {
return nil, err
}

var concepts []*Concept
query := omopDataSource.Db.Model(&Concept{}).
Expand Down Expand Up @@ -71,7 +74,10 @@ func (h Concept) RetrieveInfoBySourceIdAndConceptId(sourceId int, conceptId int6
// Raises an error if any of the concepts is not found.
func (h Concept) RetrieveInfoBySourceIdAndConceptIds(sourceId int, conceptIds []int64) ([]*ConceptSimple, error) {
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
omopDataSource, err := dataSourceModel.GetDataSource(sourceId, Omop)
if err != nil {
return nil, err
}

var conceptItems []*ConceptSimple
query := omopDataSource.Db.Model(&Concept{}).
Expand All @@ -96,7 +102,10 @@ func (h Concept) RetrieveInfoBySourceIdAndConceptIds(sourceId int, conceptIds []

func (h Concept) RetrieveInfoBySourceIdAndConceptTypes(sourceId int, conceptTypes []string) ([]*ConceptSimple, error) {
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
omopDataSource, err := dataSourceModel.GetDataSource(sourceId, Omop)
if err != nil {
return nil, err
}

var conceptItems []*ConceptSimple
query := omopDataSource.Db.Model(&Concept{}).
Expand All @@ -122,8 +131,9 @@ func (h Concept) RetrieveInfoBySourceIdAndConceptTypes(sourceId int, conceptType
// how many persons in the cohort have that value in their observation records.
// E.g. if we have a cohort of size N and a concept that can have values "A" or "B",
// then it will return something like:
// {ConceptValue: "A", NPersonsInCohortWithValue: M},
// {ConceptValue: "B", NPersonsInCohortWithValue: N-M},
//
// {ConceptValue: "A", NPersonsInCohortWithValue: M},
// {ConceptValue: "B", NPersonsInCohortWithValue: N-M},
func (h Concept) RetrieveBreakdownStatsBySourceIdAndCohortId(sourceId int, cohortDefinitionId int, breakdownConceptId int64) ([]*ConceptBreakdown, error) {
// this is identical to the result of the function below if called with empty filterConceptIds[] and empty filterCohortPairs... so call that:
filterConceptIds := []int64{}
Expand All @@ -134,14 +144,22 @@ func (h Concept) RetrieveBreakdownStatsBySourceIdAndCohortId(sourceId int, cohor
// Basically same goal as described in function above, but only count persons that have a non-null value for each
// of the ids in the given filterConceptIds. So, using the example documented in the function above, it will
// return something like:
// {ConceptValue: "A", NPersonsInCohortWithValue: M-X},
// {ConceptValue: "B", NPersonsInCohortWithValue: N-M-X},
//
// {ConceptValue: "A", NPersonsInCohortWithValue: M-X},
// {ConceptValue: "B", NPersonsInCohortWithValue: N-M-X},
//
// where X is the number of persons that have NO value or just a "null" value for one or more of the ids in the given filterConceptIds.
func (h Concept) RetrieveBreakdownStatsBySourceIdAndCohortIdAndConceptIdsAndCohortPairs(sourceId int, cohortDefinitionId int, filterConceptIds []int64, filterCohortPairs []utils.CustomDichotomousVariableDef, breakdownConceptId int64) ([]*ConceptBreakdown, error) {

var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, Omop)
resultsDataSource := dataSourceModel.GetDataSource(sourceId, Results)
omopDataSource, err := dataSourceModel.GetDataSource(sourceId, Omop)
if err != nil {
return nil, err
}
resultsDataSource, err := dataSourceModel.GetDataSource(sourceId, Results)
if err != nil {
return nil, err
}

// count persons, grouping by concept value:
var conceptBreakdownList []*ConceptBreakdown
Expand Down
20 changes: 16 additions & 4 deletions models/datadictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ func (u DataDictionary) GetDataDictionary() (*DataDictionaryModel, error) {
panic("More than one data source! Exiting")
}
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sources[0].SourceId, Omop)
miscDataSource := dataSourceModel.GetDataSource(sources[0].SourceId, Misc)
omopDataSource, err := dataSourceModel.GetDataSource(sources[0].SourceId, Omop)
if err != nil {
log.Panicf("Error: Failed to get data source: %v", err)
}
miscDataSource, err := dataSourceModel.GetDataSource(sources[0].SourceId, Misc)
if err != nil {
log.Panicf("Error: Failed to get data source: %v", err)
}

if u.CheckIfDataDictionaryIsFilled(miscDataSource) {
var newDataDictionary DataDictionaryModel
Expand Down Expand Up @@ -138,8 +144,14 @@ func (u DataDictionary) GenerateDataDictionary() {
panic("More than one data source! Exiting")
}
var dataSourceModel = new(Source)
omopDataSource := dataSourceModel.GetDataSource(sources[0].SourceId, Omop)
miscDataSource := dataSourceModel.GetDataSource(sources[0].SourceId, Misc)
omopDataSource, err := dataSourceModel.GetDataSource(sources[0].SourceId, Omop)
if err != nil {
log.Panicf("Error: Failed to get data source: %v", err)
}
miscDataSource, err := dataSourceModel.GetDataSource(sources[0].SourceId, Misc)
if err != nil {
log.Panicf("Error: Failed to get data source: %v", err)
}

if u.CheckIfDataDictionaryIsFilled(miscDataSource) {
log.Print("Data Dictionary Result already filled. Skipping generation.")
Expand Down
9 changes: 6 additions & 3 deletions models/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ const (

// Get the data source details for given source id and source type.
// The source type can be one of the type SourceType.
func (h Source) GetDataSource(sourceId int, sourceType SourceType) *utils.DbAndSchema {
func (h Source) GetDataSource(sourceId int, sourceType SourceType) (*utils.DbAndSchema, error) {
dataSource, _ := h.GetSourceByIdWithConnection(sourceId)

sourceConnectionString := dataSource.SourceConnection
dbSchema, _ := h.GetSourceSchemaNameBySourceIdAndSourceType(sourceId, sourceType)
dbSchemaName := dbSchema.SchemaName
dbAndSchema := utils.GetDataSourceDB(sourceConnectionString, dbSchemaName)
return dbAndSchema
dbAndSchema, err := utils.GetDataSourceDB(sourceConnectionString, dbSchemaName)
if err != nil {
return nil, err
}
return dbAndSchema, nil
}

func (h Source) GetSourceByName(name string) (*Source, error) {
Expand Down
6 changes: 3 additions & 3 deletions tests/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ExecSQLString(sqlString string, sourceId int) (tx *gorm.DB) {

} else {
// look up the data source in source table:
omopDataSource := dataSourceModel.GetDataSource(sourceId, models.Omop)
omopDataSource, _ := dataSourceModel.GetDataSource(sourceId, models.Omop)
return omopDataSource.Db.Model(models.Source{}).Exec(sqlString)
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func GetOmopDataSource() *utils.DbAndSchema {

func GetOmopDataSourceForSourceId(sourceId int) *utils.DbAndSchema {
var dataSourceModel = new(models.Source)
omopDataSource := dataSourceModel.GetDataSource(sourceId, models.Omop)
omopDataSource, _ := dataSourceModel.GetDataSource(sourceId, models.Omop)
return omopDataSource
}

Expand All @@ -141,7 +141,7 @@ func GetResultsDataSource() *utils.DbAndSchema {

func GetResultsDataSourceForSourceId(sourceId int) *utils.DbAndSchema {
var dataSourceModel = new(models.Source)
dataSource := dataSourceModel.GetDataSource(sourceId, models.Results)
dataSource, _ := dataSourceModel.GetDataSource(sourceId, models.Results)
return dataSource
}

Expand Down
12 changes: 7 additions & 5 deletions utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type DbAndSchema struct {

var dataSourceDbMap = make(map[string]*DbAndSchema)

func GetDataSourceDB(sourceConnectionString string, dbSchema string) *DbAndSchema {
func GetDataSourceDB(sourceConnectionString string, dbSchema string) (*DbAndSchema, error) {
sourceAndSchemaKey := "source:" + sourceConnectionString + ",schema:" + dbSchema
if dataSourceDbMap[sourceAndSchemaKey] != nil {
// return the already initialized object:
return dataSourceDbMap[sourceAndSchemaKey]
return dataSourceDbMap[sourceAndSchemaKey], nil
}
// otherwise, open a new connection:
dsn := GenerateDsn(sourceConnectionString)
Expand All @@ -43,19 +43,21 @@ func GetDataSourceDB(sourceConnectionString string, dbSchema string) *DbAndSchem
dataSourceDb.Vendor = "postgresql"
} else {
log.Printf("connecting to cohorts 'sqlserver' db...")
dataSource, _ := gorm.Open(sqlserver.Open(dsn),
dataSource, err := gorm.Open(sqlserver.Open(dsn),
&gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: dbSchema + ".",
SingularTable: true,
}})
// TODO - should throw error if db connection fails! Currently fails "silently" by printing error to log and then just returning ...
if err != nil {
return nil, err
}
dataSourceDb.Db = dataSource
dataSourceDb.Vendor = "sqlserver"
}
dataSourceDb.Schema = dbSchema
dataSourceDbMap[sourceAndSchemaKey] = dataSourceDb
return dataSourceDb
return dataSourceDb, nil
}

// Adds a default timeout to a query
Expand Down
Loading