From 44e0d7137794a7ee1e2ea964e53bc34707814693 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 19 Mar 2018 13:03:54 +0100 Subject: [PATCH] Status Report able to verify all the market was completed --- .vs/AACloud/v15/.suo | Bin 83456 -> 86016 bytes Context.js | 8 +- Datasource.js | 3 - Status Report.js | 262 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 261 insertions(+), 12 deletions(-) diff --git a/.vs/AACloud/v15/.suo b/.vs/AACloud/v15/.suo index 756c698bdd9fee132496ec170228e84a30d6092c..3f51af9d3d903a0ef45e21b450e8a4a4044b1f6f 100644 GIT binary patch delta 4885 zcmeHK3s93+7XHtl1QLigL0&3I0EN^iDistElqjeup+!Zl81cFFU4tUNkgct{sF;KP zXnm&E?R2&~mfsH6>KMhHRae(q-0j2d3QnhMoz_>QD?Z4clSidmt#+n6vom{VzMOl{ zJ^wlP-t)LOWfiKjovO;zkN`mt6oh7%%SA*0g6{piy8t3ai&2`nv*PU~hwEVw7Kj&QKrB%#Sw5KKLmV&3Or=Cg-)5mcV}<*owiyhP|rFngFT_Am>xhncqY zFpnShFfaCD53}4r@Auqi4>L6oWe;{GD`mwYHE6KpyATa0RSnW74KQF!*Fu277{+7TL{&jm^pU)K7=8+cQ@97?dbO|F^) zo3qTD(*CeezS$;F#DtAXSsAkmy7Dfjkib;JGy<=egZvf33X*c#~QRg22=d% z`Bd2u-T*b7>p%m}OWLObj;v1Jjyf zR`YRBY{3j{NBjI8F_?Guc3U~zaET7AO2zBYyq`A>YKq_b@^m|KM=Z8br%|rlVD{W0lx6{-g#t zw7JgsOrf*TjK{(f(7G_6eHX|%b(ug7T`!q1bIx$A%_Oe^GNS)wP%koJE5LBK?qf-sm=RP~sIuO-#kJyWQB* z?Q2M#uLv=}oW@*)xNi%s!_3e$TUZJ7#6@94c}J=unhchkQv8)q3Y3}0m2o8K+n4N| zsRb-+8zaYbhyy+~v__FrCXozBs!vW8OKy5oZklxxur8`s08MckQj=Xnz!Q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}{=a_rjc|TXEGHJKkCvgsEpoV9~w-cx`_*X6{*qC41N4mfbd7aG)*t z?$K z{TV`x9CY*pFw{O42Q}=X@iIC3>{#I$YMKDyVjlkHTqdUO&ccuOq~omf5-x6xwAnkr zc*oTS_>Z}ZLAdnS6n59!*fGA=@^#nGBlI0(UCVH6xMzenaboKr98?;L-`r1HV}T03 zn<@#TQAre-?)428FoDPl7xP-1$sg3XVoW-z?T>Jy45m{BjKWb>!6X4R++G4((6Bxe zAB-%)wzvIJePc9Uza539JM&R}Cku{Z^Sz~bp)?tH#6|ckm*w@m!S=uc9`*F(RV9gn{wQu%1)U7Fc|&+k%2|6 zl^A+^ASSo=0=wIo{3*0P1kbnr953A4NXCqQ+JE_*`&I!~(K*?;xN;=UHlWe5|M8ltyy^+QW+hsqffMRxV2%eZ4Nd^y~2#>QJRznm_@EmXQU6vGlImb|VXnJevu>=7P2D&ZMGJDixPaGS*w^-VBPKTNy#v zb)7#|na1{QU)TzTN`>t*L9p%1eq;l^xay<&!(~C5ve0m7>)&*-1De}izgfNNl|?NY z<(j!^%38y@V81UNS@-9hol<;mb$uV@CXVB#CI-dllv&SrxLG)-@N{D5KI@aQ?$49- PtHIJImHrm?e getExecutionHistory -> Entering function."); } - let fileName = "Execution.History.json" + let fileName = "Execution.History.json"; let filePath = bot.filePathRoot + "/Output/" + bot.process; if (FULL_LOG === true) { logger.write("[INFO] initialize -> getExecutionHistory -> fileName = " + fileName); } @@ -237,7 +237,7 @@ let date = new Date(thisObject.statusReport.lastExecution); - let fileName = "Execution.Context.json" + let fileName = "Execution.Context.json"; let dateForPath = date.getUTCFullYear() + '/' + utilities.pad(date.getUTCMonth() + 1, 2) + '/' + utilities.pad(date.getUTCDate(), 2) + '/' + utilities.pad(date.getUTCHours(), 2) + '/' + utilities.pad(date.getUTCMinutes(), 2); let filePath = bot.filePathRoot + "/Output/" + bot.process + '/' + dateForPath; @@ -397,7 +397,7 @@ if (FULL_LOG === true) { logger.write("[INFO] saveThemAll -> writeExecutionContext -> Entering function."); } - let fileName = "Execution.Context.json" + let fileName = "Execution.Context.json"; let dateForPath = bot.processDatetime.getUTCFullYear() + '/' + utilities.pad(bot.processDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(bot.processDatetime.getUTCDate(), 2) + '/' + utilities.pad(bot.processDatetime.getUTCHours(), 2) + '/' + utilities.pad(bot.processDatetime.getUTCMinutes(), 2); let filePath = bot.filePathRoot + "/Output/" + bot.process + '/' + dateForPath; @@ -458,7 +458,7 @@ if (FULL_LOG === true) { logger.write("[INFO] saveThemAll -> writeExucutionHistory -> Entering function."); } - let fileName = "Execution.History.json" + let fileName = "Execution.History.json"; let filePath = bot.filePathRoot + "/Output/" + bot.process; if (FULL_LOG === true) { logger.write("[INFO] saveThemAll -> writeExucutionHistory -> fileName = " + fileName); } diff --git a/Datasource.js b/Datasource.js index 54c6993c7..b03af1daa 100644 --- a/Datasource.js +++ b/Datasource.js @@ -82,19 +82,16 @@ callBackFunction(global.DEFAULT_OK_RESPONSE); return; } - break; case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time. logger.write("[ERROR] initialize -> onDone -> Retry Later. Requesting Execution Retry."); callBackFunction(err); return; } - break; case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. logger.write("[ERROR] initialize -> onDone -> Operation Failed. Aborting the process."); callBackFunction(err); return; } - break; } } catch (err) { diff --git a/Status Report.js b/Status Report.js index 5e1654300..b9f713d65 100644 --- a/Status Report.js +++ b/Status Report.js @@ -17,7 +17,8 @@ file: undefined, // Here we have the JSON object representing the file content. initialize: initialize, load: load, - save: save + save: save, + verifyMarketComplete: verifyMarketComplete }; let bot = BOT; @@ -93,7 +94,7 @@ if (FULL_LOG === true) { logger.write("[INFO] initialize -> load -> Entering function."); } let rootPath = owner.devTeam + "/" + owner.bot + "." + owner.botVersion.major + "." + owner.botVersion.minor + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + owner.dataSetVersion; - let fileName = "Status.Report.json" + let fileName = "Status.Report.json"; let filePath = rootPath + "/Reports/" + owner.process + timePath; if (FULL_LOG === true) { logger.write("[INFO] initialize -> load -> fileName = " + fileName); } @@ -116,7 +117,7 @@ let customOK = { result: global.CUSTOM_OK_RESPONSE.result, message: "Status Report was never created." - } + }; logger.write("[WARN] initialize -> load -> onFileReceived -> customOK = " + customOK.message); callBackFunction(customOK); return; @@ -169,13 +170,13 @@ let customErr = { result: global.CUSTOM_FAIL_RESPONSE.result, message: "Only bots owners of a Status Report can save them." - } + }; logger.write("[ERROR] save -> customErr = " + customErr.message); callBackFunction(customErr); return; } - let fileName = "Status.Report.json" + let fileName = "Status.Report.json"; let filePath = bot.filePathRoot + "/Reports/" + owner.process + timePath; if (FULL_LOG === true) { logger.write("[INFO] save -> fileName = " + fileName); } @@ -228,4 +229,255 @@ callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } + + function verifyMarketComplete(callBackFunction) { + + /* + The mission of this function is to update the main status report on a scheme where there are monthly sub reports. + This report contains the date of the last file sucessfully checked but in a consecutive way. + + For example: if the market starts in March, and March, April and June are checked, then the file will be the last of June even if September is also checked. + + IMPORTANT NOTE: Once executed, this function will leave thisObject in an inconsistant state, so you need to re-initialize it if you wish to use it again. + + */ + + try { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> Entering function."); } + + let initialYear; + let initialMonth; + + let finalYear = (new Date()).getUTCFullYear(); + let finalMonth = (new Date()).getUTCMonth() + 1; + + /* Lets load again this main status report as it might have changed since its initialization and first load. */ + + load(onLoad); + + function onLoad(err) { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> onLoad -> Entering function."); } + + if ( + err.result === global.CUSTOM_FAIL_RESPONSE.result && + (err.message === 'Status Report was never created.') + ) { + logger.write("[INFO] verifyMarketComplete -> onLoad -> err = " + err.message); + + /* The first month of the market didnt create the main report yet. Aborting verification. */ + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> onLoad -> Verification ABORTED since the main status report does not exist."); } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write("[ERROR] verifyMarketComplete -> onLoad -> err = " + err.message); + callBackFunction(err); + return; + } + + /* Here we get the initial month and year from where our verification process will start. */ + + initialYear = thisObject.file.lastFile.year; + initialMonth = thisObject.file.lastFile.month; + + loopCycle(); + + } catch (err) { + logger.write("[ERROR] verifyMarketComplete -> onLoad -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + function loopCycle() { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> loopCycle -> Entering function."); } + + /* Here we read the status report file of each month / year to verify if it is complete or not. */ + + let paddedInitialMonth = utilities.pad(initialMonth, 2); + + timePath = "/" + initialYear + "/" + paddedInitialMonth; + + load(onLoad); + + function onLoad(err) { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> loopCycle -> onLoad -> Entering function."); } + + if ( + err.result === global.CUSTOM_FAIL_RESPONSE.result && + (err.message === 'Status Report was never created.') + ) { + logger.write("[INFO] verifyMarketComplete -> loopCycle -> onLoad -> err = " + err.message); + + /* The first month of the market didnt create the main report yet. Aborting verification. */ + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> loopCycle -> onLoad -> Verification ABORTED since the status report for year " + initialYear + " and month " + initialMonth + " did not exist. "); } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write("[ERROR] verifyMarketComplete -> loopCycle -> onLoad -> err = " + err.message); + callBackFunction(err); + return; + } + + if (thisObject.file.monthChecked === true) { + + readAndWriteNewReport(JSON.stringify(thisObject.file)); + + } else { + + /* If any of the files says that month is not checked then it is enough to know the market continuity is broken. */ + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> loopCycle -> onLoad -> Verification ABORTED since the status report for year " + initialYear + " and month " + initialMonth + " is not marked as complete."); } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + } catch (err) { + logger.write("[ERROR] verifyMarketComplete -> loopCycle -> onLoad -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + } catch (err) { + logger.write("[ERROR] verifyMarketComplete -> loopCycle -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + function readAndWriteNewReport(monthlyStatusReport) { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> readAndWriteNewReport -> Entering function."); } + + /* We will read the current file to preserve its data, and save it again with the new lastFile */ + + monthlyStatusReport = JSON.parse(monthlyStatusReport); + + timePath = ""; + + load(onLoad); + + function onLoad(err) { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> readAndWriteNewReport -> onLoad -> Entering function."); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write("[ERROR] verifyMarketComplete -> readAndWriteNewReport -> onLoad -> err = " + err.message); + callBackFunction(err); + return; + } + + if (monthlyStatusReport.lastTrade.id > thisObject.file.lastTrade.id) { + + thisObject.file.lastFile = monthlyStatusReport.lastFile; + thisObject.file.lastTrade = monthlyStatusReport.lastTrade; + thisObject.file.lastTrade.counter = undefined; + + save(onSave); + + function onSave(err) { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> readAndWriteNewReport -> onLoad -> onSave -> Entering function."); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + logger.write("[ERROR] verifyMarketComplete -> readAndWriteNewReport -> onLoad -> onSave -> err = " + err.message); + callBackFunction(err); + return; + } + + loop(); // Lets see the next month. + + } catch (err) { + logger.write("[ERROR] verifyMarketComplete -> readAndWriteNewReport -> onLoad -> onSave -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + } else { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> readAndWriteNewReport -> onLoad -> Main Status Report Not Updated."); } + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> readAndWriteNewReport -> onLoad -> Current Trade Id (" + monthlyStatusReport.lastTrade.id + ") is <= than Id at main status report file. (" + statusReport.lastTrade.id + ")"); } + + loop(); // Lets see the next month. + } + + } catch (err) { + logger.write("[ERROR] verifyMarketComplete -> readAndWriteNewReport -> onLoad -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + } catch (err) { + logger.write("[ERROR] verifyMarketComplete -> loopCycle -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + + function loop() { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> loop -> Entering function."); } + + initialMonth++; + + if (initialMonth > 12) { + + initialMonth = 1; + initialYear++; + + } + + if ((initialYear === finalYear && initialMonth > finalMonth) || (initialYear > finalYear)) { + + /* We arrived to the point where we have checked all the status reports of every month and they are all complete. */ + + if (FULL_LOG === true) { logger.write("[INFO] verifyMarketComplete -> loop -> Verification Finished. Success. :-)"); } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + loopCycle(); + + } catch (err) { + logger.write("[ERROR] verifyMarketComplete -> loop -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } + } + catch (err) + { + logger.write("[ERROR] verifyMarketComplete -> err = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + } }; \ No newline at end of file