From 89f8e0574fed178967b6bfc0d97d57da81e12aa2 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 20 Feb 2018 10:13:10 +0100 Subject: [PATCH] Optimization --- AAWebPlatform.njsproj | 3 + ChartUtilities.js | 2 + FileCache.js | 25 ++++--- FileCursor.js | 155 ++++++++++++++++++++++++++++++++++++++++++ index.html | 1 + 5 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 FileCursor.js diff --git a/AAWebPlatform.njsproj b/AAWebPlatform.njsproj index f8c383642..bd33a2d0c 100644 --- a/AAWebPlatform.njsproj +++ b/AAWebPlatform.njsproj @@ -42,6 +42,9 @@ Code + + Code + Code diff --git a/ChartUtilities.js b/ChartUtilities.js index 521e6ecb5..48496e01e 100644 --- a/ChartUtilities.js +++ b/ChartUtilities.js @@ -2,6 +2,8 @@ function getSideDays(timePeriod) { + let daysOnSides; + if (timePeriod < _1_HOUR_IN_MILISECONDS) { daysOnSides = 1; } diff --git a/FileCache.js b/FileCache.js index f1a1078e1..07c7a3013 100644 --- a/FileCache.js +++ b/FileCache.js @@ -33,12 +33,16 @@ function newFileCache() { let fileCache = { getMarketFile: getMarketFile, getFileCursor: getFileCursor, + setDatetime: setDatetime, initialize: initialize } let fileCloud; let marketFiles = new Map; + let fileCursors = new Map; + + let datetime; return fileCache; @@ -71,8 +75,6 @@ function newFileCache() { fileCloud = newFileCloud(); fileCloud.initialize(pTeamCodeName, pBotCodeName); - let filesReceived = 0; - for (i = 0; i < marketFilesPeriods.length; i++) { let periodTime = marketFilesPeriods[i][0]; @@ -86,13 +88,8 @@ function newFileCache() { marketFiles.set(periodTime, file); - filesReceived++; + callBackFunction(); // Note that the call back is called for every file loaded. - if (filesReceived === layer.validPeriods.length) { - - callBackFunction(); - - } } } } @@ -106,10 +103,16 @@ function newFileCache() { } - function getFileCursor(pPeriod, pDatetime) { - - + function getFileCursor(pPeriod) { + return fileCursors.get(pPeriod); } + + function setDatetime(newDatetime) { + + datetime = newDatetime; + + } + } \ No newline at end of file diff --git a/FileCursor.js b/FileCursor.js new file mode 100644 index 000000000..5f6d3244d --- /dev/null +++ b/FileCursor.js @@ -0,0 +1,155 @@ + +function newFileCursor() { + + + + let dailyFiles = new Map; + let cursorDate = new Date(INITIAL_DATE); + + let fileCursor = { + setDatetime: setDatetime, + dailyFiles: undefined, + initialize: initialize + } + + fileCursor.dailyFiles = dailyFiles; + + let market; + let exchange; + let cursorSize = 3; + + let server = newFileServer(); + + return fileCursor; + + function initialize(pExchange, pMarket, callBackFunction) { + + market = pMarket; + exchange = pExchange; + + server.initialize(); + + getFiles(callBackFunction); + + } + + function setDatetime(datetime) { + + if (datetime === undefined) { return; } + + cursorDate = datetime; + + getFiles(); + + collectGarbage(); + } + + function getFiles(callBackFunction) { + + let i = 0; + let j = 0; + + let dateString; + + getNextFile(); + + function getNextFile() { + + let targetDate = new Date(cursorDate); + targetDate.setDate(targetDate.getDate() + j); + + if (j === 0) { j++; } + else { + if (j < 0) { + j = -j; + j++; + } + else { + j = -j; + } + } + + dateString = targetDate.getUTCFullYear() + '-' + pad(targetDate.getUTCMonth() + 1, 2) + '-' + pad(targetDate.getUTCDate(), 2); + + let currentDay = Math.trunc((new Date()).valueOf() / (24 * 60 * 60 * 1000)); + let targetDay = Math.trunc(targetDate.valueOf() / (24 * 60 * 60 * 1000)); + + if (targetDay > currentDay) { + + controlLoop(); + + } else { + + if (fileCursor.dailyFiles.get(dateString) === undefined) { + + server.getDailyFile(exchange, market, targetDate, newDailyFilesReady) + + } else { + + controlLoop(); + } + + } + + } + + function newDailyFilesReady(dailyFileReceived) { + + fileCursor.dailyFiles.set(dateString, dailyFileReceived); + + controlLoop(); + + } + + + function controlLoop() { + + if (j === 3) { // We call the callBack after we have the first few files. + + if (callBackFunction !== undefined) { + + callBackFunction(); + + if (market === INITIAL_DEFAULT_MARKET) { // This is when we dont need a splash screen anymore! + + splashScreenNeeded = false; + + } + + } + + } + + i++; + + if (i < cursorSize) { + + getNextFile(); + + } + + } + } + + function collectGarbage() { + + date = removeTime(cursorDate); + + let minDate = date.valueOf() - cursorSize / 2 * 24 * 60 * 60 * 1000; + let maxDate = date.valueOf() + cursorSize / 2 * 24 * 60 * 60 * 1000; + + for (let key of fileCursor.dailyFiles.keys()) { + + let keyDate = new Date(key); + + if (keyDate.valueOf() < minDate || keyDate.valueOf() > maxDate) { + + fileCursor.dailyFiles.delete(key); + + } + + } + + } + +} \ No newline at end of file diff --git a/index.html b/index.html index ee4997093..5cf79f66f 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,7 @@ +