Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Beginning of calculating new risk score and state resource to refresh…
… single property (formula isn't quite there yet)
- Loading branch information
Showing
10 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
plugins/tymly-rankings-plugin/lib/components/services/rankings/calculate-new-risk-score.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
module.exports = function calculateNewRiskScore (range, riskScore, growthCurve, mean, stdev) { | ||
let value | ||
switch (range) { | ||
case 'veryHigh': | ||
case 'high': | ||
value = +growthCurve + +mean + +stdev | ||
break | ||
case 'medium': | ||
case 'low': | ||
case 'veryLow': | ||
value = (+riskScore / 2) + +growthCurve | ||
break | ||
} | ||
|
||
if (value < riskScore) return value.toFixed(2) | ||
else return riskScore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
.../tymly-rankings-plugin/lib/components/state-resources/refresh-risk-score/doc/example.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"RefreshRiskScore": { | ||
"Type": "Task", | ||
"Resource": "module:refreshRiskScore", | ||
"ResourceConfig": { | ||
"schema": "test", | ||
"category": "factory" | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
plugins/tymly-rankings-plugin/lib/components/state-resources/refresh-risk-score/doc/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
description: 'Regenerates a risk score usin growth curve', | ||
example: require('./example.json') | ||
} |
46 changes: 46 additions & 0 deletions
46
plugins/tymly-rankings-plugin/lib/components/state-resources/refresh-risk-score/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const calculateNewRiskScore = require('./../../services/rankings/calculate-new-risk-score.js') | ||
|
||
class RefreshRiskScore { | ||
init (resourceConfig, env, callback) { | ||
this.rankings = env.blueprintComponents.rankings | ||
this.storage = env.bootedServices.storage | ||
this.client = env.bootedServices.storage.client | ||
callback(null) | ||
} | ||
|
||
async run (event, context) { | ||
const uprn = event.uprn | ||
const schema = event.schema | ||
const category = event.category | ||
const key = schema + '_' + category | ||
|
||
const rankingModel = this.storage.models[`${schema}_${this.rankings[key].rankingModel}`] | ||
const statsModel = this.storage.models[`${schema}_${this.rankings[key].statsModel}`] | ||
|
||
const rankingDoc = await rankingModel.findById(uprn) | ||
const statsDoc = await statsModel.findById(category) | ||
const riskScore = await this.client.query(getRiskScoreSQL({ | ||
schema: schema, | ||
category: category, | ||
uprn: uprn | ||
})) | ||
|
||
const updatedRiskScore = calculateNewRiskScore( | ||
rankingDoc.range, | ||
riskScore.rows[0].risk_score, | ||
rankingDoc.growthCurve, | ||
statsDoc.mean, | ||
statsDoc.stdev | ||
) | ||
context.sendTaskSuccess({updatedRiskScore}) | ||
} | ||
} | ||
|
||
function getRiskScoreSQL (options) { | ||
return `SELECT risk_score FROM ${_.snakeCase(options.schema)}.${_.snakeCase(options.category)}_scores WHERE uprn = ${options.uprn}` | ||
} | ||
|
||
module.exports = RefreshRiskScore |
13 changes: 13 additions & 0 deletions
13
plugins/tymly-rankings-plugin/lib/components/state-resources/refresh-risk-score/schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"schema": { | ||
"type": "string", | ||
"description": "The schema to refresh the ranking within" | ||
}, | ||
"category": { | ||
"type": "string", | ||
"description": "Identifies the property type to refresh e.g. factory, hospital etc." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
plugins/tymly-rankings-plugin/test/fixtures/blueprint/state-machines/refresh-risk-score.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"Comment": "State machine to test refresh risk score state resource", | ||
"version": "1.0", | ||
"StartAt": "RefreshRiskScore", | ||
"States": { | ||
"RefreshRiskScore": { | ||
"Type": "Task", | ||
"Resource": "module:refreshRiskScore", | ||
"InputPath": "$", | ||
"End": true | ||
} | ||
}, | ||
"restrictions": [ | ||
{ | ||
"roleId": "$authenticated", | ||
"allows": [ | ||
"*" | ||
] | ||
} | ||
] | ||
} |
91 changes: 91 additions & 0 deletions
91
plugins/tymly-rankings-plugin/test/refresh-risk-score-tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const expect = chai.expect | ||
const tymly = require('tymly') | ||
const path = require('path') | ||
const HlPgClient = require('hl-pg-client') | ||
const process = require('process') | ||
const sqlScriptRunner = require('./fixtures/sql-script-runner.js') | ||
|
||
describe('Tests the refresh risk score State Resource', function () { | ||
this.timeout(process.env.TIMEOUT || 5000) | ||
let statebox, tymlyService | ||
|
||
// explicitly opening a db connection as seom setup needs to be carried | ||
// out before tymly can be started up | ||
const pgConnectionString = process.env.PG_CONNECTION_STRING | ||
const client = new HlPgClient(pgConnectionString) | ||
|
||
before(function () { | ||
if (process.env.PG_CONNECTION_STRING && !/^postgres:\/\/[^:]+:[^@]+@(?:localhost|127\.0\.0\.1).*$/.test(process.env.PG_CONNECTION_STRING)) { | ||
console.log(`Skipping tests due to unsafe PG_CONNECTION_STRING value (${process.env.PG_CONNECTION_STRING})`) | ||
this.skip() | ||
} | ||
}) | ||
|
||
it('should create the test resources', () => { | ||
return sqlScriptRunner('./db-scripts/setup.sql', client) | ||
}) | ||
|
||
it('should run the tymly service', function (done) { | ||
tymly.boot( | ||
{ | ||
pluginPaths: [ | ||
path.resolve(__dirname, './..'), | ||
require.resolve('tymly-pg-plugin') | ||
], | ||
blueprintPaths: [ | ||
path.resolve(__dirname, './fixtures/blueprint'), | ||
path.resolve(__dirname, '../lib/blueprints/ranking-blueprint') | ||
], | ||
config: {} | ||
}, | ||
function (err, tymlyServices) { | ||
expect(err).to.eql(null) | ||
tymlyService = tymlyServices.tymly | ||
statebox = tymlyServices.statebox | ||
done() | ||
} | ||
) | ||
}) | ||
|
||
it('should start the state resource execution to generate risk score', function (done) { | ||
statebox.startExecution( | ||
{ | ||
schema: 'test', | ||
category: 'factory', | ||
uprn: 3 | ||
}, // input | ||
'test_refreshRiskScore_1_0', // state machine name | ||
{ | ||
sendResponse: 'COMPLETE' | ||
}, // options | ||
function (err, executionDescription) { | ||
if (err) { | ||
return done(err) | ||
} | ||
console.log(JSON.stringify(executionDescription, null, 2)) | ||
expect(executionDescription.currentStateName).to.eql('RefreshRiskScore') | ||
expect(executionDescription.currentResource).to.eql('module:refreshRiskScore') | ||
expect(executionDescription.stateMachineName).to.eql('test_refreshRiskScore_1_0') | ||
expect(executionDescription.status).to.eql('SUCCEEDED') | ||
done() | ||
} | ||
) | ||
}) | ||
|
||
it('should clean up the test resources', () => { | ||
return sqlScriptRunner('./db-scripts/cleanup.sql', client) | ||
}) | ||
|
||
it('should shutdown Tymly', async () => { | ||
await tymlyService.shutdown() | ||
}) | ||
|
||
it('Should close database connections', function (done) { | ||
client.end() | ||
done() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters