diff --git a/DataSet.js b/DataSet.js index fe188ba16..7dce95fe8 100644 --- a/DataSet.js +++ b/DataSet.js @@ -79,77 +79,22 @@ exports.newDataSet = function newDataSet(BOT, logger) { let network = global.TASK_NETWORK let datasetProductDefinition = thisObject.node.parentNode - for (let i = 0; i < network.networkNodes.length; i++) { - let networkNode = network.networkNodes[i] - if (networkNode.dataStorage !== undefined) { - if (networkNode.dataStorage.sessionIndependentData !== undefined) { - for (let j = 0; j < networkNode.dataStorage.sessionIndependentData.exchangeDataProducts.length; j++) { - let exchangeDataProduct = networkNode.dataStorage.sessionIndependentData.exchangeDataProducts[j] - for (let k = 0; k < exchangeDataProduct.singleMarketData.length; k++) { - let singleMarketData = exchangeDataProduct.singleMarketData[k] - if (singleMarketData.referenceParent !== undefined) { - let market = singleMarketData.referenceParent - let currentProcessMarket = bot.processNode.marketReference.referenceParent + let nodeArray = global.NODE_MESH_TO_PATH_ARRAY(network, datasetProductDefinition.id) + let networkNode = global.FIND_NODE_IN_NODE_ARRAY(nodeArray, 'Network Node') - if (currentProcessMarket.id === market.id) { - for (let m = 0; m < singleMarketData.dataProducts.length; m++) { - let dataProduct = singleMarketData.dataProducts[m] - if (dataProduct.referenceParent !== undefined) { - let productDefinition = dataProduct.referenceParent - if (datasetProductDefinition.id === productDefinition.id) { - - /* We found where the data is located on the network. */ - if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Retrieving data from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.'); } - - fileStorage = FILE_STORAGE.newFileStorage(logger, networkNode.config.host, networkNode.config.webPort); - callBackFunction(global.DEFAULT_OK_RESPONSE); - return - } - } - } - } - } - } - } - } - if (networkNode.dataStorage.sessionBasedData !== undefined) { - for (let j = 0; j < networkNode.dataStorage.sessionBasedData.exchangeDataProducts.length; j++) { - let exchangeDataProduct = networkNode.dataStorage.sessionBasedData.exchangeDataProducts[j] - for (let s = 0; s < exchangeDataProduct.sessionReferences.length; s++) { - let sessionReference = exchangeDataProduct.sessionReferences[s] - let singleMarketData = sessionReference.singleMarketData - if (singleMarketData.referenceParent !== undefined) { - let market = singleMarketData.referenceParent - let currentProcessMarket = bot.processNode.marketReference.referenceParent - - if (currentProcessMarket.id === market.id) { - for (let m = 0; m < singleMarketData.dataProducts.length; m++) { - let dataProduct = singleMarketData.dataProducts[m] - if (dataProduct.referenceParent !== undefined) { - let productDefinition = dataProduct.referenceParent - if (datasetProductDefinition.id === productDefinition.id) { - - /* We found where the data is located on the network. */ - if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Retrieving data from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.'); } - - fileStorage = FILE_STORAGE.newFileStorage(logger, networkNode.config.host, networkNode.config.webPort); - callBackFunction(global.DEFAULT_OK_RESPONSE); - return - } - } - } - } - } - } - } - } - } + if (networkNode === undefined) { + logger.write(MODULE_NAME, "[ERROR] initialize -> Network Node not found.") + logger.write(MODULE_NAME, "[ERROR] 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."); + logger.write(MODULE_NAME, "[ERROR] initialize -> Could not find where " + datasetProductDefinition.name + " for " + bot.exchange + " " + bot.market.baseAsset + "/" + bot.market.quotedAsset + " is stored within the network."); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return } - logger.write(MODULE_NAME, "[ERROR] 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."); - logger.write(MODULE_NAME, "[ERROR] initialize -> Could not find where " + datasetProductDefinition.name + " for " + bot.exchange + " " + bot.market.baseAsset + "/" + bot.market.quotedAsset + " is stored within the network."); + /* We found where the data is located on the network. */ + if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Retrieving data from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.'); } - callBackFunction(global.DEFAULT_FAIL_RESPONSE); + fileStorage = FILE_STORAGE.newFileStorage(logger, networkNode.config.host, networkNode.config.webPort); + callBackFunction(global.DEFAULT_OK_RESPONSE); } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); diff --git a/server.js b/server.js index 3e6bb2824..0b014d460 100644 --- a/server.js +++ b/server.js @@ -137,6 +137,140 @@ global.NODE_BRANCH_TO_ARRAY = function (node, nodeType) { } } +global.FIND_NODE_IN_NODE_MESH = function (node, nodeType) { + /* + This function scans a node mesh for a certain node type and + returns the first instance found. + */ + let nodeFound + scanNodeMesh(node, nodeType) + return nodeFound + + function scanNodeMesh(startingNode) { + if (startingNode === undefined) { return } + if (nodeFound !== undefined) { return } + + let nodeDefinition = APP_SCHEMA_MAP.get(startingNode.type) + if (nodeDefinition === undefined) { return } + + if (startingNode.type === nodeType) { + nodeFound = startingNode + return + } + + /* We scan through this node children */ + if (nodeDefinition.properties !== undefined) { + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + scanNodeMesh(startingNode[property.name]) + } + break + case 'array': { + let startingNodePropertyArray = startingNode[property.name] + if (startingNodePropertyArray !== undefined) { + for (let m = 0; m < startingNodePropertyArray.length; m++) { + scanNodeMesh(startingNodePropertyArray[m]) + } + } + break + } + } + } + } + /* We scan parents nodes. */ + if (startingNode.parentNode !== undefined) { + scanNodeMesh(startingNode.parentNode) + } + /* We scan reference parents too. */ + if (startingNode.referenceParent !== undefined) { + scanNodeMesh(startingNode.referenceParent) + } + } +} + +global.NODE_MESH_TO_PATH_ARRAY = function (node, nodeId) { + /* + This function scans a node mesh for a certain node if and + returns an array with the path within that mesh to the + requested node. + */ + let nodeArray = [] + scanNodeMesh(node) + return nodeArray + + function scanNodeMesh(startingNode) { + if (startingNode === undefined) { return } + + let nodeDefinition = APP_SCHEMA_MAP.get(startingNode.type) + if (nodeDefinition === undefined) { return } + + if (startingNode.id === nodeId) { + nodeArray.push(startingNode) + return + } + + /* We scan through this node children */ + if (nodeDefinition.properties !== undefined) { + for (let i = 0; i < nodeDefinition.properties.length; i++) { + let property = nodeDefinition.properties[i] + + switch (property.type) { + case 'node': { + scanNodeMesh(startingNode[property.name]) + if (nodeArray.length > 0) { + nodeArray.unshift(startingNode) + return + } + } + break + case 'array': { + let startingNodePropertyArray = startingNode[property.name] + if (startingNodePropertyArray !== undefined) { + for (let m = 0; m < startingNodePropertyArray.length; m++) { + scanNodeMesh(startingNodePropertyArray[m]) + if (nodeArray.length > 0) { + nodeArray.unshift(startingNode) + return + } + } + } + break + } + } + } + } + /* We scan parents nodes. */ + if (startingNode.parentNode !== undefined) { + scanNodeMesh(startingNode.parentNode) + if (nodeArray.length > 0) { + nodeArray.unshift(startingNode) + return + } + } + /* We scan reference parents too. */ + if (startingNode.referenceParent !== undefined) { + scanNodeMesh(startingNode.referenceParent) + if (nodeArray.length > 0) { + nodeArray.unshift(startingNode) + return + } + } + } +} + +global.FIND_NODE_IN_NODE_ARRAY = function (nodeArray, nodeType) { + for (let i=0; i < nodeArray.length; i++) { + let node = nodeArray[i] + if (node.type === nodeType) { + return node + } + } +} + + process.on('uncaughtException', function (err) { console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message) console.log('[ERROR] Task Server -> server -> uncaughtException -> err.stack = ' + err.stack)