From d2f815d256018f17f262f7d6518d10e44a511d38 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 17 Sep 2021 11:32:11 +0200 Subject: [PATCH 1/6] Network Node saving events and pushing them to storage --- .gitignore | 1 + Desktop/UI/WebApp.js | 4 +- Network/NetwokNode.js | 94 +++++++++++++++++++ NetworkRoot.js | 4 +- .../{TS => SA}/Globals/TimeConstants.js | 0 .../SA/Utilities/FilesAndDirectories.js | 21 +++++ .../replaceby-config-property.json | 4 +- .../API-Data-Fetcher-Bot/FetchingProcess.js | 4 +- .../BollingerBandsMultiTimeFrameDaily.js | 22 ++--- .../CandlesVolumesMultiTimeFrameDaily.js | 22 ++--- .../CandlesVolumesMultiTimeFrameMarket.js | 20 ++-- .../Indicator-Bot/MultiTimeFrameDaily.js | 12 +-- .../Learning-Bot/LearningSession.js | 6 +- .../Low-Frequency-Learning/LearningEpisode.js | 2 +- .../LearningSimulation.js | 6 +- .../Exchange-Raw-Data/HistoricOHLCVs.js | 48 +++++----- .../Low-Frequency-Trading/TradingEpisode.js | 2 +- .../Low-Frequency-Trading/TradingOrders.js | 2 +- .../Low-Frequency-Trading/TradingPosition.js | 2 +- .../TradingSimulation.js | 6 +- .../Bot-Modules/Trading-Bot/TradingSession.js | 6 +- .../DataDependenciesFunctions.js | 2 +- .../FromOneMinToMultiTimeFrameFunctions.js | 16 ++-- .../ProcessFilesFunctions.js | 4 +- .../SingleMarketFunctions.js | 10 +- .../TS/Utilities/DateTimeFunctions.js | 6 +- Projects/ProjectsSchema.json | 14 +-- .../DK/modules/WebAppInterface.js | 4 +- .../NT/Modules/ClientInterface.js | 5 +- 29 files changed, 235 insertions(+), 114 deletions(-) rename Projects/Foundations/{TS => SA}/Globals/TimeConstants.js (100%) diff --git a/.gitignore b/.gitignore index c7aaa09d1..07cadad8b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ Data-Storage Log-Files My-Workspaces My-Social-Trading-Data +My-Network-Nodes-Data debug.log .DS_Store *.db diff --git a/Desktop/UI/WebApp.js b/Desktop/UI/WebApp.js index bce4eb31d..4a3b9a63b 100644 --- a/Desktop/UI/WebApp.js +++ b/Desktop/UI/WebApp.js @@ -319,7 +319,7 @@ function newWebApp() { eventType: eventType, eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(), targetUserProfileId: userProfileId, - timestamp: new Date() + timestamp: (new Date()).valueOf() } event = { @@ -355,7 +355,7 @@ function newWebApp() { eventType: SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST, eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(), text: postText, - timestamp: new Date() + timestamp: (new Date()).valueOf() } event = { diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index af1913a24..54f053306 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -4,6 +4,8 @@ exports.newNetworkNode = function newNetworkNode() { run: run } + let indexLastSavedEvent = -1 + return thisObject function run() { @@ -18,7 +20,99 @@ exports.newNetworkNode = function newNetworkNode() { async function start() { let socialGraphService = NT.projects.socialTrading.modules.socialGraph.newSocialGraph() await socialGraphService.initialize() + + setInterval(saveEventsAtStorage, 60000) + } + } + + async function saveEventsAtStorage() { + + let oneMore = true + let needToDoGit = false + + while (oneMore === true) { + oneMore = saveOneMinuteOfEvents() + if (oneMore === true) { + needToDoGit = true + } } + if (needToDoGit === true) { + doGit() + } + + function saveOneMinuteOfEvents() { + /* + Here we will save all the events that were not saved before, + in one minute chunchs files. + */ + let eventsToSave = [] + let minuteToSave + + for (let i = indexLastSavedEvent + 1; i < NT.projects.socialTrading.globals.memory.arrays.EVENTS.length; i++) { + let event = NT.projects.socialTrading.globals.memory.arrays.EVENTS[i] + + let currentMinute = Math.trunc((new Date()).valueOf() / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) + let eventMinute = Math.trunc(event.timestamp / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) + + if (minuteToSave === undefined) { + minuteToSave = eventMinute + } + /* + We will save events only of the last closed minute. + We will also pack events into one minute chunks only. + */ + if ( + eventMinute < currentMinute && + eventMinute === minuteToSave + ) { + let eventToSave = { + eventId: event.eventId, + eventType: event.eventType, + emitterUserProfileId: event.emitterUserProfileId, + targetUserProfileId: event.targetUserProfileId, + emitterBotProfileId: event.emitterBotProfileId, + targetBotProfileId: event.targetBotProfileId, + emitterPostHash: event.emitterPostHash, + targetPostHash: event.targetPostHash, + timestamp: event.timestamp, + botAsset: event.botAsset, + botExchange: event.botExchange, + botEnabled: event.botEnabled + } + + eventsToSave.push(eventToSave) + indexLastSavedEvent = i + } + } + + if (eventsToSave.length === 0) { return false } + + let timestamp = minuteToSave * SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS + + const fileContent = JSON.stringify(eventsToSave, undefined, 4) + const fileName = "Events" + ".json" + + let filePath = './My-Network-Nodes-Data/Nodes/Node-1/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDatetime(timestamp) + + SA.projects.foundations.utilities.filesAndDirectories.mkDirByPathSync(filePath + '/') + SA.nodeModules.fs.writeFileSync(filePath + '/' + fileName, fileContent) + + return true + } + + async function doGit() { + const options = { + baseDir: process.cwd() + '/My-Network-Nodes-Data', + binary: 'git', + maxConcurrentProcesses: 6, + } + const commitMessage = 'New Events' + const git = SA.nodeModules.simpleGit(options) + + await git.add('./*') + await git.commit(commitMessage) + await git.push('origin') + } } } diff --git a/NetworkRoot.js b/NetworkRoot.js index 075a6f6a7..9fbe271d2 100644 --- a/NetworkRoot.js +++ b/NetworkRoot.js @@ -41,9 +41,11 @@ exports.newNetworkRoot = function newNetworkRoot() { */ SA.nodeModules = { fs: require('fs'), + path: require('path'), nodeFetch: require('node-fetch'), web3: require('web3'), - ws: require('ws') + ws: require('ws'), + simpleGit: require('simple-git') } /* diff --git a/Projects/Foundations/TS/Globals/TimeConstants.js b/Projects/Foundations/SA/Globals/TimeConstants.js similarity index 100% rename from Projects/Foundations/TS/Globals/TimeConstants.js rename to Projects/Foundations/SA/Globals/TimeConstants.js diff --git a/Projects/Foundations/SA/Utilities/FilesAndDirectories.js b/Projects/Foundations/SA/Utilities/FilesAndDirectories.js index f635977ab..9db22af7e 100644 --- a/Projects/Foundations/SA/Utilities/FilesAndDirectories.js +++ b/Projects/Foundations/SA/Utilities/FilesAndDirectories.js @@ -4,6 +4,7 @@ exports.newFoundationsUtilitiesFilesAndDirectories = function () { getDirectories: getDirectories, getAllFilesInDirectoryAndSubdirectories: getAllFilesInDirectoryAndSubdirectories, pathFromDate: pathFromDate, + pathFromDatetime: pathFromDatetime, mkDirByPathSync: mkDirByPathSync } @@ -55,15 +56,35 @@ exports.newFoundationsUtilitiesFilesAndDirectories = function () { } function pathFromDate(timestamp) { + let file = { date: new Date(timestamp) } + file.year = file.date.getUTCFullYear() file.month = file.date.getUTCMonth() + 1 file.day = file.date.getUTCDate() + return file.year + '/' + SA.projects.foundations.utilities.miscellaneousFunctions.pad(file.month, 2) + '/' + SA.projects.foundations.utilities.miscellaneousFunctions.pad(file.day, 2) } + function pathFromDatetime(timestamp) { + + let file = { date: new Date(timestamp) } + + file.year = file.date.getUTCFullYear() + file.month = file.date.getUTCMonth() + 1 + file.day = file.date.getUTCDate() + file.hour = file.date.getUTCHours() + file.minute = file.date.getMinutes() + + return file.year + '/' + + SA.projects.foundations.utilities.miscellaneousFunctions.pad(file.month, 2) + '/' + + SA.projects.foundations.utilities.miscellaneousFunctions.pad(file.day, 2) + '/' + + SA.projects.foundations.utilities.miscellaneousFunctions.pad(file.hour, 2) + '/' + + SA.projects.foundations.utilities.miscellaneousFunctions.pad(file.minute, 2) + } + /* Function to create folders of missing folders at any path. */ function mkDirByPathSync(targetDir, { isRelativeToScript = false } = {}) { const path = SA.nodeModules.path diff --git a/Projects/Foundations/Schemas/Docs-Concepts/R/replaceBy/replaceBy-Config-Property/replaceby-config-property.json b/Projects/Foundations/Schemas/Docs-Concepts/R/replaceBy/replaceBy-Config-Property/replaceby-config-property.json index cd560e721..4d53d5ba4 100644 --- a/Projects/Foundations/Schemas/Docs-Concepts/R/replaceBy/replaceBy-Config-Property/replaceby-config-property.json +++ b/Projects/Foundations/Schemas/Docs-Concepts/R/replaceBy/replaceBy-Config-Property/replaceby-config-property.json @@ -141,7 +141,7 @@ }, { "style": "Javascript", - "text": "parameterValue = \nMath.trunc((new Date()).valueOf() / \nTS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * \nTS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS", + "text": "parameterValue = \nMath.trunc((new Date()).valueOf() / \nSA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * \nSA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS", "updated": 1622477009814 }, { @@ -151,7 +151,7 @@ }, { "style": "Javascript", - "text": "parameterValue = Math.trunc((new Date()).valueOf() / \nTS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * \nTS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS + \nTS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1", + "text": "parameterValue = Math.trunc((new Date()).valueOf() / \nSA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * \nSA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS + \nSA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1", "updated": 1622477088684 }, { diff --git a/Projects/Foundations/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js b/Projects/Foundations/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js index d6f697034..1c3a6ef54 100644 --- a/Projects/Foundations/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js +++ b/Projects/Foundations/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js @@ -462,11 +462,11 @@ exports.newFoundationsBotModulesFetchingProcess = function (processIndex) { break } case '@BeginCurrentMinute': { - parameterValue = Math.trunc((new Date()).valueOf() / TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS + parameterValue = Math.trunc((new Date()).valueOf() / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS break } case '@EndCurrentMinute': { - parameterValue = Math.trunc((new Date()).valueOf() / TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS + TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1 + parameterValue = Math.trunc((new Date()).valueOf() / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS + SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1 break } } diff --git a/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Bollinger-Bands/BollingerBandsMultiTimeFrameDaily.js b/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Bollinger-Bands/BollingerBandsMultiTimeFrameDaily.js index 7fdba9a41..e7a62f89e 100644 --- a/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Bollinger-Bands/BollingerBandsMultiTimeFrameDaily.js +++ b/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Bollinger-Bands/BollingerBandsMultiTimeFrameDaily.js @@ -94,7 +94,7 @@ thisReport.beginingOfMarket.days + " " + thisReport.beginingOfMarket.hours + ":" + thisReport.beginingOfMarket.minutes + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); /* Second, we get the report from Exchange Raw Data, to know when the marted ends. */ statusReport = statusDependenciesModule.reportsByMainUtility.get('Market Ending Point') @@ -158,15 +158,15 @@ contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); contextVariables.lastBandFile = new Date( contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); contextVariables.lastBandFile = new Date( contextVariables.lastBandFile.valueOf() + - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); buildBands(); return; @@ -199,16 +199,16 @@ contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); contextVariables.lastBandFile = new Date( contextVariables.lastBandFile.valueOf() + - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); beginingOfMarket = new Date( contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> getContextVariables -> thisReport.lastFile === undefined") @@ -237,7 +237,7 @@ try { let n; - processDate = new Date(contextVariables.lastBandFile.valueOf() - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. + processDate = new Date(contextVariables.lastBandFile.valueOf() - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); // Go back one day to start well when we advance time at the begining of the loop. let fromDate = new Date(processDate.valueOf()) let lastDate = TS.projects.foundations.utilities.dateTimeFunctions.removeTime(new Date()) @@ -249,8 +249,8 @@ function advanceTime() { try { - processDate = new Date(processDate.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); - previousDay = new Date(processDate.valueOf() - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); + processDate = new Date(processDate.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); + previousDay = new Date(processDate.valueOf() - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> buildBands -> advanceTime -> processDate = " + processDate) @@ -784,7 +784,7 @@ try { let dataRange = { begin: pBegin.valueOf(), - end: pEnd.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + end: pEnd.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS } let fileContent = JSON.stringify(dataRange); diff --git a/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Candles-Volumes/CandlesVolumesMultiTimeFrameDaily.js b/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Candles-Volumes/CandlesVolumesMultiTimeFrameDaily.js index 83cf08131..56962ad7f 100644 --- a/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Candles-Volumes/CandlesVolumesMultiTimeFrameDaily.js +++ b/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Candles-Volumes/CandlesVolumesMultiTimeFrameDaily.js @@ -89,7 +89,7 @@ thisReport.beginingOfMarket.days + " " + thisReport.beginingOfMarket.hours + ":" + thisReport.beginingOfMarket.minutes + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) /* Second, we get the report from Exchange Raw Data, to know when the marted ends. */ statusReport = statusDependenciesModule.reportsByMainUtility.get('Market Ending Point') @@ -125,7 +125,7 @@ thisReport.lastFile.year + "-" + thisReport.lastFile.month + "-" + thisReport.lastFile.days + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) /* Finally we get our own Status Report. */ statusReport = statusDependenciesModule.reportsByMainUtility.get('Self Reference') @@ -150,15 +150,15 @@ beginingOfMarket = new Date(thisReport.beginingOfMarket) if (beginingOfMarket.valueOf() !== contextVariables.datetimeBeginingOfMarketFile.valueOf()) { // Reset Mechanism for Begining of the Market - beginingOfMarket = new Date(contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + beginingOfMarket = new Date(contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeLastProducedFile.valueOf() - - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // Go back one day to start well. + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // Go back one day to start well. buildCandles() return @@ -171,7 +171,7 @@ For that reason we go back one day, the partial information is discarded and added again with whatever new info is available. */ - contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) buildCandles() return @@ -180,13 +180,13 @@ contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) - contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); // Go back one day to start well. + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) + contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); // Go back one day to start well. buildCandles() return @@ -216,7 +216,7 @@ advanceTime() function advanceTime() { - contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); + contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> buildCandles -> advanceTime -> New processing time @ " + contextVariables.datetimeLastProducedFile.getUTCFullYear() + "/" + (contextVariables.datetimeLastProducedFile.getUTCMonth() + 1) + "/" + contextVariables.datetimeLastProducedFile.getUTCDate() + ".") @@ -665,7 +665,7 @@ try { let dataRange = { begin: pBegin.valueOf(), - end: pEnd.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + end: pEnd.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS } let fileContent = JSON.stringify(dataRange); diff --git a/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Candles-Volumes/CandlesVolumesMultiTimeFrameMarket.js b/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Candles-Volumes/CandlesVolumesMultiTimeFrameMarket.js index 0ca8fa025..ea86ca2f6 100644 --- a/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Candles-Volumes/CandlesVolumesMultiTimeFrameMarket.js +++ b/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/Candles-Volumes/CandlesVolumesMultiTimeFrameMarket.js @@ -107,7 +107,7 @@ thisReport.beginingOfMarket.days + " " + thisReport.beginingOfMarket.hours + ":" + thisReport.beginingOfMarket.minutes + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); /* Second, we get the report from Exchange Raw Data, to know when the marted ends. */ statusReport = statusDependenciesModule.reportsByMainUtility.get('Market Ending Point') @@ -143,7 +143,7 @@ thisReport.lastFile.year + "-" + thisReport.lastFile.month + "-" + thisReport.lastFile.days + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); /* Finally we get our own Status Report. */ statusReport = statusDependenciesModule.reportsByMainUtility.get('Self Reference') @@ -174,15 +174,15 @@ contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeLastProducedFile.valueOf() - - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); // Go back one day to start well. + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); // Go back one day to start well. buildCandles() return @@ -196,7 +196,7 @@ For that reason we go back one day, the partial information is discarded and added again with whatever new info is available. */ - contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); + contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); findPreviousContent() return @@ -205,15 +205,15 @@ contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeLastProducedFile.valueOf() - - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); // Go back one day to start well. + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); // Go back one day to start well. buildCandles() return @@ -394,7 +394,7 @@ date and then adding again all the elements found right now at that date and then from there into the future. */ - contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); + contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS); TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> buildCandles -> advanceTime -> New processing time @ " + contextVariables.datetimeLastProducedFile.getUTCFullYear() + "/" + (contextVariables.datetimeLastProducedFile.getUTCMonth() + 1) + "/" + contextVariables.datetimeLastProducedFile.getUTCDate() + ".") diff --git a/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/MultiTimeFrameDaily.js b/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/MultiTimeFrameDaily.js index 681896424..d4815fe6b 100644 --- a/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/MultiTimeFrameDaily.js +++ b/Projects/Foundations/TS/Bot-Modules/Indicator-Bot/MultiTimeFrameDaily.js @@ -104,7 +104,7 @@ thisReport.beginingOfMarket.days + " " + thisReport.beginingOfMarket.hours + ":" + thisReport.beginingOfMarket.minutes + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS + SA.projects.foundations.globals.timeConstants.GMT_SECONDS ) } else { // This is the standard way. contextVariables.dateBeginOfMarket = new Date(thisReport.beginingOfMarket) @@ -198,7 +198,7 @@ contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS + SA.projects.foundations.globals.timeConstants.GMT_SECONDS ) TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, @@ -242,7 +242,7 @@ let botNeverRan = true; TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).DAILY_FILES_PROCESS_DATETIME = - new Date(contextVariables.lastFile.valueOf() - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // Go back one day to start well when we advance time at the begining of the loop. + new Date(contextVariables.lastFile.valueOf() - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // Go back one day to start well when we advance time at the begining of the loop. let fromDate = new Date(TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).DAILY_FILES_PROCESS_DATETIME.valueOf()) let lastDate = TS.projects.foundations.utilities.dateTimeFunctions.removeTime(new Date()) @@ -251,9 +251,9 @@ function advanceTime() { TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).DAILY_FILES_PROCESS_DATETIME = new Date(TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).DAILY_FILES_PROCESS_DATETIME.valueOf() + - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) previousDay = new Date(TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).DAILY_FILES_PROCESS_DATETIME.valueOf() - - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> processTimeFrames -> advanceTime -> TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).DAILY_FILES_PROCESS_DATETIME = " + @@ -541,7 +541,7 @@ function writeDataRange(pBegin, pEnd, productCodeName, callBack) { let dataRange = { begin: pBegin.valueOf(), - end: pEnd.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + end: pEnd.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS }; let fileContent = JSON.stringify(dataRange) let fileName = '/Data.Range.json'; diff --git a/Projects/Foundations/TS/Bot-Modules/Learning-Bot/LearningSession.js b/Projects/Foundations/TS/Bot-Modules/Learning-Bot/LearningSession.js index 4112e69bf..ed0f52dcb 100644 --- a/Projects/Foundations/TS/Bot-Modules/Learning-Bot/LearningSession.js +++ b/Projects/Foundations/TS/Bot-Modules/Learning-Bot/LearningSession.js @@ -241,7 +241,7 @@ type: 'Time Range', config: { initialDatetime: (new Date()).valueOf(), - finalDatetime: (new Date()).valueOf() + TS.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS + finalDatetime: (new Date()).valueOf() + SA.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS } } } else { @@ -292,8 +292,8 @@ /* Session Type Forced Values */ let today = (new Date()).valueOf() - let aYearAgo = today - TS.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS - let aYearFromNow = today + TS.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS + let aYearAgo = today - SA.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS + let aYearFromNow = today + SA.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS switch (TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.type) { case 'Back Learning Session': { useDefaultDatetimes(aYearAgo, today) diff --git a/Projects/Foundations/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningEpisode.js b/Projects/Foundations/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningEpisode.js index 276a85fa2..6eba82d6e 100644 --- a/Projects/Foundations/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningEpisode.js +++ b/Projects/Foundations/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningEpisode.js @@ -101,7 +101,7 @@ exports.newFoundationsBotModulesLearningEpisode = function (processIndex) { learningEngine.learningCurrent.learningEpisode.learningEpisodeStatistics.days.value = learningEngine.learningCurrent.learningEpisode.learningEpisodeCounters.periods.value * sessionParameters.timeFrame.config.value / - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS } function cycleBasedStatistics() { diff --git a/Projects/Foundations/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningSimulation.js b/Projects/Foundations/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningSimulation.js index 7e2548f3f..53ee68f92 100644 --- a/Projects/Foundations/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningSimulation.js +++ b/Projects/Foundations/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningSimulation.js @@ -312,7 +312,7 @@ exports.newFoundationsBotModulesLearningSimulation = function (processIndex) { if (sessionParameters.heartbeats.config.date === true || sessionParameters.heartbeats.config.candleIndex === true) { /* We will produce a simulation level heartbeat in order to inform the user this is running. */ - heartBeatDate = new Date(Math.trunc(learningEngine.learningCurrent.learningEpisode.candle.begin.value / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + heartBeatDate = new Date(Math.trunc(learningEngine.learningCurrent.learningEpisode.candle.begin.value / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime) let lastDate = new Date(sessionParameters.timeRange.config.finalDatetime) @@ -502,7 +502,7 @@ exports.newFoundationsBotModulesLearningSimulation = function (processIndex) { the day, we will advance current process day one day. By doing so, during the next execution, the simulation will receive the candles and indicators files of the next day. */ - let candlesPerDay = TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value + let candlesPerDay = SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value if ( TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_PROCESSING_DAILY_FILES && learningEngine.learningCurrent.learningEpisode.candle.index.value + 1 + 1 === candlesPerDay @@ -516,7 +516,7 @@ exports.newFoundationsBotModulesLearningSimulation = function (processIndex) { */ learningEngine.learningCurrent.learningEpisode.candle.index.value = 0 learningEngine.learningCurrent.learningEpisode.processDate.value = - learningEngine.learningCurrent.learningEpisode.processDate.value + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + learningEngine.learningCurrent.learningEpisode.processDate.value + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS return false } diff --git a/Projects/Foundations/TS/Bot-Modules/Sensor-Bot/Exchange-Raw-Data/HistoricOHLCVs.js b/Projects/Foundations/TS/Bot-Modules/Sensor-Bot/Exchange-Raw-Data/HistoricOHLCVs.js index 716d10eff..34cbb378e 100644 --- a/Projects/Foundations/TS/Bot-Modules/Sensor-Bot/Exchange-Raw-Data/HistoricOHLCVs.js +++ b/Projects/Foundations/TS/Bot-Modules/Sensor-Bot/Exchange-Raw-Data/HistoricOHLCVs.js @@ -217,8 +217,8 @@ thisReport = statusDependencies.statusReports.get(reportKey) if (thisReport.file.beginingOfMarket !== undefined) { // This means this is not the first time this process has run. - beginingOfMarket = new Date(thisReport.file.beginingOfMarket.year + "-" + thisReport.file.beginingOfMarket.month + "-" + thisReport.file.beginingOfMarket.days + " " + thisReport.file.beginingOfMarket.hours + ":" + thisReport.file.beginingOfMarket.minutes + TS.projects.foundations.globals.timeConstants.GMT_SECONDS); - lastFile = new Date(thisReport.file.lastFile.year + "-" + thisReport.file.lastFile.month + "-" + thisReport.file.lastFile.days + " " + thisReport.file.lastFile.hours + ":" + thisReport.file.lastFile.minutes + TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + beginingOfMarket = new Date(thisReport.file.beginingOfMarket.year + "-" + thisReport.file.beginingOfMarket.month + "-" + thisReport.file.beginingOfMarket.days + " " + thisReport.file.beginingOfMarket.hours + ":" + thisReport.file.beginingOfMarket.minutes + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); + lastFile = new Date(thisReport.file.lastFile.year + "-" + thisReport.file.lastFile.month + "-" + thisReport.file.lastFile.days + " " + thisReport.file.lastFile.hours + ":" + thisReport.file.lastFile.minutes + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); lastId = thisReport.file.lastId lastCandleOfTheDay = thisReport.file.lastCandleOfTheDay } else { // This means this is the first time this process has run. @@ -449,7 +449,7 @@ await exchange.fetchOHLCV(symbol, '1d', earliestMarketSince, searchSize, params) if (earlyOHLCVs.length === searchSize && previousMarketSince !== earlyOHLCVs[0][0]) { // If array is full, and the date returned isn't the same then we are still working in valid data, search further back - earliestMarketSince = (earlyOHLCVs[0][0] - (TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS * searchSize)) + earliestMarketSince = (earlyOHLCVs[0][0] - (SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS * searchSize)) previousMarketSince = earlyOHLCVs[0][0] } else if (earlyOHLCVs.length === searchSize && previousMarketSince === earlyOHLCVs[0][0]) { // If array is full, but the first OHLCV date is the same as last time then we've found our Market Start @@ -463,7 +463,7 @@ return earlyOHLCVs[0][0] } else if ((earlyOHLCVs.length === 0 && searchSize !== 1)) { // If array is empty, we've gone too far, half the search size and retry - earliestMarketSince = (earliestMarketSince + (TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS * searchSize)) + earliestMarketSince = (earliestMarketSince + (SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS * searchSize)) if (searchSize > 1) { searchSize = Math.floor(searchSize / 2) } @@ -504,7 +504,7 @@ await exchange.fetchOHLCV(symbol, '1m', since, limit, params) if (nextValidOHLCVs.length === 0) { - since = (since + (TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * limit)) + since = (since + (SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * limit)) } else { foundDate = true @@ -549,7 +549,7 @@ let OHLCV = OHLCVs[0] initialProcessTimestamp = OHLCV[0] // 'timestamp' - beginingOfMarket = new Date(Math.trunc(OHLCV[0] / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // 'timestamp' + beginingOfMarket = new Date(Math.trunc(OHLCV[0] / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // 'timestamp' fromDate = new Date(beginingOfMarket.valueOf()) firstTimeThisProcessRun = false } @@ -627,11 +627,11 @@ let startOfFetchTrades = since let endOfFetchTrades if (lastFile === undefined) { - endOfFetchTrades = (Math.trunc(since / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + endOfFetchTrades = (Math.trunc(since / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) } else { endOfFetchTrades = lastFile.valueOf() } - endOfFetchTrades += TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + endOfFetchTrades += SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS while (TS.projects.foundations.globals.taskVariables.IS_TASK_STOPPING !== true) { var trades = await exchange.fetchTrades(symbol, startOfFetchTrades, limit, params) if (trades.length > 0) { @@ -680,7 +680,7 @@ let heartBeatCounter = 0 let savingProcedureFinished = false let endOfTheOHLCVArrayReached = false - let currentDay = Math.trunc((initialProcessTimestamp - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + let currentDay = Math.trunc((initialProcessTimestamp - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) let lastCandle = { begin: 0, @@ -742,8 +742,8 @@ hold at least the last know value. */ let candle = { - begin: currentDay * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * minuteOfTheDay, - end: currentDay * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * minuteOfTheDay + TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1, + begin: currentDay * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * minuteOfTheDay, + end: currentDay * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * minuteOfTheDay + SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1, open: lastCandle.close, close: lastCandle.close, min: lastCandle.close, @@ -751,8 +751,8 @@ } let volume = { - begin: currentDay * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * minuteOfTheDay, - end: currentDay * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * minuteOfTheDay + TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1, + begin: currentDay * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * minuteOfTheDay, + end: currentDay * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS * minuteOfTheDay + SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1, buy: lastVolume.buy, sell: lastVolume.sell } @@ -796,7 +796,7 @@ timestamp in order to calculate OHLCVMinute. */ let OHLCV = { - timestamp: (new Date()).valueOf() + TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS, + timestamp: (new Date()).valueOf() + SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS, open: 0, hight: 0, low: 0, @@ -824,7 +824,7 @@ } } - let candleMinute = Math.trunc(candle.begin / TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) + let candleMinute = Math.trunc(candle.begin / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) let OHLCVMinute /* Some exchanges return inconsistent data. It is not guaranteed @@ -833,7 +833,7 @@ between timestamps will be the same. To fix this, we will do this. */ - OHLCVMinute = Math.trunc(OHLCV.timestamp / TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) + OHLCVMinute = Math.trunc(OHLCV.timestamp / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) /* If the minute of the record item received from the exchange is @@ -873,7 +873,7 @@ } /* Recalculate this to see if we need to break the loop*/ - OHLCVMinute = Math.trunc(OHLCV.timestamp / TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) + OHLCVMinute = Math.trunc(OHLCV.timestamp / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) } /* @@ -981,14 +981,14 @@ let fileName = 'Data.json' filesToCreate++ - fileStorage.createTextFile(getFilePath(day * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS, CANDLES_FOLDER_NAME) + '/' + fileName, candlesFileContent + '\n', onFileCreated); + fileStorage.createTextFile(getFilePath(day * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS, CANDLES_FOLDER_NAME) + '/' + fileName, candlesFileContent + '\n', onFileCreated); filesToCreate++ - fileStorage.createTextFile(getFilePath(day * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS, VOLUMES_FOLDER_NAME) + '/' + fileName, volumesFileContent + '\n', onFileCreated); + fileStorage.createTextFile(getFilePath(day * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS, VOLUMES_FOLDER_NAME) + '/' + fileName, volumesFileContent + '\n', onFileCreated); if (ohlcvsFileContent !== undefined) { filesToCreate++ - fileStorage.createTextFile(getFilePath(day * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS, OHLCVS_FOLDER_NAME) + '/' + fileName, ohlcvsFileContent + '\n', onFileCreated); + fileStorage.createTextFile(getFilePath(day * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS, OHLCVS_FOLDER_NAME) + '/' + fileName, ohlcvsFileContent + '\n', onFileCreated); mustLoadRawData = true } else { mustLoadRawData = false @@ -1010,11 +1010,11 @@ let dataLength = rawDataArray.length if (dataLength > 0) { // first get the start of the day after this day we are checking - let timestamp = (day * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + let timestamp = (day * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS if (rawDataArray[dataLength - 1][0] < timestamp) { // there is no data for the next day, so now trim this day's data to remove anything before it - timestamp -= TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + timestamp -= SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS let dataIndex = 0 while (dataIndex < dataLength - 1) { if (rawDataArray[dataIndex][0] < timestamp) { // this data is from a previous day @@ -1043,7 +1043,7 @@ return; } filesCreated++ - lastFile = new Date((currentDay * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS)) + lastFile = new Date((currentDay * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS)) if (filesCreated === filesToCreate) { controlLoop() } diff --git a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingEpisode.js b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingEpisode.js index 6c3b51cb2..0ff1c6762 100644 --- a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingEpisode.js +++ b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingEpisode.js @@ -113,7 +113,7 @@ exports.newFoundationsBotModulesTradingEpisode = function (processIndex) { tradingEngine.tradingCurrent.tradingEpisode.tradingEpisodeStatistics.days.value = tradingEngine.tradingCurrent.tradingEpisode.tradingEpisodeCounters.periods.value * sessionParameters.timeFrame.config.value / - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS } function cycleBasedStatistics() { diff --git a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js index 91d1958d2..26b4207e5 100644 --- a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js +++ b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingOrders.js @@ -1286,7 +1286,7 @@ exports.newFoundationsBotModulesTradingOrders = function (processIndex) { } function updateStatistics(tradingEngineOrder) { - tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + tradingEngineOrder.orderStatistics.days.value = tradingEngineOrder.orderCounters.periods.value * sessionParameters.timeFrame.config.value / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS tradingEngineOrder.orderStatistics.days.value = TS.projects.foundations.utilities.miscellaneousFunctions.truncateToThisPrecision(tradingEngineOrder.orderStatistics.days.value, 10) } diff --git a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingPosition.js b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingPosition.js index f2de2313c..c2aaa9302 100644 --- a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingPosition.js +++ b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingPosition.js @@ -513,7 +513,7 @@ exports.newFoundationsBotModulesTradingPosition = function (processIndex) { /* Days Calculation */ tradingEngine.tradingCurrent.position.positionStatistics.days.value = tradingEngine.tradingCurrent.position.positionCounters.periods.value * - sessionParameters.timeFrame.config.value / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + sessionParameters.timeFrame.config.value / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS tradingEngine.tradingCurrent.position.positionStatistics.days.value = TS.projects.foundations.utilities.miscellaneousFunctions.truncateToThisPrecision(tradingEngine.tradingCurrent.position.positionStatistics.days.value, 10) } diff --git a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingSimulation.js b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingSimulation.js index bcc9a2a6a..bb3701eef 100644 --- a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingSimulation.js +++ b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/Low-Frequency-Trading/TradingSimulation.js @@ -303,7 +303,7 @@ exports.newFoundationsBotModulesTradingSimulation = function (processIndex) { if (sessionParameters.heartbeats.config.date === true || sessionParameters.heartbeats.config.candleIndex === true) { /* We will produce a simulation level heartbeat in order to inform the user this is running. */ - heartBeatDate = new Date(Math.trunc(tradingEngine.tradingCurrent.tradingEpisode.candle.begin.value / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) * TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + heartBeatDate = new Date(Math.trunc(tradingEngine.tradingCurrent.tradingEpisode.candle.begin.value / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) * SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) let fromDate = new Date(sessionParameters.timeRange.config.initialDatetime) let lastDate = new Date(sessionParameters.timeRange.config.finalDatetime) @@ -493,7 +493,7 @@ exports.newFoundationsBotModulesTradingSimulation = function (processIndex) { the day, we will advance current process day one day. By doing so, during the next execution, the simulation will receive the candles and indicators files of the next day. */ - let candlesPerDay = TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value + let candlesPerDay = SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS / sessionParameters.timeFrame.config.value if ( TS.projects.foundations.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_PROCESSING_DAILY_FILES && tradingEngine.tradingCurrent.tradingEpisode.candle.index.value + 1 + 1 === candlesPerDay @@ -507,7 +507,7 @@ exports.newFoundationsBotModulesTradingSimulation = function (processIndex) { */ tradingEngine.tradingCurrent.tradingEpisode.candle.index.value = 0 tradingEngine.tradingCurrent.tradingEpisode.processDate.value = - tradingEngine.tradingCurrent.tradingEpisode.processDate.value + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + tradingEngine.tradingCurrent.tradingEpisode.processDate.value + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS return false } diff --git a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/TradingSession.js b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/TradingSession.js index 290876baf..404516556 100644 --- a/Projects/Foundations/TS/Bot-Modules/Trading-Bot/TradingSession.js +++ b/Projects/Foundations/TS/Bot-Modules/Trading-Bot/TradingSession.js @@ -249,7 +249,7 @@ type: 'Time Range', config: { initialDatetime: (new Date()).valueOf(), - finalDatetime: (new Date()).valueOf() + TS.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS + finalDatetime: (new Date()).valueOf() + SA.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS } } } else { @@ -300,8 +300,8 @@ /* Session Type Forced Values */ let today = (new Date()).valueOf() - let aYearAgo = today - TS.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS - let aYearFromNow = today + TS.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS + let aYearAgo = today - SA.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS + let aYearFromNow = today + SA.projects.foundations.globals.timeConstants.ONE_YEAR_IN_MILISECONDS switch (TS.projects.foundations.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.type) { case 'Backtesting Session': { useDefaultDatetimes(aYearAgo, today) diff --git a/Projects/Foundations/TS/Function-Libraries/DataDependenciesFunctions.js b/Projects/Foundations/TS/Function-Libraries/DataDependenciesFunctions.js index d80fb517a..937a8809f 100644 --- a/Projects/Foundations/TS/Function-Libraries/DataDependenciesFunctions.js +++ b/Projects/Foundations/TS/Function-Libraries/DataDependenciesFunctions.js @@ -315,7 +315,7 @@ exports.newFoundationsFunctionLibrariesDataDependenciesFunctions = function () { /* We will need to fetch the data of the current day and the previous day, in order for .previous properties in conditions and formulas to work well. */ - let previousDate = new Date(processDate.valueOf() - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + let previousDate = new Date(processDate.valueOf() - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) let currentDate = new Date(processDate.valueOf()) let previousFile = await getDataFileFromDate(previousDate) diff --git a/Projects/Foundations/TS/Function-Libraries/FromOneMinToMultiTimeFrameFunctions.js b/Projects/Foundations/TS/Function-Libraries/FromOneMinToMultiTimeFrameFunctions.js index 7300d33b0..70f231667 100644 --- a/Projects/Foundations/TS/Function-Libraries/FromOneMinToMultiTimeFrameFunctions.js +++ b/Projects/Foundations/TS/Function-Libraries/FromOneMinToMultiTimeFrameFunctions.js @@ -215,15 +215,15 @@ exports.newFoundationsFunctionLibrariesFromOneMinToMultiTimeFrameFunctions = fun contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeLastProducedFile.valueOf() - - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // Go back one day to start well. + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // Go back one day to start well. callbackFirstRun() return @@ -236,7 +236,7 @@ exports.newFoundationsFunctionLibrariesFromOneMinToMultiTimeFrameFunctions = fun For that reason we go back one day, the partial information is discarded and added again with whatever new info is available. */ - contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() - SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) callbackNotFirstRun() return @@ -249,15 +249,15 @@ exports.newFoundationsFunctionLibrariesFromOneMinToMultiTimeFrameFunctions = fun contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeBeginingOfMarketFile.getUTCFullYear() + "-" + (contextVariables.datetimeBeginingOfMarketFile.getUTCMonth() + 1) + "-" + contextVariables.datetimeBeginingOfMarketFile.getUTCDate() + " " + "00:00" + - TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) contextVariables.datetimeLastProducedFile = new Date( contextVariables.datetimeLastProducedFile.valueOf() - - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // Go back one day to start well. + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) // Go back one day to start well. callbackFirstRun() return @@ -298,7 +298,7 @@ exports.newFoundationsFunctionLibrariesFromOneMinToMultiTimeFrameFunctions = fun from there into the future. */ contextVariables.datetimeLastProducedFile = new Date(contextVariables.datetimeLastProducedFile.valueOf() + - TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] advanceTime -> New processing time @ " + diff --git a/Projects/Foundations/TS/Function-Libraries/ProcessFilesFunctions.js b/Projects/Foundations/TS/Function-Libraries/ProcessFilesFunctions.js index b8ca6d72b..a818ee06a 100644 --- a/Projects/Foundations/TS/Function-Libraries/ProcessFilesFunctions.js +++ b/Projects/Foundations/TS/Function-Libraries/ProcessFilesFunctions.js @@ -100,7 +100,7 @@ exports.newFoundationsFunctionLibrariesProcessFilesFunctions = function () { } function startFromBegining() { - contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + TS.projects.foundations.globals.timeConstants.GMT_SECONDS) + contextVariables.lastFile = new Date(contextVariables.dateBeginOfMarket.getUTCFullYear() + "-" + (contextVariables.dateBeginOfMarket.getUTCMonth() + 1) + "-" + contextVariables.dateBeginOfMarket.getUTCDate() + " " + "00:00" + SA.projects.foundations.globals.timeConstants.GMT_SECONDS) TS.projects.foundations.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> getContextVariables -> startFromBegining -> contextVariables.lastFile = " + contextVariables.lastFile) @@ -147,7 +147,7 @@ exports.newFoundationsFunctionLibrariesProcessFilesFunctions = function () { async function writeDataRange(productCodeName) { let dataRange = { begin: contextVariables.dateBeginOfMarket.valueOf(), - end: processDate.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + end: processDate.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS } let fileContent = JSON.stringify(dataRange) diff --git a/Projects/Foundations/TS/Function-Libraries/SingleMarketFunctions.js b/Projects/Foundations/TS/Function-Libraries/SingleMarketFunctions.js index b0904da95..bd89bf2a8 100644 --- a/Projects/Foundations/TS/Function-Libraries/SingleMarketFunctions.js +++ b/Projects/Foundations/TS/Function-Libraries/SingleMarketFunctions.js @@ -219,7 +219,7 @@ let system = { // These are the available system variables to be used in User Code and Formulas timeFrame: timeFrame, - ONE_DAY_IN_MILISECONDS: TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS + ONE_DAY_IN_MILISECONDS: SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS } let variable = {} // This is the structure where the user will define its own variables that will be shared across different code blocks and formulas. let results = [] @@ -312,8 +312,8 @@ let yesterday = {} let system = { // These are the available system variables to be used in User Code and Formulas timeFrame: timeFrame, - ONE_DAY_IN_MILISECONDS: TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS, - ONE_MIN_IN_MILISECONDS: TS.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS + ONE_DAY_IN_MILISECONDS: SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS, + ONE_MIN_IN_MILISECONDS: SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS } let variable = {} // This is the structure where the user will define its own variables that will be shared across different code blocks and formulas. let results = [] @@ -407,7 +407,7 @@ if (processingDailyFiles) { /* Initialization of Last Instance */ - lastInstantOfTheDay = currentDay.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS - 1; + lastInstantOfTheDay = currentDay.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS - 1; if (interExecutionMemory[productName] === undefined) { /* The first time the intialization variables goes to the Inter Execution Memory. */ @@ -515,7 +515,7 @@ at the next day, in whole, even if it starts in the previous day. */ - let lastInstantOdDay = currentDay.valueOf() + TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS - 1; + let lastInstantOdDay = currentDay.valueOf() + SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS - 1; if (record.end < currentDay.valueOf() - 1) { continue; } if (record.end === lastInstantOdDay) { continue; } diff --git a/Projects/Foundations/TS/Utilities/DateTimeFunctions.js b/Projects/Foundations/TS/Utilities/DateTimeFunctions.js index f3c1aa6c2..19ca6d4b2 100644 --- a/Projects/Foundations/TS/Utilities/DateTimeFunctions.js +++ b/Projects/Foundations/TS/Utilities/DateTimeFunctions.js @@ -10,7 +10,7 @@ exports.newFoundationsUtilitiesDateTimeFunctions = function () { function removeTime(datetime) { let date = new Date(datetime) - return new Date(date.getUTCFullYear() + "-" + (date.getUTCMonth() + 1) + "-" + date.getUTCDate() + " " + "00:00" + TS.projects.foundations.globals.timeConstants.GMT_SECONDS); + return new Date(date.getUTCFullYear() + "-" + (date.getUTCMonth() + 1) + "-" + date.getUTCDate() + " " + "00:00" + SA.projects.foundations.globals.timeConstants.GMT_SECONDS); } function getPercentage(fromDate, currentDate, lastDate) { @@ -26,8 +26,8 @@ exports.newFoundationsUtilitiesDateTimeFunctions = function () { } function areTheseDatesEqual(date1, date2) { - let day1Days = Math.trunc(date1.valueOf() / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) - let day2Days = Math.trunc(date2.valueOf() / TS.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + let day1Days = Math.trunc(date1.valueOf() / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) + let day2Days = Math.trunc(date2.valueOf() / SA.projects.foundations.globals.timeConstants.ONE_DAY_IN_MILISECONDS) if (day1Days === day2Days) { return true diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 3eec6d184..341264f52 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -340,12 +340,6 @@ "functionName": "newFoundationsGlobalsStandardResponses", "fileName": "StandardResponses.js" }, - { - "name": "Time Constants", - "propertyName": "timeConstants", - "functionName": "newFoundationsGlobalsTimeConstants", - "fileName": "TimeConstants.js" - }, { "name": "Process Variables", "propertyName": "processVariables", @@ -883,6 +877,14 @@ ] }, "SA": { + "globals":[ + { + "name": "Time Constants", + "propertyName": "timeConstants", + "functionName": "newFoundationsGlobalsTimeConstants", + "fileName": "TimeConstants.js" + } + ], "utilities": [ { "name": "Plugins", diff --git a/Projects/Social-Trading/DK/modules/WebAppInterface.js b/Projects/Social-Trading/DK/modules/WebAppInterface.js index 135ae7dcf..8c154ef2e 100644 --- a/Projects/Social-Trading/DK/modules/WebAppInterface.js +++ b/Projects/Social-Trading/DK/modules/WebAppInterface.js @@ -47,7 +47,7 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule queryMessage.emitterUserProfileId = DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID messageHeader.queryMessage = JSON.stringify(queryMessage) - console.log((new Date()).toISOString(), '- Web App Interface', '- Query Message Received', JSON.stringify(queryMessage)) + // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Message Received', JSON.stringify(queryMessage)) let response = { result: 'Ok', @@ -55,7 +55,7 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule data: await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) } - console.log((new Date()).toISOString(), '- Web App Interface', '- Query Respose Sent', JSON.stringify(response)) + // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Respose Sent', JSON.stringify(response)) return response } diff --git a/Projects/Social-Trading/NT/Modules/ClientInterface.js b/Projects/Social-Trading/NT/Modules/ClientInterface.js index 497d3a817..cb01af216 100644 --- a/Projects/Social-Trading/NT/Modules/ClientInterface.js +++ b/Projects/Social-Trading/NT/Modules/ClientInterface.js @@ -130,6 +130,7 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule let event = NT.projects.socialTrading.modules.event.newSocialTradingModulesEvent() event.initialize(eventReceived) NT.projects.socialTrading.globals.memory.maps.EVENTS.set(eventReceived.eventId, event) + NT.projects.socialTrading.globals.memory.arrays.EVENTS.push(event) let response = { result: 'Ok', @@ -207,7 +208,7 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule let query = NT.projects.socialTrading.modules.query.newSocialTradingModulesQuery() query.initialize(queryReceived) - console.log((new Date()).toISOString(), '- Client Interface', '- Query Message Received', queryMessage) + // console.log((new Date()).toISOString(), '- Client Interface', '- Query Message Received', queryMessage) let response = { result: 'Ok', @@ -215,7 +216,7 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule data: query.execute() } - console.log((new Date()).toISOString(), '- Client Interface', '- Query Respose Sent', JSON.stringify(response)) + // console.log((new Date()).toISOString(), '- Client Interface', '- Query Respose Sent', JSON.stringify(response)) return response From 5d2f74c8de2dd737cbee9d09452452d3bab11041 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 17 Sep 2021 12:10:06 +0200 Subject: [PATCH 2/6] Storage Module Implemented --- Network/NetwokNode.js | 107 ++-------------- Projects/ProjectsSchema.json | 6 + .../Social-Trading/NT/Modules/SocialGraph.js | 2 +- Projects/Social-Trading/NT/Modules/Storage.js | 114 ++++++++++++++++++ 4 files changed, 128 insertions(+), 101 deletions(-) create mode 100644 Projects/Social-Trading/NT/Modules/Storage.js diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index 54f053306..fc8e00c2c 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -1,118 +1,25 @@ exports.newNetworkNode = function newNetworkNode() { let thisObject = { + socialGraphService: undefined, + storage: undefined, run: run } - let indexLastSavedEvent = -1 - return thisObject - function run() { + async function run() { /* Let's start the Network Interfaces */ NT.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface() NT.webSocketsInterface.initialize() - start() + thisObject.storage = NT.projects.socialTrading.modules.storage.newSocialTradingModulesStorage() + thisObject.storage.initialize() - async function start() { - let socialGraphService = NT.projects.socialTrading.modules.socialGraph.newSocialGraph() - await socialGraphService.initialize() + thisObject.socialGraphService = NT.projects.socialTrading.modules.socialGraph.newNetworkModulesSocialGraph() + await thisObject.socialGraphService.initialize() - setInterval(saveEventsAtStorage, 60000) - } - } - - async function saveEventsAtStorage() { - - let oneMore = true - let needToDoGit = false - - while (oneMore === true) { - oneMore = saveOneMinuteOfEvents() - if (oneMore === true) { - needToDoGit = true - } - } - - if (needToDoGit === true) { - doGit() - } - - function saveOneMinuteOfEvents() { - /* - Here we will save all the events that were not saved before, - in one minute chunchs files. - */ - let eventsToSave = [] - let minuteToSave - - for (let i = indexLastSavedEvent + 1; i < NT.projects.socialTrading.globals.memory.arrays.EVENTS.length; i++) { - let event = NT.projects.socialTrading.globals.memory.arrays.EVENTS[i] - - let currentMinute = Math.trunc((new Date()).valueOf() / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) - let eventMinute = Math.trunc(event.timestamp / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) - - if (minuteToSave === undefined) { - minuteToSave = eventMinute - } - /* - We will save events only of the last closed minute. - We will also pack events into one minute chunks only. - */ - if ( - eventMinute < currentMinute && - eventMinute === minuteToSave - ) { - let eventToSave = { - eventId: event.eventId, - eventType: event.eventType, - emitterUserProfileId: event.emitterUserProfileId, - targetUserProfileId: event.targetUserProfileId, - emitterBotProfileId: event.emitterBotProfileId, - targetBotProfileId: event.targetBotProfileId, - emitterPostHash: event.emitterPostHash, - targetPostHash: event.targetPostHash, - timestamp: event.timestamp, - botAsset: event.botAsset, - botExchange: event.botExchange, - botEnabled: event.botEnabled - } - - eventsToSave.push(eventToSave) - indexLastSavedEvent = i - } - } - - if (eventsToSave.length === 0) { return false } - - let timestamp = minuteToSave * SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS - - const fileContent = JSON.stringify(eventsToSave, undefined, 4) - const fileName = "Events" + ".json" - - let filePath = './My-Network-Nodes-Data/Nodes/Node-1/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDatetime(timestamp) - - SA.projects.foundations.utilities.filesAndDirectories.mkDirByPathSync(filePath + '/') - SA.nodeModules.fs.writeFileSync(filePath + '/' + fileName, fileContent) - - return true - } - - async function doGit() { - const options = { - baseDir: process.cwd() + '/My-Network-Nodes-Data', - binary: 'git', - maxConcurrentProcesses: 6, - } - const commitMessage = 'New Events' - const git = SA.nodeModules.simpleGit(options) - - await git.add('./*') - await git.commit(commitMessage) - await git.push('origin') - } } } diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 341264f52..4b7784507 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -1442,6 +1442,12 @@ "propertyName": "peerInterface", "functionName": "newSocialTradingModulesPeerInterface", "fileName": "PeerInterface.js" + }, + { + "name": "Storage", + "propertyName": "storage", + "functionName": "newSocialTradingModulesStorage", + "fileName": "Storage.js" } ], "utilities": [ diff --git a/Projects/Social-Trading/NT/Modules/SocialGraph.js b/Projects/Social-Trading/NT/Modules/SocialGraph.js index 3ac068e92..7bf110fd3 100644 --- a/Projects/Social-Trading/NT/Modules/SocialGraph.js +++ b/Projects/Social-Trading/NT/Modules/SocialGraph.js @@ -1,4 +1,4 @@ -exports.newSocialGraph = function newSocialGraph() { +exports.newNetworkModulesSocialGraph = function newNetworkModulesSocialGraph() { /* This module represents the Social Graph Service that this deals with the Social Graph this node mantains. diff --git a/Projects/Social-Trading/NT/Modules/Storage.js b/Projects/Social-Trading/NT/Modules/Storage.js new file mode 100644 index 000000000..6789e3ccc --- /dev/null +++ b/Projects/Social-Trading/NT/Modules/Storage.js @@ -0,0 +1,114 @@ +exports.newSocialTradingModulesStorage = function newSocialTradingModulesStorage() { + /* + This module represents the Network Node Physical Storage. + It is used to save the in-memory events and also to load + them when the node is starting. + */ + let thisObject = { + initialize: initialize, + finalize: finalize + } + + let indexLastSavedEvent = -1 + + return thisObject + + function finalize() { + + } + + function initialize() { + setInterval(saveEventsAtStorage, 60000) + } + + async function saveEventsAtStorage() { + + let oneMore = true + let needToDoGit = false + + while (oneMore === true) { + oneMore = saveOneMinuteOfEvents() + if (oneMore === true) { + needToDoGit = true + } + } + + if (needToDoGit === true) { + doGit() + } + + function saveOneMinuteOfEvents() { + /* + Here we will save all the events that were not saved before, + in one minute chunchs files. + */ + let eventsToSave = [] + let minuteToSave + + for (let i = indexLastSavedEvent + 1; i < NT.projects.socialTrading.globals.memory.arrays.EVENTS.length; i++) { + let event = NT.projects.socialTrading.globals.memory.arrays.EVENTS[i] + + let currentMinute = Math.trunc((new Date()).valueOf() / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) + let eventMinute = Math.trunc(event.timestamp / SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS) + + if (minuteToSave === undefined) { + minuteToSave = eventMinute + } + /* + We will save events only of the last closed minute. + We will also pack events into one minute chunks only. + */ + if ( + eventMinute < currentMinute && + eventMinute === minuteToSave + ) { + let eventToSave = { + eventId: event.eventId, + eventType: event.eventType, + emitterUserProfileId: event.emitterUserProfileId, + targetUserProfileId: event.targetUserProfileId, + emitterBotProfileId: event.emitterBotProfileId, + targetBotProfileId: event.targetBotProfileId, + emitterPostHash: event.emitterPostHash, + targetPostHash: event.targetPostHash, + timestamp: event.timestamp, + botAsset: event.botAsset, + botExchange: event.botExchange, + botEnabled: event.botEnabled + } + + eventsToSave.push(eventToSave) + indexLastSavedEvent = i + } + } + + if (eventsToSave.length === 0) { return false } + + let timestamp = minuteToSave * SA.projects.foundations.globals.timeConstants.ONE_MIN_IN_MILISECONDS + + const fileContent = JSON.stringify(eventsToSave, undefined, 4) + const fileName = "Events" + ".json" + + let filePath = './My-Network-Nodes-Data/Nodes/Node-1/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDatetime(timestamp) + + SA.projects.foundations.utilities.filesAndDirectories.mkDirByPathSync(filePath + '/') + SA.nodeModules.fs.writeFileSync(filePath + '/' + fileName, fileContent) + + return true + } + + async function doGit() { + const options = { + baseDir: process.cwd() + '/My-Network-Nodes-Data', + binary: 'git', + maxConcurrentProcesses: 6, + } + const commitMessage = 'New Events' + const git = SA.nodeModules.simpleGit(options) + + await git.add('./*') + await git.commit(commitMessage) + await git.push('origin') + } + } +} \ No newline at end of file From 71de99b11b29e1eead3744601de4c1732b76074f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 17 Sep 2021 16:27:50 +0200 Subject: [PATCH 3/6] Applying events from storage implemented. --- Network/NetwokNode.js | 5 +-- NetworkRoot.js | 1 + Projects/Social-Trading/NT/Modules/Storage.js | 40 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index fc8e00c2c..3d3b993dc 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -15,11 +15,10 @@ exports.newNetworkNode = function newNetworkNode() { NT.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface() NT.webSocketsInterface.initialize() - thisObject.storage = NT.projects.socialTrading.modules.storage.newSocialTradingModulesStorage() - thisObject.storage.initialize() - thisObject.socialGraphService = NT.projects.socialTrading.modules.socialGraph.newNetworkModulesSocialGraph() await thisObject.socialGraphService.initialize() + thisObject.storage = NT.projects.socialTrading.modules.storage.newSocialTradingModulesStorage() + thisObject.storage.initialize() } } diff --git a/NetworkRoot.js b/NetworkRoot.js index 9fbe271d2..a0e5bbebf 100644 --- a/NetworkRoot.js +++ b/NetworkRoot.js @@ -42,6 +42,7 @@ exports.newNetworkRoot = function newNetworkRoot() { SA.nodeModules = { fs: require('fs'), path: require('path'), + util: require('util'), nodeFetch: require('node-fetch'), web3: require('web3'), ws: require('ws'), diff --git a/Projects/Social-Trading/NT/Modules/Storage.js b/Projects/Social-Trading/NT/Modules/Storage.js index 6789e3ccc..8a966b26f 100644 --- a/Projects/Social-Trading/NT/Modules/Storage.js +++ b/Projects/Social-Trading/NT/Modules/Storage.js @@ -18,9 +18,49 @@ exports.newSocialTradingModulesStorage = function newSocialTradingModulesStorage } function initialize() { + loadEventsFromStorage() setInterval(saveEventsAtStorage, 60000) } + async function loadEventsFromStorage() { + + SA.projects.foundations.utilities.filesAndDirectories.getAllFilesInDirectoryAndSubdirectories('./My-Network-Nodes-Data/Nodes/Node-1/', onFiles) + + function onFiles(fileList) { + + for (let i = 0; i < fileList.length; i++) { + let filePath = './My-Network-Nodes-Data/Nodes/Node-1/' + fileList[i] + + for (let k = 0; k < 5; k++) { + filePath = filePath.replace('\\', '/') + } + + let fileContent = SA.nodeModules.fs.readFileSync(filePath) + + let eventsList = JSON.parse(fileContent) + + for (let j = 0; j < eventsList.length; j++) { + let storedEvent = eventsList[j] + + try { + let event = NT.projects.socialTrading.modules.event.newSocialTradingModulesEvent() + event.initialize(storedEvent) + NT.projects.socialTrading.globals.memory.maps.EVENTS.set(storedEvent.eventId, event) + NT.projects.socialTrading.globals.memory.arrays.EVENTS.push(event) + indexLastSavedEvent = NT.projects.socialTrading.globals.memory.arrays.EVENTS.length - 1 + } catch (err) { + if (err.stack !== undefined) { + console.log('[ERROR] Client Interface -> err.stack = ' + err.stack) + } + let errorMessage = err.message + if (errorMessage === undefined) { errorMessage = err } + throw ('Could not apply the events from storage. ' + errorMessage) + } + } + } + } + } + async function saveEventsAtStorage() { let oneMore = true From a99506897807631fb4ef6f8148dcb504b169aa11 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 17 Sep 2021 17:57:53 +0200 Subject: [PATCH 4/6] Bug Fixed --- .../Social-Trading/NT/Modules/Queries/Events.js | 12 ++++++------ .../NT/Modules/Social-Graph/Post.js | 2 ++ Projects/Social-Trading/NT/Modules/Storage.js | 2 +- .../NT/Utilities/QueriesValidations.js | 15 +++++++++++---- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Projects/Social-Trading/NT/Modules/Queries/Events.js b/Projects/Social-Trading/NT/Modules/Queries/Events.js index b8085c5b9..352954b92 100644 --- a/Projects/Social-Trading/NT/Modules/Queries/Events.js +++ b/Projects/Social-Trading/NT/Modules/Queries/Events.js @@ -59,17 +59,17 @@ exports.newSocialTradingModulesQueriesEvents = function newSocialTradingModulesQ Any of the above happening, means that indeed it is related. */ - let emitterUserProfile = NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID.get(eventReceived.emitterUserProfileId) - let emitterBotProfile = emitterUserProfile.bots.get(eventReceived.emitterBotProfileId) - let emitterPost = emitterUserProfile.posts.get(eventReceived.emitterPostHash) + let emitterUserProfile = NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID.get(event.emitterUserProfileId) + let emitterBotProfile = emitterUserProfile.bots.get(event.emitterBotProfileId) + let emitterPost = emitterUserProfile.posts.get(event.emitterPostHash) - let targetUserProfile = NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID.get(eventReceived.targetUserProfileId) + let targetUserProfile = NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID.get(event.targetUserProfileId) let targetBotProfile let targetPost if (targetUserProfile !== undefined) { - targetBotProfile = targetUserProfile.bots.get(eventReceived.targetBotProfileId) - targetPost = targetUserProfile.posts.get(eventReceived.targetPostHash) + targetBotProfile = targetUserProfile.bots.get(event.targetBotProfileId) + targetPost = targetUserProfile.posts.get(event.targetPostHash) } /* Test #1 : The Emitter or Target profile must be the same as the Context Profile. diff --git a/Projects/Social-Trading/NT/Modules/Social-Graph/Post.js b/Projects/Social-Trading/NT/Modules/Social-Graph/Post.js index 42ffbc346..d462cab83 100644 --- a/Projects/Social-Trading/NT/Modules/Social-Graph/Post.js +++ b/Projects/Social-Trading/NT/Modules/Social-Graph/Post.js @@ -51,6 +51,8 @@ exports.newSocialTradingModulesSocialGraphPost = function newSocialTradingModule REACTION_CARE: 6 } + thisObject.replies = new Map() + thisObject.reactions = new Map() thisObject.reactions.set(REACTION_TYPES.REACTION_LIKE, 0) thisObject.reactions.set(REACTION_TYPES.REACTION_LOVE, 0) diff --git a/Projects/Social-Trading/NT/Modules/Storage.js b/Projects/Social-Trading/NT/Modules/Storage.js index 8a966b26f..eaab5a677 100644 --- a/Projects/Social-Trading/NT/Modules/Storage.js +++ b/Projects/Social-Trading/NT/Modules/Storage.js @@ -54,7 +54,7 @@ exports.newSocialTradingModulesStorage = function newSocialTradingModulesStorage } let errorMessage = err.message if (errorMessage === undefined) { errorMessage = err } - throw ('Could not apply the events from storage. ' + errorMessage) + console.log ('Could not apply the event from storage. -> errorMessage = ' + errorMessage) } } } diff --git a/Projects/Social-Trading/NT/Utilities/QueriesValidations.js b/Projects/Social-Trading/NT/Utilities/QueriesValidations.js index 5dc208fb7..7f2e6f566 100644 --- a/Projects/Social-Trading/NT/Utilities/QueriesValidations.js +++ b/Projects/Social-Trading/NT/Utilities/QueriesValidations.js @@ -4,7 +4,7 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU */ let thisObject = { profilesValidations: profilesValidations, - postValidations: postValidations, + postValidations: postValidations, arrayValidations: arrayValidations } @@ -14,6 +14,13 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU /* Validate User Profile */ + if (queryReceived.emitterUserProfileId !== undefined) { + thisObject.profile = NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID.get(queryReceived.emitterUserProfileId) + if (thisObject.profile === undefined) { + throw ('Emitter User Profile Not Found.') + } + } + if (queryReceived.targetUserProfileId !== undefined) { thisObject.profile = NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID.get(queryReceived.targetUserProfileId) if (thisObject.profile === undefined) { @@ -92,11 +99,11 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU } if ( - queryReceived.direction !== SA.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE && + queryReceived.direction !== SA.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE && queryReceived.direction !== SA.projects.socialTrading.globals.queryConstants.DIRECTION_PAST && - queryReceived.direction !== SA.projects.socialTrading.globals.queryConstants.DIRECTION_UP && + queryReceived.direction !== SA.projects.socialTrading.globals.queryConstants.DIRECTION_UP && queryReceived.direction !== SA.projects.socialTrading.globals.queryConstants.DIRECTION_DOWN - ) { + ) { throw ('Direction Not Supported.') } From 760c8d91965eb62e844287119d63dbc03a95339a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 18 Sep 2021 12:09:17 +0200 Subject: [PATCH 5/6] Events Showing up in Timeline --- Desktop/UI/WebApp.js | 46 +++++++++++++++---- Desktop/WebServer/index.html | 6 +-- .../DK/modules/WebAppInterface.js | 4 +- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/Desktop/UI/WebApp.js b/Desktop/UI/WebApp.js index 4a3b9a63b..301757206 100644 --- a/Desktop/UI/WebApp.js +++ b/Desktop/UI/WebApp.js @@ -121,17 +121,47 @@ function newWebApp() { } function addToContentDiv(events) { + try { + let contentDiv = document.getElementById('content-div') - let contentDiv = document.getElementById('content-div') + /* + Delete al current content. + */ + while (contentDiv.childNodes.length > 4) { + let childToRemove = contentDiv.childNodes[contentDiv.childNodes.length - 1] + contentDiv.removeChild(childToRemove) + } + /* + Render the timeline. + */ + for (let i = 0; i < events.length; i++) { + let event = events[i] - for (let i = 0; i < events.length; i++) { - let event = events[i] + let postDiv = document.createElement("div") + postDiv.setAttribute("class", "post-div") + let textNode - let postDiv = document.createElement("div") - postDiv.setAttribute("class", "post-div") - let textNode = document.createTextNode('POST #' + i) - postDiv.appendChild(textNode) - contentDiv.appendChild(postDiv) + switch (event.eventType) { + case SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST: { + textNode = document.createTextNode( event.emitterUserProfile.userProfileHandle + " Posted ") + break + } + case SA.projects.socialTrading.globals.eventTypes.FOLLOW_USER_PROFILE: { + textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " Followed " + event.targetUserProfile.userProfileHandle) + break + } + case SA.projects.socialTrading.globals.eventTypes.UNFOLLOW_USER_PROFILE: { + textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " Unfollowed " + event.targetUserProfile.userProfileHandle) + break + } + } + + postDiv.appendChild(textNode) + contentDiv.appendChild(postDiv) + } + } + catch (err) { + console.log('[ERROR] err.stack = ' + err.stack) } } } diff --git a/Desktop/WebServer/index.html b/Desktop/WebServer/index.html index 0f0c0a561..9b57e56e0 100644 --- a/Desktop/WebServer/index.html +++ b/Desktop/WebServer/index.html @@ -119,11 +119,11 @@ > -
-
+
+
Placeholder
-
+
diff --git a/Projects/Social-Trading/DK/modules/WebAppInterface.js b/Projects/Social-Trading/DK/modules/WebAppInterface.js index 8c154ef2e..64ae992cc 100644 --- a/Projects/Social-Trading/DK/modules/WebAppInterface.js +++ b/Projects/Social-Trading/DK/modules/WebAppInterface.js @@ -47,7 +47,7 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule queryMessage.emitterUserProfileId = DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID messageHeader.queryMessage = JSON.stringify(queryMessage) - // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Message Received', JSON.stringify(queryMessage)) + // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Message Received', JSON.stringify(queryMessage)) let response = { result: 'Ok', @@ -55,7 +55,7 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule data: await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) } - // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Respose Sent', JSON.stringify(response)) + // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Respose Sent', JSON.stringify(response)) return response } From 8757d26487d048d2cd10d592bb82ca0c24fee6fd Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 18 Sep 2021 13:09:10 +0200 Subject: [PATCH 6/6] Post Text fetched from github and shown at the timeline. --- Desktop/UI/WebApp.js | 14 ++-- DesktopRoot.js | 3 +- .../DK/modules/WebAppInterface.js | 77 ++++++++++++++++--- 3 files changed, 74 insertions(+), 20 deletions(-) diff --git a/Desktop/UI/WebApp.js b/Desktop/UI/WebApp.js index 301757206..21af37e22 100644 --- a/Desktop/UI/WebApp.js +++ b/Desktop/UI/WebApp.js @@ -139,23 +139,23 @@ function newWebApp() { let postDiv = document.createElement("div") postDiv.setAttribute("class", "post-div") - let textNode + let textNode switch (event.eventType) { case SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST: { - textNode = document.createTextNode( event.emitterUserProfile.userProfileHandle + " Posted ") + textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " POSTED " + event.postText) break } case SA.projects.socialTrading.globals.eventTypes.FOLLOW_USER_PROFILE: { - textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " Followed " + event.targetUserProfile.userProfileHandle) + textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " FOLLOWED " + event.targetUserProfile.userProfileHandle) break } case SA.projects.socialTrading.globals.eventTypes.UNFOLLOW_USER_PROFILE: { - textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " Unfollowed " + event.targetUserProfile.userProfileHandle) + textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " UNFOLLOWED " + event.targetUserProfile.userProfileHandle) break } - } - + } + postDiv.appendChild(textNode) contentDiv.appendChild(postDiv) } @@ -384,7 +384,7 @@ function newWebApp() { eventMessage = { eventType: SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST, eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(), - text: postText, + postText: postText, timestamp: (new Date()).valueOf() } diff --git a/DesktopRoot.js b/DesktopRoot.js index 7ab83062a..87b9e57f9 100644 --- a/DesktopRoot.js +++ b/DesktopRoot.js @@ -48,7 +48,8 @@ exports.newDesktopRoot = function newDesktopRoot() { open: require('open'), http: require('http'), octokit: require("@octokit/rest"), - simpleGit: require('simple-git') + simpleGit: require('simple-git'), + nodeFetch: require('node-fetch') } /* diff --git a/Projects/Social-Trading/DK/modules/WebAppInterface.js b/Projects/Social-Trading/DK/modules/WebAppInterface.js index 64ae992cc..609cb33c5 100644 --- a/Projects/Social-Trading/DK/modules/WebAppInterface.js +++ b/Projects/Social-Trading/DK/modules/WebAppInterface.js @@ -47,15 +47,34 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule queryMessage.emitterUserProfileId = DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID messageHeader.queryMessage = JSON.stringify(queryMessage) - // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Message Received', JSON.stringify(queryMessage)) + let response - let response = { - result: 'Ok', - message: 'Web App Interface Query Processed.', - data: await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) + // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Message Received', JSON.stringify(queryMessage)) + + if (queryMessage.queryType !== SA.projects.socialTrading.globals.queryTypes.EVENTS) { + response = { + result: 'Ok', + message: 'Web App Interface Query Processed.', + data: await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) + } + } else { + + let events = await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) + for (let i = 0; i < events.length; i++) { + let event = events[i] + if (event.eventType === SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST) { + event.postText = await getPostText(event.emitterUserProfile.userProfileHandle, event.emitterPost.emitterPostHash, event.timestamp) + } + } + + response = { + result: 'Ok', + message: 'Web App Interface Query Processed.', + data: events + } } - // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Respose Sent', JSON.stringify(response)) + // console.log((new Date()).toISOString(), '- Web App Interface', '- Query Respose Sent', JSON.stringify(response)) return response } @@ -100,8 +119,8 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule and a hash of the content was generated, and that is what is going to the Network Node. */ - eventMessage.emitterPostHash = await savePostAtStorage(eventMessage.text, commitMessage, eventMessage.timestamp) - eventMessage.text = undefined + eventMessage.emitterPostHash = await savePostAtStorage(eventMessage.postText, commitMessage, eventMessage.timestamp) + eventMessage.postText = undefined } eventMessage.emitterUserProfileId = DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID @@ -124,19 +143,18 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule } } - async function savePostAtStorage(text, commitMessage, timestamp) { + async function savePostAtStorage(postText, commitMessage, timestamp) { const { createHash } = await import('crypto') const hash = createHash('sha256') let post = { timestamp: timestamp, - text: text + postText: postText } const fileContent = JSON.stringify(post, undefined, 4) const fileHash = hash.update(fileContent).digest('hex') const fileName = fileHash + ".json" - - let filePath = './My-Social-Trading-Data/User-Posts/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDate(timestamp) + const filePath = './My-Social-Trading-Data/User-Posts/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDate(timestamp) SA.projects.foundations.utilities.filesAndDirectories.mkDirByPathSync(filePath + '/') SA.nodeModules.fs.writeFileSync(filePath + '/' + fileName, fileContent) @@ -154,4 +172,39 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule return fileHash } + + async function getPostText(userProfileHandle, postHash, timestamp) { + let promise = new Promise((resolve, reject) => { +//https://raw.githubusercontent.com/Luis-Fernando-Molina/My-Social-Trading-Data/main/User-Posts/2021/09/17/0608070d8d2aab12e63cf3aa871918a04e237edff4fc0f886d158311811cf7c9.json + + const userProfileHandle = "Luis-Fernando-Molina" /// TODO Delete this line + + const fileName = postHash + ".json" + const filePath = 'My-Social-Trading-Data/main/User-Posts/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDate(timestamp) + + const fetch = SA.nodeModules.nodeFetch + let url = 'https://raw.githubusercontent.com/' + userProfileHandle + '/' + filePath + '/' + fileName + + fetch(url) + .then((response) => { + + if (response.status != 200) { + reject('Github.com responded with status ' + response.status) + return + } + + response.text().then(body => { + post = JSON.parse(body) + resolve(post.postText) + }) + }) + .catch(err => { + resolve('Post Text could not be fetched. ' + err.message) + }) + + } + ) + + return promise + } } \ No newline at end of file
Profile Pic