From e2a61d26adbb6ab472c549843f176cf7bc0eedd6 Mon Sep 17 00:00:00 2001 From: sam-keen Date: Thu, 24 Mar 2022 14:58:24 +0000 Subject: [PATCH] Proper way to get environment variables, and hoisted obtaining the env vars to the consumer of the stats manager --- apps/explorer-e2e/.env | 2 + apps/explorer-e2e/.env.devent | 2 + apps/explorer-e2e/.env.mainnet | 2 + apps/explorer-e2e/.env.stagnet1 | 2 + apps/explorer-e2e/.env.stagnet2 | 2 + apps/explorer-e2e/.env.testnet | 2 + apps/explorer/.env.devent | 2 + apps/explorer/.env.local | 2 + apps/explorer/.env.mainnet | 2 + apps/explorer/.env.stagnet1 | 2 + apps/explorer/.env.stagnet2 | 2 + apps/explorer/.env.testnet | 2 + apps/explorer/src/app/config/index.tsx | 2 + apps/explorer/src/app/routes/home/index.tsx | 13 ++- apps/stats-e2e/.env | 3 + apps/stats-e2e/.env.devnet | 3 + apps/stats-e2e/.env.mainnet | 3 + apps/stats-e2e/.env.stagnet1 | 3 + apps/stats-e2e/.env.stagnet2 | 3 + apps/stats-e2e/.env.testnet | 3 + apps/stats-e2e/src/integration/app.spec.ts | 34 +------ apps/stats/.env.devnet | 3 + apps/stats/.env.mainnet | 3 + apps/stats/.env.stagnet1 | 3 + apps/stats/.env.stagnet2 | 3 + apps/stats/.env.testnet | 3 + apps/stats/src/app.tsx | 13 ++- apps/stats/src/config/index.tsx | 4 + .../stats-manager/stats-manager.tsx | 88 ++++--------------- 29 files changed, 105 insertions(+), 106 deletions(-) create mode 100644 apps/stats-e2e/.env create mode 100644 apps/stats-e2e/.env.devnet create mode 100644 apps/stats-e2e/.env.mainnet create mode 100644 apps/stats-e2e/.env.stagnet1 create mode 100644 apps/stats-e2e/.env.stagnet2 create mode 100644 apps/stats-e2e/.env.testnet create mode 100644 apps/stats/.env.devnet create mode 100644 apps/stats/.env.mainnet create mode 100644 apps/stats/.env.stagnet1 create mode 100644 apps/stats/.env.stagnet2 create mode 100644 apps/stats/.env.testnet create mode 100644 apps/stats/src/config/index.tsx diff --git a/apps/explorer-e2e/.env b/apps/explorer-e2e/.env index bf9d3dbbc..ef2059492 100644 --- a/apps/explorer-e2e/.env +++ b/apps/explorer-e2e/.env @@ -2,6 +2,8 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://lb.testnet.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://lb.testnet.vega.xyz/tm/websocket" NX_VEGA_URL = "https://lb.testnet.vega.xyz/query" +NX_VEGA_ENV = 'Testnet' +NX_VEGA_REST = 'https://lb.testnet.vega.xyz/datanode/rest' NX_EXPLORER_ASSETS = 1 NX_EXPLORER_GENESIS = 1 diff --git a/apps/explorer-e2e/.env.devent b/apps/explorer-e2e/.env.devent index 996011a8e..932a90ac2 100644 --- a/apps/explorer-e2e/.env.devent +++ b/apps/explorer-e2e/.env.devent @@ -3,6 +3,8 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://n04.d.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://n04.d.vega.xyz/tm/websocket" NX_VEGA_URL = "https://n04.d.vega.xyz/query" +NX_VEGA_ENV = 'Devnet' +NX_VEGA_REST = 'https://n04.d.vega.xyz/datanode/rest' # App flags NX_EXPLORER_ASSETS = 1 diff --git a/apps/explorer-e2e/.env.mainnet b/apps/explorer-e2e/.env.mainnet index 13b74ef15..d097f3aa0 100644 --- a/apps/explorer-e2e/.env.mainnet +++ b/apps/explorer-e2e/.env.mainnet @@ -3,6 +3,8 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://mainnet-observer-proxy01.ops.vega.xyz/" NX_TENDERMINT_WEBSOCKET_URL = "wss://mainnet-observer-proxy01.ops.vega.xyz/websocket" NX_VEGA_URL = "https://api.token.vega.xyz/query" +NX_VEGA_ENV = 'Mainnet' +NX_VEGA_REST = 'https://api.token.vega.xyz/' # App flags NX_EXPLORER_ASSETS = 1 diff --git a/apps/explorer-e2e/.env.stagnet1 b/apps/explorer-e2e/.env.stagnet1 index 871afdc6d..1e191685c 100644 --- a/apps/explorer-e2e/.env.stagnet1 +++ b/apps/explorer-e2e/.env.stagnet1 @@ -3,6 +3,8 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://n03.s.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://n03.s.vega.xyz/tm/websocket" NX_VEGA_URL = "https://n03.s.vega.xyz/query" +NX_VEGA_ENV = 'Stagnet 1' +NX_VEGA_REST = 'https://n03.s.vega.xyz/datanode/rest' # App flags NX_EXPLORER_ASSETS = 1 diff --git a/apps/explorer-e2e/.env.stagnet2 b/apps/explorer-e2e/.env.stagnet2 index e4cbaab0d..4d959a05a 100644 --- a/apps/explorer-e2e/.env.stagnet2 +++ b/apps/explorer-e2e/.env.stagnet2 @@ -3,6 +3,8 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://n03.stagnet2.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://n03.stagnet2.vega.xyz/tm/websocket" NX_VEGA_URL = "https://n03.stagnet2.vega.xyz/query" +NX_VEGA_ENV = 'Stagnet 2' +NX_VEGA_REST = 'https://n01.stagnet2.vega.xyz/datanode/rest' # App flags NX_EXPLORER_ASSETS = 1 diff --git a/apps/explorer-e2e/.env.testnet b/apps/explorer-e2e/.env.testnet index 5b296d196..95e4b1e16 100644 --- a/apps/explorer-e2e/.env.testnet +++ b/apps/explorer-e2e/.env.testnet @@ -3,6 +3,8 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://lb.testnet.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://lb.testnet.vega.xyz/tm/websocket" NX_VEGA_URL = "https://lb.testnet.vega.xyz/query" +NX_VEGA_ENV = 'Testnet' +NX_VEGA_REST = 'https://lb.testnet.vega.xyz/datanode/rest' # App flags NX_EXPLORER_ASSETS = 1 diff --git a/apps/explorer/.env.devent b/apps/explorer/.env.devent index 3af139aa8..e90da3e6b 100644 --- a/apps/explorer/.env.devent +++ b/apps/explorer/.env.devent @@ -3,3 +3,5 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://n04.d.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://n04.d.vega.xyz/tm/websocket" NX_VEGA_URL = "https://n04.d.vega.xyz/query" +NX_VEGA_ENV = 'Devnet' +NX_VEGA_REST = 'https://n04.d.vega.xyz/datanode/rest' diff --git a/apps/explorer/.env.local b/apps/explorer/.env.local index 5b296d196..95e4b1e16 100644 --- a/apps/explorer/.env.local +++ b/apps/explorer/.env.local @@ -3,6 +3,8 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://lb.testnet.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://lb.testnet.vega.xyz/tm/websocket" NX_VEGA_URL = "https://lb.testnet.vega.xyz/query" +NX_VEGA_ENV = 'Testnet' +NX_VEGA_REST = 'https://lb.testnet.vega.xyz/datanode/rest' # App flags NX_EXPLORER_ASSETS = 1 diff --git a/apps/explorer/.env.mainnet b/apps/explorer/.env.mainnet index ad8c0ef9d..4bdd55411 100644 --- a/apps/explorer/.env.mainnet +++ b/apps/explorer/.env.mainnet @@ -3,3 +3,5 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://mainnet-observer-proxy01.ops.vega.xyz/" NX_TENDERMINT_WEBSOCKET_URL = "wss://mainnet-observer-proxy01.ops.vega.xyz/websocket" NX_VEGA_URL = "https://api.token.vega.xyz/query" +NX_VEGA_ENV = 'Mainnet' +NX_VEGA_REST = 'https://api.token.vega.xyz/' diff --git a/apps/explorer/.env.stagnet1 b/apps/explorer/.env.stagnet1 index 83cda47ac..e042ac19d 100644 --- a/apps/explorer/.env.stagnet1 +++ b/apps/explorer/.env.stagnet1 @@ -3,3 +3,5 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://n03.s.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://n03.s.vega.xyz/tm/websocket" NX_VEGA_URL = "https://n03.s.vega.xyz/query" +NX_VEGA_ENV = 'Stagnet 1' +NX_VEGA_REST = 'https://n03.s.vega.xyz/datanode/rest' diff --git a/apps/explorer/.env.stagnet2 b/apps/explorer/.env.stagnet2 index 05b15b546..c6fd590fe 100644 --- a/apps/explorer/.env.stagnet2 +++ b/apps/explorer/.env.stagnet2 @@ -3,3 +3,5 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://n03.stagnet2.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://n03.stagnet2.vega.xyz/tm/websocket" NX_VEGA_URL = "https://n03.stagnet2.vega.xyz/query" +NX_VEGA_ENV = 'Stagnet 2' +NX_VEGA_REST = 'https://n01.stagnet2.vega.xyz/datanode/rest' diff --git a/apps/explorer/.env.testnet b/apps/explorer/.env.testnet index 5218200b8..dd89127e2 100644 --- a/apps/explorer/.env.testnet +++ b/apps/explorer/.env.testnet @@ -3,3 +3,5 @@ NX_CHAIN_EXPLORER_URL = "https://explorer.vega.trading/.netlify/functions/chain- NX_TENDERMINT_URL = "https://lb.testnet.vega.xyz/tm" NX_TENDERMINT_WEBSOCKET_URL = "wss://lb.testnet.vega.xyz/tm/websocket" NX_VEGA_URL = "https://lb.testnet.vega.xyz/query" +NX_VEGA_ENV = 'Testnet' +NX_VEGA_REST = 'https://lb.testnet.vega.xyz/datanode/rest' diff --git a/apps/explorer/src/app/config/index.tsx b/apps/explorer/src/app/config/index.tsx index a8e193ed3..0e1833f63 100644 --- a/apps/explorer/src/app/config/index.tsx +++ b/apps/explorer/src/app/config/index.tsx @@ -3,4 +3,6 @@ export const DATA_SOURCES = { tendermintUrl: process.env['NX_TENDERMINT_URL'] as string, tendermintWebsocketUrl: process.env['NX_TENDERMINT_WEBSOCKET_URL'] as string, dataNodeUrl: process.env['NX_VEGA_URL'] as string, + envName: process.env['NX_VEGA_ENV'] as string, + restEndpoint: process.env['NX_VEGA_REST'] as string, }; diff --git a/apps/explorer/src/app/routes/home/index.tsx b/apps/explorer/src/app/routes/home/index.tsx index fe02a45ba..aba729439 100644 --- a/apps/explorer/src/app/routes/home/index.tsx +++ b/apps/explorer/src/app/routes/home/index.tsx @@ -1,9 +1,20 @@ +import { DATA_SOURCES } from '../../config'; import { StatsManager } from '@vegaprotocol/network-stats'; +const envName = DATA_SOURCES.envName; +const restEndpoint = DATA_SOURCES.restEndpoint; +const statsEndpoint = `${restEndpoint}/statistics`; +const nodesEndpoint = `${restEndpoint}/nodes-data`; + const Home = () => { return (
- +
); }; diff --git a/apps/stats-e2e/.env b/apps/stats-e2e/.env new file mode 100644 index 000000000..8cee97c89 --- /dev/null +++ b/apps/stats-e2e/.env @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Testnet' +NX_VEGA_REST = 'https://lb.testnet.vega.xyz/datanode/rest' diff --git a/apps/stats-e2e/.env.devnet b/apps/stats-e2e/.env.devnet new file mode 100644 index 000000000..4d9a3fcbe --- /dev/null +++ b/apps/stats-e2e/.env.devnet @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Devnet' +NX_VEGA_REST = 'https://n04.d.vega.xyz/datanode/rest' diff --git a/apps/stats-e2e/.env.mainnet b/apps/stats-e2e/.env.mainnet new file mode 100644 index 000000000..791b64252 --- /dev/null +++ b/apps/stats-e2e/.env.mainnet @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Mainnet' +NX_VEGA_REST = 'https://api.token.vega.xyz/' diff --git a/apps/stats-e2e/.env.stagnet1 b/apps/stats-e2e/.env.stagnet1 new file mode 100644 index 000000000..259a3fbed --- /dev/null +++ b/apps/stats-e2e/.env.stagnet1 @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Stagnet 1' +NX_VEGA_REST = 'https://n03.s.vega.xyz/datanode/rest' diff --git a/apps/stats-e2e/.env.stagnet2 b/apps/stats-e2e/.env.stagnet2 new file mode 100644 index 000000000..76a344730 --- /dev/null +++ b/apps/stats-e2e/.env.stagnet2 @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Stagnet 2' +NX_VEGA_REST = 'https://n01.stagnet2.vega.xyz/datanode/rest' diff --git a/apps/stats-e2e/.env.testnet b/apps/stats-e2e/.env.testnet new file mode 100644 index 000000000..8cee97c89 --- /dev/null +++ b/apps/stats-e2e/.env.testnet @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Testnet' +NX_VEGA_REST = 'https://lb.testnet.vega.xyz/datanode/rest' diff --git a/apps/stats-e2e/src/integration/app.spec.ts b/apps/stats-e2e/src/integration/app.spec.ts index 1c6b1c020..7ae0dc040 100644 --- a/apps/stats-e2e/src/integration/app.spec.ts +++ b/apps/stats-e2e/src/integration/app.spec.ts @@ -1,37 +1,9 @@ +const textToCheck = process.env['NX_VEGA_ENV']; + describe('stats', () => { beforeEach(() => cy.visit('/')); - let textToCheck = ''; - - const dataNodeUrl = process.env['NX_VEGA_URL']; - - if (dataNodeUrl) { - if (dataNodeUrl.includes('api')) { - textToCheck = 'Mainnet'; - } - - if (dataNodeUrl.includes('testnet')) { - textToCheck = 'Testnet'; - } - - if (dataNodeUrl.includes('n04.d')) { - textToCheck = 'Devnet'; - } - - if (dataNodeUrl.includes('n03.s.vega')) { - textToCheck = 'Stagnet 1'; - } - - if (dataNodeUrl.includes('stagnet2')) { - textToCheck = 'Stagnet 2'; - } - } else { - // Network-stats falls back to showing Mainnet stats if no - // environment variables have been set - textToCheck = 'Mainnet'; - } - - it('should display header based on environment', () => { + it('should display header based on environment name', () => { cy.get('h3').should('have.text', `/ ${textToCheck}`); }); }); diff --git a/apps/stats/.env.devnet b/apps/stats/.env.devnet new file mode 100644 index 000000000..4d9a3fcbe --- /dev/null +++ b/apps/stats/.env.devnet @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Devnet' +NX_VEGA_REST = 'https://n04.d.vega.xyz/datanode/rest' diff --git a/apps/stats/.env.mainnet b/apps/stats/.env.mainnet new file mode 100644 index 000000000..791b64252 --- /dev/null +++ b/apps/stats/.env.mainnet @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Mainnet' +NX_VEGA_REST = 'https://api.token.vega.xyz/' diff --git a/apps/stats/.env.stagnet1 b/apps/stats/.env.stagnet1 new file mode 100644 index 000000000..259a3fbed --- /dev/null +++ b/apps/stats/.env.stagnet1 @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Stagnet 1' +NX_VEGA_REST = 'https://n03.s.vega.xyz/datanode/rest' diff --git a/apps/stats/.env.stagnet2 b/apps/stats/.env.stagnet2 new file mode 100644 index 000000000..76a344730 --- /dev/null +++ b/apps/stats/.env.stagnet2 @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Stagnet 2' +NX_VEGA_REST = 'https://n01.stagnet2.vega.xyz/datanode/rest' diff --git a/apps/stats/.env.testnet b/apps/stats/.env.testnet new file mode 100644 index 000000000..8cee97c89 --- /dev/null +++ b/apps/stats/.env.testnet @@ -0,0 +1,3 @@ +# App configuration variables +NX_VEGA_ENV = 'Testnet' +NX_VEGA_REST = 'https://lb.testnet.vega.xyz/datanode/rest' diff --git a/apps/stats/src/app.tsx b/apps/stats/src/app.tsx index 6ca239e4b..3f01d7596 100644 --- a/apps/stats/src/app.tsx +++ b/apps/stats/src/app.tsx @@ -1,7 +1,13 @@ import React, { useState } from 'react'; +import { DATA_SOURCES } from './config'; import { Header } from './components/header'; import { StatsManager } from '@vegaprotocol/network-stats'; +const envName = DATA_SOURCES.envName; +const restEndpoint = DATA_SOURCES.restEndpoint; +const statsEndpoint = `${restEndpoint}/statistics`; +const nodesEndpoint = `${restEndpoint}/nodes-data`; + function App() { const [darkMode, setDarkMode] = useState( document.documentElement.classList.contains('dark-mode-preferred') @@ -15,7 +21,12 @@ function App() { >
- +
); diff --git a/apps/stats/src/config/index.tsx b/apps/stats/src/config/index.tsx new file mode 100644 index 000000000..7aaf89475 --- /dev/null +++ b/apps/stats/src/config/index.tsx @@ -0,0 +1,4 @@ +export const DATA_SOURCES = { + envName: process.env['NX_VEGA_ENV'] as string, + restEndpoint: process.env['NX_VEGA_REST'] as string, +}; diff --git a/libs/network-stats/src/components/stats-manager/stats-manager.tsx b/libs/network-stats/src/components/stats-manager/stats-manager.tsx index fada84816..c7b3a1e40 100644 --- a/libs/network-stats/src/components/stats-manager/stats-manager.tsx +++ b/libs/network-stats/src/components/stats-manager/stats-manager.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState, useRef } from 'react'; +import { useEffect, useState } from 'react'; import classnames from 'classnames'; import { statsFields } from '../../config/stats-fields'; import { @@ -10,85 +10,28 @@ import { TableRow } from '../table-row'; import { PromotedStats } from '../promoted-stats'; import { PromotedStatsItem } from '../promoted-stats-item'; -interface statsManagerProps { +interface StatsManagerProps { + envName: string; + statsEndpoint: string; + nodesEndpoint: string; className?: string; } -interface dataSourceProps { - title: string; - stats: string; - nodes: string; -} - -const dataSources = { - mainnet: { - title: 'Mainnet', - stats: 'https://api.token.vega.xyz/statistics', - nodes: 'https://api.token.vega.xyz/nodes-data', - }, - testnet: { - title: 'Testnet', - stats: 'https://lb.testnet.vega.xyz/statistics', - nodes: 'https://lb.testnet.vega.xyz/datanode/rest/nodes-data', - }, - devnet: { - title: 'Devnet', - stats: 'https://n04.d.vega.xyz/datanode/rest/statistics', - nodes: 'https://n04.d.vega.xyz/datanode/rest/nodes-data', - }, - stagnet1: { - title: 'Stagnet 1', - stats: 'https://n03.s.vega.xyz/datanode/rest/statistics', - nodes: 'https://n03.s.vega.xyz/datanode/rest/nodes-data', - }, - stagnet2: { - title: 'Stagnet 2', - stats: 'https://n01.stagnet2.vega.xyz/datanode/rest/statistics', - nodes: 'https://n01.stagnet2.vega.xyz/datanode/rest/nodes-data', - }, -}; - -const getDataSource = () => { - const dataNodeUrl = process.env['NX_VEGA_URL']; - - if (dataNodeUrl) { - if (dataNodeUrl.includes('api')) { - return dataSources.mainnet; - } - - if (dataNodeUrl.includes('testnet')) { - return dataSources.testnet; - } - - if (dataNodeUrl.includes('n04.d')) { - return dataSources.devnet; - } - - if (dataNodeUrl.includes('n03.s.vega')) { - return dataSources.stagnet1; - } - - if (dataNodeUrl.includes('stagnet2')) { - return dataSources.stagnet2; - } - } - - return dataSources.mainnet; -}; - -export const StatsManager = ({ className }: statsManagerProps) => { - const dataSource = useRef(getDataSource()); +export const StatsManager = ({ + envName, + statsEndpoint, + nodesEndpoint, + className, +}: StatsManagerProps) => { const [data, setData] = useState(null); const [error, setError] = useState(null); useEffect(() => { - getDataSource(); - async function getStats() { try { const [res1, res2] = await Promise.all([ - fetch(dataSource.current.stats), - fetch(dataSource.current.nodes), + fetch(statsEndpoint), + fetch(nodesEndpoint), ]); const [{ statistics }, { nodeData }] = await Promise.all([ res1.json(), @@ -134,7 +77,7 @@ export const StatsManager = ({ className }: statsManagerProps) => { return () => { clearInterval(interval); }; - }, []); + }, [nodesEndpoint, statsEndpoint]); const classes = classnames( className, @@ -144,8 +87,7 @@ export const StatsManager = ({ className }: statsManagerProps) => { return (

- {(error && `/ ${error}`) || - (data ? `/ ${dataSource.current.title}` : '/ Connecting...')} + {(error && `/ ${error}`) || (data ? `/ ${envName}` : '/ Connecting...')}

{data?.promoted ? (