From 61edf51f70e23559825d4c3f4d5071af94773cf9 Mon Sep 17 00:00:00 2001 From: Tom Bertrand Date: Mon, 8 Jan 2024 21:43:27 -0500 Subject: [PATCH] Update index.tsx --- server/client/mongo/schema.js | 10 +-- server/constants.js | 10 ++- server/cron/ws.js | 123 ++++++++++++++++++++------ server/utils/index.js | 2 +- server/ws/index.js | 1 + src/api/contexts/MarketStatistics.tsx | 3 + src/i18n/locales/en.json | 4 +- src/pages/Home/index.tsx | 90 ++++++++++++------- 8 files changed, 174 insertions(+), 69 deletions(-) diff --git a/server/client/mongo/schema.js b/server/client/mongo/schema.js index acd37125..edd625dd 100644 --- a/server/client/mongo/schema.js +++ b/server/client/mongo/schema.js @@ -3,8 +3,8 @@ const db = require("./"); const { EXPIRE_1M, EXPIRE_48H, - EXPIRE_1W, - EXPIRE_2W, + EXPIRE_7D, + EXPIRE_14D, TOTAL_CONFIRMATIONS_COLLECTION, TOTAL_VOLUME_COLLECTION, LARGE_TRANSACTIONS, @@ -24,7 +24,7 @@ async function createIndexes() { if (!indexExists1) { database .collection(LARGE_TRANSACTIONS) - .createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_1W, ...extraOptions }); + .createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_7D, ...extraOptions }); console.log("Index LARGE_TRANSACTIONS createdAt created successfully"); } @@ -42,7 +42,7 @@ async function createIndexes() { if (!indexExists3) { database .collection(TOTAL_CONFIRMATIONS_COLLECTION) - .createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_2W, ...extraOptions }); + .createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_14D, ...extraOptions }); console.log("Index TOTAL_CONFIRMATIONS_COLLECTION createdAt created successfully"); } @@ -50,7 +50,7 @@ async function createIndexes() { if (!indexExists4) { database .collection(TOTAL_VOLUME_COLLECTION) - .createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_48H, ...extraOptions }); + .createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_14D, ...extraOptions }); console.log("Index TOTAL_VOLUME_COLLECTION createdAt created successfully"); } diff --git a/server/constants.js b/server/constants.js index f79febd6..1e5f031e 100644 --- a/server/constants.js +++ b/server/constants.js @@ -5,8 +5,8 @@ const EXPIRE_1H = 3600; const EXPIRE_6H = 3600 * 6; const EXPIRE_24H = EXPIRE_1H * 24; const EXPIRE_48H = EXPIRE_24H * 2; -const EXPIRE_1W = EXPIRE_24H * 7; -const EXPIRE_2W = EXPIRE_24H * 14; +const EXPIRE_7D = EXPIRE_24H * 7; +const EXPIRE_14D = EXPIRE_24H * 14; const EXPIRE_1Y = EXPIRE_24H * 365; const EXPIRE_5Y = EXPIRE_24H * 365 * 5; const TOTAL_CONFIRMATIONS_COLLECTION = "TOTAL_CONFIRMATIONS"; @@ -18,6 +18,7 @@ const TOTAL_VOLUME_COLLECTION = "TOTAL_VOLUME"; const TOTAL_VOLUME_24H = "TOTAL_VOLUME_24H"; const TOTAL_VOLUME_7D = "TOTAL_VOLUME_7D"; const TOTAL_VOLUME_48H = "TOTAL_VOLUME_48H"; +const TOTAL_VOLUME_14D = "TOTAL_VOLUME_14D"; const MARKET_CAP_RANK_COLLECTION = "MARKET_CAP_RANK"; const MARKET_CAP_STATS_COLLECTION = "MARKET_CAP_STATS"; const MARKET_CAP_RANK = "MARKET_CAP_RANK"; @@ -78,8 +79,8 @@ module.exports = { EXPIRE_6H, EXPIRE_24H, EXPIRE_48H, - EXPIRE_1W, - EXPIRE_2W, + EXPIRE_7D, + EXPIRE_14D, EXPIRE_1Y, EXPIRE_5Y, TOTAL_CONFIRMATIONS_COLLECTION, @@ -91,6 +92,7 @@ module.exports = { TOTAL_VOLUME_24H, TOTAL_VOLUME_7D, TOTAL_VOLUME_48H, + TOTAL_VOLUME_14D, MARKET_CAP_RANK_COLLECTION, MARKET_CAP_STATS_COLLECTION, MARKET_CAP_RANK, diff --git a/server/cron/ws.js b/server/cron/ws.js index 8dfa10bc..c7d256d5 100644 --- a/server/cron/ws.js +++ b/server/cron/ws.js @@ -8,6 +8,8 @@ const { EXPIRE_1M, EXPIRE_24H, EXPIRE_1W, + EXPIRE_7D, + EXPIRE_14D, EXPIRE_48H, TOTAL_CONFIRMATIONS_COLLECTION, TOTAL_CONFIRMATIONS_24H, @@ -16,7 +18,9 @@ const { TOTAL_CONFIRMATIONS_14D, TOTAL_VOLUME_COLLECTION, TOTAL_VOLUME_24H, + TOTAL_VOLUME_7D, TOTAL_VOLUME_48H, + TOTAL_VOLUME_14D, CONFIRMATIONS_PER_SECOND, } = require("../constants"); const { rawToRai } = require("../utils"); @@ -36,7 +40,7 @@ cron.schedule("*/3 * * * * *", async () => { { $match: { createdAt: { - $lt: new Date(Date.now() - EXPIRE_1M * 1000), + $gte: new Date(Date.now() - EXPIRE_1M * 1000), }, }, }, @@ -71,32 +75,32 @@ cron.schedule("*/10 * * * * *", async () => { { $match: { createdAt: { - $lt: new Date(Date.now() - EXPIRE_24H * 1000), + $gte: new Date(Date.now() - EXPIRE_24H * 1000), }, }, }, - { $group: { _id: null, totalConfirmations: { $sum: "$value" } } }, + { $group: { _id: null, totalConfirmations24h: { $sum: "$value" } } }, ]) .toArray() - .then(([{ totalConfirmations = 0 } = {}]) => { - nodeCache.set(TOTAL_CONFIRMATIONS_24H, totalConfirmations); + .then(([{ totalConfirmations24h = 0 } = {}]) => { + nodeCache.set(TOTAL_CONFIRMATIONS_24H, totalConfirmations24h); }); -// conf 7d - database + // conf 7d + database .collection(TOTAL_CONFIRMATIONS_COLLECTION) .aggregate([ { $match: { createdAt: { - $lt: new Date(Date.now() - EXPIRE_1W * 1000), + $gte: new Date(Date.now() - EXPIRE_1W * 1000), }, }, }, - { $group: { _id: null, totalConfirmations14d: { $sum: "$value" } } }, + { $group: { _id: null, totalConfirmations7d: { $sum: "$value" } } }, ]) .toArray() - .then(([{ totalConfirmations14d = 0 } = {}]) => { - nodeCache.set(TOTAL_CONFIRMATIONS_7D, totalConfirmations14d); + .then(([{ totalConfirmations7d = 0 } = {}]) => { + nodeCache.set(TOTAL_CONFIRMATIONS_7D, totalConfirmations7d); }); database @@ -105,25 +109,25 @@ cron.schedule("*/10 * * * * *", async () => { { $match: { createdAt: { - $lt: new Date(Date.now() - EXPIRE_48H * 1000), + $gte: new Date(Date.now() - EXPIRE_48H * 1000), }, }, }, - { $group: { _id: null, totalConfirmations: { $sum: "$value" } } }, + { $group: { _id: null, totalConfirmations48h: { $sum: "$value" } } }, ]) .toArray() - .then(([{ totalConfirmations = 0 } = {}]) => { - nodeCache.set(TOTAL_CONFIRMATIONS_48H, totalConfirmations); + .then(([{ totalConfirmations48h = 0 } = {}]) => { + nodeCache.set(TOTAL_CONFIRMATIONS_48H, totalConfirmations48h); }); - //conf 1w - database + //conf 1w + database .collection(TOTAL_CONFIRMATIONS_COLLECTION) .aggregate([ { $match: { createdAt: { - $lt: new Date(Date.now() - EXPIRE_1W * 1000), + $gte: new Date(Date.now() - EXPIRE_14D * 1000), }, }, }, @@ -140,15 +144,32 @@ cron.schedule("*/10 * * * * *", async () => { { $match: { createdAt: { - $lt: new Date(Date.now() - EXPIRE_24H * 1000), + $gte: new Date(Date.now() - EXPIRE_24H * 1000), + }, + }, + }, + { $group: { _id: null, totalVolume24h: { $sum: "$value" } } }, + ]) + .toArray() + .then(([{ totalVolume24h = 0 } = {}]) => { + nodeCache.set(TOTAL_VOLUME_24H, rawToRai(totalVolume24h)); + }); + + database + .collection(TOTAL_VOLUME_COLLECTION) + .aggregate([ + { + $match: { + createdAt: { + $gte: new Date(Date.now() - EXPIRE_7D * 1000), }, }, }, - { $group: { _id: null, totalVolume: { $sum: "$value" } } }, + { $group: { _id: null, totalVolume7d: { $sum: "$value" } } }, ]) .toArray() - .then(([{ totalVolume = 0 } = {}]) => { - nodeCache.set(TOTAL_VOLUME_24H, rawToRai(totalVolume)); + .then(([{ totalVolume7d = 0 } = {}]) => { + nodeCache.set(TOTAL_VOLUME_7D, rawToRai(totalVolume7d)); }); database @@ -157,15 +178,65 @@ cron.schedule("*/10 * * * * *", async () => { { $match: { createdAt: { - $lt: new Date(Date.now() - EXPIRE_48H * 1000), + $gte: new Date(Date.now() - TOTAL_VOLUME_14D * 1000), + }, + }, + }, + { $group: { _id: null, totalVolume14d: { $sum: "$value" } } }, + ]) + .toArray() + .then(([{ totalVolume14d = 0 } = {}]) => { + nodeCache.set(TOTAL_VOLUME_14D, rawToRai(totalVolume14d)); + }); + + database + .collection(TOTAL_VOLUME_COLLECTION) + .aggregate([ + { + $match: { + createdAt: { + $gte: new Date(Date.now() - EXPIRE_48H * 1000), + }, + }, + }, + { $group: { _id: null, totalVolume48h: { $sum: "$value" } } }, + ]) + .toArray() + .then(([{ totalVolume48h = 0 } = {}]) => { + nodeCache.set(TOTAL_VOLUME_48H, rawToRai(totalVolume48h)); + }); + + database + .collection(TOTAL_VOLUME_COLLECTION) + .aggregate([ + { + $match: { + createdAt: { + $gte: new Date(Date.now() - EXPIRE_7D * 1000), + }, + }, + }, + { $group: { _id: null, totalVolume7d: { $sum: "$value" } } }, + ]) + .toArray() + .then(([{ totalVolume7d = 0 } = {}]) => { + nodeCache.set(TOTAL_VOLUME_7D, rawToRai(totalVolume7d)); + }); + database + .collection(TOTAL_VOLUME_COLLECTION) + .aggregate([ + { + $match: { + createdAt: { + $gte: new Date(Date.now() - EXPIRE_14D * 1000), }, }, }, - { $group: { _id: null, totalVolume: { $sum: "$value" } } }, + { $group: { _id: null, totalVolume14d: { $sum: "$value" } } }, ]) .toArray() - .then(([{ totalVolume = 0 } = {}]) => { - nodeCache.set(TOTAL_VOLUME_48H, rawToRai(totalVolume)); + .then(([{ totalVolume14d = 0 } = {}]) => { + nodeCache.set(TOTAL_VOLUME_14D, rawToRai(totalVolume14d)); }); } catch (err) { Sentry.captureException(err, { diff --git a/server/utils/index.js b/server/utils/index.js index 7c59402e..801dbfe9 100644 --- a/server/utils/index.js +++ b/server/utils/index.js @@ -2,7 +2,7 @@ const BigNumber = require("bignumber.js"); const rawToRai = raw => { const value = new BigNumber(raw.toString()); - return value.shiftedBy(30 * -1).toNumber(); + return value.shiftedBy(30 * -1).toNumber() || 0; }; const raiToRaw = rai => { diff --git a/server/ws/index.js b/server/ws/index.js index 09ba9caf..690c7686 100644 --- a/server/ws/index.js +++ b/server/ws/index.js @@ -61,6 +61,7 @@ ws.onmessage = msg => { if (topic === "confirmation") { accumulatedConfirmations = accumulatedConfirmations + 1; + // 10,000 NANO if (subtype === "send" && amount.length >= 35) { diff --git a/src/api/contexts/MarketStatistics.tsx b/src/api/contexts/MarketStatistics.tsx index a576e678..cb7087b1 100644 --- a/src/api/contexts/MarketStatistics.tsx +++ b/src/api/contexts/MarketStatistics.tsx @@ -9,6 +9,7 @@ export const TOTAL_CONFIRMATIONS_7D = "TOTAL_CONFIRMATIONS_7D"; export const TOTAL_CONFIRMATIONS_14D = "TOTAL_CONFIRMATIONS_14D"; export const TOTAL_VOLUME_24H = "TOTAL_VOLUME_24H"; export const TOTAL_VOLUME_7D = "TOTAL_VOLUME_7D"; +export const TOTAL_VOLUME_14D = "TOTAL_VOLUME_14D"; export const TOTAL_CONFIRMATIONS_48H = "TOTAL_CONFIRMATIONS_48H"; export const TOTAL_VOLUME_48H = "TOTAL_VOLUME_48H"; export const BITCOIN_TOTAL_TRANSACTION_FEES_24H = "BITCOIN_TOTAL_TRANSACTION_FEES_24H"; @@ -26,6 +27,7 @@ export interface Response { [TOTAL_CONFIRMATIONS_48H]: number; [TOTAL_CONFIRMATIONS_14D]: number; [TOTAL_VOLUME_48H]: number; + [TOTAL_VOLUME_14D]: number; [BITCOIN_TOTAL_TRANSACTION_FEES_24H]: number; [BITCOIN_TOTAL_TRANSACTION_FEES_7D]: number; [BITCOIN_TOTAL_TRANSACTION_FEES_14D]: number; @@ -70,6 +72,7 @@ export const MarketStatisticsContext = React.createContext({ [TOTAL_VOLUME_7D]: 0, [TOTAL_CONFIRMATIONS_48H]: 0, [TOTAL_VOLUME_48H]: 0, + [TOTAL_VOLUME_14D]: 0, [BITCOIN_TOTAL_TRANSACTION_FEES_24H]: 0, [BITCOIN_TOTAL_TRANSACTION_FEES_7D]: 0, [BITCOIN_TOTAL_TRANSACTION_FEES_14D]: 0, diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index 327afb65..be228cb5 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -99,7 +99,9 @@ "senderFilter": "Filter blocks to include only or exclude all send transactions from a specific address. To filter multiple addresses, separate them with a comma.", "receiverFilter": "Filter blocks to include only or exclude all send transactions from a specific address. To filter multiple addresses, separate them with a comma.", "filterTransactions": "Only accounts with equal or less than 5,000 confirmed transactions can be filtered", - "averageTps": "On {{date}}, {{block_count}} {{block_type}} blocks were broadcasted at a rate of {{bps}} BPS (blocks per second) to measure the network efficiency. The network reached a P90 of {{cps_p90}} CPS (confirmations per seconds) across the nodes. For more details visit https://nanotps.net/." + "averageTps": "On {{date}}, {{block_count}} {{block_type}} blocks were broadcasted at a rate of {{bps}} BPS (blocks per second) to measure the network efficiency. The network reached a P90 of {{cps_p90}} CPS (confirmations per seconds) across the nodes. For more details visit https://nanotps.net/.", + "last24Hours": "Displays a comparison of data trends and metrics from the last 24 hours against those from the preceding 48 hours, providing insights into recent changes and patterns.", + "last7days": "Shows a comparative analysis of data and trends over the past 7 days against the previous 14 days, highlighting shifts and patterns over a broader time frame for a comprehensive view." }, "search": { "searchBy": "Search by Address / Block / Known Account", diff --git a/src/pages/Home/index.tsx b/src/pages/Home/index.tsx index ab888237..8bb60738 100644 --- a/src/pages/Home/index.tsx +++ b/src/pages/Home/index.tsx @@ -4,9 +4,8 @@ import { useMediaQuery } from "react-responsive"; import { Link } from "react-router-dom"; import { CheckOutlined, CloseOutlined } from "@ant-design/icons"; -import { Card, Col, Row, Switch } from "antd"; +import { Card, Col, Row, Switch, Tooltip } from "antd"; import BigNumber from "bignumber.js"; -import qs from "qs"; import { BlockCountContext } from "api/contexts/BlockCount"; import { ConfirmationHistoryContext } from "api/contexts/ConfirmationHistory"; @@ -20,6 +19,8 @@ import { TOTAL_CONFIRMATIONS_14D, TOTAL_CONFIRMATIONS_24H, TOTAL_CONFIRMATIONS_48H, + TOTAL_VOLUME_7D, + TOTAL_VOLUME_14D, TOTAL_VOLUME_24H, TOTAL_VOLUME_48H, } from "api/contexts/MarketStatistics"; @@ -28,6 +29,7 @@ import { CurrencyDecimal, CurrencySymbol, PreferencesContext } from "api/context import { RepresentativesContext } from "api/contexts/Representatives"; import useAvailableSupply from "api/hooks/use-available-supply"; import LoadingStatistic from "components/LoadingStatistic"; +import QuestionCircle from "components/QuestionCircle"; import StatisticsChange from "components/StatisticsChange"; import { formatBytes } from "components/utils"; @@ -47,7 +49,6 @@ const HomePage = () => { const urlParams = new URLSearchParams(new URL(currentUrl).search); let isFeatureActive = urlParams.get("7d"); - const { marketStatistics, isInitialLoading: isMarketStatisticsInitialLoading, @@ -128,42 +129,45 @@ const HomePage = () => { .times(100) .toNumber(); } - // Start here - let totalConfirmations14dAgo = 0; - let confirmationChange7d = 0; - let totalConfirmations7dAgo = 0; - let confirmationChange14d = 0; - if (marketStatistics[TOTAL_CONFIRMATIONS_7D] && marketStatistics[TOTAL_CONFIRMATIONS_7D]) { - totalConfirmations7dAgo = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_7D]) - .minus(marketStatistics[TOTAL_CONFIRMATIONS_7D]) + let totalConfirmations48hAgo = 0; + let confirmationChange24h = 0; + + if (marketStatistics[TOTAL_CONFIRMATIONS_24H] && marketStatistics[TOTAL_CONFIRMATIONS_48H]) { + totalConfirmations48hAgo = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_48H]) + .minus(marketStatistics[TOTAL_CONFIRMATIONS_24H]) .toNumber(); - confirmationChange14d = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_14D]) - .minus(totalConfirmations14dAgo) - .dividedBy(totalConfirmations14dAgo) + confirmationChange24h = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_24H]) + .minus(totalConfirmations48hAgo) + .dividedBy(totalConfirmations48hAgo) .times(100) .toNumber(); } - if (marketStatistics[TOTAL_CONFIRMATIONS_7D] && marketStatistics[TOTAL_CONFIRMATIONS_7D]) { - confirmationChange14d = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_7D]) - .minus(marketStatistics[TOTAL_CONFIRMATIONS_7D]) + // Start here + + let onChainVolume14dAgo = 0; + let onChainVolumeChange7d = 0; + if (marketStatistics[TOTAL_VOLUME_7D] && marketStatistics[TOTAL_VOLUME_14D]) { + onChainVolume14dAgo = new BigNumber(marketStatistics[TOTAL_VOLUME_14D]) + .minus(marketStatistics[TOTAL_VOLUME_7D]) .toNumber(); - confirmationChange7d = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_7D]) - .minus(totalConfirmations7dAgo) - .dividedBy(totalConfirmations7dAgo) + onChainVolumeChange7d = new BigNumber(marketStatistics[TOTAL_VOLUME_7D]) + .minus(onChainVolume14dAgo) + .dividedBy(onChainVolume14dAgo) .times(100) .toNumber(); } - let totalConfirmations48hAgo = 0; - let confirmationChange24h = 0; - if (marketStatistics[TOTAL_CONFIRMATIONS_24H] && marketStatistics[TOTAL_CONFIRMATIONS_48H]) { - totalConfirmations48hAgo = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_48H]) - .minus(marketStatistics[TOTAL_CONFIRMATIONS_24H]) + + let totalConfirmations14dAgo = 0; + let confirmationChange7d = 0; + if (marketStatistics[TOTAL_CONFIRMATIONS_7D] && marketStatistics[TOTAL_CONFIRMATIONS_14D]) { + totalConfirmations14dAgo = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_14D]) + .minus(marketStatistics[TOTAL_CONFIRMATIONS_7D]) .toNumber(); - confirmationChange24h = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_24H]) - .minus(totalConfirmations48hAgo) - .dividedBy(totalConfirmations48hAgo) + confirmationChange7d = new BigNumber(marketStatistics[TOTAL_CONFIRMATIONS_7D]) + .minus(totalConfirmations14dAgo) + .dividedBy(totalConfirmations14dAgo) .times(100) .toNumber(); } @@ -259,7 +263,19 @@ const HomePage = () => { > + {is24Hours ?t("pages.home.last24Hours"): t("pages.home.last7Days")} + + + + + + } extra={ isFeatureActive ? ( { } tooltip={t("tooltips.onChainVolume")} title={t("pages.home.onChainVolume")} - suffix={} - value={new BigNumber(marketStatistics[TOTAL_VOLUME_24H]) + suffix={ + + } + value={new BigNumber( + is24Hours + ? marketStatistics[TOTAL_VOLUME_24H] + : marketStatistics[TOTAL_VOLUME_7D], + ) .decimalPlaces(5) .toNumber()} /> @@ -292,7 +317,8 @@ const HomePage = () => { isLoading={ isMarketStatisticsInitialLoading || isMarketStatisticsError || - !confirmationChange24h + !confirmationChange24h || + !confirmationChange7d } title={t("pages.home.confirmedTransactions")} suffix={