HUGE Refactoring Superalgos Project to Foundations Project done

This commit is contained in:
Luis Fernando Molina committed 2021-06-09 22:21:01 +02:00
1 parent c9c0aeaca7
commit 87edcc043d
3557 files changed
+10791 -10789

No files matched your search

@@ -0,0 +1,259 @@
exports.newFoundationsProcessModulesDataDependencies = function (processIndex) {
/*
This module mantains a curated array of Data Dependency nodes,
which is not exactly the ones listed at the Process Dependencies,
but a smaller subset based on the circunstances found at the user's
workspace. For example:
1. Dependencies without a Reference Parent are filtered out.
2. Dependencies which don't have a Data Set are filtered out.
It also mantains an array of Data Set Modules, which are the
one's that ultimately know how to load a file based on the
network configuration found at the workspace at the moment this
Task was launched.
Finally it mantains a Filter map, that allows the users of this module
to filter out data dependencies based on the code written by users
at the Javascript Code and Formula nodes.
*/
const MODULE_NAME = "Data Dependencies"
let thisObject = {
filters: undefined,
defaultExchange: undefined,
defaultMarket: undefined,
curatedDependencyNodeArray: undefined,
dataSetsModulesArray: [],
initialize: initialize,
finalize: finalize
}
thisObject.filters = {
market: {
list: new Map()
},
exchange: {
list: new Map(),
markets: new Map(),
products: new Map(),
timeFrames: new Map()
}
}
return thisObject
function initialize(callBackFunction) {
try {
/* Basic Valdidations */
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processDependencies !== undefined) {
/*
First we will get all the data dependencies of the bot, as defined at the Trading Mine.
*/
thisObject.curatedDependencyNodeArray = TS.projects.foundations.utilities.nodeFunctions.nodeBranchToArray(TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processDependencies, 'Data Dependency')
if (thisObject.curatedDependencyNodeArray.length === 0) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onInitilized -> It is not possible to not have data dependencies at all.")
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
return
}
/*
We will filter out declared dependencies that are not present in the workspace.
This will allow the user to have less Data Mines loaded at the workspace
that the ones that a Trading Mine depends on.
*/
thisObject.curatedDependencyNodeArray = TS.projects.foundations.utilities.nodeFunctions.filterOutNodeWihtoutReferenceParentFromNodeArray(thisObject.curatedDependencyNodeArray)
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onInitilized -> It is not possible not to have process dependencies, which means not data dependencies.")
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
return
}
if (thisObject.curatedDependencyNodeArray.length === 0) {
// We allow old indicators not to declare their data dependencies.
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
return
}
/*
Store here the default exchange and market.
*/
thisObject.defaultExchange = TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName
thisObject.defaultMarket =
TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName +
"-" +
TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName
/* Session based dependency thisObject.filters */
let receivedDependencyFilters = TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).DEPENDENCY_FILTER
if (receivedDependencyFilters !== undefined) {
/*
Javascript Maps can not travel stringified at ta JSON object, so we receive
these maps in an Array format, and here we will just convert them to Maps,
that is how we need them.
*/
arrayToMap(receivedDependencyFilters.market.list, thisObject.filters.market.list)
arrayToMap(receivedDependencyFilters.exchange.list, thisObject.filters.exchange.list)
arrayToMap(receivedDependencyFilters.exchange.markets, thisObject.filters.exchange.markets)
arrayToMap(receivedDependencyFilters.exchange.products, thisObject.filters.exchange.products)
arrayToMap(receivedDependencyFilters.exchange.timeFrames, thisObject.filters.exchange.timeFrames)
function arrayToMap(array, map) {
for (let i = 0; i < array.length; i++) {
let key = array[i]
map.set(key, true)
}
}
}
/*
In case we do not receive any filters, like in the case of Indicators and Sensors,
or simply because the Learning System is still empty,
we need to add the default exchange and market to these maps in order
for the following sections to work, and later we at least have candles at the
Learning Simulation.
*/
thisObject.filters.market.list.set(thisObject.defaultMarket, true)
thisObject.filters.exchange.list.set(thisObject.defaultExchange, true)
thisObject.filters.exchange.markets.set(thisObject.defaultExchange + '-' + thisObject.defaultMarket, true)
/*
For each dependency declared at the curatedDependencyNodeArray, we will initialize a
DataSet as part of this initialization process.
*/
let alreadyCalledBack = false
/*
The current curatedDependencyNodeArray that we have includes all the dependencies daclared in the Data Mine | Trading Mine | Learning Mine
process dependencies, minus the ones without reference parent, meaning that references Data Mines that are
not present at the workspace. From all the remaining dependencies there will be others that we need to
filter out, and they are the ones which the user does not have a data product anywhere on the network where
this task is running, that is storing the data associated to this dependency. The way we use to filter this
out is by analizyng the response of the initialization call to the DataSet module. If it did not fail but
it was not initialized, then we will assume this happened because the data could not be located and we will
assume the user did this on purpose and we will filter out this dependency by not putting it on this new array.
*/
let newNodeArray = []
let exchangeList = Array.from(thisObject.filters.exchange.list.keys())
let marketList = Array.from(thisObject.filters.market.list.keys())
/*
For each Exchange / Market we will add the datasets objects to our collection.
*/
for (let e = 0; e < exchangeList.length; e++) {
let exchange = exchangeList[e]
for (let m = 0; m < marketList.length; m++) {
let market = marketList[m]
if (receivedDependencyFilters !== undefined) {
/*
If we received a Dependency Filter from the UI, we will apply it here so
as not to add DataSets that we wont actually need.
*/
if (thisObject.filters.exchange.markets.get(exchange + '-' + market) === true) {
addDataSets(exchange, market)
}
} else {
/*
If we did not received these dependency filters, like when we are processing
Indicators, we will just add all datasets from exchange / market combination
because it will only be one combination: the default exchange and the default market.
*/
addDataSets(exchange, market)
}
}
}
if (alreadyCalledBack === false) {
alreadyCalledBack = true
thisObject.curatedDependencyNodeArray = newNodeArray
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
return
}
function addDataSets(exchange, market) {
for (let i = 0; i < thisObject.curatedDependencyNodeArray.length; i++) {
let dataDependency = thisObject.curatedDependencyNodeArray[i]
if (receivedDependencyFilters !== undefined) {
/*
If we received a Dependency Filter from the UI, we will make these excta checks
before instantiating a Data Set Module.
*/
if (dataDependency.referenceParent === undefined) {
continue
}
if (dataDependency.referenceParent.parentNode === undefined) {
continue
}
if (dataDependency.referenceParent.parentNode.config.singularVariableName === undefined) {
continue
}
/*
This is the product related to this Data Dependency, as the user would type it
at a Condition or Formula and appear at the UI provided filter.
*/
let product = dataDependency.referenceParent.parentNode.config.singularVariableName
if (thisObject.filters.exchange.products.get(exchange + '-' + market + '-' + product) !== true) {
/*
We will filter out all the data dependencies which are not at the filters, except for candles,
because we will need the candles dataset to run the simulation.
*/
if (dataDependency.referenceParent.parentNode.config.codeName !== 'Candles') {
continue
}
}
addDataSet(exchange, market)
} else {
addDataSet(exchange, market)
}
function addDataSet(exchange, market) {
let dataSetModule = TS.projects.foundations.processModules.dataset.newFoundationsProcessModulesDataset(processIndex)
dataSetModule.initialize(exchange, market, dataDependency, onInitilized)
function onInitilized(err, wasInitialized) {
if (err.result !== TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE.result) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onInitilized -> err = " + JSON.stringify(err))
alreadyCalledBack = true
callBackFunction(err)
return
}
if (wasInitialized === true) {
newNodeArray.push(thisObject.curatedDependencyNodeArray[i])
thisObject.dataSetsModulesArray.push(dataSetModule)
}
}
}
}
}
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
}
}
function finalize() {
for (let i = 0; i < thisObject.dataSetsModulesArray.length; i++) {
let dataSetModule = thisObject.dataSetsModulesArray[i]
dataSetModule.finalize()
}
thisObject.dataSetsModulesArray = undefined
thisObject.curatedDependencyNodeArray = undefined
thisObject.filters = undefined
thisObject.defaultExchange = undefined
thisObject.defaultMarket = undefined
bot = undefined
}
}
@@ -0,0 +1,328 @@
exports.newFoundationsProcessModulesDataset = function (processIndex) {
/*
This module represents a Dataset at a certain localtion on the
network, and it is able to load a file from that dataset or
write one into it.
*/
const MODULE_NAME = "Dataset";
let thisObject = {
node: undefined,
networkNode: undefined,
exchange: undefined,
market: undefined,
product: undefined,
initialize: initialize,
finalize: finalize,
getTextFile: getTextFile,
createTextFile: createTextFile
};
/* Storage account to be used here. */
let fileStorage
return thisObject;
function initialize(exchange, market, dataDependency, callBackFunction) {
try {
thisObject.node = dataDependency.referenceParent;
thisObject.exchange = exchange
thisObject.market = market
/* Some very basic validations that we have all the information needed. */
if (thisObject.node === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Data Dependency without Reference Parent -> dataDependency = " + JSON.stringify(dataDependency));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (thisObject.node.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Dataset witn no codeName defined -> Product Dataset = " + JSON.stringify(thisObject.node));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (thisObject.node.parentNode === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Dataset not attached to a Product Definition -> Dataset = " + JSON.stringify(thisObject.node));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (thisObject.node.parentNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Product Definition witn no codeName defined -> Product Definition = " + JSON.stringify(thisObject.node.parentNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (thisObject.node.parentNode.parentNode === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Product Definition not attached to a Bot -> Product Definition = " + JSON.stringify(thisObject.node.parentNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (thisObject.node.parentNode.parentNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Bot witn no codeName defined. Bot = " + JSON.stringify(thisObject.node.parentNode.parentNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (thisObject.node.parentNode.parentNode.parentNode === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Bot not attached to a Data Mine or Trading Mine. Bot = " + JSON.stringify(thisObject.node.parentNode.parentNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (thisObject.node.parentNode.parentNode.parentNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Data Mine witn no codeName defined. Data Mine = " + JSON.stringify(thisObject.node.parentNode.parentNode.parentNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
/* Now we will see where do we need to fetch this data from. */
let network = TS.projects.foundations.globals.taskConstants.NETWORK_NODE
let productDefinition = thisObject.node.parentNode
thisObject.product = productDefinition.config.singularVariableName
/*
We will find the networkNode that leads to this Product Definition
and is related to this exchange and market.
*/
let networkNode = fincNetworkNode(network, productDefinition, thisObject.exchange, thisObject.market)
if (networkNode === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> Network Node not found.")
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> Initialization Failed because we could not find where the data of this dataset is located within the network. Check the logs for more info.");
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> Could not find where " + productDefinition.name + " for " + thisObject.exchange + " " + thisObject.market + " is stored within the network.");
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE, false);
return
}
/* We found where the data is located on the network. */
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] initialize -> Retrieving data from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.')
fileStorage = TS.projects.foundations.taskModules.fileStorage.newFileStorage(processIndex, networkNode.config.host, networkNode.config.webPort);
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE, true);
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack);
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
}
function finalize() {
fileStorage = undefined
thisObject.networkNode = undefined
thisObject.exchange = undefined
thisObject.market = undefined
bot = undefined
thisObject = undefined
}
function getTextFile(pFolderPath, pFileName, callBackFunction) {
try {
let mine = thisObject.node.parentNode.parentNode.parentNode.config.codeName
let mineType = thisObject.node.parentNode.parentNode.parentNode.type.replace(' ', '-')
let project = thisObject.node.parentNode.parentNode.parentNode.project
let botCodeName = thisObject.node.parentNode.parentNode.config.codeName
let filePathRoot = 'Project/' + project + "/" + mineType + "/" + mine + "/" + botCodeName + '/' + thisObject.exchange + "/" + thisObject.market
let filePath = filePathRoot + "/Output/" + pFolderPath;
filePath += '/' + pFileName
fileStorage.getTextFile(filePath, onFileReceived);
function onFileReceived(err, text) {
callBackFunction(err, text);
}
}
catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] 'getTextFile' -> err = " + err.stack);
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
}
function createTextFile(pFolderPath, pFileName, pFileContent, callBackFunction) {
try {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] createTextFile -> pFolderPath = " + pFolderPath)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] createTextFile -> pFileName = " + pFileName)
let ownerId = thisObject.node.dataMine + "-" + thisObject.node.bot
let botId = TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.parentNode.config.codeName + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.config.codeName
if (ownerId !== botId) {
let customErr = {
result: TS.projects.foundations.globals.standardResponses.CUSTOM_FAIL_RESPONSE.result,
message: "Only bots owners of a Data Set can create text files on them."
};
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] save -> customErr = " + customErr.message);
callBackFunction(customErr);
return;
}
let filePathRoot = 'Project/' + thisObject.node.project + "/" + thisObject.node.mineType + "/" + thisObject.node.dataMine + "/" + thisObject.node.bot + '/' + thisObject.exchange + "/" + thisObject.market
let filePath = filePathRoot + "/Output/" + pFolderPath + '/' + pFileName;
fileStorage.createTextFile(filePath, pFileContent, onFileCreated);
function onFileCreated(err) {
callBackFunction(err);
}
}
catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] 'createTextFile' -> err = " + err.stack);
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
}
function fincNetworkNode(network, productDefinition, exchange, market) {
/*
The problem that we need to solve here is the following:
A. We are interested in a particular Exchange and Market.
B. We have the Network we received from the UI whe the user ran the Task.
C. We have the Product Definition that is the parent node of the Data Set represented by this object.
D. This Product Definition is referenced by one or more Data Products nodes at some branch of the Network Hierarchy.
E. We need to find the right Data Product node, the one who is itself a descendent of the Market Data Products
node that is referencing the market and exchange that we have as a parameter of this function.
So, the strategy we will implement is the following:
1. We will scan all the branches of the Network received from the UI.
2. Once we find a Market Data Products, Market Trading Products o Market Learning Products we will check
that they are referencing the market and exchange we need, other wise we ignore them and continue
with the scanning.
3. Once found, we will go into their own branches and find a path until the Product Definition we need to reach.
4. Once reached, we will know which Network Node is on the full path from the Network to the Product Definition and
we solved the problem.
*/
let found = false
let networkNode
let splittedMarket = market.split('-')
let baseAsset = splittedMarket[0]
let quotedAsset = splittedMarket[1]
scanNodeMesh(network)
if (found === true) {
return networkNode
} else {
return
}
function scanNodeMesh(startingNode) {
if (startingNode === undefined) { return }
let schemaDocument = TS.projects.foundations.globals.taskConstants.APP_SCHEMA_MAP.get(startingNode.project + '-' + startingNode.type)
if (schemaDocument === undefined) { return }
if (startingNode.id === productDefinition.id) {
/*
We reached the point that we found a path to the Product Definition.
Now we know that the last netwrokNode is the node that has the correct
path.
*/
found = true
return
}
if (startingNode.type === 'Network Node') {
/*
We will store the Network Node here so that if we find the rignt path
we can know from which Netwrok Node it going through.
*/
networkNode = startingNode
}
if (
startingNode.type === 'Market Data Products' ||
startingNode.type === 'Market Trading Products' ||
startingNode.type === 'Market Learning Products'
) {
/*
We will check that this guy is referencing the right market and the
market is a descendant of the right exchange. Otherwise we wont process
their children.
*/
if (startingNode.referenceParent === undefined) {
return
}
if (startingNode.referenceParent.baseAsset === undefined) {
return
}
if (startingNode.referenceParent.quotedAsset === undefined) {
return
}
if (startingNode.referenceParent.baseAsset.referenceParent === undefined) {
return
}
if (startingNode.referenceParent.quotedAsset.referenceParent === undefined) {
return
}
if (startingNode.referenceParent.baseAsset.referenceParent.config.codeName !== baseAsset) {
return
}
if (startingNode.referenceParent.quotedAsset.referenceParent.config.codeName !== quotedAsset) {
return
}
if (startingNode.referenceParent.parentNode === undefined) {
return
}
if (startingNode.referenceParent.parentNode.parentNode === undefined) {
return
}
if (startingNode.referenceParent.parentNode.parentNode.config.codeName !== exchange) {
return
}
}
/* We scan through this node children */
if (schemaDocument.childrenNodesProperties !== undefined) {
for (let i = 0; i < schemaDocument.childrenNodesProperties.length; i++) {
let property = schemaDocument.childrenNodesProperties[i]
switch (property.type) {
case 'node': {
if (found !== true) {
scanNodeMesh(startingNode[property.name])
}
}
break
case 'array': {
let startingNodePropertyArray = startingNode[property.name]
if (startingNodePropertyArray !== undefined) {
for (let m = 0; m < startingNodePropertyArray.length; m++) {
if (found !== true) {
scanNodeMesh(startingNodePropertyArray[m])
}
}
}
break
}
}
}
}
/* We scan parents nodes. */
if (startingNode.parentNode !== undefined) {
if (found !== true) {
scanNodeMesh(startingNode.parentNode)
}
}
/* We scan reference parents too. */
if (startingNode.referenceParent !== undefined) {
if (found !== true) {
scanNodeMesh(startingNode.referenceParent)
}
}
}
}
};
@@ -0,0 +1,286 @@
exports.newFoundationsProcessModulesProcessExecutionEvents = function (processIndex) {
/*
Here we manage the process execution events. This allow us to emit events
when the process starts and finishes. Also to wait for a dependent event
of another process.
*/
const MODULE_NAME = "Process Execution Events";
let thisObject = {
networkNode: undefined,
initialize: initialize,
finalize: finalize,
start: start,
finish: finish
};
let processThisDependsOn; // This is the process this process needs to wait for it to finishes in order to start.
let currentProcessKey
let eventServerClient
return thisObject;
function initialize(callBackFunction) {
try {
let name = 'Not Depends on any Process'
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent === undefined) {
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Initialization Failed because the Process Instance needs to have a Reference Parent.");
return
}
let market = TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName
currentProcessKey = TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.name + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.type + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.id + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName + "-" + market
/*
A process can depend for its own execution on another process to finish, and
this can be achieved in two different ways. The first one is by referencing
the Execution Started Event node from a Process Definition node to a Execution
Ended Event node of another Process Definition. The second way is when there is
a Execution Started Event node at a Process Instance. This allows the user to
override the definition created by the Data Mine for an specific task.
*/
/*
Here we check if we can find this relationship at the Data Mine level.
*/
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.executionStartedEvent !== undefined) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.executionStartedEvent.referenceParent !== undefined) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.executionStartedEvent.referenceParent.parentNode !== undefined) {
processThisDependsOn = TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.executionStartedEvent.referenceParent.parentNode
name = processThisDependsOn.name
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] initialize -> " + currentProcessKey + " based on the Data Mine depends on " + name)
}
}
}
/*
Here we check if at the task level if there is an overriding configuration.
*/
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].executionStartedEvent !== undefined) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].executionStartedEvent.referenceParent !== undefined) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].executionStartedEvent.referenceParent.parentNode !== undefined) {
processThisDependsOn = TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].executionStartedEvent.referenceParent.parentNode
name = processThisDependsOn.name
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] initialize -> " + currentProcessKey + " based on the User's Task depends on " + name)
}
}
}
if (processThisDependsOn !== undefined) {
/* We need to remember that this process waits for another process in order to start. */
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).WAIT_FOR_EXECUTION_FINISHED_EVENT = true
/* We need to find at which network node is running the process that we need to hear from when it finished. */
let network = TS.projects.foundations.globals.taskConstants.NETWORK_NODE
/*
Notice that the first task that matches the dependency is going to be considered.
This means that if the same task, with the same process, pointing to the same
process definition in the same data mine, with the same exchange and market
configured, is at two different network nodes at the same time, the current task
is going to depend on the first one found, not necesarily the one running at the
same network node. If you need to control witch task specifically to depend on
and need to exactly the same task to exist, you should split the network in two
different networks, each one with unique tasks so as to avoid confussion.
*/
for (let i = 0; i < network.networkNodes.length; i++) {
let networkNode = network.networkNodes[i]
if (networkNode.dataTasks !== undefined) {
for (let z = 0; z < networkNode.dataTasks.projectDataTasks.length; z++) {
let projectDataTasks = networkNode.dataTasks.projectDataTasks[z]
if (projectDataTasks.exchangeDataTasks === undefined) {continue}
for (let j = 0; j < projectDataTasks.exchangeDataTasks.length; j++) {
let exchangeTasks = projectDataTasks.exchangeDataTasks[j]
for (let p = 0; p < exchangeTasks.marketDataTasks.length; p++) {
let marketTasks = exchangeTasks.marketDataTasks[p]
if (marketTasks.referenceParent === undefined) { continue }
if (marketTasks.referenceParent.id !== TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.id) { continue }
for (let q = 0; q < marketTasks.dataMineTasks.length; q++) {
let mineTasks = marketTasks.dataMineTasks[q]
for (let k = 0; k < mineTasks.taskManagers.length; k++) {
let taskManager = mineTasks.taskManagers[k]
for (let m = 0; m < taskManager.tasks.length; m++) {
let task = taskManager.tasks[m]
if (task.bot !== undefined) {
for (let n = 0; n < task.bot.processes.length; n++) {
let process = task.bot.processes[n]
if (process.referenceParent !== undefined) {
let processDefinition = process.referenceParent
if (processThisDependsOn.id === processDefinition.id) {
/*
We found where the task that runs the process definition
we are waiting for is located on the network.
*/
thisObject.networkNode = networkNode
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME,
"[INFO] initialize -> Connecting to Websockets Server " + networkNode.name +
" -> host = " + networkNode.config.host +
' -> port = ' + networkNode.config.webSocketsPort + '.');
eventServerClient = TS.projects.foundations.taskModules.eventServerClient.newFoundationsTaskModulesEventServerClient(networkNode.config.host, networkNode.config.webSocketsPort)
eventServerClient.initialize(onConnected)
function onConnected() {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] initialize -> Connected to Websockets Server " + networkNode.name +
" -> host = " + networkNode.config.host +
' -> port = ' + networkNode.config.webSocketsPort + '.');
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE);
return
}
return
}
}
}
}
}
}
}
}
}
}
}
}
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> Initialization Failed because we could not find where the task that runs the Process Definition we are waiting for is located within the network. Check the logs for more info.");
return
}
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE);
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack);
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
}
function finalize() {
if (eventServerClient !== undefined) {
eventServerClient.finalize()
eventServerClient = undefined
}
processThisDependsOn = undefined
currentProcessKey = undefined
thisObject.networkNode = undefined
bot = undefined
thisObject = undefined
}
function start(callBackFunction) {
try {
if (processThisDependsOn !== undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> waitForDependantProcess -> Entering Code Block.")
/* This forces this process to wait until the process that this one depends on, emits its event signaling that the process execution has finished. */
let extraCallerId = '-' + Math.trunc(Math.random() * 10000) + '-'
let market = TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName
let key = processThisDependsOn.name + "-" + processThisDependsOn.type + "-" + processThisDependsOn.id + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName + "-" + market
let callerId = TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.parentNode.config.codeName + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.config.codeName + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.config.codeName + extraCallerId
let subscriptionId
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> waitForDependantProcess -> started listening to Process Execution Finished. ")
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> waitForDependantProcess -> key = " + key)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> waitForDependantProcess -> callerId = " + callerId)
eventServerClient.listenToEvent(key, 'Process Execution Finished', undefined, callerId, responseCallBack, eventsCallBack)
TS.projects.foundations.functionLibraries.processFunctions.processHeartBeat(processIndex, undefined, undefined, "Waiting for " + thisObject.networkNode.name + "->" + processThisDependsOn.parentNode.parentNode.name + "->" + processThisDependsOn.parentNode.config.codeName + "->" + processThisDependsOn.config.codeName)
function responseCallBack(message) {
if (message.result !== TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE.result) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> responseCallBack -> message = " + JSON.stringify(message))
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
} else {
subscriptionId = message.eventSubscriptionId
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> waitForDependantProcess -> subscriptionId = " + subscriptionId)
}
}
function eventsCallBack(message) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> eventsCallBack -> Entering function.")
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> eventsCallBack -> message = " + JSON.stringify(message))
/* We continue the normal flow after we learn the dependent process has finished its execution. */
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> eventsCallBack -> stopListening to Process Execution Finished. ")
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> eventsCallBack -> subscriptionId = " + subscriptionId)
eventServerClient.stopListening(key, 'Process Execution Finished', subscriptionId)
if (message.event.err.result === TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE.result) {
/* We emit our own event that the process started. */
let event = {
err: TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE
}
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.createEventHandler(currentProcessKey, 'Process Execution Started')
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.raiseEvent(currentProcessKey, 'Process Execution Started', event)
TS.projects.foundations.functionLibraries.processFunctions.processHeartBeat(processIndex, undefined, undefined, "Running...")
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> eventsCallBack -> " + currentProcessKey + " Process Execution Started because " + key + " Finished.")
}
callBackFunction(message.event.err);
}
} else {
/* In this case, the Process does not depends on a process which needs to wait for. */
/* We emit our own event that the process started. */
let event = {
err: TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE
}
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.createEventHandler(currentProcessKey, 'Process Execution Started')
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.raiseEvent(currentProcessKey, 'Process Execution Started', event)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> " + currentProcessKey + " Process Execution Started ")
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE);
}
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack);
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
}
function finish(callBackFunction) {
try {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] finish -> Entering function.")
let event = {
err: TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE
}
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.createEventHandler(currentProcessKey, 'Process Execution Finished')
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.raiseEvent(currentProcessKey, 'Process Execution Finished', event)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] finish -> " + currentProcessKey + " Process Execution Finished ")
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE);
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] finish -> err = " + err.stack);
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
}
};
@@ -0,0 +1,189 @@
exports.newFoundationsProcessModulesProcessOutput = function (processIndex) {
/*
Here we emit the events that signals that a Dataset was updated.
*/
const MODULE_NAME = "Process Output";
let thisObject = {
raiseEvents: raiseEvents,
asyncRaiseEvents: asyncRaiseEvents
};
return thisObject;
function raiseEvents(lastFile, timeFrames, callBackFunction) {
try {
/* We will reaise the events for the datasets impacted by the process that just finished. */
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processOutput !== undefined) {
let outputDatasets = TS.projects.foundations.utilities.nodeFunctions.nodeBranchToArray(TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processOutput, 'Output Dataset')
for (let j = 0; j < outputDatasets.length; j++) {
let outputDataset = outputDatasets[j]
let botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'Indicator Bot')
if (botNode === undefined) {
botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'Trading Bot')
}
if (botNode === undefined) {
botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'Sensor Bot')
}
if (botNode === undefined) {
botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'API Data Fetcher Bot')
}
if (botNode === undefined) {
botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'Learning Bot')
}
/* Some validations to verify that everything is in place. */
if (outputDataset.referenceParent !== undefined) {
if (outputDataset.referenceParent.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Dataset Definition not defined. Dataset Definition = " + JSON.stringify(outputDataset.referenceParent));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (outputDataset.referenceParent.parentNode !== undefined) {
if (outputDataset.referenceParent.parentNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Product not defined. Product = " + JSON.stringify(outputDataset.referenceParent.parentNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (botNode !== undefined) {
if (botNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Bot not defined. Bot = " + JSON.stringify(botNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
if (botNode.parentNode !== undefined) {
if (botNode.parentNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Data Mine not defined. Data Mine = " + JSON.stringify(botNode.parentNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> Bot not attached to a Data Mine or Trading Mine. Bot = " + JSON.stringify(botNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> Product not attached to a Bot. Product = " + JSON.stringify(outputDataset.referenceParent.parentNode));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> Dataset Definition not attached to a Product. Dataset Definition = " + JSON.stringify(outputDataset.referenceParent));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> Output Dataset with no reference to a Dataset Definition. Output Dataset = " + JSON.stringify(outputDataset));
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
return
}
/* All good, lets emit the event. */
let dataMine = botNode.parentNode.config.codeName
let botName = botNode.config.codeName
let product = outputDataset.referenceParent.parentNode.config.codeName
let dataset = outputDataset.referenceParent.config.codeName
let key = dataMine + "-" + botName + "-" + product + "-" + dataset + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName
let event = {
lastFile: lastFile,
timeFrames: timeFrames
}
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.createEventHandler(key, 'Dataset Updated')
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.raiseEvent(key, 'Dataset Updated', event)
}
}
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE);
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> err = " + err.stack);
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
}
function asyncRaiseEvents(lastFile, timeFrames) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processOutput !== undefined) {
let outputDatasets = TS.projects.foundations.utilities.nodeFunctions.nodeBranchToArray(TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processOutput, 'Output Dataset')
for (let j = 0; j < outputDatasets.length; j++) {
let outputDataset = outputDatasets[j]
let botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'Indicator Bot')
if (botNode === undefined) {
botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'Trading Bot')
}
if (botNode === undefined) {
botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'Sensor Bot')
}
if (botNode === undefined) {
botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'API Data Fetcher Bot')
}
if (botNode === undefined) {
botNode = TS.projects.foundations.utilities.nodeFunctions.findNodeInNodeMesh(outputDataset, 'Learning Bot')
}
/* Some validations to verify that everything is in place. */
if (outputDataset.referenceParent !== undefined) {
if (outputDataset.referenceParent.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Dataset Definition not defined. Dataset Definition = " + JSON.stringify(outputDataset.referenceParent));
throw (TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
if (outputDataset.referenceParent.parentNode !== undefined) {
if (outputDataset.referenceParent.parentNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Product not defined. Product = " + JSON.stringify(outputDataset.referenceParent.parentNode));
throw (TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
if (botNode !== undefined) {
if (botNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Bot not defined. Bot = " + JSON.stringify(botNode));
throw (TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
if (botNode.parentNode !== undefined) {
if (botNode.parentNode.config.codeName === undefined) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> codeName at Mine not defined. Mine = " + JSON.stringify(botNode.parentNode));
throw (TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> Bot not attached to a Data Mine | Trading Mine | Learning Mine. Bot = " + JSON.stringify(botNode));
throw (TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> Product not attached to a Bot. Product = " + JSON.stringify(outputDataset.referenceParent.parentNode));
throw (TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> Dataset Definition not attached to a Product. Dataset Definition = " + JSON.stringify(outputDataset.referenceParent));
throw (TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] raiseEvents -> Output Dataset with no reference to a Dataset Definition. Output Dataset = " + JSON.stringify(outputDataset));
throw (TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
}
/* All good, lets emit the event. */
let dataMine = botNode.parentNode.config.codeName
let botName = botNode.config.codeName
let product = outputDataset.referenceParent.parentNode.config.codeName
let dataset = outputDataset.referenceParent.config.codeName
let key = dataMine + "-" + botName + "-" + product + "-" + dataset + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName + "-" + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + '/' + TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName
let event = {
lastFile: lastFile,
timeFrames: timeFrames
}
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.createEventHandler(key, 'Dataset Updated')
TS.projects.foundations.globals.taskConstants.EVENT_SERVER_CLIENT_MODULE_OBJECT.raiseEvent(key, 'Dataset Updated', event)
}
}
}
};
@@ -0,0 +1,158 @@
exports.newFoundationsProcessModulesStatusDependencies = function (processIndex) {
const MODULE_NAME = "Status Dependencies"
let thisObject = {
nodeArray: undefined,
statusReports: new Map(),
reportsByMainUtility: new Map(),
initialize: initialize,
finalize: finalize,
keys: []
}
return thisObject
function initialize(callBackFunction) {
try {
/* Basic Valdidations */
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processDependencies !== undefined) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processDependencies.statusDependencies !== undefined) {
thisObject.nodeArray = TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processDependencies.statusDependencies
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onInitilized -> It is not possible to not have status dependencies at all.")
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
return
}
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onInitilized -> It is not possible to not have process dependencies, which means not status dependencies.")
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
return
}
/*For each dependency we will initialize the status report, and load it as part of this initialization process.*/
let alreadyCalledBack = false
let loadCount = 0
let dependenciesToProcess = []
for (let i = 0; i < thisObject.nodeArray.length; i++) {
let statusDependency = {
dependency: thisObject.nodeArray[i]
}
dependenciesToProcess.push(statusDependency)
}
for (let i = 0; i < dependenciesToProcess.length; i++) {
let statusReportModule = TS.projects.foundations.processModules.statusReport.newFoundationsProcessModulesStatusReport(processIndex)
let statusDependency = dependenciesToProcess[i]
statusReportModule.initialize(statusDependency.dependency, onInitilized)
function onInitilized(err) {
if (err.result !== TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE.result) {
if (alreadyCalledBack === false) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onInitilized -> err = " + err.stack)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onInitilized -> err.message = " + err.message)
alreadyCalledBack = true
callBackFunction(err)
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> Can not call back because I already did.")
}
return
}
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] initialize -> onInitilized -> Loading Status Report # " + (i + 1))
statusReportModule.load(onLoad)
}
function onLoad(err) {
try {
statusReportModule.status = err.message
switch (err.message) {
case TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE.message: {
addReport()
return
}
case "Status Report was never created.": {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> onLoad -> err = " + err.stack)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> onLoad -> Report Not Found. -> Status Dependency = " + JSON.stringify(statusDependency.dependency))
addReport()
return
}
case "Status Report is corrupt.": {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> onLoad -> err = " + err.stack)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> onLoad -> Report Not Found. -> Status Dependency = " + JSON.stringify(statusDependency.dependency))
addReport()
return
}
default:
{
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onLoad -> Operation Failed.")
if (alreadyCalledBack === false) {
alreadyCalledBack = true
callBackFunction(err)
}
return
}
}
}
catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> onLoad -> err = " + err.stack)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
}
}
function addReport() {
loadCount++
let key = statusDependency.dependency.dataMine + "-" + statusDependency.dependency.bot + "-" + statusDependency.dependency.process
thisObject.keys.push(key)
thisObject.statusReports.set(key, statusReportModule)
if (statusReportModule.mainUtility !== undefined) {
thisObject.reportsByMainUtility.set(statusReportModule.mainUtility, statusReportModule)
}
if (loadCount === dependenciesToProcess.length) {
if (alreadyCalledBack === false) {
alreadyCalledBack = true
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
return
} else {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[WARN] initialize -> addReport -> Can not call back because I already did.")
}
}
}
}
} catch (err) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
}
}
function finalize() {
thisObject.statusReports.forEach(forEachStatusDependency)
function forEachStatusDependency(statusDependency) {
statusDependency.finalize()
}
thisObject.statusReports = undefined
bot = undefined
thisObject = undefined
}
}
@@ -0,0 +1,487 @@
exports.newFoundationsProcessModulesStatusReport = function (processIndex) {
/*
A Status Report is a file that every bot reads at the begining of its execution
and saves after it finishes its job. The purpose of the file is to record checkpoint information
of what was the last thing done by the bot and helpfull enough to start the next execution.
It usually does not include business related context data.
*/
const MODULE_NAME = "Status Report"
let thisObject = {
networkNode: undefined,
mainUtility: undefined,
file: undefined, // Here we have the JSON object representing the file content.
initialize: initialize,
finalize: finalize,
load: load,
save: save,
asyncSave: asyncSave,
status: undefined
}
let statusDependencyNode
/* Storage account to be used here. */
let fileStorage
let sessionPath = ''
return thisObject
function initialize(pStatusDependencyNode, callBackFunction) {
try {
statusDependencyNode = pStatusDependencyNode
/* Some very basic validations that we have all the information needed. */
if (statusDependencyNode.referenceParent === undefined) {
validationFailed(statusDependencyNode, "Status Dependency without Reference Parent.")
return
}
if (statusDependencyNode.referenceParent.parentNode === undefined) {
validationFailed(statusDependencyNode.referenceParent, "Status Report not attached to a Process Definition.")
return
}
if (statusDependencyNode.referenceParent.parentNode.config.codeName === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode, "Process Definition witn no codeName defined.")
return
}
if (statusDependencyNode.referenceParent.parentNode.parentNode === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode, "Process Definition not attached to a Bot.")
return
}
if (statusDependencyNode.referenceParent.parentNode.parentNode.config.codeName === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode, "Bot with no codeName defined.")
return
}
if (statusDependencyNode.referenceParent.parentNode.parentNode.parentNode === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode, "Bot not attached to a Data Mine.")
return
}
if (statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.config.codeName === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode.parentNode, "Data Mine witn no codeName defined.")
return
}
function validationFailed(errorInNode, errorMessage) {
let nodeString = JSON.stringify(errorInNode)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] initialize -> " + errorMessage + ' -> nodeString = ' + nodeString
)
TS.projects.foundations.functionLibraries.processFunctions.processError(
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_KEY, errorInNode, errorMessage
)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
}
/* Simplifying the access to basic info */
statusDependencyNode.bot = statusDependencyNode.referenceParent.parentNode.parentNode.config.codeName
statusDependencyNode.process = statusDependencyNode.referenceParent.parentNode.config.codeName
statusDependencyNode.bottype = statusDependencyNode.referenceParent.parentNode.parentNode.type
statusDependencyNode.dataMine = statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.config.codeName
statusDependencyNode.mineType = statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.type.replace(' ', '-')
statusDependencyNode.project = statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.project
/* We retrieve the report main utility */
if (statusDependencyNode.config !== undefined) {
if (statusDependencyNode.config.mainUtility !== undefined) {
thisObject.mainUtility = statusDependencyNode.config.mainUtility
}
}
if (TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE !== undefined && statusDependencyNode.bottype === "Trading Bot") {
sessionPath = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_FOLDER_NAME + "/"
}
/* Now we will see where do we need to fetch this status report from. */
let network = TS.projects.foundations.globals.taskConstants.NETWORK_NODE
let processThisDependsOn = statusDependencyNode.referenceParent.parentNode
for (let i = 0; i < network.networkNodes.length; i++) {
let networkNode = network.networkNodes[i]
if (checkThisDataBranch(networkNode.dataTasks) === true) { return }
if (checkThisTradingBranch(networkNode.testingTradingTasks) === true) { return }
if (checkThisTradingBranch(networkNode.productionTradingTasks) === true) { return }
if (checkThisLearningBranch(networkNode.learningTasks) === true) { return }
function checkThisDataBranch(branch) {
if (branch === undefined) { return }
for (let z = 0; z < branch.projectDataTasks.length; z++) {
let projectDataTasks = branch.projectDataTasks[z]
if (projectDataTasks.exchangeDataTasks === undefined) { continue }
for (let j = 0; j < projectDataTasks.exchangeDataTasks.length; j++) {
let exchangeTasks = projectDataTasks.exchangeDataTasks[j]
for (let p = 0; p < exchangeTasks.marketDataTasks.length; p++) {
let marketTasks = exchangeTasks.marketDataTasks[p]
if (marketTasks.referenceParent === undefined) { continue }
if (marketTasks.referenceParent.id !== TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.id) { continue }
for (let q = 0; q < marketTasks.dataMineTasks.length; q++) {
let mineTasks = marketTasks.dataMineTasks[q]
for (let k = 0; k < mineTasks.taskManagers.length; k++) {
let taskManager = mineTasks.taskManagers[k]
for (let m = 0; m < taskManager.tasks.length; m++) {
let task = taskManager.tasks[m]
if (task.bot === undefined) { continue }
if (task.bot.processes === undefined) { continue }
for (let n = 0; n < task.bot.processes.length; n++) {
let process = task.bot.processes[n]
if (process.referenceParent === undefined) { continue }
let processDefinition = process.referenceParent
if (processThisDependsOn.id === processDefinition.id) {
if (process.type === 'Trading Process Instance') {
if (process.session !== undefined) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].session.id !== process.session.id) {
continue
}
} else {
continue
}
}
/*
We found where the task that runs the process definition this status report depends on
and where it is located on the network.
*/
thisObject.networkNode = networkNode
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] initialize -> Retrieving status report from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.')
fileStorage = TS.projects.foundations.taskModules.fileStorage.newFileStorage(processIndex, networkNode.config.host, networkNode.config.webPort)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
return true
}
}
}
}
}
}
}
}
}
function checkThisTradingBranch(branch) {
if (branch === undefined) { return }
for (let z = 0; z < branch.projectTradingTasks.length; z++) {
let projectTradingTasks = branch.projectTradingTasks[z]
for (let j = 0; j < projectTradingTasks.exchangeTradingTasks.length; j++) {
let exchangeTasks = projectTradingTasks.exchangeTradingTasks[j]
for (let p = 0; p < exchangeTasks.marketTradingTasks.length; p++) {
let marketTasks = exchangeTasks.marketTradingTasks[p]
if (marketTasks.referenceParent === undefined) { continue }
if (marketTasks.referenceParent.id !== TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.id) { continue }
for (let q = 0; q < marketTasks.tradingMineTasks.length; q++) {
let mineTasks = marketTasks.tradingMineTasks[q]
for (let k = 0; k < mineTasks.taskManagers.length; k++) {
let taskManager = mineTasks.taskManagers[k]
for (let m = 0; m < taskManager.tasks.length; m++) {
let task = taskManager.tasks[m]
if (task.bot === undefined) { continue }
if (task.bot.processes === undefined) { continue }
for (let n = 0; n < task.bot.processes.length; n++) {
let process = task.bot.processes[n]
if (process.referenceParent === undefined) { continue }
let processDefinition = process.referenceParent
if (processThisDependsOn.id === processDefinition.id) {
if (process.type === 'Trading Process Instance') {
if (process.session !== undefined) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].session.id !== process.session.id) {
continue
}
} else {
continue
}
}
/*
We found where the task that runs the process definition this status report depends on
and where it is located on the network.
*/
thisObject.networkNode = networkNode
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] initialize -> Retrieving status report from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.')
fileStorage = TS.projects.foundations.taskModules.fileStorage.newFileStorage(processIndex, networkNode.config.host, networkNode.config.webPort)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
return true
}
}
}
}
}
}
}
}
}
function checkThisLearningBranch(branch) {
if (branch === undefined) { return }
for (let z = 0; z < branch.projectLearningTasks.length; z++) {
let projectLearningTasks = branch.projectLearningTasks[z]
for (let j = 0; j < projectLearningTasks.exchangeLearningTasks.length; j++) {
let exchangeTasks = projectLearningTasks.exchangeLearningTasks[j]
for (let p = 0; p < exchangeTasks.marketLearningTasks.length; p++) {
let marketTasks = exchangeTasks.marketLearningTasks[p]
if (marketTasks.referenceParent === undefined) { continue }
if (marketTasks.referenceParent.id !== TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.id) { continue }
for (let q = 0; q < marketTasks.learningMineTasks.length; q++) {
let mineTasks = marketTasks.learningMineTasks[q]
for (let k = 0; k < mineTasks.taskManagers.length; k++) {
let taskManager = mineTasks.taskManagers[k]
for (let m = 0; m < taskManager.tasks.length; m++) {
let task = taskManager.tasks[m]
if (task.bot === undefined) { continue }
if (task.bot.processes === undefined) { continue }
for (let n = 0; n < task.bot.processes.length; n++) {
let process = task.bot.processes[n]
if (process.referenceParent === undefined) { continue }
let processDefinition = process.referenceParent
if (processThisDependsOn.id === processDefinition.id) {
if (process.type === 'Learning Process Instance') {
if (process.session !== undefined) {
if (TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].session.id !== process.session.id) {
continue
}
} else {
continue
}
}
/*
We found where the task that runs the process definition this status report depends on
and where it is located on the network.
*/
thisObject.networkNode = networkNode
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] initialize -> Retrieving status report from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.')
fileStorage = TS.projects.foundations.taskModules.fileStorage.newFileStorage(processIndex, networkNode.config.host, networkNode.config.webPort)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
return true
}
}
}
}
}
}
}
}
}
}
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] initialize -> Initialization Failed because we could not find where the data of this status report is located within the network. Check the logs for more info.")
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] initialize -> bot = " + statusDependencyNode.bot)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] initialize -> process = " + statusDependencyNode.process)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] initialize -> bottype = " + statusDependencyNode.bottype)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] initialize -> dataMine = " + statusDependencyNode.dataMine)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
}
}
function finalize() {
fileStorage = undefined
thisObject.networkNode = undefined
bot = undefined
thisObject = undefined
}
function load(callBackFunction) {
try {
let fileName = "Status.Report.json"
let filePath
let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.process
let botId =
TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.parentNode.config.codeName + "-" +
TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.config.codeName + "-" +
TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.config.codeName
if (ownerId !== botId) {
let filePathRoot =
'Project/' +
statusDependencyNode.project + "/" +
statusDependencyNode.mineType + "/" +
statusDependencyNode.dataMine + "/" +
statusDependencyNode.bot + '/' +
TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.config.codeName + "/" +
TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + "-" +
TS.projects.foundations.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName
filePath = filePathRoot + "/Reports/" + sessionPath + statusDependencyNode.process
} else {
filePath = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).FILE_PATH_ROOT + "/Reports/" + sessionPath + statusDependencyNode.process
}
filePath += '/' + fileName
let canUserPrevious
/*
If we are funning Trading Engines, we can not allow ourselves to use a Status Report that is not
the latest one, because it might contain transactioinal information related to the context of the
operations the trading engine is doing.
On the contraty, if we are running a Sensor bot, API Data Fetcher, or an Indicator we might, if necesary,
use a previous version of a Status Report since there will be no big impact, just some reprocessing.
*/
if (TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE !== undefined) {
canUserPrevious = false
} else {
canUserPrevious = true
}
fileStorage.getTextFile(filePath, onFileReceived, undefined, canUserPrevious)
function onFileReceived(err, text) {
if (err.result === TS.projects.foundations.globals.standardResponses.CUSTOM_FAIL_RESPONSE.result && (err.message === 'Folder does not exist.' || err.message === 'File does not exist.')
|| err.code === "The specified key does not exist.") {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[WARN] load -> onFileReceived -> err = " + err.message)
/* In this case we can assume that this is the first execution ever of this bot.*/
thisObject.file = JSON.parse('{}')
let customOK = {
result: TS.projects.foundations.globals.standardResponses.CUSTOM_OK_RESPONSE.result,
message: "Status Report was never created."
}
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[WARN] load -> onFileReceived -> customOK = " + customOK.message)
callBackFunction(customOK)
return
}
if (err.result !== TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE.result) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] load -> onFileReceived -> err = " + err.message)
callBackFunction(err)
return
}
try {
thisObject.file = JSON.parse(text)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE)
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
/*
It might happen that the file content is corrupt. We will consider this as a temporary situation,
since sometimes the file is being updated at the moment of the read. The bot can not run without a
valid Status Report but we can request the platform to retry later.
*/
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] load -> onFileReceived -> Error Parsing the Status report. -> Err = " + err.message)
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[WARN] load -> onFileReceived -> Error Parsing the Status report. -> text = " + text)
let customFail = {
result: TS.projects.foundations.globals.standardResponses.CUSTOM_FAIL_RESPONSE.result,
message: "Status Report is corrupt."
}
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[WARN] load -> onFileReceived -> customFail = " + customFail.message)
callBackFunction(customFail)
return
}
}
} catch (err) {
TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] load -> err = " + err.stack)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
}
}
function save(callBackFunction) {
try {
let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.process
let botId =
TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.parentNode.config.codeName + "-" +
TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.config.codeName + "-" +
TS.projects.foundations.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.config.codeName
/*
Context is a special case where the report is created by the Context.js module itself.
*/
if (ownerId !== botId && statusDependencyNode.process !== "Context") {
let customErr = {
result: TS.projects.foundations.globals.standardResponses.CUSTOM_FAIL_RESPONSE.result,
message: "Only bots owners of a Status Report can save them."
}
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] save -> customErr = " + customErr.message)
callBackFunction(customErr)
return
}
let fileName = "Status.Report.json"
let filePath = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).FILE_PATH_ROOT + "/Reports/" + sessionPath + statusDependencyNode.process
filePath += '/' + fileName
let fileContent = JSON.stringify(thisObject.file)
fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated, true)
function onFileCreated(err) {
if (err.result !== TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE.result) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] save -> onFileCreated -> err = " + err.stack)
callBackFunction(err)
return
}
/* All good, lets emit the event that means data has been updated. */
let processOutput = TS.projects.foundations.processModules.processOutput.newFoundationsProcessModulesProcessOutput(processIndex)
processOutput.raiseEvents(thisObject.file.lastFile, thisObject.file.timeFrames, callBackFunction)
return
}
}
catch (err) {
TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(
MODULE_NAME, "[ERROR] save -> err = " + err.stack)
callBackFunction(TS.projects.foundations.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
}
}
async function asyncSave() {
let fileName = "Status.Report.json"
let filePath = TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).FILE_PATH_ROOT + "/Reports/" + sessionPath + statusDependencyNode.process
filePath += '/' + fileName
let fileContent = JSON.stringify(thisObject.file)
let response = await fileStorage.asyncCreateTextFile(filePath, fileContent + '\n', true)
if (response.err.result !== TS.projects.foundations.globals.standardResponses.DEFAULT_OK_RESPONSE.result) {
throw (response.err)
}
/* All good, lets emit the event that means data has been updated. */
let processOutput = TS.projects.foundations.processModules.processOutput.newFoundationsProcessModulesProcessOutput(processIndex)
processOutput.asyncRaiseEvents(thisObject.file.lastFile, thisObject.file.timeFrames)
}
}