Skip to content

Commit

Permalink
make sure sortTitle and SortDate are correctly set when extracting se…
Browse files Browse the repository at this point in the history
…archParamters

Failures during searchparameter extraction should not cause the resource to be skipped.
Update searchparameter extraction to correctly handle Time objects
Update searchparameter extraction to default to nil if empty list is returned during extraction.
  • Loading branch information
AnalogJ committed Aug 2, 2023
1 parent 9e776c6 commit 28cb918
Show file tree
Hide file tree
Showing 60 changed files with 3,728 additions and 1,102 deletions.
6 changes: 4 additions & 2 deletions backend/pkg/database/sqlite_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,14 @@ func (sr *SqliteRepository) UpsertResource(ctx context.Context, wrappedResourceM
}

wrappedFhirResourceModel.SetOriginBase(wrappedResourceModel.OriginBase)
//wrappedFhirResourceModel.SetResourceRaw(wrappedResourceModel.ResourceRaw)
wrappedFhirResourceModel.SetSortTitle(wrappedResourceModel.SortTitle)
wrappedFhirResourceModel.SetSortDate(wrappedResourceModel.SortDate)

//TODO: this takes too long, we need to find a way to do this processing faster or in the background async.
err = wrappedFhirResourceModel.PopulateAndExtractSearchParameters(json.RawMessage(wrappedResourceModel.ResourceRaw))
if err != nil {
return false, fmt.Errorf("An error ocurred while extracting SearchParameters using FHIRPath (%s/%s): %v", wrappedResourceModel.SourceResourceType, wrappedResourceModel.SourceResourceID, err)
sr.Logger.Warnf("ignoring: an error occurred while extracting SearchParameters using FHIRPath (%s/%s): %v", wrappedResourceModel.SourceResourceType, wrappedResourceModel.SourceResourceID, err)
//wrappedFhirResourceModel.SetResourceRaw(wrappedResourceModel.ResourceRaw)
}

