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