Skip to content

Commit

Permalink
Add configuration management and physical activity changes
Browse files Browse the repository at this point in the history
  • Loading branch information
samhq committed Feb 24, 2019
1 parent 0dcf207 commit 03b614b
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 579 deletions.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ ART_ALG_LINUX_32 := $(DEV_ALG_NAME)-linux-386.bin
ART_ALG_LINUX_64 := $(DEV_ALG_NAME)-linux-amd64.bin

# default files to include
GUIDELINE_JSON := guideline_hearts.json
GUIDELINE_CONTENT_JSON := guideline_hearts_content.json
GOALS_JSON := goals_hearts.json
GOALS_CONTENT_JSON := goals_hearts_content.json
# GUIDELINE_JSON := guideline_hearts.json
# GUIDELINE_CONTENT_JSON := guideline_hearts_content.json
# GOALS_JSON := goals_hearts.json
# GOALS_CONTENT_JSON := goals_hearts_content.json
SAMPLE_REQUEST_JSON := sample-request.json
HELP_FILE := ./documentation
CONTENTS_FILE := ./contents
CONFIG_FILE := ohas.toml

# tests
COVER_OUT := cover.out
Expand Down Expand Up @@ -122,7 +124,8 @@ artifacts_linux: ## Create artifacts for linux
zip_artifacts: ## Create a zip archive with artifacts
$(MAKE) -f $(MKFILE_PATH) clean_releases
mkdir -p $(REL_PATH)
zip -j -v $(REL_PATH)/$(ART_ARCHIVE) $(BUILD_DARWIN)/$(ART_DARWIN_64) $(BUILD_LINUX)/$(ART_LINUX_64) $(GUIDELINE_JSON) $(GUIDELINE_CONTENT_JSON) $(GOALS_JSON) $(GOALS_CONTENT_JSON) $(SAMPLE_REQUEST_JSON)
zip -j -v $(REL_PATH)/$(ART_ARCHIVE) $(BUILD_DARWIN)/$(ART_DARWIN_64) $(BUILD_LINUX)/$(ART_LINUX_64) $(CONFIG_FILE) $(SAMPLE_REQUEST_JSON)
zip -v -r -u $(REL_PATH)/$(ART_ARCHIVE) $(CONTENTS_FILE)
zip -v -r -u $(REL_PATH)/$(ART_ARCHIVE) $(HELP_FILE)

house_keep: ## Remove any .DS_Store files
Expand Down
12 changes: 8 additions & 4 deletions cmd/ohal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"runtime/pprof"
"sort"

"github.com/openhealthalgorithms/service/pkg/config"

