Skip to content

Commit

Permalink
chore: port more clinical implementation and improve dependency injec…
Browse files Browse the repository at this point in the history
…tion

Implement OCL as a service
Improve FHIR repository dependency injection

Signed-off-by: Otieno Calvine <nyarangaotieno@gmail.com>
  • Loading branch information
NYARAS committed Sep 24, 2021
1 parent 9dc2b2a commit ab23719
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 458 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/kr/pretty v0.2.0 // indirect
github.com/savannahghi/converterandformatter v0.0.11
github.com/savannahghi/enumutils v0.0.3
github.com/savannahghi/firebasetools v0.0.15
github.com/savannahghi/scalarutils v0.0.2
github.com/savannahghi/serverutils v0.0.4
github.com/sirupsen/logrus v1.8.1
github.com/vektah/gqlparser/v2 v2.1.0
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210622215436-a8dc77f794b6
golang.org/x/oauth2 v0.0.0-20210622215436-a8dc77f794b6 // indirect
golang.org/x/tools v0.1.4 // indirect
google.golang.org/api v0.49.0
google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ github.com/kataras/iris/v12 v12.1.8/go.mod h1:LMYy4VlP67TQ3Zgriz8RE2h2kMZV2SgMYb
github.com/kataras/neffos v0.0.14/go.mod h1:8lqADm8PnbeFfL7CLXh1WHw53dG27MC3pgi2R1rmoTE=
github.com/kataras/pio v0.0.2/go.mod h1:hAoW0t9UmXi4R5Oyq5Z4irTbaTsOemSrDGUtaTl7Dro=
github.com/kataras/sitemap v0.0.5/go.mod h1:KY2eugMKiPwsJgx7+U103YZehfvNGOXURubcGyk0Bz8=
github.com/kevinburke/go.uuid v1.2.0 h1:+1qP8NdkJfgOSTrrrUuA7h0djr1VY77HFXYjR+zUcUo=
github.com/kevinburke/go.uuid v1.2.0/go.mod h1:9gVngk1Hq1FjwewVAjsWEUT+xc6jP+p62CASaGmQ0NQ=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.9.7/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down Expand Up @@ -319,6 +321,8 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/savannahghi/converterandformatter v0.0.11 h1:N9UPNhabmrxKAnM4E68qrP6/urijMBr0/1EzSWPX3C4=
github.com/savannahghi/converterandformatter v0.0.11/go.mod h1:PjKS+eAlw+z/3aZFN1brsX+jxpTlLgs7boOY7EbUbhs=
github.com/savannahghi/enumutils v0.0.3 h1:0IPGS/Q27B8mZw+0YOb1r7Au1MgJQldfuwYid3byUx0=
github.com/savannahghi/enumutils v0.0.3/go.mod h1:DDdjQBO1qyf5BxLzhTs1uN91drCIHH2Lvr8aLdJwu/o=
github.com/savannahghi/errorcodeutil v0.0.1/go.mod h1:nNBaBjatvoRusnDr2aRoNr4Rpmz9Z779mjcL0tr/IXk=
Expand Down Expand Up @@ -360,6 +364,10 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 h1:5u+EJUQiosu3JFX0XS0qTf5FznsMOzTjGqavBGuCbo0=
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2/go.mod h1:4kyMkleCiLkgY6z8gK5BkI01ChBtxR0ro3I1ZDcGM3w=
github.com/ttacon/libphonenumber v1.2.1 h1:fzOfY5zUADkCkbIafAed11gL1sW+bJ26p6zWLBMElR4=
github.com/ttacon/libphonenumber v1.2.1/go.mod h1:E0TpmdVMq5dyVlQ7oenAkhsLu86OkUl+yR4OAxyEg/M=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down
131 changes: 43 additions & 88 deletions pkg/clinical/infrastructure/datastore/fhir/dataset.go
Original file line number Diff line number Diff line change
@@ -1,102 +1,57 @@
package fhir

import (
"context"
"fmt"
"log"

"github.com/savannahghi/serverutils"
"google.golang.org/api/healthcare/v1"
)

// constants used to configure the Google Cloud Healthcare API
const (
DatasetLocation = "europe-west4"
)

