From 5c9afee90d3f52a09341c65ebc58df16d5a6dff2 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 6 Dec 2020 12:40:03 +0100 Subject: [PATCH] Webhooks Cleaned --- .../Sensor-Bot/Webhooks/CheckWebhook.js | 82 ++++++++----------- .../UI/Function-Libraries/TaskFunctions.js | 4 +- 2 files changed, 38 insertions(+), 48 deletions(-) diff --git a/Projects/Superalgos/TS/Bot-Modules/Sensor-Bot/Webhooks/CheckWebhook.js b/Projects/Superalgos/TS/Bot-Modules/Sensor-Bot/Webhooks/CheckWebhook.js index 30d4680e2..68b5c1987 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Sensor-Bot/Webhooks/CheckWebhook.js +++ b/Projects/Superalgos/TS/Bot-Modules/Sensor-Bot/Webhooks/CheckWebhook.js @@ -1,9 +1,7 @@  exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { - const FULL_LOG = true; - const LOG_FILE_CONTENT = false; - const MODULE_NAME = "User Bot"; + const MODULE_NAME = "Check Webhooks"; const FOLDER_NAME = "External-Signals"; thisObject = { @@ -11,7 +9,6 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { start: start }; - let fileStorage = TS.projects.superalgos.taskModules.fileStorage.newFileStorage(processIndex); let statusDependencies @@ -19,43 +16,35 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { function initialize(pStatusDependencies, callBackFunction) { try { - - - - statusDependencies = pStatusDependencies; - callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE); - } catch (err) { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] initialize -> err = " + err.stack); callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE); } } function start(callBackFunction) { try { - if (TS.projects.superalgos.globals.taskVariables.IS_TASK_STOPPING === true) { callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE); return } let fileContent - getContextVariables(saveMessages) - + function getContextVariables(callBack) { - try { - let reportKey; + let reportKey = "Masters" + "-" + "Webhooks" + "-" + "Check-Webhook" - reportKey = "Masters" + "-" + "Webhooks" + "-" + "Check-Webhook" - - if (FULL_LOG === true) { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> getContextVariables -> reportKey = " + reportKey); } + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[INFO] start -> getContextVariables -> reportKey = " + reportKey) if (statusDependencies.statusReports.get(reportKey).status === "Status Report is corrupt.") { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. "); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> getContextVariables -> Can not continue because dependecy Status Report is corrupt. "); callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE); return; } @@ -63,14 +52,15 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { thisReport = statusDependencies.statusReports.get(reportKey) if (thisReport.file.lasrRun !== undefined) { - + let fileName = 'Data.json' let filePath = TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).FILE_PATH_ROOT + "/Output/" + FOLDER_NAME + "/" + 'Single-File' fileStorage.getTextFile(filePath + '/' + fileName, onFileReceived); function onFileReceived(err, text) { if (err.result !== TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE.result) { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> onFileReceived -> Could read file. -> filePath = " + filePath + "/" + fileName); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> getContextVariables -> onFileReceived -> Could read file. -> filePath = " + filePath + "/" + fileName); callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE); } else { @@ -80,14 +70,15 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { } } else { // If there is no status report, we assume there is no previous file or that if there is we will override it. callBack() - } - - + } } catch (err) { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> getContextVariables -> err = " + err.stack); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> getContextVariables -> err = " + err.stack); if (err.message === "Cannot read property 'file' of undefined") { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Check the bot Status Dependencies. "); - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[HINT] start -> getContextVariables -> Dependencies loaded -> keys = " + JSON.stringify(statusDependencies.keys)); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[HINT] start -> getContextVariables -> Check the bot Status Dependencies. "); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[HINT] start -> getContextVariables -> Dependencies loaded -> keys = " + JSON.stringify(statusDependencies.keys)); } callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE); abort = true @@ -96,20 +87,18 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { function saveMessages() { try { - let http = require('http'); - let url = 'http://' + global.env.WEB_SERVER_URL + - ':' + global.env.HTTP_INTERFACE_PORT + - '/Webhook/Fetch-Messages/' + - TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.name + "/" + - TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + "-" + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName + let url = 'http://' + global.env.WEB_SERVER_URL + + ':' + global.env.HTTP_INTERFACE_PORT + + '/Webhook/Fetch-Messages/' + + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.parentNode.parentNode.name + "/" + + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + "-" + + TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName http.get(url, onResponse); function onResponse(response) { - const chunks = [] - response.on('data', onMessegesArrived) response.on('end', onEnd) @@ -123,7 +112,7 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { if (fileContent !== undefined) { // we are going to append the curernt messages to the existing file. let fileContentArray = JSON.parse(fileContent) - let messagesArray = JSON.parse(messages) + let messagesArray = JSON.parse(messages) for (let i = 0; i < messagesArray.length; i++) { let message = messagesArray[i] @@ -142,7 +131,8 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { function onFileCreated(err) { if (err.result !== TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE.result) { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> saveMessages -> onResponse -> onEnd -> onFileCreated -> Could not save file. -> filePath = " + filePath + "/" + fileName); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> saveMessages -> onResponse -> onEnd -> onFileCreated -> Could not save file. -> filePath = " + filePath + "/" + fileName); callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE); } else { writeStatusReport() @@ -152,7 +142,8 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { } } catch (err) { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> saveMessages -> err = " + err.stack); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> saveMessages -> err = " + err.stack); callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE); abort = true } @@ -160,30 +151,29 @@ exports.newSuperalgosBotModulesCheckWebhook = function (processIndex) { function writeStatusReport() { try { - thisReport.file = { lasrRun: (new Date()).toISOString() }; - thisReport.save(onSaved); function onSaved(err) { if (err.result !== TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE.result) { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> onSaved -> err = " + err.stack); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> writeStatusReport -> onSaved -> err = " + err.stack); callBackFunction(err); return; } callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE); } } catch (err) { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> writeStatusReport -> err = " + err.stack); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> writeStatusReport -> err = " + err.stack); callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE); } } - - } catch (err) { - TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> err = " + err.stack); callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE); } } diff --git a/Projects/Superalgos/UI/Function-Libraries/TaskFunctions.js b/Projects/Superalgos/UI/Function-Libraries/TaskFunctions.js index e4c96e235..057e577d1 100644 --- a/Projects/Superalgos/UI/Function-Libraries/TaskFunctions.js +++ b/Projects/Superalgos/UI/Function-Libraries/TaskFunctions.js @@ -756,7 +756,7 @@ function newSuperalgosFunctionLibraryTaskFunctions() { } let botInstance - switch (TS.projects.superalgos.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.type) { + switch (bot.type) { case 'Sensor Bot': { botInstance = UI.projects.superalgos.functionLibraries.uiObjectsFromNodes.addUIObject(task, 'Sensor Bot Instance') botInstance.name = bot.name @@ -782,7 +782,7 @@ function newSuperalgosFunctionLibraryTaskFunctions() { for (let j = 0; j < bot.processes.length; j++) { let process = bot.processes[j] let processInstance - switch (TS.projects.superalgos.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.parentNode.type) { + switch (bot.type) { case 'Sensor Bot': { processInstance = UI.projects.superalgos.functionLibraries.uiObjectsFromNodes.addUIObject(botInstance, 'Sensor Process Instance') processInstance.payload.referenceParent = process