forked from arcojuana/AACloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusReport.js
417 lines (330 loc) · 21.5 KB
/
StatusReport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
exports.newStatusReport = function newStatusReport(BOT, logger, UTILITIES, PROCESS_OUTPUT) {
/*
A Status Report is a file that every bot reads at the begining of its execution and saves after it finishes its job.
The purpose of the file is to record checkpoint information of what was the last thing done by the bot and helpfull enough to start the next execution.
It usually does not include business related context data.
*/
const MODULE_NAME = "Status Report";
let bot = BOT;
let thisObject = {
networkNode: undefined,
mainUtility: undefined,
file: undefined, // Here we have the JSON object representing the file content.
initialize: initialize,
finalize: finalize,
load: load,
save: save,
asyncSave: asyncSave,
status: undefined
};
let statusDependencyNode;
/* Utilities needed. */
let utilities = UTILITIES.newCloudUtilities(logger);
/* Storage account to be used here. */
const FILE_STORAGE = require('./FileStorage.js');
let fileStorage
let sessionPath = ''
return thisObject;
function initialize(pStatusDependencyNode, callBackFunction) {
try {
statusDependencyNode = pStatusDependencyNode;
logger.fileName = MODULE_NAME + "." + statusDependencyNode.type + "." + statusDependencyNode.name + "." + statusDependencyNode.id;
/* Some very basic validations that we have all the information needed. */
if (statusDependencyNode.referenceParent === undefined) {
validationFailed(statusDependencyNode, "Status Dependency without Reference Parent.")
return
}
if (statusDependencyNode.referenceParent.parentNode === undefined) {
validationFailed(statusDependencyNode.referenceParent, "Status Report not attached to a Process Definition.")
return
}
if (statusDependencyNode.referenceParent.parentNode.config.codeName === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode, "Process Definition witn no codeName defined.")
return
}
if (statusDependencyNode.referenceParent.parentNode.parentNode === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode, "Process Definition not attached to a Bot.")
return
}
if (statusDependencyNode.referenceParent.parentNode.parentNode.config.codeName === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode, "Bot with no codeName defined.")
return
}
if (statusDependencyNode.referenceParent.parentNode.parentNode.parentNode === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode, "Bot not attached to a Data Mine.")
return
}
if (statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.config.codeName === undefined) {
validationFailed(statusDependencyNode.referenceParent.parentNode.parentNode.parentNode, "Data Mine witn no codeName defined.")
return
}
function validationFailed(errorInNode, errorMessage) {
let nodeString = JSON.stringify(errorInNode)
logger.write(MODULE_NAME, "[ERROR] initialize -> " + errorMessage + ' -> nodeString = ' + nodeString)
global.PROCESS_ERROR(bot.processKey, errorInNode, errorMessage)
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
/* Simplifying the access to basic info */
statusDependencyNode.bot = statusDependencyNode.referenceParent.parentNode.parentNode.config.codeName
statusDependencyNode.process = statusDependencyNode.referenceParent.parentNode.config.codeName
statusDependencyNode.bottype = statusDependencyNode.referenceParent.parentNode.parentNode.type
statusDependencyNode.dataMine = statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.config.codeName
statusDependencyNode.mineType = statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.type.replace(' ', '-')
statusDependencyNode.project = statusDependencyNode.referenceParent.parentNode.parentNode.parentNode.project
/* We retrieve the report main utility */
if (statusDependencyNode.config !== undefined) {
if (statusDependencyNode.config.mainUtility !== undefined) {
thisObject.mainUtility = statusDependencyNode.config.mainUtility
}
}
if (bot.TRADING_SESSION !== undefined && statusDependencyNode.bottype === "Trading Bot") {
sessionPath = bot.TRADING_SESSION.folderName + "/"
}
if (bot.LEARNING_SESSION !== undefined && statusDependencyNode.bottype === "Learning Bot") {
sessionPath = bot.LEARNING_SESSION.folderName + "/"
}
/* Now we will see where do we need to fetch this status report from. */
let network = global.TASK_NETWORK
let processThisDependsOn = statusDependencyNode.referenceParent.parentNode
for (let i = 0; i < network.networkNodes.length; i++) {
let networkNode = network.networkNodes[i]
if (checkThisDataBranch(networkNode.dataMining) === true) { return }
if (checkThisTradingBranch(networkNode.testingEnvironment) === true) { return }
if (checkThisTradingBranch(networkNode.productionEnvironment) === true) { return }
function checkThisDataBranch(branch) {
if (branch === undefined) { return }
for (let j = 0; j < branch.exchangeDataTasks.length; j++) {
let exchangeTasks = branch.exchangeDataTasks[j]
for (let p = 0; p < exchangeTasks.marketDataTasks.length; p++) {
let marketTasks = exchangeTasks.marketDataTasks[p]
if (marketTasks.referenceParent === undefined) { continue }
if (marketTasks.referenceParent.id !== global.MARKET_NODE.id) { continue }
for (let q = 0; q < marketTasks.dataMineTasks.length; q++) {
let mineTasks = marketTasks.dataMineTasks[q]
for (let k = 0; k < mineTasks.taskManagers.length; k++) {
let taskManager = mineTasks.taskManagers[k]
for (let m = 0; m < taskManager.tasks.length; m++) {
let task = taskManager.tasks[m]
if (task.bot === undefined) { continue }
for (let n = 0; n < task.bot.processes.length; n++) {
let process = task.bot.processes[n]
if (process.referenceParent === undefined) { continue }
let processDefinition = process.referenceParent
if (processThisDependsOn.id === processDefinition.id) {
if (process.type === 'Trading Process Instance') {
if (process.session !== undefined) {
if (bot.processNode.session.id !== process.session.id) {
continue
}
} else {
continue
}
}
/*
We found where the task that runs the process definition this status report depends on
and where it is located on the network.
*/
thisObject.networkNode = networkNode
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Retrieving status report from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.'); }
fileStorage = FILE_STORAGE.newFileStorage(logger, networkNode.config.host, networkNode.config.webPort);
callBackFunction(global.DEFAULT_OK_RESPONSE);
return true
}
}
}
}
}
}
}
}
function checkThisTradingBranch(branch) {
if (branch === undefined) { return }
for (let j = 0; j < branch.exchangeTradingTasks.length; j++) {
let exchangeTasks = branch.exchangeTradingTasks[j]
for (let p = 0; p < exchangeTasks.marketTradingTasks.length; p++) {
let marketTasks = exchangeTasks.marketTradingTasks[p]
if (marketTasks.referenceParent === undefined) { continue }
if (marketTasks.referenceParent.id !== global.MARKET_NODE.id) { continue }
for (let q = 0; q < marketTasks.tradingMineTasks.length; q++) {
let mineTasks = marketTasks.tradingMineTasks[q]
for (let k = 0; k < mineTasks.taskManagers.length; k++) {
let taskManager = mineTasks.taskManagers[k]
for (let m = 0; m < taskManager.tasks.length; m++) {
let task = taskManager.tasks[m]
if (task.bot === undefined) { continue }
for (let n = 0; n < task.bot.processes.length; n++) {
let process = task.bot.processes[n]
if (process.referenceParent === undefined) { continue }
let processDefinition = process.referenceParent
if (processThisDependsOn.id === processDefinition.id) {
if (process.type === 'Trading Process Instance') {
if (process.session !== undefined) {
if (bot.processNode.session.id !== process.session.id) {
continue
}
} else {
continue
}
}
/*
We found where the task that runs the process definition this status report depends on
and where it is located on the network.
*/
thisObject.networkNode = networkNode
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Retrieving status report from " + networkNode.name + " -> host = " + networkNode.config.host + ' -> port = ' + networkNode.config.webPort + '.'); }
fileStorage = FILE_STORAGE.newFileStorage(logger, networkNode.config.host, networkNode.config.webPort);
callBackFunction(global.DEFAULT_OK_RESPONSE);
return true
}
}
}
}
}
}
}
}
}
logger.write(MODULE_NAME, "[ERROR] initialize -> Initialization Failed because we could not find where the data of this status report is located within the network. Check the logs for more info.");
logger.write(MODULE_NAME, "[ERROR] initialize -> bot = " + statusDependencyNode.bot);
logger.write(MODULE_NAME, "[ERROR] initialize -> process = " + statusDependencyNode.process);
logger.write(MODULE_NAME, "[ERROR] initialize -> bottype = " + statusDependencyNode.bottype);
logger.write(MODULE_NAME, "[ERROR] initialize -> dataMine = " + statusDependencyNode.dataMine);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
function finalize() {
fileStorage = undefined
thisObject.networkNode = undefined
bot = undefined
thisObject = undefined
}
function load(callBackFunction) {
try {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] load -> Entering function."); }
let fileName = "Status.Report.json";
let filePath;
let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.process
let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.process
if (ownerId !== botId) {
let filePathRoot = 'Project/' + statusDependencyNode.project + "/" + statusDependencyNode.mineType + "/" + statusDependencyNode.dataMine + "/" + statusDependencyNode.bot + '/' + bot.exchange + "/" + bot.market.baseAsset + "-" + bot.market.quotedAsset
filePath = filePathRoot + "/Reports/" + sessionPath + statusDependencyNode.process;
} else {
filePath = bot.filePathRoot + "/Reports/" + sessionPath + statusDependencyNode.process;
}
filePath += '/' + fileName
let canUserPrevious
/*
If we are funning Trading Engines, we can not allow ourselves to use a Status Report that is not the latest one, because it might contain
transactioinal information related to the context of the operations the trading engine is doing.
On the contraty, if we are running a Sensor bot or an Indicator bot, we might, if necesary, use a previous version of a Status Report since
there will be no big impact, just some reprocessing.
*/
if (bot.TRADING_SESSION !== undefined || bot.LEARNING_SESSION !== undefined) {
canUserPrevious = false
} else {
canUserPrevious = true
}
fileStorage.getTextFile(filePath, onFileReceived, undefined, canUserPrevious);
function onFileReceived(err, text) {
if (err.result === global.CUSTOM_FAIL_RESPONSE.result && (err.message === 'Folder does not exist.' || err.message === 'File does not exist.')
|| err.code === "The specified key does not exist.") {
logger.write(MODULE_NAME, "[INFO] load -> onFileReceived -> err = " + err.message);
/* In this case we can assume that this is the first execution ever of this bot.*/
thisObject.file = JSON.parse('{}');
let customOK = {
result: global.CUSTOM_OK_RESPONSE.result,
message: "Status Report was never created."
};
logger.write(MODULE_NAME, "[WARN] load -> onFileReceived -> customOK = " + customOK.message);
callBackFunction(customOK);
return;
}
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, "[ERROR] load -> onFileReceived -> err = " + err.message);
callBackFunction(err);
return;
}
if (global.LOG_CONTROL[MODULE_NAME].logContent === true) {
logger.write(MODULE_NAME, "[INFO] load -> onFileReceived -> Content received = " + text);
}
try {
thisObject.file = JSON.parse(text);
callBackFunction(global.DEFAULT_OK_RESPONSE);
} catch (err) {
/*
It might happen that the file content is corrupt. We will consider this as a temporary situation, since sometimes the file
is being updated at the moment of the read. The bot can not run without a valid Status Report but we can request the platform to retry later.
*/
logger.write(MODULE_NAME, "[ERROR] load -> onFileReceived -> Error Parsing the Status report. -> Err = " + err.message);
logger.write(MODULE_NAME, "[WARN] load -> onFileReceived -> Error Parsing the Status report. -> text = " + text);
let customFail = {
result: global.CUSTOM_FAIL_RESPONSE.result,
message: "Status Report is corrupt."
};
logger.write(MODULE_NAME, "[WARN] load -> onFileReceived -> customFail = " + customFail.message);
callBackFunction(customFail);
return;
}
}
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] load -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
function save(callBackFunction) {
try {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] save -> Entering function."); }
let ownerId = statusDependencyNode.dataMine + "-" + statusDependencyNode.bot + "-" + statusDependencyNode.process
let botId = bot.dataMine + "-" + bot.codeName + "-" + bot.process
if (ownerId !== botId && statusDependencyNode.process !== "Context") { // Context is a special case where the report is created by the Context.js module itself.
let customErr = {
result: global.CUSTOM_FAIL_RESPONSE.result,
message: "Only bots owners of a Status Report can save them."
};
logger.write(MODULE_NAME, "[ERROR] save -> customErr = " + customErr.message);
callBackFunction(customErr);
return;
}
let fileName = "Status.Report.json";
let filePath = bot.filePathRoot + "/Reports/" + sessionPath + statusDependencyNode.process;
filePath += '/' + fileName
let fileContent = JSON.stringify(thisObject.file);
fileStorage.createTextFile(filePath, fileContent + '\n', onFileCreated, true);
function onFileCreated(err) {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] save -> onFileCreated -> Entering function."); }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, "[ERROR] save -> onFileCreated -> err = " + err.stack);
callBackFunction(err);
return;
}
if (global.LOG_CONTROL[MODULE_NAME].logContent === true) {
logger.write(MODULE_NAME, "[INFO] save -> onFileCreated -> Content written = " + fileContent);
}
/* All good, lets emit the event that means data has been updated. */
let processOutput = PROCESS_OUTPUT.newProcessOutput(bot, logger)
processOutput.raiseEvents(thisObject.file.lastFile, thisObject.file.timeFrames, callBackFunction);
return;
}
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] save -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
async function asyncSave() {
let fileName = "Status.Report.json";
let filePath = bot.filePathRoot + "/Reports/" + sessionPath + statusDependencyNode.process;
filePath += '/' + fileName
let fileContent = JSON.stringify(thisObject.file);
let response = await fileStorage.asyncCreateTextFile(filePath, fileContent + '\n', true)
if (response.err.result !== global.DEFAULT_OK_RESPONSE.result) {
throw (response.err)
}
/* All good, lets emit the event that means data has been updated. */
let processOutput = PROCESS_OUTPUT.newProcessOutput(bot, logger)
processOutput.asyncRaiseEvents(thisObject.file.lastFile, thisObject.file.timeFrames)
}
}