From fd4344e7933fc9de393dd46075fa0bfd064be595 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 29 Jul 2019 10:42:44 +0200 Subject: [PATCH] Changes to make it work Locally --- ChartsSpace/PlottersManager.js | 4 + Dashboard.js | 13 ++- StrategySpace/StrategizerGateway.js | 147 ++++++++++++++++------------ TopSpace/Login.js | 19 ++-- 4 files changed, 106 insertions(+), 77 deletions(-) diff --git a/ChartsSpace/PlottersManager.js b/ChartsSpace/PlottersManager.js index 9cfdd845f..ef59d597b 100644 --- a/ChartsSpace/PlottersManager.js +++ b/ChartsSpace/PlottersManager.js @@ -142,6 +142,10 @@ function newPlottersManager () { function initializeCompetitionPlotters (callBack) { try { + /* Disabling this functionality since it is not currently available */ + callBack(GLOBAL.DEFAULT_OK_RESPONSE) + return + /* At this current version of the platform, we will support only one competition with only one plotter. */ const COMPETITION_HOST = 'AAMasters' diff --git a/Dashboard.js b/Dashboard.js index 4e69b50cb..ccf9635d3 100644 --- a/Dashboard.js +++ b/Dashboard.js @@ -7,9 +7,9 @@ try { viewPort = newViewPort() } catch (e) { setTimeout(() => { - console.log("Loading deferred.") + console.log('Loading deferred.') viewPort = newViewPort() - }, 1000); + }, 1000) } function newDashboard () { @@ -36,6 +36,13 @@ function newDashboard () { if (canvas !== undefined) { canvas.finalize() } + /* Here we check where we are executing. In case we are Local we need to create a Local Dummy User and Team. */ + + if (window.canvasApp.executingAt === 'Local') { + window.localStorage.setItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY, 'Local Access Token') // Dummy Access Token + window.localStorage.setItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY, '{"authId":"x|x","alias":"user"}') // Dummy Local User + } + /* Here we will setup the global eventHandler that will enable the Canvas App to react to events happening outside its execution scope. */ window.canvasApp.eventHandler = newEventHandler() @@ -161,7 +168,7 @@ function newDashboard () { }) .then(response => { teams = response.data.teams_Teams - //window.localStorage.setItem('Teams', JSON.stringify(response.data.teams_Teams)) + // window.localStorage.setItem('Teams', JSON.stringify(response.data.teams_Teams)) resolve({ teams: response.data.teams_Teams }) }) .catch(err => { diff --git a/StrategySpace/StrategizerGateway.js b/StrategySpace/StrategizerGateway.js index 589ff517e..442c335bc 100644 --- a/StrategySpace/StrategizerGateway.js +++ b/StrategySpace/StrategizerGateway.js @@ -45,41 +45,47 @@ function newStrategizerGateway () { return } - user = JSON.parse(user) - let fbSlug = 'simulator-bot-' + user.alias.replace('.', '') - - graphQLServer = await axios({ - url: window.canvasApp.graphQL.masterAppApiUrl, - method: 'post', - data: { - query: - ` - query($fbSlug: String!){ - strategizer_TradingSystemByFb(fbSlug: $fbSlug){ - id - fbSlug - data - } - } - `, - - variables: { - fbSlug: fbSlug - } - }, - headers: { - authorization: 'Bearer ' + accessToken - } - }) - - if (graphQLServer.data.errors) { - logger.write('[ERROR] loadFromStrategyzer -> GraphQL Error: ' + JSON.stringify(graphQLServer.data.errors)) - return false + if (window.canvasApp.executingAt === 'Local') { + window.localStorage.setItem(CANVAS_APP_NAME + '.' + MODULE_NAME + '.' + user.alias, 'Local File') + thisObject.strategizerData = JSON.parse('{"type":"Definition"}') + thisObject.idAtStrategizer = 'Local File' } else { - thisObject.strategizerData = JSON.parse(JSON.stringify(graphQLServer.data.data.strategizer_TradingSystemByFb.data)) - thisObject.idAtStrategizer = graphQLServer.data.data.strategizer_TradingSystemByFb.id - window.localStorage.setItem(CANVAS_APP_NAME + '.' + MODULE_NAME + '.' + user.alias, thisObject.idAtStrategizer) - return true + user = JSON.parse(user) + let fbSlug = 'simulator-bot-' + user.alias.replace('.', '') + + graphQLServer = await axios({ + url: window.canvasApp.graphQL.masterAppApiUrl, + method: 'post', + data: { + query: + ` + query($fbSlug: String!){ + strategizer_TradingSystemByFb(fbSlug: $fbSlug){ + id + fbSlug + data + } + } + `, + + variables: { + fbSlug: fbSlug + } + }, + headers: { + authorization: 'Bearer ' + accessToken + } + }) + + if (graphQLServer.data.errors) { + logger.write('[ERROR] loadFromStrategyzer -> GraphQL Error: ' + JSON.stringify(graphQLServer.data.errors)) + return false + } else { + thisObject.strategizerData = JSON.parse(JSON.stringify(graphQLServer.data.data.strategizer_TradingSystemByFb.data)) + thisObject.idAtStrategizer = graphQLServer.data.data.strategizer_TradingSystemByFb.id + window.localStorage.setItem(CANVAS_APP_NAME + '.' + MODULE_NAME + '.' + user.alias, thisObject.idAtStrategizer) + return true + } } } catch (err) { logger.write('[ERROR] loadFromStrategyzer -> err = ' + err.stack) @@ -100,10 +106,6 @@ function newStrategizerGateway () { return } - user = JSON.parse(user) - - /* See if we need to update or create a new record at the strategizer */ - let tradingSystem = canvas.strategySpace.workspace.getProtocolTradingSystem() if (tradingSystem === undefined || tradingSystem === null) { logger.write('[ERROR] saveToStrategyzer -> Can not save when tradingSystem is null or undefined.') @@ -115,37 +117,52 @@ function newStrategizerGateway () { logger.write('[ERROR] saveToStrategyzer -> Can not save when idAtStrategizer is null or undefined.') return } - graphQLServer = await axios({ - url: window.canvasApp.graphQL.masterAppApiUrl, - method: 'post', - data: { - query: ` - mutation strategizer_EditTradingSystem - ( - $id: ID!, $data: strategizer_JSON! - ) - { - strategizer_EditTradingSystem(id: $id, tradingSystem: { data: $data }) - { - id - } - } - `, - variables: { - id: idAtStrategizer, - data: tradingSystem + + if (window.canvasApp.executingAt === 'Local') { + callServer(tradingSystem, 'SaveDefinition', onSaved) + function onSaved (err) { + if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) { + return true + } else { + logger.write('[ERROR] saveToStrategyzer -> Can not save definition to local server. err = ' + err.messsage) + return false } - }, - headers: { - authorization: 'Bearer ' + accessToken } - }) + } else { + user = JSON.parse(user) - if (graphQLServer.data.errors) { - logger.write('[ERROR] saveToStrategyzer -> GraphsQL Error -> graphQLServer.data.errors = ' + JSON.stringify(graphQLServer.data.errors)) + graphQLServer = await axios({ + url: window.canvasApp.graphQL.masterAppApiUrl, + method: 'post', + data: { + query: ` + mutation strategizer_EditTradingSystem + ( + $id: ID!, $data: strategizer_JSON! + ) + { + strategizer_EditTradingSystem(id: $id, tradingSystem: { data: $data }) + { + id + } + } + `, + variables: { + id: idAtStrategizer, + data: tradingSystem + } + }, + headers: { + authorization: 'Bearer ' + accessToken + } + }) + + if (graphQLServer.data.errors) { + logger.write('[ERROR] saveToStrategyzer -> GraphsQL Error -> graphQLServer.data.errors = ' + JSON.stringify(graphQLServer.data.errors)) + } + + return true } - - return true } catch (err) { logger.write('[ERROR] saveToStrategyzer -> err = ' + err.stack) } diff --git a/TopSpace/Login.js b/TopSpace/Login.js index b3964fbeb..79b09d25a 100644 --- a/TopSpace/Login.js +++ b/TopSpace/Login.js @@ -1,5 +1,5 @@ -function newLogin() { +function newLogin () { const MODULE_NAME = 'Login' const ERROR_LOG = true const logger = newWebDebugLog() @@ -27,8 +27,10 @@ function newLogin() { return thisObject - async function initialize(sharedStatus) { + async function initialize (sharedStatus) { try { + if (window.canvasApp.executingAt === 'Local') { return } + const accessToken = window.localStorage.getItem('access_token') if (accessToken) { @@ -37,14 +39,13 @@ function newLogin() { } thisObject.container.eventHandler.listenToEvent('onMouseClick', onClick) - } catch (err) { if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = ' + err) } if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err.stack = ' + err.stack) } } } - async function getEvents(accessToken, sharedStatus) { + async function getEvents (accessToken, sharedStatus) { var d = new Date() var nowSeconds = Math.round(d.getTime() / 1000) var twoWeeksAgoSeconds = nowSeconds - 1209600 @@ -72,7 +73,7 @@ function newLogin() { } } } - }`, + }` }, variables: { variables: { maxStartDate: nowSeconds, minEndDate: twoWeeksAgoSeconds } @@ -99,7 +100,7 @@ function newLogin() { } } - async function getClones(accessToken) { + async function getClones (accessToken) { let response = await axios({ url: window.canvasApp.graphQL.masterAppApiUrl, method: 'post', @@ -166,11 +167,11 @@ function newLogin() { } } - function onClick() { + function onClick () { } - function getContainer(point) { + function getContainer (point) { /* First we check if this point is inside this object UI. */ if (thisObject.container.frame.isThisPointHere(point, true) === true) { return this.container @@ -180,7 +181,7 @@ function newLogin() { } } - function draw() { + function draw () { return // nothing to show. } }