diff --git a/Files/FileCursor.js b/Files/FileCursor.js index 72d89ef8d..dde4e44f4 100644 --- a/Files/FileCursor.js +++ b/Files/FileCursor.js @@ -7,21 +7,18 @@ function newFileCursor() { const logger = newDebugLog(); logger.fileName = MODULE_NAME; - let files = new Map; let cursorDate; let thisObject = { reload: reload, setDatetime: setDatetime, setTimePeriod: setTimePeriod, - files: undefined, + files: new Map, getExpectedFiles: getExpectedFiles, initialize: initialize, finalize: finalize } - thisObject.files = files; - let minCursorSize = 10; let maxCursorSize = 30; @@ -48,18 +45,11 @@ function newFileCursor() { clearInterval(intervalHandle); - files = undefined; + thisObject.files = undefined; cursorDate = undefined; - market = undefined; - exchange = undefined; fileCloud = undefined; - devTeam = undefined; - bot = undefined; - thisSet = undefined; periodName = undefined; timePeriod = undefined; - beginDateRange = undefined; - endDateRange = undefined; } catch (err) { diff --git a/Files/FileSequence.js b/Files/FileSequence.js index eca8c348e..2aa3ca2c0 100644 --- a/Files/FileSequence.js +++ b/Files/FileSequence.js @@ -8,6 +8,7 @@ function newFileSequence() { logger.fileName = MODULE_NAME; let thisObject = { + eventHandler: undefined, getFile: getFile, getExpectedFiles: getExpectedFiles, getFilesLoaded: getFilesLoaded, @@ -19,6 +20,14 @@ function newFileSequence() { let fileCloud; let files = new Map; let maxSequence = -1; // This is replaced by the content of the sequence file, which contains an index that starts on zero. In the case that the sequence file is not found the default value is -1 sin when you add 1 it gives you the amount of files in the sequence, zero. + let market; + let devTeam; + let bot; + let thisSet; + let product; + + thisObject.eventHandler = newEventHandler(); + let intervalHandle; return thisObject; @@ -28,6 +37,10 @@ function newFileSequence() { try { if (INFO_LOG === true) { logger.write("[INFO] finalize -> Entering function."); } + if (INFO_LOG === true) { logger.write("[INFO] finalize -> devTeam = " + devTeam.codeName); } + if (INFO_LOG === true) { logger.write("[INFO] finalize -> bot = " + bot.codeName); } + if (INFO_LOG === true) { logger.write("[INFO] finalize -> thisSet = " + thisSet.codeName); } + if (INFO_LOG === true) { logger.write("[INFO] finalize -> intervalHandle = " + intervalHandle); } clearInterval(intervalHandle); @@ -35,12 +48,6 @@ function newFileSequence() { fileCloud = undefined; files = undefined; maxSequence = undefined; - devTeam = undefined; - bot = undefined; - product = undefined; - thisSet = undefined; - exchange = undefined; - market = undefined; } catch (err) { @@ -63,13 +70,15 @@ function newFileSequence() { } + intervalHandle = setInterval(updateFiles, _1_MINUTE_IN_MILISECONDS); + + if (INFO_LOG === true) { logger.write("[INFO] initialize -> intervalHandle = " + intervalHandle); } + market = pMarket; devTeam = pDevTeam; bot = pBot; - product = pProduct; thisSet = pSet; - - intervalHandle = setInterval(updateFiles, _1_MINUTE_IN_MILISECONDS); + product = pProduct; fileCloud = newFileCloud(); fileCloud.initialize(bot); @@ -127,6 +136,8 @@ function newFileSequence() { maxSequence = Number(file); + if (INFO_LOG === true) { logger.write("[INFO] initialize -> onSequenceFileReceived -> maxSequence = " + maxSequence); } + /* Now we will get the sequence of files */ for (let i = 0; i <= maxSequence; i++) { @@ -204,11 +215,144 @@ function newFileSequence() { if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> Entering function."); } + /* + To keep this data structure up-to-date we need to: + 1. Re-read the sequence file. + 2. Re-load the current last file. + 3. Re-load all new files after our last one. + + */ + + let currentMaxSequence = maxSequence; + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> currentMaxSequence = " + currentMaxSequence); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> intervalHandle = " + intervalHandle); } + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> devTeam = " + devTeam.codeName); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> bot = " + bot.codeName); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> thisSet = " + thisSet.codeName); } + + fileCloud.getFile(devTeam, bot, thisSet, exchange, market, undefined, undefined, "Sequence", undefined, onSequenceFileReceived); + + function onSequenceFileReceived(err, file) { + + try { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> Entering function."); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> key = " + devTeam.codeName + "-" + bot.codeName + "-" + product.codeName); } + + switch (err.result) { + case GLOBAL.DEFAULT_OK_RESPONSE.result: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> Received OK Response."); } + break; + } + + case GLOBAL.DEFAULT_FAIL_RESPONSE.result: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> Received FAIL Response."); } + return; + } + + case GLOBAL.CUSTOM_OK_RESPONSE.result: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> Received CUSTOM OK Response."); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> err.message = " + err.message); } + return; + } + + case GLOBAL.CUSTOM_FAIL_RESPONSE.result: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> Received CUSTOM FAIL Response."); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> err.message = " + err.message); } + return; + } + + default: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> Received Unexpected Response."); } + return; + } + } + + maxSequence = Number(file); + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> maxSequence = " + maxSequence); } + + /* Now we will get the sequence of files, but in this case only from the currentMaxSequence and above. */ + + for (let i = currentMaxSequence; i <= maxSequence; i++) { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> i = " + i); } + + fileCloud.getFile(devTeam, bot, thisSet, exchange, market, undefined, undefined, i, undefined, onFileReceived); + + function onFileReceived(err, file) { + + try { + + switch (err.result) { + case GLOBAL.DEFAULT_OK_RESPONSE.result: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> Received OK Response."); } + break; + } + + case GLOBAL.DEFAULT_FAIL_RESPONSE.result: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> Received FAIL Response."); } + return; + } + + case GLOBAL.CUSTOM_FAIL_RESPONSE.result: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> Received CUSTOM FAIL Response."); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> err.message = " + err.message); } + return; + } + + default: { + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> Received Unexpected Response."); } + return; + } + } + + files.set(i, file); + + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> File Updated."); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> i = " + i); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> file.length = " + file.length); } + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> filesLoaded = " + filesLoaded); } + + if (i !== currentMaxSequence) { + + filesLoaded++; + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> filesLoaded = " + filesLoaded); } + + } + + if (filesLoaded === maxSequence + 1) { + + thisObject.eventHandler.raiseEvent("Files Updated", undefined); + if (INFO_LOG === true) { logger.write("[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> Files Updated event Raised."); } + + } + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] updateFiles -> onSequenceFileReceived -> onFileReceived -> err = " + err); } + } + } + } + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] updateFiles -> onSequenceFileReceived -> err = " + err); } + } + } } catch (err) { - if (ERROR_LOG === true) { logger.write("[ERROR] updateFiles -> err = " + err); } } } diff --git a/Files/MarketFiles.js b/Files/MarketFiles.js index d3657dfbf..0caaeb34c 100644 --- a/Files/MarketFiles.js +++ b/Files/MarketFiles.js @@ -44,12 +44,7 @@ function newMarketFiles() { clearInterval(intervalHandle); filesLoaded = undefined; - market = undefined; - exchange = undefined; - devTeam = undefined; - bot = undefined; - thisSet = undefined; - periodName = undefined; + files = undefined; } catch (err) { diff --git a/ProductStorage.js b/ProductStorage.js index 6db6296be..4deaf42d6 100644 --- a/ProductStorage.js +++ b/ProductStorage.js @@ -31,7 +31,7 @@ function newProductStorage(pName) { marketFiles: [], dailyFiles: [], singleFile: [], - fileSequence: [], + fileSequences: [], setDatetime: setDatetime, setTimePeriod: setTimePeriod, @@ -75,6 +75,14 @@ function newProductStorage(pName) { thisObject.dailyFile = undefined; + for (let i = 0; i < thisObject.fileSequences.length; i++) { + + let fileSequence = thisObject.fileSequences[i]; + fileSequence.finalize(); + } + + thisObject.dailyFile = undefined; + } catch (err) { if (ERROR_LOG === true) { logger.write("[ERROR] finalize -> err = " + err); } @@ -153,9 +161,9 @@ function newProductStorage(pName) { if (INFO_LOG === true) { logger.write("[INFO] initialize -> dataSetsToLoad = " + dataSetsToLoad); } - let fileSequence = newFileSequence(); - fileSequence.initialize(pDevTeam, pBot, pProduct, thisSet, pExchange, pMarket, onFileSequenceReady); - thisObject.fileSequence.push(fileSequence); + let fileSequences = newFileSequence(); + fileSequences.initialize(pDevTeam, pBot, pProduct, thisSet, pExchange, pMarket, onFileSequenceReady); + thisObject.fileSequences.push(fileSequences); } break; }