createResult := sr.GormClient.WithContext(ctx).Where(models.OriginBase{
Expand Down
57 changes: 44 additions & 13 deletions backend/pkg/models/database/fhir_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type FhirAccount struct {
Language datatypes.JSON `gorm:"column:language;type:text;serializer:json" json:"language,omitempty"`
// When the resource version last changed
// https://hl7.org/fhir/r4/search.html#date
LastUpdated time.Time `gorm:"column:lastUpdated;type:datetime" json:"lastUpdated,omitempty"`
LastUpdated *time.Time `gorm:"column:lastUpdated;type:datetime" json:"lastUpdated,omitempty"`
// Human-readable label
// https://hl7.org/fhir/r4/search.html#string
Name datatypes.JSON `gorm:"column:name;type:text;serializer:json" json:"name,omitempty"`
Expand All @@ -31,7 +31,7 @@ type FhirAccount struct {
Owner datatypes.JSON `gorm:"column:owner;type:text;serializer:json" json:"owner,omitempty"`
// Transaction window
// https://hl7.org/fhir/r4/search.html#date
Period time.Time `gorm:"column:period;type:datetime" json:"period,omitempty"`
Period *time.Time `gorm:"column:period;type:datetime" json:"period,omitempty"`
// Profiles this resource claims to conform to
// https://hl7.org/fhir/r4/search.html#reference
Profile datatypes.JSON `gorm:"column:profile;type:text;serializer:json" json:"profile,omitempty"`
Expand Down Expand Up @@ -137,7 +137,12 @@ func (s *FhirAccount) PopulateAndExtractSearchParameters(resourceRaw json.RawMes
}, [])
JSON.stringify(IdentifierProcessed)
if(IdentifierProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(IdentifierProcessed)
}
`)
if err == nil && identifierResult.String() != "undefined" {
s.Identifier = []byte(identifierResult.String())
Expand Down Expand Up @@ -179,7 +184,12 @@ func (s *FhirAccount) PopulateAndExtractSearchParameters(resourceRaw json.RawMes
}, [])
JSON.stringify(LanguageProcessed)
if(LanguageProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(LanguageProcessed)
}
`)
if err == nil && languageResult.String() != "undefined" {
s.Language = []byte(languageResult.String())
Expand All @@ -189,7 +199,12 @@ func (s *FhirAccount) PopulateAndExtractSearchParameters(resourceRaw json.RawMes
if err == nil && lastUpdatedResult.String() != "undefined" {
t, err := time.Parse(time.RFC3339, lastUpdatedResult.String())
if err == nil {
s.LastUpdated = t
s.LastUpdated = &t
} else if err != nil {
d, err := time.Parse("2006-01-02", lastUpdatedResult.String())
if err == nil {
s.LastUpdated = &d
}
}
}
// extracting Name
Expand Down Expand Up @@ -241,29 +256,36 @@ func (s *FhirAccount) PopulateAndExtractSearchParameters(resourceRaw json.RawMes
return accumulator
}, [])
JSON.stringify(NameProcessed)
if(NameProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(NameProcessed)
}
`)
if err == nil && nameResult.String() != "undefined" {
s.Name = []byte(nameResult.String())
}
// extracting Owner
ownerResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'Account.owner'))")
if err == nil && ownerResult.String() != "undefined" {
s.Owner = []byte(ownerResult.String())
}
// extracting Period
periodResult, err := vm.RunString("window.fhirpath.evaluate(fhirResource, 'Account.servicePeriod')[0]")
if err == nil && periodResult.String() != "undefined" {
t, err := time.Parse(time.RFC3339, periodResult.String())
if err == nil {
s.Period = t
s.Period = &t
} else if err != nil {
d, err := time.Parse("2006-01-02", periodResult.String())
if err == nil {
s.Period = &d
}
}
}
// extracting Profile
profileResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'Resource.meta.profile'))")
if err == nil && profileResult.String() != "undefined" {
s.Profile = []byte(profileResult.String())
}
// extracting SourceUri
sourceUriResult, err := vm.RunString("window.fhirpath.evaluate(fhirResource, 'Resource.meta.source')[0]")
Expand Down Expand Up @@ -307,15 +329,19 @@ func (s *FhirAccount) PopulateAndExtractSearchParameters(resourceRaw json.RawMes
}, [])
JSON.stringify(StatusProcessed)
if(StatusProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(StatusProcessed)
}
`)
if err == nil && statusResult.String() != "undefined" {
s.Status = []byte(statusResult.String())
}
// extracting Subject
subjectResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'Account.subject'))")
if err == nil && subjectResult.String() != "undefined" {
s.Subject = []byte(subjectResult.String())
}
// extracting Tag
tagResult, err := vm.RunString(`
Expand Down Expand Up @@ -354,7 +380,12 @@ func (s *FhirAccount) PopulateAndExtractSearchParameters(resourceRaw json.RawMes
}, [])
JSON.stringify(TagProcessed)
if(TagProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(TagProcessed)
}
`)
if err == nil && tagResult.String() != "undefined" {
s.Tag = []byte(tagResult.String())
Expand Down
74 changes: 56 additions & 18 deletions backend/pkg/models/database/fhir_adverse_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type FhirAdverseEvent struct {
Category datatypes.JSON `gorm:"column:category;type:text;serializer:json" json:"category,omitempty"`
// When the event occurred
// https://hl7.org/fhir/r4/search.html#date
Date time.Time `gorm:"column:date;type:datetime" json:"date,omitempty"`
Date *time.Time `gorm:"column:date;type:datetime" json:"date,omitempty"`
// Type of the event itself in relation to the subject
// https://hl7.org/fhir/r4/search.html#token
Event datatypes.JSON `gorm:"column:event;type:text;serializer:json" json:"event,omitempty"`
Expand All @@ -31,7 +31,7 @@ type FhirAdverseEvent struct {
Language datatypes.JSON `gorm:"column:language;type:text;serializer:json" json:"language,omitempty"`
// When the resource version last changed
// https://hl7.org/fhir/r4/search.html#date
LastUpdated time.Time `gorm:"column:lastUpdated;type:datetime" json:"lastUpdated,omitempty"`
LastUpdated *time.Time `gorm:"column:lastUpdated;type:datetime" json:"lastUpdated,omitempty"`
// Location where adverse event occurred
// https://hl7.org/fhir/r4/search.html#reference
Location datatypes.JSON `gorm:"column:location;type:text;serializer:json" json:"location,omitempty"`
Expand Down Expand Up @@ -161,7 +161,12 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
}, [])
JSON.stringify(ActualityProcessed)
if(ActualityProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(ActualityProcessed)
}
`)
if err == nil && actualityResult.String() != "undefined" {
s.Actuality = []byte(actualityResult.String())
Expand Down Expand Up @@ -203,7 +208,12 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
}, [])
JSON.stringify(CategoryProcessed)
if(CategoryProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(CategoryProcessed)
}
`)
if err == nil && categoryResult.String() != "undefined" {
s.Category = []byte(categoryResult.String())
Expand All @@ -213,7 +223,12 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
if err == nil && dateResult.String() != "undefined" {
t, err := time.Parse(time.RFC3339, dateResult.String())
if err == nil {
s.Date = t
s.Date = &t
} else if err != nil {
d, err := time.Parse("2006-01-02", dateResult.String())
if err == nil {
s.Date = &d
}
}
}
// extracting Event
Expand Down Expand Up @@ -253,7 +268,12 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
}, [])
JSON.stringify(EventProcessed)
if(EventProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(EventProcessed)
}
`)
if err == nil && eventResult.String() != "undefined" {
s.Event = []byte(eventResult.String())
Expand Down Expand Up @@ -295,7 +315,12 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
}, [])
JSON.stringify(LanguageProcessed)
if(LanguageProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(LanguageProcessed)
}
`)
if err == nil && languageResult.String() != "undefined" {
s.Language = []byte(languageResult.String())
Expand All @@ -305,28 +330,29 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
if err == nil && lastUpdatedResult.String() != "undefined" {
t, err := time.Parse(time.RFC3339, lastUpdatedResult.String())
if err == nil {
s.LastUpdated = t
s.LastUpdated = &t
} else if err != nil {
d, err := time.Parse("2006-01-02", lastUpdatedResult.String())
if err == nil {
s.LastUpdated = &d
}
}
}
// extracting Location
locationResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'AdverseEvent.location'))")
if err == nil && locationResult.String() != "undefined" {
s.Location = []byte(locationResult.String())
}
// extracting Profile
profileResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'Resource.meta.profile'))")
if err == nil && profileResult.String() != "undefined" {
s.Profile = []byte(profileResult.String())
}
// extracting Recorder
recorderResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'AdverseEvent.recorder'))")
if err == nil && recorderResult.String() != "undefined" {
s.Recorder = []byte(recorderResult.String())
}
// extracting Resultingcondition
resultingconditionResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'AdverseEvent.resultingCondition'))")
if err == nil && resultingconditionResult.String() != "undefined" {
s.Resultingcondition = []byte(resultingconditionResult.String())
}
// extracting Seriousness
seriousnessResult, err := vm.RunString(`
Expand Down Expand Up @@ -365,7 +391,12 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
}, [])
JSON.stringify(SeriousnessProcessed)
if(SeriousnessProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(SeriousnessProcessed)
}
`)
if err == nil && seriousnessResult.String() != "undefined" {
s.Seriousness = []byte(seriousnessResult.String())
Expand Down Expand Up @@ -407,7 +438,12 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
}, [])
JSON.stringify(SeverityProcessed)
if(SeverityProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(SeverityProcessed)
}
`)
if err == nil && severityResult.String() != "undefined" {
s.Severity = []byte(severityResult.String())
Expand All @@ -420,17 +456,14 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
// extracting Study
studyResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'AdverseEvent.study'))")
if err == nil && studyResult.String() != "undefined" {
s.Study = []byte(studyResult.String())
}
// extracting Subject
subjectResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'AdverseEvent.subject'))")
if err == nil && subjectResult.String() != "undefined" {
s.Subject = []byte(subjectResult.String())
}
// extracting Substance
substanceResult, err := vm.RunString("JSON.stringify(window.fhirpath.evaluate(fhirResource, 'AdverseEvent.suspectEntity.instance'))")
if err == nil && substanceResult.String() != "undefined" {
s.Substance = []byte(substanceResult.String())
}
// extracting Tag
tagResult, err := vm.RunString(`
Expand Down Expand Up @@ -469,7 +502,12 @@ func (s *FhirAdverseEvent) PopulateAndExtractSearchParameters(resourceRaw json.R
}, [])
JSON.stringify(TagProcessed)
if(TagProcessed.length == 0) {
"undefined"
}
else {
JSON.stringify(TagProcessed)
}
`)
if err == nil && tagResult.String() != "undefined" {
s.Tag = []byte(tagResult.String())
Expand Down
Loading

0 comments on commit 28cb918

Please sign in to comment.