Skip to content

kofman-alex/triag

Repository files navigation

TRIAG – A Rule Service for Member Activity Monitoring

Getting Started

Prerequisites

  • Python 3.7 or later
  • PostgreSQL 13

Install required packages:

pip install -r requirements.txt
pip install -r requirements_test.txt

Setup

  1. Create the database schema:

    cd setup
    SCHEMA=<schema> ./setup_in_schema.sh "postgresql:https://USER:PASSWORD@/DATABASE?host=UNIX_SOCKET_PATH"

    For example, for schema test in a ClodSQL PostgreSQL instance, running the command on MacOS is as follows:

    SCHEMA=test ./setup_in_schema.sh "postgresql:https://USER:PASSWORD@/DATABASE?host=/Users/USERNAME/cloudsql/CONNECTION_NAME"
  2. Deploy the rules:

    cd setup
    SCHEMA=<schema> ./create_rules_in_schema.sh "postgresql:https://USER:PASSWORD@/DATABASE?host=UNIX_SOCKET_PATH"

Running the rule service locally

export USER=<USER> 
export PASSWORD=<PASSWORD>
export DATABASE=<DATABASE>
export UNIX_SOCKET=<UNIX_SOCKET_PATH>
export SCHEMA=<SCHEMA>
python rule_service.py

To execute the rule set on the contents of the database:

curl https://127.0.0.1:8080/execute_ruleset

The valid response would be:

{"status":"OK"}

Testing

Testing the rule engine and rules correctness:

export USER=<USER> 
export PASSWORD=<PASSWORD>
export DATABASE=<DATABASE>
export UNIX_SOCKET=<UNIX_SOCKET_PATH>
export SCHEMA=<SCHEMA>
pytest

Instead of environment variables, the configuration can also be specified in a json file (see example). The command line would be as follows:

pytest [--config=<config file>]

if --config option not explicitly set, then the default path is secrets/config.json.

Trigger a remote rule service

A remote instance of the rule service can be invoked by a GET HTTP request, as follows:

curl https://host:port/execute_ruleset

Command Line Tool

You can use the triagctl command line tool to manage and test the service.

Run python triagctl --help to see the syntax and the available commands.

usage: triagctl.py [-h] [--config CONFIG] [--command COMMAND]
                   [--user_id USER_ID] [--ts TS] [--type TYPE]
                   [--description DESCRIPTION] [--debug] [--scenario SCENARIO]
                   [--rule_spec RULE_SPEC] [--rule_id RULE_ID]

TRIAG Command Line Tool

optional arguments:
  -h, --help            show this help message and exit
  --config CONFIG       Configuration file (JSON)
  --command COMMAND     Command ["add-event | get-events | get-alerts |
                        create-scenario | clear-events | clear-alerts | clear-
                        all | add-rule"]
  --user_id USER_ID     User ID
  --ts TS               Timestamp (yyyy-mm-ddThh:mm:ss)
  --type TYPE           Event type
  --description DESCRIPTION
                        Event description
  --debug               Print debug information
  --scenario SCENARIO   Name of the scenario to create: inactivity | missing-
                        medication | pro-deterioration | activity-endorsement
  --rule_spec RULE_SPEC
                        Path to the rule spec (JSON)
  --rule_id RULE_ID     Rule ID

For example of the config file see config.json template.

Examples

  1. Add an event

     python triagctl.py --command add-event --user_id=1 --ts=2021-03-23T13:02:22+0300 --type=medication --description=Done
  2. Print events

     python triagctl.py --command=get-events
  3. Print alerts

     python triagctl.py --command=get-alerts
  4. Add a new rule

     python triagctl.py --command=add-rule --rule_spec=test_data/sample_rule.json
  5. Delete a rule

     python triagctl.py --command=delete-rule --rule_id=greeting
  6. Print rules

     python triagctl.py --command=get-rules

Architecture

See the architecture overview document.