frontend-monorepo-127 Generalize stats page to work for all environments

This commit is contained in:
sam-keen 2022-03-24 12:04:01 +00:00
parent 05d53bbf4c
commit 5d6147ec7e
74 changed files with 187 additions and 92 deletions

View File

@ -1,4 +1,4 @@
import { StatsManager } from '@vegaprotocol/mainnet-stats-manager';
import { StatsManager } from '@vegaprotocol/network-stats';
const Home = () => {
return (

View File

@ -8,7 +8,7 @@
"supportFile": "./src/support/index.ts",
"pluginsFile": false,
"video": true,
"videosFolder": "../../dist/cypress/apps/stats-mainnet-e2e/videos",
"screenshotsFolder": "../../dist/cypress/apps/stats-mainnet-e2e/screenshots",
"videosFolder": "../../dist/cypress/apps/stats-e2e/videos",
"screenshotsFolder": "../../dist/cypress/apps/stats-e2e/screenshots",
"chromeWebSecurity": false
}

View File

@ -0,0 +1,28 @@
{
"root": "apps/stats-e2e",
"sourceRoot": "apps/stats-e2e/src",
"projectType": "application",
"targets": {
"e2e": {
"executor": "@nrwl/cypress:cypress",
"options": {
"cypressConfig": "apps/stats-e2e/cypress.json",
"devServerTarget": "stats:serve"
},
"configurations": {
"production": {
"devServerTarget": "stats:serve:production"
}
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["apps/stats-e2e/**/*.{js,ts}"]
}
}
},
"tags": [],
"implicitDependencies": ["stats"]
}

View File

@ -0,0 +1,37 @@
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', () => {
cy.get('h3').should('have.text', `/ ${textToCheck}`);
});
});

View File

@ -1,28 +0,0 @@
{
"root": "apps/stats-mainnet-e2e",
"sourceRoot": "apps/stats-mainnet-e2e/src",
"projectType": "application",
"targets": {
"e2e": {
"executor": "@nrwl/cypress:cypress",
"options": {
"cypressConfig": "apps/stats-mainnet-e2e/cypress.json",
"devServerTarget": "stats-mainnet:serve"
},
"configurations": {
"production": {
"devServerTarget": "stats-mainnet:serve:production"
}
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["apps/stats-mainnet-e2e/**/*.{js,ts}"]
}
}
},
"tags": [],
"implicitDependencies": ["stats-mainnet"]
}

View File

@ -1,7 +0,0 @@
describe('stats-mainnet', () => {
beforeEach(() => cy.visit('/'));
it('should display header', () => {
cy.get('h3').should('have.text', '/ Mainnet');
});
});

View File

@ -1,10 +1,10 @@
module.exports = {
displayName: 'stats-mainnet',
displayName: 'stats',
preset: '../../jest.preset.js',
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
'^.+\\.[tj]sx?$': 'babel-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/apps/stats-mainnet',
coverageDirectory: '../../coverage/apps/stats',
};

View File

