Skip to content

Commit

Permalink
Fix conditions parsing, add api version to output, add debug param to…
Browse files Browse the repository at this point in the history
… view the input object
  • Loading branch information
samhq committed Feb 9, 2019
1 parent 555e40d commit 8a87e65
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 24 deletions.
3 changes: 2 additions & 1 deletion cmd/ohal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"runtime/pprof"
"sort"

"github.com/openhealthalgorithms/service/pkg"
"github.com/openhealthalgorithms/service/pkg/algorithms"
heartsAlg "github.com/openhealthalgorithms/service/pkg/algorithms/hearts"
"github.com/openhealthalgorithms/service/pkg/riskmodels"
Expand All @@ -28,7 +29,7 @@ var (
memprofile = "./ohas-mem-prof.prof"

appName = "ohal"
appVersion = "v0.4.5"
appVersion = pkg.GetVersion()
appCommit = "0000000"
)

Expand Down
7 changes: 4 additions & 3 deletions cmd/ohas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"syscall"

"github.com/openhealthalgorithms/service/pkg"
"github.com/openhealthalgorithms/service/pkg/logger"
"github.com/openhealthalgorithms/service/pkg/service"
"github.com/openhealthalgorithms/service/pkg/tools"
Expand All @@ -20,7 +21,7 @@ import (

var (
appName = "ohas"
appVersion = "v0.4.5"
appVersion = pkg.GetVersion()
appCommit = "0000000"

serviceSrv *http.Server
Expand Down Expand Up @@ -121,7 +122,7 @@ func main() {

cmd := exec.Command(os.Args[0], "main")
cmd.Start()
logEntry.Debugf("Service process ID is : ", cmd.Process.Pid)
logEntry.Debugf("Service process ID is : %d", cmd.Process.Pid)
savePID(cmd.Process.Pid)
return nil
},
Expand Down Expand Up @@ -174,7 +175,7 @@ func startServer(logEntry *logrus.Entry) error {
}()

srv := service.NewService()
srv.StartHttpServer()
srv.StartHTTPServer()

logEntry.Debug("server started")

Expand Down
11 changes: 8 additions & 3 deletions documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ curl -X POST \
-d '{
"config": {
"algorithm": "hearts",
"risk_model": "whocvd"
"risk_model": "whocvd",
"debug": false
},
"params": {
"demographics": {
Expand Down Expand Up @@ -504,7 +505,8 @@ The **response** will be a json object.
],
"meta": {
"algorithm": "Hearts Algorithm",
"request_id": "fcdff736-5178-40d1-8c3a-727b057400bd"
"request_id": "fcdff736-5178-40d1-8c3a-727b057400bd",
"api_version": "v0.4.6"
},
"referrals": {
"reasons": [
Expand Down Expand Up @@ -543,10 +545,13 @@ First part is **config**. It consist of the algorithm name and the risk model to
```javascript
"config": {
"algorithm": "hearts",
"risk_model": "whocvd"
"risk_model": "whocvd",
"debug": false
}
```

If you give `"debug": true`, then the request JSON will be added to output as `input`. It is an **OPTIONAL** parameter. You can safely remove it if you don't need it.

### param

All parameters are inside this object.
Expand Down
10 changes: 10 additions & 0 deletions pkg/algorithms/hearts/hearts.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,16 @@ func (d *Data) get(ctx context.Context) error {
goals := engineGoalContent.GenerateGoalsGuideline(codes...)
assessment.GoalsAttributes = goals

if p.Debug {
m := make(map[string]interface{})
err := json.Unmarshal(p.Input, &m)
if err != nil {
assessment.Input = map[string]interface{}{"error": "Cannot preview inputs"}
} else {
assessment.Input = m
}
}

d.Algorithm = assessment
d.Errors = errs

Expand Down
12 changes: 8 additions & 4 deletions pkg/datastructure/result.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package datastructure

import (
"github.com/openhealthalgorithms/service/pkg"
"github.com/pborman/uuid"
)

// Result object
type Result struct {
MetaAttributes Meta `structs:"meta" json:"meta"`
AssessmentsAttributes Assessments `structs:"assessments" json:"assessments"`
GoalsAttributes Goals `structs:"goals" json:"goals"`
AssessmentReferralAttibutes AssessmentReferral `structs:"referrals" json:"referrals"`
MetaAttributes Meta `structs:"meta" json:"meta"`
AssessmentsAttributes Assessments `structs:"assessments" json:"assessments"`
GoalsAttributes Goals `structs:"goals" json:"goals"`
AssessmentReferralAttibutes AssessmentReferral `structs:"referrals" json:"referrals"`
Input map[string]interface{} `structs:"input,omitempty" json:"input,omitempty"`
// RecommendationsAttributes Recommendations `structs:"recommendations" json:"recommendations"`
}

// Meta object
type Meta struct {
AlgorithmName string `structs:"algorithm" json:"algorithm"`
RequestID uuid.UUID `structs:"request_id" json:"request_id"`
APIVersion string `structs:"api_version" json:"api_version"`
}

/* * * * * Assessments * * * * */
Expand Down Expand Up @@ -163,6 +166,7 @@ func NewResult(algorithmName string) Result {

result.MetaAttributes.AlgorithmName = algorithmName
result.MetaAttributes.RequestID = uuid.NewRandom()
result.MetaAttributes.APIVersion = pkg.GetVersion()

return result
}
7 changes: 4 additions & 3 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

"github.com/openhealthalgorithms/service/pkg"
"github.com/openhealthalgorithms/service/pkg/algorithms/hearts"
"github.com/openhealthalgorithms/service/pkg/database"
"github.com/openhealthalgorithms/service/pkg/tools"
Expand Down Expand Up @@ -65,8 +66,8 @@ func NewServiceWithPortAddress(port, addr string) Service {
return Service{Port: port, Addr: addr, Router: router}
}

// StartHttpServer method
func (s *Service) StartHttpServer() {
// StartHTTPServer method
func (s *Service) StartHTTPServer() {
var err error

// Check if the DB file exists
Expand Down Expand Up @@ -110,7 +111,7 @@ func defaultHandler(w http.ResponseWriter, r *http.Request) {
}

func versionRequestHandler(w http.ResponseWriter, r *http.Request) {
result := &versionResponse{Version: "0.4.5"}
result := &versionResponse{Version: pkg.GetVersion()}

respondSuccess(w, result)
}
Expand Down
34 changes: 24 additions & 10 deletions pkg/tools/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Configs struct {
Algorithm string
RiskModel string
Debug bool
}

// Demographics object
Expand Down Expand Up @@ -122,6 +123,7 @@ type Params struct {
Measurements
Activities
// Assessments
Input []byte
}

// ParseParams function
Expand All @@ -139,6 +141,8 @@ func getInputs(data []byte) (Params, error) {
mandatory := make([]string, 0)
unsupported := make([]string, 0)

out.Input = data

// Config
if stringValue, err = jp.GetString(data, "config", "algorithm"); err == nil {
out.Algorithm = strings.ToLower(stringValue)
Expand All @@ -152,6 +156,12 @@ func getInputs(data []byte) (Params, error) {
mandatory = append(mandatory, "risk_model")
}

if val, err := jp.GetBoolean(data, "config", "debug"); err == nil {
out.Debug = val
} else {
out.Debug = false
}

// Params
// Demographics
if stringValue, err = jp.GetString(data, "params", "demographics", "gender"); err == nil {
Expand Down Expand Up @@ -474,26 +484,30 @@ func getInputs(data []byte) (Params, error) {
if stringValue, err = jp.GetString(value, "name"); err == nil {
name = stringValue
}
out.Conditions[strings.ToUpper(name)] = true
conditionBool := false
if boolValue, err := jp.GetBoolean(value, "is_active"); err == nil {
conditionBool = boolValue
}
out.Conditions[strings.ToUpper(name)] = conditionBool
switch name {
case "asthma":
out.Asthma = true
out.Asthma = conditionBool
case "tuberculosis":
out.Tuberculosis = true
out.Tuberculosis = conditionBool
case "diabetes":
out.Diabetes = true
out.Diabetes = conditionBool
case "hypertension":
out.Hypertension = true
out.Hypertension = conditionBool
case "ckd":
out.Ckd = true
out.Ckd = conditionBool
case "cvd":
out.Cvd = true
out.Cvd = conditionBool
case "pvd":
out.Pvd = true
out.Pvd = conditionBool
case "pregnant":
out.Pregnant = true
out.Pregnant = conditionBool
case "arrhythmia":
out.Arrhythmia = true
out.Arrhythmia = conditionBool
}
}
}, "params", "components", "medical_history")
Expand Down
8 changes: 8 additions & 0 deletions pkg/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pkg

const version = "v0.4.6"

// GetVersion returns the current version
func GetVersion() string {
return version
}

0 comments on commit 8a87e65

Please sign in to comment.