Risk Calculator
This repository consits of functions related to risk-calculator. Its task is to periodically read data a from cloudant (raw sensor data), cleans and normalise the data for calculation and gives current risk-data (ANSHIN-index) as output.
This implementation comprises of smaller sequences, actions and some default openwhisk-cloudant actions.
Implementation steps
Pre-requisite:
- Install IBM cloud CLI
- Setting up CLI environment.
- Create cloudant instance
Steps to implement risk-calulator are mentioned as follows:
- target/create namespace
- create required cloudant-bindings
- deploying the risk-calculator actions, sequences and trigger
Target namespace
To us IBM Cloud functions, create or target a namespace. In this sample the namespace name is risk-calculation-notification
.
-
List namespaces to check your namespace exists
ibmcloud fn namespace list
-
if the namespace doesnot exist, create a namespace
ibmcloud fn namespace create <namespace_name> [--description <"description of your namespace">]
Sample
ibmcloud fn namespace create risk-calculation-notification --description "This namespace contains IBM cloud functions related to risk calculation and storage in risk-history DB"
-
Verify namespace creation
ibmcloud fn namespace get <namespace_name_or_id> --properties
Sample
ibmcloud fn namespace get risk-calculation-notification --properties
-
-
Target namespace
ibmcloud fn namespace target <namespace_name_or_id>
Sample
ibmcloud fn namespace target risk-calculation-notification
Binding the /whisk.system/cloudant package to your IBM Cloudant database
-
Create a /whisk.system/cloudant package binding that is configured for your IBM Cloudant account and verify it. In this example, the package name is
myCloudant
.ibmcloud fn package bind /whisk.system/cloudant myCloudant ibmcloud fn package list
Example output
packages /< ...namespace ....>/myCloudant private
-
Get the service instance to bind to an action or package. If you don't see, create one.
ibmcloud resource service-instances
Example output
Name Location State Type cloudant-dev jp-tok active service_instance
-
Get the credentials for your cloudant-service instance.
ibmcloud resource service-keys --instance-name cloudant-dev
Example output
Name State Created At ServiceCredential_Writer active Thu Jun 4 08:37:15 UTC 2020 cloudant-key-writer active Wed Jul 22 02:32:19 UTC 2020 cloudant-key-reader active Wed Jul 22 02:32:25 UTC 2020
-
Bind the service to your created package
myCloudant
, and very a successful bound.ibmcloud fn service bind cloudantnosqldb myCloudant --instance cloudant-dev --keyname 'ServiceCredential_Writer' ibmcloud fn package get myCloudant parameters
Deploying the Risk Calculator package
-
Clone the Risk-Calculator template repo.
git clone https://github.com/Hitachi-CTI-Call-For-Code-COVID-19-Team/risk-calculator.git
REQUIRED SETTING:
- Set Cloudant credentials: input credentials here and here.
you can gets cloudant credentials from delivery repo
(delivery/scripts/.credentials)
or as obtained in above (Cloudant-binding) steps.
- Set Cloudant credentials: input credentials here and here.
you can gets cloudant credentials from delivery repo
-
Deploy the template
You must include a package name to contain your action. Replace with a custom name for your package. In this sample, package name is
risk-calculator
PACKAGE_NAME=<name> ibmcloud fn deploy -m manifest.yaml
Sample
PACKAGE_NAME= risk-calculator ibmcloud fn deploy -m manifest.yaml
After the template deploys, you can make further edits to the code to customize it as needed, or go back and check out the catalog of available templates.
Creating actions from the CLI
-
Creating actions.
ibmcloud fn action create <action_name> <file> --kind <runtime>
Samples
cd risk-calculator/runtimes/actions/riskCalculationFlow/ ibmcloud fn action create risk-calculator/getTodaysSensorDBName periodicDataQuery/getTodaysSensorDBName.py --kind python:3.7 ibmcloud fn action create risk-calculator/createQuerySelector periodicDataQuery/createQuerySelector.js --kind nodejs:10 ibmcloud fn action create risk-calculator/checkQueryValidity periodicDataQuery/checkQueryValidity.py --kind python:3.7 ibmcloud fn action create risk-calculator/normaliseDataPerQueryInterval prepareDataForCalculation/normaliseDataPerQueryInterval.py --kind python:3.7 ibmcloud fn action create risk-calculator/queryRelevantStaff prepareDataForCalculation/queryRelevantStaff.py --kind python:3.7 ibmcloud fn action create risk-calculator/calculator calculator.py --kind python:3.7 ibmcloud fn action create risk_calculator/formatData_BulkWrite formatData_BulkWrite.py --kind python:3.7
-
Verify that the actions are in your actions list.
ibmcloud fn action list
Creating a sequence from the CLI
-
Create a sequence created actions (here, the directory name represents sequence names).
ibmcloud fn action create <sequence_name> --sequence <action_1>,<action_2>
Sample:
This sample Create sthree sequences named:
periodicDataQuery, prepareDataForCalculation, riskCalculationFlow
.- Uses Cloudant-package functions named
exec-query-find
andmanage-bulk-documents
.
ibmcloud fn action create periodicDataQuery --sequence getTodaysSensorDBName,createQuerySelector,/_/myCloudant/exec-query-find,checkQueryValidity ibmcloud fn action create prepareDataForCalculation --sequence normaliseDataPerQueryInterval,queryRelevantStaff ibmcloud fn action create risk-calculator/riskCalculationFlow --sequence periodicDataQuery,prepareDataForCalculation,risk_calculator/calculator,risk_calculator/formatData_BulkWrite,/_/myCloudant/manage-bulk-documents
- Uses Cloudant-package functions named
Creating triggers from the CLI
-
Create the trigger. Triggers must be created directly within a namespace and can't be created inside packages. In this case, we create a periodic trigger.
ibmcloud fn trigger create TRIGGER_NAME
Sample:
Trigger for ONE minute, you can change cron expression in
cron "* * * * *"
to change the intervalibmcloud fn trigger create periodicOneMinute --feed /whisk.system/alarms/alarm -p cron "* * * * *" -p trigger_payload "{}"
Example output
ok: created trigger periodicOneMinute
-
Verify that the trigger is created.
ibmcloud fn trigger list
Example output
triggers /NAMESPACE/periodicOneMinute private
Next, you can test the trigger or create a rule to associate the trigger with an action.
-
Set rule to conect trigger to sequence
Create a rule that invokes the risk-calculation sequence every time the everyOneMinute trigger gets fired.
ibmcloud fn rule create ruleRC everyOneMinute risk_calculator/riskCalculationFlow
Example output
ok: created rule ruleRC
-
Check that the action is being invoked by polling for activation logs.
ibmcloud fn activation poll
You can see that the activations occur every minute for the trigger, the rule, and the action.