-
-
Save TobyEalden/f443f6fa7773d4a822b4e37c6805b20f to your computer and use it in GitHub Desktop.
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
module.exports = (function() { | |
"use strict"; | |
const Promise = require("bluebird"); | |
function databot(input, output, context) { | |
const tdxApi = context.tdxApi; | |
const mappingId = input.mappingId; | |
// filter boundary dataset with a list of all ccgs | |
const filter = { | |
mappingType: "ons-mapping", | |
parentType: "CCG15CD", | |
}; | |
tdxApi.getDistinctAsync(mappingId, "parentId", filter, {parentId: 1}, {limit: 0}) | |
.then((response) => { | |
const ccgArray = response.data; | |
return Promise.each(ccgArray, (ccgCode) => { | |
const instanceData = { | |
name: `national demands ${ccgCode}`, | |
shareKeyId: context.shareKeyId, | |
shareKeySecret: context.shareKeySecret, | |
authTokenTTL: 31536000, | |
debugMode: true, | |
inputs: { | |
"gpDemand-databotType": "poplet", | |
"gpDemand-ccgCode": ccgCode, | |
"gpDemand-mappingId": mappingId, | |
"gpDemand-parentFolderId": input.parentFolderId, | |
}, | |
}; | |
// start one databot instance depending on each ccg code | |
return tdxApi.startDatabotInstanceAsync(input.databotChainId, instanceData) | |
.then((startResult) => { | |
output.debug(`started databot instance id is ${startResult.response.instanceId}`); | |
return new Promise((resolve, reject) => { | |
// request status until it finishes | |
const timer = setInterval(() => { | |
tdxApi.getDatabotInstanceStatusAsync(startResult.response.instanceId) | |
.then((statusResult) => { | |
if (statusResult.status === "complete") { | |
clearInterval(timer); | |
output.debug("finished ccg %s", ccgCode); | |
resolve(); | |
} else if (statusResult.status === "error") { | |
clearInterval(timer); | |
reject(new Error(`ccg ${ccgCode} failed`)); | |
} | |
output.debug(`progressing ${Math.round(statusResult.progress)}%`); | |
}) | |
.catch((err) => { | |
clearInterval(timer); | |
reject(err); | |
}); | |
}, 10000); | |
}); | |
}) | |
.catch((err) => { | |
return Promise.reject(err)); | |
}); | |
}); | |
}) | |
.catch((err) => { | |
output.debug(`catch error ${err.message}`); | |
}); | |
} | |
const input = require("nqm-databot-utils").input; | |
input.pipe(databot); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment