From d947e34cb50f07cb4a98f8f821029a6ca616598e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 30 Apr 2018 12:54:44 +0200 Subject: [PATCH] FileCursor Error Handling --- .vs/AAWeb/v15/.suo | Bin 67584 -> 67584 bytes Files/FileCursor.js | 628 +++++++++++++++++++++++++------------------- server.js | 2 +- 3 files changed, 352 insertions(+), 278 deletions(-) diff --git a/.vs/AAWeb/v15/.suo b/.vs/AAWeb/v15/.suo index ecb8b28dc0adc46e55ff124c1e0d0a4d71bf6d5a..363efc75ff16ab803f2f4e6533b775d65c2e69c7 100644 GIT binary patch delta 396 zcmZpez|t^*WkU`Vt3$ucoWjk8Ouv~~3(nOZ&fi?f#%{q_zPZxwzdVSFYggnh7h_-$ zW?*38X6T=+xHNw9yG4eRxu&K~I%W!{_VZ4bYSfs#aF)Sl#-{&*lM5TIC+8nGo_u%y zoXJj&X`2hC8nQAXblPn`x2S@NXhY}1%$Zy;b;9Jv*;<>;7M)_Aw1;Q%!<9*swbp8H z?p;>QJ~@A>;%41-ml-E7Sj00qaEupy1-r8cV_dB%vOYyz90B2YU7C>=NqWTb3n tJl@2}%D}+Pz_7XIL<{5Ohf_2sn;zSZRV6p$qBBgBc}|qWG@NU(1OPRzm_`5q delta 394 zcmZpez|t^*WkU`V>w|?3k8(E`GW}*|O~_-~2xPLcTQD|nuC)6v52E7Q6}g(l7#M^Z z7#O%2CSROCck=9pw8?WCO(%0rO`H7TguvwW=Ts->O;KLdBs6*Al*-9Wvl1rTFXo*5 zdXWZ5>E?o|hOA(HMzaqss$e3{v^nU;OfHx@Ve*ODW}CGZonoH+aAneDt+m>ldzTfn zPu@61cJlqD0+U5fIB!l_H-T|-;2QDCbCzptwx5y21hRRv(dH>klaDRdn;ba9Z?eK# zuE_~&IKV1uwpB1sW}IohS$o$>=E)P+NKKx Entering function."); } - if (INFO_LOG === true) { logger.write("[INFO] initialize -> key = " + pDevTeam.codeName + "-" + pBot.codeName + "-" + pSet.codeName + "-" + pPeriodName); } + try { - market = pMarket; - exchange = pExchange; - fileCloud = pFileCloud; - devTeam = pDevTeam; - bot = pBot; - thisSet = pSet; - periodName = pPeriodName; - cursorDate = pCursorDate; - timePeriod = pTimePeriod; + if (INFO_LOG === true) { logger.write("[INFO] initialize -> Entering function."); } + if (INFO_LOG === true) { logger.write("[INFO] initialize -> key = " + pDevTeam.codeName + "-" + pBot.codeName + "-" + pSet.codeName + "-" + pPeriodName); } - setTimePeriod(pCurrentTimePeriod, pCursorDate, callBackFunction); + market = pMarket; + exchange = pExchange; + fileCloud = pFileCloud; + devTeam = pDevTeam; + bot = pBot; + thisSet = pSet; + periodName = pPeriodName; + cursorDate = pCursorDate; + timePeriod = pTimePeriod; + setTimePeriod(pCurrentTimePeriod, pCursorDate, callBackFunction); + + } catch (err) { + + if (ERROR_LOG === true) { logger.write("[ERROR] initialize -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); + } } function setTimePeriod(pTimePeriod, pDatetime, callBackFunction) { - if (INFO_LOG === true) { logger.write("[INFO] setTimePeriod -> Entering function."); } + try { - /* + if (INFO_LOG === true) { logger.write("[INFO] setTimePeriod -> Entering function."); } - We are implementing here an algorithm designed to save bandwidth, memory and processing power at the browser. - We say there is a saving mode where the cursor is running at a minimum size. When the end user aproaches the time period the cursor - is set, then it should exit the saving mode and go to its actual size. + /* + + We are implementing here an algorithm designed to save bandwidth, memory and processing power at the browser. + We say there is a saving mode where the cursor is running at a minimum size. When the end user aproaches the time period the cursor + is set, then it should exit the saving mode and go to its actual size. + + To do this we are going to measure the distance from the Time Period received to the one the cursors was initialized with. + If these periods are consecutive, it means that the cursor should exit saving mode and load its full size. + + */ - To do this we are going to measure the distance from the Time Period received to the one the cursors was initialized with. - If these periods are consecutive, it means that the cursor should exit saving mode and load its full size. + cursorDate = pDatetime; // Adjust the cursor date to the one received. - */ + let positionA; - cursorDate = pDatetime; // Adjust the cursor date to the one received. + for (let i = 0; i < dailyFilePeriods.length; i++) { - let positionA; + let period = dailyFilePeriods[i]; - for (let i = 0; i < dailyFilePeriods.length; i++) { + if (period[0] === pTimePeriod) { - let period = dailyFilePeriods[i]; + positionA = i; - if (period[0] === pTimePeriod) { + } - positionA = i; + if (period[0] === timePeriod) { + + positionB = i; + + } + } + + if (Math.abs(positionB - positionA) <= 1) { + + exitSavingMode(); + //console.log("File Cursor with period " + periodName + " EXITED saving mode after being notified the user is at period " + convertTimePeriodToName(pTimePeriod)); + + } else { + + enterSavingMode(); + //console.log("File Cursor with period " + periodName + " ENTERED saving mode after being notified the user is at period " + convertTimePeriodToName(pTimePeriod)); } - if (period[0] === timePeriod) { + getFiles(callBackFunction); - positionB = i; + collectGarbage(callBackFunction); + function enterSavingMode() { + + try { + + if (INFO_LOG === true) { logger.write("[INFO] setTimePeriod -> enterSavingMode -> Entering function."); } + + switch (timePeriod) { + + case _45_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 30; + } + break; + case _40_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 20; + } + break; + case _30_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + case _20_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + case _15_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + case _10_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + case _5_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + case _4_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + case _3_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + case _2_MINUTES_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + case _1_MINUTE_IN_MILISECONDS: + { + minCursorSize = 3; + maxCursorSize = 15; + } + break; + default: + } + + } catch (err) { + + if (ERROR_LOG === true) { logger.write("[ERROR] setTimePeriod -> enterSavingMode -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); + } } - } - if (Math.abs(positionB - positionA) <= 1) { + function exitSavingMode() { - exitSavingMode(); - //console.log("File Cursor with period " + periodName + " EXITED saving mode after being notified the user is at period " + convertTimePeriodToName(pTimePeriod)); + try { - } else { + if (INFO_LOG === true) { logger.write("[INFO] setTimePeriod -> exitSavingMode -> Entering function."); } - enterSavingMode(); - //console.log("File Cursor with period " + periodName + " ENTERED saving mode after being notified the user is at period " + convertTimePeriodToName(pTimePeriod)); + switch (timePeriod) { - } + case _45_MINUTES_IN_MILISECONDS: + { + minCursorSize = 15; + maxCursorSize = 30; + } + break; + case _40_MINUTES_IN_MILISECONDS: + { + minCursorSize = 13; + maxCursorSize = 20; + } + break; + case _30_MINUTES_IN_MILISECONDS: + { + minCursorSize = 11; + maxCursorSize = 15; + } + break; + case _20_MINUTES_IN_MILISECONDS: + { + minCursorSize = 9; + maxCursorSize = 15; + } + break; + case _15_MINUTES_IN_MILISECONDS: + { + minCursorSize = 7; + maxCursorSize = 15; + } + break; + case _10_MINUTES_IN_MILISECONDS: + { + minCursorSize = 7; + maxCursorSize = 15; + } + break; + case _5_MINUTES_IN_MILISECONDS: + { + minCursorSize = 5; + maxCursorSize = 15; + } + break; + case _4_MINUTES_IN_MILISECONDS: + { + minCursorSize = 5; + maxCursorSize = 15; + } + break; + case _3_MINUTES_IN_MILISECONDS: + { + minCursorSize = 5; + maxCursorSize = 15; + } + break; + case _2_MINUTES_IN_MILISECONDS: + { + minCursorSize = 5; + maxCursorSize = 15; + } + break; + case _1_MINUTE_IN_MILISECONDS: + { + minCursorSize = 5; + maxCursorSize = 15; + } + break; + default: + } - getFiles(callBackFunction); + } catch (err) { - collectGarbage(); - - function enterSavingMode() { - - if (INFO_LOG === true) { logger.write("[INFO] setTimePeriod -> enterSavingMode -> Entering function."); } - - switch (timePeriod) { - - case _45_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 30; - } - break; - case _40_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 20; - } - break; - case _30_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - case _20_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - case _15_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - case _10_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - case _5_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - case _4_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - case _3_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - case _2_MINUTES_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - case _1_MINUTE_IN_MILISECONDS: - { - minCursorSize = 3; - maxCursorSize = 15; - } - break; - default: + if (ERROR_LOG === true) { logger.write("[ERROR] setTimePeriod -> exitSavingMode -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); + } } - } - function exitSavingMode() { + } catch (err) { - if (INFO_LOG === true) { logger.write("[INFO] setTimePeriod -> exitSavingMode -> Entering function."); } - - switch (timePeriod) { - - case _45_MINUTES_IN_MILISECONDS: - { - minCursorSize = 15; - maxCursorSize = 30; - } - break; - case _40_MINUTES_IN_MILISECONDS: - { - minCursorSize = 13; - maxCursorSize = 20; - } - break; - case _30_MINUTES_IN_MILISECONDS: - { - minCursorSize = 11; - maxCursorSize = 15; - } - break; - case _20_MINUTES_IN_MILISECONDS: - { - minCursorSize = 9; - maxCursorSize = 15; - } - break; - case _15_MINUTES_IN_MILISECONDS: - { - minCursorSize = 7; - maxCursorSize = 15; - } - break; - case _10_MINUTES_IN_MILISECONDS: - { - minCursorSize = 7; - maxCursorSize = 15; - } - break; - case _5_MINUTES_IN_MILISECONDS: - { - minCursorSize = 5; - maxCursorSize = 15; - } - break; - case _4_MINUTES_IN_MILISECONDS: - { - minCursorSize = 5; - maxCursorSize = 15; - } - break; - case _3_MINUTES_IN_MILISECONDS: - { - minCursorSize = 5; - maxCursorSize = 15; - } - break; - case _2_MINUTES_IN_MILISECONDS: - { - minCursorSize = 5; - maxCursorSize = 15; - } - break; - case _1_MINUTE_IN_MILISECONDS: - { - minCursorSize = 5; - maxCursorSize = 15; - } - break; - default: - } + if (ERROR_LOG === true) { logger.write("[ERROR] setTimePeriod -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); } } @@ -262,153 +293,196 @@ function newFileCursor() { function setDatetime(pDatetime, callBackFunction) { - if (INFO_LOG === true) { logger.write("[INFO] setDatetime -> Entering function."); } + try { - if (pDatetime === undefined) { return; } + if (INFO_LOG === true) { logger.write("[INFO] setDatetime -> Entering function."); } - cursorDate = pDatetime; + if (pDatetime === undefined) { return; } - getFiles(callBackFunction); + cursorDate = pDatetime; - collectGarbage(); + getFiles(callBackFunction); + + collectGarbage(callBackFunction); + + } catch (err) { + + if (ERROR_LOG === true) { logger.write("[ERROR] setDatetime -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); + } } function getFiles(callBackFunction) { - if (INFO_LOG === true) { logger.write("[INFO] getFiles -> Entering function."); } + try { - let i = 0; - let j = 0; + if (INFO_LOG === true) { logger.write("[INFO] getFiles -> Entering function."); } - let dateString; + let i = 0; + let j = 0; - getNextFile(); + let dateString; - function getNextFile() { + getNextFile(); - if (INFO_LOG === true) { logger.write("[INFO] getFiles -> getNextFile -> Entering function."); } + function getNextFile() { - let targetDate = new Date(cursorDate); - targetDate.setUTCDate(targetDate.getUTCDate() + j); + try { - /* Small algorith to allow load first the current date, then alternate between the most forwad and the most backwards ones. */ - if (j === 0) { j++; } - else { - if (j < 0) { - j = -j; - j++; - } - else { - j = -j; + if (INFO_LOG === true) { logger.write("[INFO] getFiles -> getNextFile -> Entering function."); } + + let targetDate = new Date(cursorDate); + targetDate.setUTCDate(targetDate.getUTCDate() + j); + + /* Small algorith to allow load first the current date, then alternate between the most forwad and the most backwards ones. */ + 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 (thisObject.files.get(dateString) === undefined) { // We dont reload files we already have. + + fileCloud.getFile(devTeam, bot, thisSet, exchange, market, periodName, targetDate, undefined, onFileReceived); + + } else { + + controlLoop(); + } + + } + + } catch (err) { + + if (ERROR_LOG === true) { logger.write("[ERROR] getFiles -> getNextFile -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); } } - dateString = targetDate.getUTCFullYear() + '-' + pad(targetDate.getUTCMonth() + 1, 2) + '-' + pad(targetDate.getUTCDate(), 2); + function onFileReceived(err, file) { - let currentDay = Math.trunc((new Date()).valueOf() / (24 * 60 * 60 * 1000)); - let targetDay = Math.trunc(targetDate.valueOf() / (24 * 60 * 60 * 1000)); + try { - if (targetDay > currentDay) { + if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> Entering function."); } - controlLoop(); + switch (err.result) { + case GLOBAL.DEFAULT_OK_RESPONSE.result: { - } else { + if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> Received OK Response."); } + break; + } - if (thisObject.files.get(dateString) === undefined) { // We dont reload files we already have. + case GLOBAL.DEFAULT_FAIL_RESPONSE.result: { - fileCloud.getFile(devTeam, bot, thisSet, exchange, market, periodName, targetDate, undefined, onFileReceived); + if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> Received FAIL Response."); } + callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE); + return; + } - } else { + case GLOBAL.CUSTOM_FAIL_RESPONSE.result: { + + if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> Received CUSTOM FAIL Response."); } + if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> err.message = " + err.message); } + + callBackFunction(err); + return; + } + + default: { + + if (INFO_LOG === true) { logger.write("[INFO] initialize -> onFileReceived -> Received Unexpected Response."); } + callBackFunction(err); + return; + } + } + + thisObject.files.set(dateString, file); controlLoop(); - } - } + } catch (err) { - } - - function onFileReceived(err, file) { - - if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> Entering function."); } - - switch (err.result) { - case GLOBAL.DEFAULT_OK_RESPONSE.result: { - - if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> Received OK Response."); } - break; - } - - case GLOBAL.DEFAULT_FAIL_RESPONSE.result: { - - if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> Received FAIL Response."); } - callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE); - return; - } - - case GLOBAL.CUSTOM_FAIL_RESPONSE.result: { - - if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> Received CUSTOM FAIL Response."); } - if (INFO_LOG === true) { logger.write("[INFO] getFiles -> onFileReceived -> err.message = " + err.message); } - - callBackFunction(err); - return; - } - - default: { - - if (INFO_LOG === true) { logger.write("[INFO] initialize -> onFileReceived -> Received Unexpected Response."); } - callBackFunction(err); - return; + if (ERROR_LOG === true) { logger.write("[ERROR] getFiles -> onFileReceived -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); } } - thisObject.files.set(dateString, file); + function controlLoop() { - controlLoop(); + try { - } + if (INFO_LOG === true) { logger.write("[INFO] getFiles -> controlLoop -> Entering function."); } + if (callBackFunction !== undefined) { - function controlLoop() { + callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE); - if (INFO_LOG === true) { logger.write("[INFO] getFiles -> controlLoop -> Entering function."); } + } - if (callBackFunction !== undefined) { + i++; - callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE); + if (i < minCursorSize) { + getNextFile(); + + } + + } catch (err) { + + if (ERROR_LOG === true) { logger.write("[ERROR] getFiles -> controlLoop -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); + } } - i++; - - if (i < minCursorSize) { - - getNextFile(); - - } + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] getFiles -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); } } - function collectGarbage() { + function collectGarbage(callBackFunction) { - if (INFO_LOG === true) { logger.write("[INFO] collectGarbage -> Entering function."); } + try { - date = removeTime(cursorDate); + if (INFO_LOG === true) { logger.write("[INFO] collectGarbage -> Entering function."); } - let minDate = date.valueOf() - maxCursorSize * ONE_DAY_IN_MILISECONDS / 2; - let maxDate = date.valueOf() + maxCursorSize * ONE_DAY_IN_MILISECONDS / 2; + date = removeTime(cursorDate); - for (let key of thisObject.files.keys()) { + let minDate = date.valueOf() - maxCursorSize * ONE_DAY_IN_MILISECONDS / 2; + let maxDate = date.valueOf() + maxCursorSize * ONE_DAY_IN_MILISECONDS / 2; - let keyDate = new Date(key); + for (let key of thisObject.files.keys()) { - if (keyDate.valueOf() < minDate || keyDate.valueOf() > maxDate) { + let keyDate = new Date(key); - thisObject.files.delete(key); + if (keyDate.valueOf() < minDate || keyDate.valueOf() > maxDate) { + thisObject.files.delete(key); + + } } + } catch (err) { + + if (ERROR_LOG === true) { logger.write("[ERROR] collectGarbage -> err = " + err); } + callBackFunction(GLOBAL.CUSTOM_FAIL_RESPONSE); } } diff --git a/server.js b/server.js index 330817bd6..1cf8bb361 100644 --- a/server.js +++ b/server.js @@ -6,7 +6,7 @@ if (CONSOLE_LOG === true) { } -const DEBUG_MODE = true; // This forces the server to read Plotters from the local drive. +const DEBUG_MODE = false; // This forces the server to read Plotters from the local drive. if (CONSOLE_LOG === true && DEBUG_MODE === true) {