forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerformanceStats.tsx
More file actions
204 lines (195 loc) · 7.28 KB
/
Copy pathPerformanceStats.tsx
File metadata and controls
204 lines (195 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import React, { useId, useMemo } from "react";
import {
Gauge,
Pause,
Play,
RotateCcw,
TrendingUp,
Zap,
} from "lucide-react";
import { Card } from "./Card";
import { usePerformanceStats } from "../hooks/usePerformanceStats";
import type { PerformanceSample } from "../types";
const MetricCard: React.FC<{
label: string;
value: string;
hint?: string;
icon: React.ReactNode;
tone?: "default" | "warning" | "success";
}> = ({ label, value, hint, icon, tone = "default" }) => {
const toneClass =
tone === "warning"
? "text-amber-600 dark:text-amber-300"
: tone === "success"
? "text-emerald-600 dark:text-emerald-300"
: "text-blue-600 dark:text-blue-300";
return (
<div className="p-4 rounded-lg border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/30 transition-colors duration-150 hover:border-blue-300 dark:hover:border-blue-600/60">
<div className="flex items-center justify-between mb-2">
<span className="text-xs uppercase tracking-wide text-gray-500 dark:text-gray-400">{label}</span>
<span className={"p-1.5 rounded-md bg-white dark:bg-gray-800 " + toneClass}>{icon}</span>
</div>
<div className="text-2xl font-semibold tabular-nums text-gray-900 dark:text-gray-50">{value}</div>
{hint && <div className="mt-1 text-[11px] text-gray-500 dark:text-gray-400">{hint}</div>}
</div>
);
};
interface SparklineProps {
samples: PerformanceSample[];
series: "tps" | "baseFee";
width?: number;
height?: number;
}
/**
* Sparkline — compact SVG visualisation of recent values. Pure-SVG keeps
* the dashboard dependency-free while still showing trend information.
*/
const Sparkline: React.FC<SparklineProps> = ({
samples,
series,
width = 280,
height = 64,
}) => {
const path = useMemo(() => {
if (samples.length < 2) return "";
const values = samples.map((s) => (series === "tps" ? s.tps : s.baseFee));
const max = Math.max(...values, series === "tps" ? 1 : 100);
const min = Math.min(...values, 0);
const span = max - min || 1;
const step = width / (samples.length - 1);
return values
.map((v, i) => {
const x = i * step;
const y = height - ((v - min) / span) * (height - 4) - 2;
return `${i === 0 ? "M" : "L"}${x.toFixed(2)},${y.toFixed(2)}`;
})
.join(" ");
}, [samples, series, width, height]);
const stroke = series === "tps" ? "#3b82f6" : "#10b981";
return (
<svg
viewBox={`0 0 ${width} ${height}`}
width="100%"
height={height}
role="img"
aria-label={`Sparkline of ${series}`}
className="block"
>
<path d={path} fill="none" stroke={stroke} strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" />
</svg>
);
};
/**
* PerformanceStats — TPS/Gas visualizer.
*
* Acceptance criteria from issue #36:
* - Stats service (usePerformanceStats hook)
* - Stats visible (metric cards + sparklines)
* - Metrics accurate (deterministic when paused)
*/
export const PerformanceStats: React.FC = () => {
const stats = usePerformanceStats();
// Stable ids for linking the sparklines to a screen-reader description
// so users get context without overwhelming the rendered output.
const panelStatusId = useId();
return (
<Card
title="Network Performance"
description="Live throughput and gas metrics streamed from the connected RPC."
actions={
<div className="flex items-center gap-2">
<button
type="button"
onClick={stats.toggle}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium rounded-md border border-gray-300 dark:border-gray-600 hover:border-blue-400 dark:hover:border-blue-500 transition-colors duration-150 focus:outline-none focus:ring-2 focus:ring-blue-500/60"
aria-pressed={!stats.running}
>
{stats.running ? <Pause size={14} /> : <Play size={14} />}
{stats.running ? "Pause" : "Resume"}
</button>
<button
type="button"
onClick={stats.restart}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium rounded-md border border-gray-300 dark:border-gray-600 hover:border-blue-400 dark:hover:border-blue-500 transition-colors duration-150 focus:outline-none focus:ring-2 focus:ring-blue-500/60"
>
<RotateCcw size={14} />
Restart
</button>
</div>
}
>
<div className="grid grid-cols-2 md:grid-cols-4 gap-3 mb-5">
<MetricCard
label="Current TPS"
value={stats.latestTps.toFixed(2)}
hint={stats.running ? "Streaming" : "Paused"}
icon={<Gauge size={14} />}
tone="success"
/>
<MetricCard
label="Peak TPS"
value={stats.peakTps.toFixed(2)}
hint={`Avg ${stats.avgTps.toFixed(2)}`}
icon={<TrendingUp size={14} />}
/>
<MetricCard
label="Base Fee"
value={`${stats.latestBaseFee} stroops`}
hint={`Avg ${stats.avgBaseFee} stroops`}
icon={<Zap size={14} />}
tone="warning"
/>
<MetricCard
label="Max Fee (1.2×)"
value={`${stats.latestMaxFee} stroops`}
hint={`Peak ${stats.peakBaseFee} baseFee`}
icon={<Zap size={14} />}
/>
</div>
{/* Separate live regions keep state changes (high-signal) cleanly
distinguishable from sample-count changes (low-signal) for AT users. */}
<p
id={panelStatusId + "-state"}
className="sr-only"
aria-live="polite"
aria-atomic="true"
>
{stats.running ? "Performance sampling is running." : "Performance sampling is paused."}
</p>
<p
id={panelStatusId + "-count"}
className="sr-only"
aria-live="off"
>
Showing {stats.samples.length} most recent samples.
</p>
<div className="grid gap-4 md:grid-cols-2">
<div className="p-4 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900/30">
<div className="flex items-center justify-between mb-2">
<h3 className="text-sm font-medium text-gray-900 dark:text-gray-50">Transactions / second</h3>
<span className="text-[11px] text-gray-500 dark:text-gray-400 tabular-nums">
last {stats.samples.length} samples
</span>
</div>
<Sparkline samples={stats.samples} series="tps" />
</div>
<div className="p-4 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900/30">
<div className="flex items-center justify-between mb-2">
<h3 className="text-sm font-medium text-gray-900 dark:text-gray-50">Base fee (stroops)</h3>
<span className="text-[11px] text-gray-500 dark:text-gray-400 tabular-nums">
window:{" "}
{stats.samples.length >= 2
? `${Math.round(
(stats.samples[stats.samples.length - 1].timestamp -
stats.samples[0].timestamp) /
1000
)}s`
: "—"}
</span>
</div>
<Sparkline samples={stats.samples} series="baseFee" />
</div>
</div>
</Card>
);
};