@ -1,6 +1,6 @@
{
"root": "apps/stats-mainnet",
"sourceRoot": "apps/stats-mainnet/src",
"root": "apps/stats",
"sourceRoot": "apps/stats/src",
"projectType": "application",
"targets": {
"build": {
@ -9,17 +9,17 @@
"defaultConfiguration": "production",
"options": {
"compiler": "babel",
"outputPath": "dist/apps/stats-mainnet",
"index": "apps/stats-mainnet/src/index.html",
"outputPath": "dist/apps/stats",
"index": "apps/stats/src/index.html",
"baseHref": "/",
"main": "apps/stats-mainnet/src/main.tsx",
"polyfills": "apps/stats-mainnet/src/polyfills.ts",
"tsConfig": "apps/stats-mainnet/tsconfig.app.json",
"main": "apps/stats/src/main.tsx",
"polyfills": "apps/stats/src/polyfills.ts",
"tsConfig": "apps/stats/tsconfig.app.json",
"assets": [
"apps/stats-mainnet/src/assets/favicon.ico",
"apps/stats-mainnet/src/assets"
"apps/stats/src/assets/favicon.ico",
"apps/stats/src/assets"
],
"styles": ["apps/stats-mainnet/src/styles/styles.css"],
"styles": ["apps/stats/src/styles/styles.css"],
"scripts": [],
"webpackConfig": "@nrwl/react/plugins/webpack"
},
@ -27,8 +27,8 @@
"production": {
"fileReplacements": [
{
"replace": "apps/stats-mainnet/src/environments/environment.ts",
"with": "apps/stats-mainnet/src/environments/environment.prod.ts"
"replace": "apps/stats/src/environments/environment.ts",
"with": "apps/stats/src/environments/environment.prod.ts"
}
],
"optimization": true,
@ -44,12 +44,12 @@
"executor": "@nrwl/web:dev-server",
"options": {
"port": 3010,
"buildTarget": "stats-mainnet:build",
"buildTarget": "stats:build",
"hmr": true
},
"configurations": {
"production": {
"buildTarget": "stats-mainnet:build:production",
"buildTarget": "stats:build:production",
"hmr": false
}
}
@ -58,14 +58,14 @@
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["apps/stats-mainnet/**/*.{ts,tsx,js,jsx}"]
"lintFilePatterns": ["apps/stats/**/*.{ts,tsx,js,jsx}"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/apps/stats-mainnet"],
"outputs": ["coverage/apps/stats"],
"options": {
"jestConfig": "apps/stats-mainnet/jest.config.js",
"jestConfig": "apps/stats/jest.config.js",
"passWithNoTests": true
}
}

View File

@ -1,6 +1,6 @@
import React, { useState } from 'react';
import { Header } from './components/header';
import { StatsManager } from '@vegaprotocol/mainnet-stats-manager';
import { StatsManager } from '@vegaprotocol/network-stats';
function App() {
const [darkMode, setDarkMode] = useState<boolean>(

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB

View File

Before

Width:  |  Height:  |  Size: 547 B

After

Width:  |  Height:  |  Size: 547 B

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import './styles/styles.css';
import App from './app';
import reportWebVitals from './reportWebVitals';
import reportWebVitals from './report-web-vitals';
ReactDOM.render(
<React.StrictMode>

View File

@ -1,7 +0,0 @@
# mainnet-stats-manager
This library was generated with [Nx](https://nx.dev).
## Running unit tests
Run `nx test mainnet-stats-manager` to execute the unit tests via [Jest](https://jestjs.io).

View File

@ -1,4 +0,0 @@
{
"name": "@vegaprotocol/mainnet-stats-manager",
"version": "0.0.1"
}

View File

@ -0,0 +1,7 @@
# network-stats
This library was generated with [Nx](https://nx.dev).
## Running unit tests
Run `nx test network-stats` to execute the unit tests via [Jest](https://jestjs.io).

View File

@ -1,9 +1,9 @@
module.exports = {
displayName: 'mainnet-stats-manager',
displayName: 'network-stats',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]sx?$': 'babel-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/mainnet-stats-manager',
coverageDirectory: '../../coverage/libs/network-stats',
};

View File

@ -0,0 +1,4 @@
{
"name": "@vegaprotocol/network-stats",
"version": "0.0.1"
}

View File

@ -1,6 +1,6 @@
{
"root": "libs/mainnet-stats-manager",
"sourceRoot": "libs/mainnet-stats-manager/src",
"root": "libs/network-stats",
"sourceRoot": "libs/network-stats/src",
"projectType": "library",
"tags": [],
"targets": {
@ -8,16 +8,16 @@
"executor": "@nrwl/web:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/mainnet-stats-manager",
"tsConfig": "libs/mainnet-stats-manager/tsconfig.lib.json",
"project": "libs/mainnet-stats-manager/package.json",
"entryFile": "libs/mainnet-stats-manager/src/index.ts",
"outputPath": "dist/libs/network-stats",
"tsConfig": "libs/network-stats/tsconfig.lib.json",
"project": "libs/network-stats/package.json",
"entryFile": "libs/network-stats/src/index.ts",
"external": ["react/jsx-runtime"],
"rollupConfig": "@nrwl/react/plugins/bundle-rollup",
"compiler": "babel",
"assets": [
{
"glob": "libs/mainnet-stats-manager/README.md",
"glob": "libs/network-stats/README.md",
"input": ".",
"output": "."
}
@ -28,14 +28,14 @@
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/mainnet-stats-manager/**/*.{ts,tsx,js,jsx}"]
"lintFilePatterns": ["libs/network-stats/**/*.{ts,tsx,js,jsx}"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/libs/mainnet-stats-manager"],
"outputs": ["coverage/libs/network-stats"],
"options": {
"jestConfig": "libs/mainnet-stats-manager/jest.config.js",
"jestConfig": "libs/network-stats/jest.config.js",
"passWithNoTests": true
}
}

View File

@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useState, useRef } from 'react';
import classnames from 'classnames';
import { statsFields } from '../../config/stats-fields';
import {
@ -14,16 +14,81 @@ interface statsManagerProps {
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<dataSourceProps>(getDataSource());
const [data, setData] = useState<IStructuredStats | null>(null);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
getDataSource();
async function getStats() {
try {
const [res1, res2] = await Promise.all([
fetch('https://api.token.vega.xyz/statistics'),
fetch('https://api.token.vega.xyz/nodes-data'),
fetch(dataSource.current.stats),
fetch(dataSource.current.nodes),
]);
const [{ statistics }, { nodeData }] = await Promise.all([
res1.json(),
@ -79,7 +144,8 @@ export const StatsManager = ({ className }: statsManagerProps) => {
return (
<div className={classes}>
<h3 className="font-alpha uppercase text-h3 pb-16 col-span-full">
{(error && `/ ${error}`) || (data ? '/ Mainnet' : '/ Connecting...')}
{(error && `/ ${error}`) ||
(data ? `/ ${dataSource.current.title}` : '/ Connecting...')}
</h3>
{data?.promoted ? (

View File

@ -18,6 +18,7 @@ export interface Stats {
chainId: string;
}
// @ts-ignore investigate why series of 'ors' didn't work instead of this 'any'
export type value = any;
export type goodThreshold = (...args: value[]) => boolean;

View File

@ -17,10 +17,8 @@
"paths": {
"@vegaprotocol/deal-ticket": ["libs/deal-ticket/src/index.ts"],
"@vegaprotocol/graphql": ["libs/graphql/src/index.ts"],
"@vegaprotocol/mainnet-stats-manager": [
"libs/mainnet-stats-manager/src/index.ts"
],
"@vegaprotocol/market-list": ["libs/market-list/src/index.ts"],
"@vegaprotocol/network-stats": ["libs/network-stats/src/index.ts"],
"@vegaprotocol/react-helpers": ["libs/react-helpers/src/index.ts"],
"@vegaprotocol/tailwindcss-config": [
"libs/tailwindcss-config/src/index.js"

View File

@ -5,11 +5,11 @@
"explorer": "apps/explorer",
"explorer-e2e": "apps/explorer-e2e",
"graphql": "libs/graphql",
"mainnet-stats-manager": "libs/mainnet-stats-manager",
"market-list": "libs/market-list",
"network-stats": "libs/network-stats",
"react-helpers": "libs/react-helpers",
"stats-mainnet": "apps/stats-mainnet",
"stats-mainnet-e2e": "apps/stats-mainnet-e2e",
"stats": "apps/stats",
"stats-e2e": "apps/stats-e2e",
"tailwindcss-config": "libs/tailwindcss-config",
"trading": "apps/trading",
"trading-e2e": "apps/trading-e2e",