Created
August 15, 2017 15:23
-
-
Save BingjieGao/8994489e78b8aa3866d29249341a9dd9 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; | |
Promise.each(ccgArray, (ccgCode) => { | |
const instanceData = { | |
name: "national demands", | |
tdxHost: "https://q.nqminds.com", | |
shareKeyId: "Bklo9r-I8-", | |
shareKeySecret: "1234554321", | |
commandHost: "https://cmd.nqminds.com", | |
queryHost: "https://q.nqminds.com", | |
accessTokenTTL: 31536000, | |
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}`); | |
// request status until it finishes | |
const timer = setInterval(() => { | |
tdxApi.getDatabotInstanceStatusAsync(startResult.response.instanceId) | |
.then((statusResult) => { | |
if (statusResult.status === "complete") { | |
clearInterval(timer); | |
output.debug("finish one ccg"); | |
return Promise.resolve(); | |
} | |
output.debug(`progressing ${Math.round(statusResult.progress)}%`); | |
}); | |
}, 10000); | |
}) | |
.catch((err) => { | |
throw new Error(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