From 57ab41de99ac117d11d0284cb621a83dfce229b6 Mon Sep 17 00:00:00 2001 From: Noah Gundotra Date: Tue, 19 Nov 2024 08:32:08 -0500 Subject: [PATCH 1/3] use validators.app info for mainnet & devnet --- app/api/ping/[network]/route.ts | 49 ++++ app/components/LiveTransactionStatsCard.tsx | 232 +++++++++++++++++- .../stats/ValidatorsAppStatsProvider.tsx | 154 ++++++++++++ app/providers/stats/index.tsx | 5 +- 4 files changed, 426 insertions(+), 14 deletions(-) create mode 100644 app/api/ping/[network]/route.ts create mode 100644 app/providers/stats/ValidatorsAppStatsProvider.tsx diff --git a/app/api/ping/[network]/route.ts b/app/api/ping/[network]/route.ts new file mode 100644 index 00000000..036298d6 --- /dev/null +++ b/app/api/ping/[network]/route.ts @@ -0,0 +1,49 @@ +import { fetch } from 'cross-fetch'; +import { NextResponse } from 'next/server'; + +type Params = { + params: { + network: 'mainnet' | 'devnet'; + }; +}; + +export type ValidatorsAppPingStats = { + interval: number; + max: number; + median: number; + min: number; + network: string; + num_of_records: number; + time_from: string; + average_slot_latency: number; + tps: number; +}; + +const PING_INTERVALS: number[] = [1, 3, 12]; + +export async function GET(_request: Request, { params: { network } }: Params) { + const responses = await Promise.all( + PING_INTERVALS.map(interval => + fetch(`https://www.validators.app/api/v1/ping-thing-stats/${network}.json?interval=${interval}`, { + cache: 'no-store', + headers: { + 'Cache-Control': 'no-store, max-age=0', + Token: process.env.PING_API_KEY || '', + }, + }) + ) + ); + const data: { [interval: number]: ValidatorsAppPingStats[] } = {}; + await Promise.all( + responses.map(async (response, index) => { + const interval = PING_INTERVALS[index]; + data[interval] = await response.json(); + }) + ); + + return NextResponse.json(data, { + headers: { + 'Cache-Control': 'no-store, max-age=0', + }, + }); +} diff --git a/app/components/LiveTransactionStatsCard.tsx b/app/components/LiveTransactionStatsCard.tsx index ec46e784..d9feb8c9 100644 --- a/app/components/LiveTransactionStatsCard.tsx +++ b/app/components/LiveTransactionStatsCard.tsx @@ -12,6 +12,14 @@ import { Bar } from 'react-chartjs-2'; import CountUp from 'react-countup'; import { RefreshCw } from 'react-feather'; +import { useCluster } from '../providers/cluster'; +import { + useValidatorsAppPingStats, + ValidatorsAppPingStatsInfo, + ValidatorsAppPingStatsRecord, +} from '../providers/stats/ValidatorsAppStatsProvider'; +import { Cluster } from '../utils/cluster'; + Chart.register(BarElement, CategoryScale, LinearScale, Tooltip); type Series = 'short' | 'medium' | 'long'; @@ -34,24 +42,23 @@ const SERIES_INFO = { export function LiveTransactionStatsCard() { const [series, setSeries] = React.useState('short'); + const { cluster } = useCluster(); return (

Live Transaction Stats

- -
- Note: We are aware of an issue with ping statistic reporting. Ping statistics may not reflect actual - network performance. Please see{' '} - - validators.app - {' '} - for more information. -
- + {cluster === Cluster.MainnetBeta || cluster === Cluster.Devnet ? ( + <> + + + + ) : ( + <> + + + + )}
); } @@ -66,6 +73,28 @@ function TpsCardBody({ series, setSeries }: { series: Series; setSeries: SetSeri return ; } +function ValidatorsAppTpsCardBody({ series, setSeries }: { series: Series; setSeries: SetSeries }) { + const performanceInfo = usePerformanceInfo(); + const statsInfo = useValidatorsAppPingStats(); + + if (performanceInfo.status !== ClusterStatsStatus.Ready || statsInfo.status !== PingStatus.Ready) { + return ( + + ); + } + + return ( + + ); +} + const TPS_CHART_OPTIONS = (historyMaxTps: number): ChartOptions<'bar'> => { return { animation: false, @@ -157,6 +186,84 @@ const TPS_CHART_OPTIONS = (historyMaxTps: number): ChartOptions<'bar'> => { }; }; +type ValidatorsAppTpsBarChartProps = { + performanceInfo: PerformanceInfo; + statsInfo: ValidatorsAppPingStatsInfo; + series: Series; + setSeries: SetSeries; +}; +function ValidatorsAppTpsBarChart({ statsInfo, performanceInfo, series, setSeries }: ValidatorsAppTpsBarChartProps) { + const seriesData = statsInfo[series] || []; + const avgTps = Math.round( + (statsInfo[SERIES[2]] || [])?.map(val => val.tps).reduce((a, b) => a + b, 0) / seriesData.length + ); + const historyMaxTps = (statsInfo[SERIES[2]] || [])?.map(val => val.tps).reduce((a, b) => Math.max(a, b), 0); + const averageTps = Math.round(avgTps).toLocaleString('en-US'); + const transactionCount = ; + const chartOptions = React.useMemo>(() => TPS_CHART_OPTIONS(historyMaxTps), [historyMaxTps]); + + const now = new Date(); + const chartData: ChartData<'bar'> = { + datasets: [ + { + backgroundColor: '#00D192', + borderWidth: 0, + data: seriesData.map(val => (val.tps >= historyMaxTps ? avgTps : val.tps || 0)), + hoverBackgroundColor: '#00D192', + }, + ], + labels: seriesData.map(val => { + const minAgo = Math.round((now.getTime() - val.timestamp.getTime()) / 60000); + return `${minAgo}min ago`; + }), + }; + + return ( + <> + + + Transaction count + {transactionCount} + + + Transactions per second (TPS) + {averageTps} + + + +
+ +
+
+
+ TPS history + +
+ {SERIES.map(key => ( + + ))} +
+
+ +
+
+ +
+
+
+
+ + ); +} + type TpsBarChartProps = { performanceInfo: PerformanceInfo; series: Series; @@ -287,6 +394,16 @@ function PingStatsCardBody({ series, setSeries }: { series: Series; setSeries: S return ; } +function ValidatorsAppPingStatsCardBody({ series, setSeries }: { series: Series; setSeries: SetSeries }) { + const pingInfo = useValidatorsAppPingStats(); + + if (pingInfo.status !== PingStatus.Ready) { + return ; + } + + return ; +} + type StatsNotReadyProps = { error: boolean; retry?: () => void }; function PingStatsNotReady({ error, retry }: StatsNotReadyProps) { if (error) { @@ -498,3 +615,92 @@ function PingBarChart({ ); } + +function ValidatorsAppPingBarChart({ + pingInfo, + series, + setSeries, +}: { + pingInfo: ValidatorsAppPingStatsInfo; + series: Series; + setSeries: SetSeries; +}) { + const seriesData = pingInfo[series] || []; + const maxMedian = seriesData.reduce((a, b) => { + return Math.max(a, b.median); + }, 0); + const backgroundColor = (val: ValidatorsAppPingStatsRecord) => { + if (val.submitted === 0) { + return '#08a274'; + } + + return '#00D192'; + }; + const now = new Date(); + const chartData: ChartData<'bar'> = { + datasets: [ + { + backgroundColor: seriesData.map(backgroundColor), + borderWidth: 0, + data: seriesData.map(val => { + if (val.submitted === 0) { + return maxMedian * 0.5; + } + return val.median || 0; + }), + hoverBackgroundColor: seriesData.map(backgroundColor), + minBarLength: 2, + }, + ], + labels: seriesData.map(val => { + const minutesAgo = Math.round((now.getTime() - val.timestamp.getTime()) / (60 * 1000)); + if (val.submitted === 0) { + return ` +
+

Ping statistics unavailable

+ ${minutesAgo} min ago +
+ `; + } + + return ` +
${val.median} ms
+
+

${val.submitted} confirmed

+ ${`

${val.average_slot_latency.toLocaleString(undefined)} Average Slot Latency

`} + ${minutesAgo}min ago +
+ `; + }), + }; + + return ( +
+
+
+ Average Ping Time + +
+ {SERIES.map(key => ( + + ))} +
+
+ +
+
+ +
+
+
+
+ ); +} diff --git a/app/providers/stats/ValidatorsAppStatsProvider.tsx b/app/providers/stats/ValidatorsAppStatsProvider.tsx new file mode 100644 index 00000000..10731e01 --- /dev/null +++ b/app/providers/stats/ValidatorsAppStatsProvider.tsx @@ -0,0 +1,154 @@ +'use client'; + +import { useCluster } from '@providers/cluster'; +import { useStatsProvider } from '@providers/stats/solanaClusterStats'; +import { Cluster } from '@utils/cluster'; +import { fetch } from 'cross-fetch'; +import React from 'react'; +import useTabVisibility from 'use-tab-visibility'; + +import { ValidatorsAppPingStats } from '@/app/api/ping/[network]/route'; + +const PING_INTERVALS: number[] = [1, 3, 12]; + +const FETCH_PING_INTERVAL = 60 * 1000; + +function getClusterSlug(cluster: Cluster) { + switch (cluster) { + case Cluster.MainnetBeta: + return 'mainnet'; + case Cluster.Devnet: + return 'devnet'; + default: + throw new Error(`Invalid cluster: ${cluster}`); + } +} + +function getUrl(cluster: Cluster) { + return `/api/ping/${getClusterSlug(cluster)}`; +} + +export enum PingStatus { + Loading, + Ready, + Error, +} + +export type ValidatorsAppPingStatsInfo = { + status: PingStatus; + short?: ValidatorsAppPingStatsRecord[]; + medium?: ValidatorsAppPingStatsRecord[]; + long?: ValidatorsAppPingStatsRecord[]; + retry?: () => void; +}; + +export type ValidatorsAppPingStatsRecord = { + tps: number; + median: number; + submitted: number; + average_slot_latency: number; + timestamp: Date; +}; + +const ValidatorsAppStatsContext = React.createContext(undefined); + +type Props = { children: React.ReactNode }; + +export function ValidatorsAppStatsProvider({ children }: Props) { + const { cluster } = useCluster(); + const { active } = useStatsProvider(); + const [rollup, setRollup] = React.useState({ + status: PingStatus.Loading, + }); + const { visible: isTabVisible } = useTabVisibility(); + React.useEffect(() => { + if (!active || !isTabVisible) { + return; + } + + if (cluster === Cluster.Testnet || cluster === Cluster.Custom) { + return; + } + + const url = getUrl(cluster); + + setRollup({ + status: PingStatus.Loading, + }); + + if (!url) { + return; + } + let stale = false; + const fetchPingMetrics = async () => { + try { + const response = await fetch(url, { + cache: 'no-store', + headers: { + 'Cache-Control': 'no-store, max-age=0', + }, + }); + if (stale) { + return; + } + const json: { [interval: number]: ValidatorsAppPingStats[] } = await response.json(); + if (stale) { + return; + } + const simplify = (data: ValidatorsAppPingStats[]) => + data.map( + ({ tps, median, time_from, num_of_records, average_slot_latency }: ValidatorsAppPingStats) => { + return { + average_slot_latency, + median, + submitted: num_of_records, + timestamp: new Date(time_from), + tps, + }; + } + ); + + const short = simplify(json[PING_INTERVALS[0]]).slice(-30); + const medium = simplify(json[PING_INTERVALS[1]]).slice(-30); + const long = simplify(json[PING_INTERVALS[2]]).slice(-30); + + setRollup({ + long, + medium, + short, + status: PingStatus.Ready, + }); + } catch { + setRollup({ + retry: () => { + setRollup({ + status: PingStatus.Loading, + }); + + fetchPingMetrics(); + }, + status: PingStatus.Error, + }); + } + }; + + const fetchPingInterval = setInterval(() => { + fetchPingMetrics(); + }, FETCH_PING_INTERVAL); + fetchPingMetrics(); + return () => { + clearInterval(fetchPingInterval); + stale = true; + }; + }, [active, cluster, isTabVisible]); + + return {children}; +} + +export function useValidatorsAppPingStats() { + const context = React.useContext(ValidatorsAppStatsContext); + if (!context) { + throw new Error(`useContext must be used within a StatsProvider`); + } + return context; +} diff --git a/app/providers/stats/index.tsx b/app/providers/stats/index.tsx index 6b44420c..f3e76a3f 100644 --- a/app/providers/stats/index.tsx +++ b/app/providers/stats/index.tsx @@ -2,12 +2,15 @@ import { SolanaPingProvider } from '@providers/stats/SolanaPingProvider'; import React from 'react'; import { SolanaClusterStatsProvider } from './solanaClusterStats'; +import { ValidatorsAppStatsProvider } from './ValidatorsAppStatsProvider'; type Props = { children: React.ReactNode }; export function StatsProvider({ children }: Props) { return ( - {children} + + {children} + ); } From 4aa6452d470596c2a02a1ddb9f08d09e9c7f98a0 Mon Sep 17 00:00:00 2001 From: Noah Gundotra Date: Wed, 20 Nov 2024 14:09:46 -0500 Subject: [PATCH 2/3] keep normal TPS chart for mainnet/devnet --- app/api/ping/[network]/route.ts | 2 +- app/components/LiveTransactionStatsCard.tsx | 12 ++++-------- app/providers/stats/ValidatorsAppStatsProvider.tsx | 7 +------ 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/app/api/ping/[network]/route.ts b/app/api/ping/[network]/route.ts index 036298d6..cff51133 100644 --- a/app/api/ping/[network]/route.ts +++ b/app/api/ping/[network]/route.ts @@ -43,7 +43,7 @@ export async function GET(_request: Request, { params: { network } }: Params) { return NextResponse.json(data, { headers: { - 'Cache-Control': 'no-store, max-age=0', + 'Cache-Control': 'public, max-age=30', }, }); } diff --git a/app/components/LiveTransactionStatsCard.tsx b/app/components/LiveTransactionStatsCard.tsx index d9feb8c9..fd7578bd 100644 --- a/app/components/LiveTransactionStatsCard.tsx +++ b/app/components/LiveTransactionStatsCard.tsx @@ -48,16 +48,11 @@ export function LiveTransactionStatsCard() {

Live Transaction Stats

+ {cluster === Cluster.MainnetBeta || cluster === Cluster.Devnet ? ( - <> - - - + ) : ( - <> - - - + )} ); @@ -73,6 +68,7 @@ function TpsCardBody({ series, setSeries }: { series: Series; setSeries: SetSeri return ; } +// eslint-disable-next-line @typescript-eslint/no-unused-vars function ValidatorsAppTpsCardBody({ series, setSeries }: { series: Series; setSeries: SetSeries }) { const performanceInfo = usePerformanceInfo(); const statsInfo = useValidatorsAppPingStats(); diff --git a/app/providers/stats/ValidatorsAppStatsProvider.tsx b/app/providers/stats/ValidatorsAppStatsProvider.tsx index 10731e01..816476a5 100644 --- a/app/providers/stats/ValidatorsAppStatsProvider.tsx +++ b/app/providers/stats/ValidatorsAppStatsProvider.tsx @@ -82,12 +82,7 @@ export function ValidatorsAppStatsProvider({ children }: Props) { let stale = false; const fetchPingMetrics = async () => { try { - const response = await fetch(url, { - cache: 'no-store', - headers: { - 'Cache-Control': 'no-store, max-age=0', - }, - }); + const response = await fetch(url); if (stale) { return; } From 3238c40f12db11d0ef90b9688115e124cc0d831f Mon Sep 17 00:00:00 2001 From: Noah Gundotra Date: Wed, 20 Nov 2024 14:14:19 -0500 Subject: [PATCH 3/3] restrict to just mainnet --- app/api/ping/[network]/route.ts | 2 +- app/components/LiveTransactionStatsCard.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api/ping/[network]/route.ts b/app/api/ping/[network]/route.ts index cff51133..97d2ecfe 100644 --- a/app/api/ping/[network]/route.ts +++ b/app/api/ping/[network]/route.ts @@ -3,7 +3,7 @@ import { NextResponse } from 'next/server'; type Params = { params: { - network: 'mainnet' | 'devnet'; + network: 'mainnet'; }; }; diff --git a/app/components/LiveTransactionStatsCard.tsx b/app/components/LiveTransactionStatsCard.tsx index fd7578bd..5d92c950 100644 --- a/app/components/LiveTransactionStatsCard.tsx +++ b/app/components/LiveTransactionStatsCard.tsx @@ -49,7 +49,7 @@ export function LiveTransactionStatsCard() {

Live Transaction Stats

- {cluster === Cluster.MainnetBeta || cluster === Cluster.Devnet ? ( + {cluster === Cluster.MainnetBeta ? ( ) : (