"github.com/openhealthalgorithms/service/pkg"
"github.com/openhealthalgorithms/service/pkg/algorithms"
heartsAlg "github.com/openhealthalgorithms/service/pkg/algorithms/hearts"
Expand Down Expand Up @@ -163,6 +165,8 @@ func setupAndRun(cliCtx *cli.Context) error {
flag.BoolVar(&debug, "debug", false, "debug flag")
flag.Parse()

currentSettings := config.CurrentSettings()

if cpuProf {
f, err := os.Create(cpuprofile)
if err != nil {
Expand Down Expand Up @@ -251,10 +255,10 @@ func setupAndRun(cliCtx *cli.Context) error {

v := types.NewValuesCtx()
v.Params.Set("params", paramObj)
v.Params.Set("guide", guideline)
v.Params.Set("guidecontent", guidelineContent)
v.Params.Set("goal", goal)
v.Params.Set("goalcontent", goalContent)
v.Params.Set("guide", currentSettings.GuidelineFile)
v.Params.Set("guidecontent", currentSettings.GuidelineContentFile)
v.Params.Set("goal", currentSettings.GoalFile)
v.Params.Set("goalcontent", currentSettings.GoalContentFile)

if debug {
v.Params.Set("debug", "true")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 35 additions & 4 deletions documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ in a zip file, unzip to a directory of your choice. This zip file contains the f
|-- icon.png
|-- index.html
|-- README.md (This file)
|-- goals_hearts_content.json (Has the goals content for various health check)
|-- goals_hearts.json (Has the goals conditions for various health check)
|-- guideline_hearts_content.json (Has the care plan messages for different attributes)
|-- guideline_hearts.json (Has the conditions/targets for various health check)
|-- contents
|-- goals_hearts_content.json (Has the goals content for various health check)
|-- goals_hearts.json (Has the goals conditions for various health check)
|-- guideline_hearts_content.json (Has the care plan messages for different attributes)
|-- guideline_hearts.json (Has the conditions/targets for various health check)
|-- ohas-darwin-amd64.bin (The binary you can use in Mac)
|-- ohas-linux-amd64.bin (The binary you can use in Linux)
|-- ohas.toml
|-- sample-request.json (A sample request object)
```

Expand All @@ -43,6 +45,35 @@ To stop Service, run the following command:
cd ~/ohas && ./ohas-linux-amd64.bin stop
```

You'll need to modify the `ohas.toml` config file for your system. By default, the system will check for this config file in the following directories (from high to low priority):

```text
/etc/ohas/
/usr/local/ohas/
/usr/local/etc/ohas/
/var/lib/ohas/
Current Directory
```

A sample structure of the config file:

```toml
[server]
port = "9595" # define port number to run the service. It's your responsibility to select an unused port number.

[files]
guideline_file = "guideline_hearts.json" # name of the guideline JSON file
guideline_content_file = "guideline_hearts_content.json" # name of the guideline content JSON file
goal_file = "goals_hearts.json" # name of the goal JSON file
goal_content_file = "goals_hearts_content.json" # name of the goal content JSON file
log_file = "ohas-logs.db" # name of the logs database file

[directories]
guideline_path = "contents" # path of the directory for guideline JSON and guideline content JSON file
goal_path = "contents" # path of the directory for goal JSON and goal content JSON file
log_file_path = "~" # path of the log file
```

To view the documentation in the given html file, run it within a webserver.
For example, you can run a simple server for preview:

Expand Down
14 changes: 14 additions & 0 deletions ohas.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[server]
port = "9595" # define port number to run the service. It's your responsibility to select an unused port number.

[files]
guideline_file = "guideline_hearts.json" # name of the guideline JSON file
guideline_content_file = "guideline_hearts_content.json" # name of the guideline content JSON file
goal_file = "goals_hearts.json" # name of the goal JSON file
goal_content_file = "goals_hearts_content.json" # name of the goal content JSON file
log_file = "ohas-logs.db" # name of the logs database file

[directories]
guideline_path = "contents" # path of the directory for guideline JSON and guideline content JSON file
goal_path = "contents" # path of the directory for goal JSON and goal content JSON file
log_file_path = "~" # path of the log file
2 changes: 1 addition & 1 deletion pkg/algorithms/hearts/hearts.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (d *Data) get(ctx context.Context) error {
}

// Physical Activity
ph, err := engineGuide.Body.Lifestyle.PhysicalActivity.Process(p.PhysicalActivity)
ph, err := engineGuide.Body.Lifestyle.PhysicalActivity.Process(p.PhysicalActivity, p.Gender, p.Age)
if err != nil {
errs = append(errs, err.Error())
} else {
Expand Down
112 changes: 41 additions & 71 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,58 @@
// Package config provides configuration constants and variables for the agent.
//
// WARNING: This package must not import any other agent's package.
// Otherwide the circle dependency might occur.
// Package config provides configuration constants and variables
package config

import (
"time"
)

// Default contains default configuration settings.
var Default Settings = NewSettings()
"path/filepath"

// NewSettings returns default configuration settings.
//
// WARNING! All the configuration MUST be done by setting appropriate values here!
func NewSettings() Settings {
agent := &Agent{
// The number of tasks being executing simultaneously.
maxTaskExecutors: 4,
// An URL which is used when --local flag is passed.
// Must not be changed.
localServerURL: "http://127.0.0.1:8080",
"github.com/spf13/viper"
)

// Interval between iterations.
loopInterval: 60 * time.Second,
startDelayDuration: 2 * time.Minute,
// CurrentSettings function selects and responds correct settings
func CurrentSettings() Settings {
v := viper.New()
defaults := defaultSettings()

// Default Timeout for shell commands.
// Must not be changed.
cmdexecTimeout: 10 * time.Second,
for key, value := range defaults {
v.SetDefault(key, value)
}

api := &API{
Methods: map[string]string{
EPCompanyConfiguration: "/api/v2.1/company-configuration",
EPConnections: "/api/v2.1/connections",
EPFile: "/api/v2/file",
EPFileSearch: "/api/v2/file-search",
EPFileSystem: "/api/v2/filesystem-snapshot",
EPLog: "/api/v2/log",
EPRegister: "/api/v2/agent/register",
EPReport: "/api/v2/report",
},
}
v.SetConfigName("ohas")
v.AddConfigPath("/etc/ohas/")
v.AddConfigPath("/usr/local/ohas/")
v.AddConfigPath("/usr/local/etc/ohas/")
v.AddConfigPath("/var/lib/ohas/")
v.AddConfigPath(".")

common := &Common{
appAuthorName: "CF Automation",
appAuthorEmail: "ms.cf.automation@aurea.com",
licenseKey: "unknown",
serverURL: "https://gravity.devfactory.com",
logDestination: "combined",
logLevel: "info",
logMaxFileSize: 10 * 1 << 20,
v.ReadInConfig()

// Daemon graceful timeout.
gracefulTimeout: 15 * time.Second,
connMapDuration: 10 * 60 * 60,
}
return configSettings(v)
}

http := &HTTP{
// This value sets actual timeout for http client.
clientTimeout: 180 * time.Second,
func defaultSettings() map[string]interface{} {
settings := map[string]interface{}{
"server.port": "9595",
"files.guideline_file": "guideline_hearts.json",
"files.guideline_content_file": "guideline_hearts_content.json",
"files.goal_file": "goals_hearts.json",
"files.goal_content_file": "goals_hearts_content.json",
"files.log_file": "logs.db",
"directories.guideline_path": "",
"directories.goal_path": "",
"directories.log_file_path": "",
}

// Settings for retry in httplib.Service.
backoffMinTimeout: 100 * time.Millisecond,
backoffMaxTimeout: 5 * time.Minute,
backoffFactor: 2,
return settings
}

// Low level http transport settings.
// They must not be changed at all.
dialTimeout: 30 * time.Second,
keepAlive: 30 * time.Second,
tLSHandshakeTimeout: 10 * time.Second,
idleConnTimeout: 90 * time.Second,
expectContinueTimeout: 1 * time.Second,
}
func configSettings(v *viper.Viper) Settings {
settings := Settings{}

settings := Settings{
Agent: agent,
API: api,
Common: common,
HTTP: http,
Operations: operationsList,
}
settings.Port = v.GetString("server.port")
settings.GuidelineFile = filepath.Join(v.GetString("directories.guideline_path"), v.GetString("files.guideline_file"))
settings.GuidelineContentFile = filepath.Join(v.GetString("directories.guideline_path"), v.GetString("files.guideline_content_file"))
settings.GoalFile = filepath.Join(v.GetString("directories.goal_path"), v.GetString("files.goal_file"))
settings.GoalContentFile = filepath.Join(v.GetString("directories.goal_path"), v.GetString("files.goal_content_file"))
settings.LogFile = filepath.Join(v.GetString("directories.log_file_path"), v.GetString("files.log_file"))

return settings
}
Loading

0 comments on commit 03b614b

Please sign in to comment.