// CreateDataset creates a cloud healthcare dataset and returns it's name
func (s Service) CreateDataset() (*healthcare.Operation, error) {
s.checkPreconditions()
datasetsService := s.healthcareService.Projects.Locations.Datasets
parent := fmt.Sprintf("projects/%s/locations/%s", s.projectID, s.location)
resp, err := datasetsService.Create(parent, &healthcare.Dataset{}).DatasetId(s.datasetID).Do()
if err != nil {
return nil, fmt.Errorf("create Data Set: %v", err)
}
return resp, nil
// Repository accesses and updates patient data that is stored on Healthcare
// FHIR repository
type Repository struct {
Dataset DatasetExtension
}

// GetDataset gets a cloud healthcare dataset.
func (s Service) GetDataset() (*healthcare.Dataset, error) {
s.checkPreconditions()
datasetsService := s.healthcareService.Projects.Locations.Datasets
name := fmt.Sprintf("projects/%s/locations/%s/datasets/%s", s.projectID, s.location, s.datasetID)
resp, err := datasetsService.Get(name).Do()
if err != nil {
return nil, fmt.Errorf("get Data Set: %v", err)
// NewFHIRRepository initializes a FHIR repository
func NewFHIRRepository(
dataset DatasetExtension,
) *Repository {
return &Repository{
Dataset: dataset,
}
return resp, nil
}

// Service is an implementation of the healthcare fhir repository
// It is implementation agnostic i.e logic should be handled using
// the preferred datastore/database
type Service struct {
healthcareService *healthcare.Service
projectID, location, datasetID, fhirStoreID string
// CreateFHIRResource creates an FHIR resource.
//
// The payload should be the result of marshalling a resource to JSON
func (fr Repository) CreateFHIRResource(resourceType string, payload map[string]interface{}) ([]byte, error) {
return nil, nil
}

// NewService initializes a Google Cloud Healthcare API service
func NewService() *Service {
projectID := serverutils.MustGetEnvVar(serverutils.GoogleCloudProjectIDEnvVarName)
_ = serverutils.MustGetEnvVar("CLOUD_HEALTH_PUBSUB_TOPIC")
datasetID := serverutils.MustGetEnvVar("CLOUD_HEALTH_DATASET_ID")
fhirStoreID := serverutils.MustGetEnvVar("CLOUD_HEALTH_FHIRSTORE_ID")

ctx := context.Background()
hsv, err := healthcare.NewService(ctx)
if err != nil {
log.Panicf("unable to initialize new Google Cloud Healthcare Service: %s", err)
}
service := &Service{
healthcareService: hsv,
projectID: projectID,
location: DatasetLocation,
datasetID: datasetID,
fhirStoreID: fhirStoreID,
}

// ensure that the dataset exists
_, err = service.GetDataset()
if err != nil {
_, err = service.CreateDataset()
if err != nil {
log.Printf(
"Unable to get or create dataset with projectID %s, "+
"location %s, datasetID %s; got error %s",
service.projectID, service.location, service.datasetID, err,
)
}
}
// DeleteFHIRResource deletes an FHIR resource.
func (fr Repository) DeleteFHIRResource(resourceType, fhirResourceID string) ([]byte, error) {
return nil, nil
}

// ensure that the FHIR store exists
_, err = service.GetFHIRStore()
if err != nil {
_, err = service.CreateFHIRStore()
if err != nil {
log.Printf(
"Unable to get or create FHIR store with projectID %s, "+
"location %s, datasetID %s, fhirStoreID %s; got error %s",
service.projectID, service.location, service.datasetID, service.fhirStoreID, err,
)
}
}
// PatchFHIRResource patches a FHIR resource.
// The payload is a JSON patch document that follows guidance on Patch from the
// FHIR standard.
// See:
// payload := []map[string]interface{}{
// {
// "op": "replace",
// "path": "/active",
// "value": active,
// },
// }
// See: https://www.hl7.org/fhir/http.html#patch
func (fr Repository) PatchFHIRResource(
resourceType, fhirResourceID string, payload []map[string]interface{}) ([]byte, error) {
return nil, nil
}

return service
// UpdateFHIRResource updates the entire contents of a resource.
func (fr Repository) UpdateFHIRResource(
resourceType, fhirResourceID string, payload map[string]interface{}) ([]byte, error) {
return nil, nil
}

func (s Service) checkPreconditions() {
if s.healthcareService == nil {
log.Panicf("cloudhealth.Service *healthcare.Service is nil")
}
// GetFHIRPatientEverything gets all resources associated with a particular
// patient compartment.
func (fr Repository) GetFHIRPatientEverything(fhirResourceID string) ([]byte, error) {
return nil, nil
}

0 comments on commit ab23719

Please sign in to comment.