|
| 1 | +import React, { useEffect, useRef } from "react"; |
| 2 | +import { MarketTick } from "../domain/MarketTicker"; |
| 3 | + |
| 4 | +interface JitterChartProps { |
| 5 | + title: string; |
| 6 | + description: string; |
| 7 | + color: string; |
| 8 | + /** Hook to the raw telemetry stream */ |
| 9 | + subscribe: (onTick: (tick: MarketTick) => void) => () => void; |
| 10 | +} |
| 11 | + |
| 12 | +export const JitterChart: React.FC<JitterChartProps> = ({ title, description, color, subscribe }) => { |
| 13 | + const canvasRef = useRef<HTMLCanvasElement>(null); |
| 14 | + const latestTickRef = useRef<MarketTick | null>(null); |
| 15 | + const pointsRef = useRef<number[]>([]); |
| 16 | + const MAX_POINTS = 200; |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const unsubscribe = subscribe((tick: MarketTick) => { |
| 20 | + latestTickRef.current = tick; |
| 21 | + }); |
| 22 | + |
| 23 | + const canvas = canvasRef.current; |
| 24 | + if (!canvas) return; |
| 25 | + const ctx = canvas.getContext("2d"); |
| 26 | + if (!ctx) return; |
| 27 | + |
| 28 | + let animationFrameId: number; |
| 29 | + |
| 30 | + const renderLoop = () => { |
| 31 | + const tick = latestTickRef.current; |
| 32 | + if (tick) { |
| 33 | + // Calculate strictly the render latency natively |
| 34 | + const renderTsc = (performance.timeOrigin + performance.now()) * 1_000_000; |
| 35 | + // Total delta from C++ HFT intake to physical pixel paint |
| 36 | + const deltaNs = renderTsc - tick.internal_arrival_ts; |
| 37 | + // Convert to milliseconds for charting |
| 38 | + const deltaMs = deltaNs / 1_000_000; |
| 39 | + |
| 40 | + // Push point to the array |
| 41 | + pointsRef.current.push(deltaMs); |
| 42 | + if (pointsRef.current.length > MAX_POINTS) { |
| 43 | + pointsRef.current.shift(); |
| 44 | + } |
| 45 | + |
| 46 | + // Clear canvas |
| 47 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 48 | + |
| 49 | + // Draw standard grid lines (1ms, 2ms, etc.) |
| 50 | + ctx.strokeStyle = "rgba(100, 100, 100, 0.2)"; |
| 51 | + ctx.lineWidth = 1; |
| 52 | + for (let i = 1; i <= 3; i++) { |
| 53 | + const y = canvas.height - (i * canvas.height / 4); |
| 54 | + ctx.beginPath(); |
| 55 | + ctx.moveTo(0, y); |
| 56 | + ctx.lineTo(canvas.width, y); |
| 57 | + ctx.stroke(); |
| 58 | + } |
| 59 | + |
| 60 | + // Draw points |
| 61 | + const pts = pointsRef.current; |
| 62 | + if (pts.length > 0) { |
| 63 | + ctx.beginPath(); |
| 64 | + // We assume 3ms max for the chart scale as baseline, cap at 3ms for drawing |
| 65 | + const scaleY = canvas.height / 3.0; |
| 66 | + |
| 67 | + for (let i = 0; i < pts.length; i++) { |
| 68 | + const x = (i / MAX_POINTS) * canvas.width; |
| 69 | + const y = canvas.height - Math.min(pts[i] * scaleY, canvas.height); |
| 70 | + |
| 71 | + if (i === 0) { |
| 72 | + ctx.moveTo(x, y); |
| 73 | + } else { |
| 74 | + ctx.lineTo(x, y); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + ctx.strokeStyle = color; |
| 79 | + ctx.lineWidth = 2; |
| 80 | + ctx.lineJoin = "round"; |
| 81 | + ctx.stroke(); |
| 82 | + |
| 83 | + // Fill under the line |
| 84 | + ctx.lineTo(canvas.width, canvas.height); |
| 85 | + ctx.lineTo(0, canvas.height); |
| 86 | + ctx.fillStyle = color.replace("hsl", "hsla").replace(")", ", 0.15)"); |
| 87 | + if (color.startsWith("rgb") || color.startsWith("#")) { |
| 88 | + ctx.fillStyle = `${color}33`; // simple hex transp |
| 89 | + } |
| 90 | + if (color.includes("qx-primary")) ctx.fillStyle = "rgba(41, 121, 255, 0.15)"; |
| 91 | + if (color.includes("qx-secondary")) ctx.fillStyle = "rgba(140, 60, 255, 0.15)"; |
| 92 | + |
| 93 | + ctx.fill(); |
| 94 | + |
| 95 | + // Current Value text |
| 96 | + ctx.fillStyle = "#ffffff"; |
| 97 | + ctx.font = "12px monospace"; |
| 98 | + ctx.textAlign = "right"; |
| 99 | + ctx.fillText(`${pts[pts.length - 1].toFixed(2)}ms`, canvas.width - 5, 15); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + animationFrameId = requestAnimationFrame(renderLoop); |
| 104 | + }; |
| 105 | + |
| 106 | + animationFrameId = requestAnimationFrame(renderLoop); |
| 107 | + |
| 108 | + return () => { |
| 109 | + unsubscribe(); |
| 110 | + cancelAnimationFrame(animationFrameId); |
| 111 | + }; |
| 112 | + }, [subscribe, color]); |
| 113 | + |
| 114 | + return ( |
| 115 | + <div className="bg-qx-surface border border-qx-border rounded-xl p-4 flex flex-col space-y-2 shadow-lg hover:shadow-xl transition-all"> |
| 116 | + <h3 className="text-qx-foreground font-bold tracking-wide text-sm">{title}</h3> |
| 117 | + <p className="text-xs text-muted-foreground">{description}</p> |
| 118 | + <div className="relative w-full h-32 mt-2 bg-background/50 rounded-lg overflow-hidden border border-qx-border/50"> |
| 119 | + <canvas |
| 120 | + ref={canvasRef} |
| 121 | + className="absolute inset-0 w-full h-full" |
| 122 | + width={400} |
| 123 | + height={100} |
| 124 | + /> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
0 commit comments