From f4a2ac6962950bbee1cfe6dcd7f82b71040bf05a Mon Sep 17 00:00:00 2001 From: Finessse Date: Sun, 28 Dec 2025 00:04:28 +0700 Subject: [PATCH] feat(monitor): add request monitoring dashboard - Add requests management endpoint - Add RequestMonitor component for real-time request tracking --- internal/api/handlers/management/requests.go | 16 + .../components/dashboard/RequestMonitor.tsx | 294 ++++++++++++++++++ 2 files changed, 310 insertions(+) create mode 100644 internal/api/handlers/management/requests.go create mode 100644 webui/src/components/dashboard/RequestMonitor.tsx diff --git a/internal/api/handlers/management/requests.go b/internal/api/handlers/management/requests.go new file mode 100644 index 00000000..c642d407 --- /dev/null +++ b/internal/api/handlers/management/requests.go @@ -0,0 +1,16 @@ +package management + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware" +) + +// GetRequests returns the list of recent requests from the in-memory ring buffer. +func (h *Handler) GetRequests(c *gin.Context) { + requests := middleware.GetRequestMonitor() + c.JSON(http.StatusOK, gin.H{ + "requests": requests, + }) +} diff --git a/webui/src/components/dashboard/RequestMonitor.tsx b/webui/src/components/dashboard/RequestMonitor.tsx new file mode 100644 index 00000000..b078e093 --- /dev/null +++ b/webui/src/components/dashboard/RequestMonitor.tsx @@ -0,0 +1,294 @@ +import React, { useState, useEffect, useCallback, useRef } from 'react' +import { useProxyContext, EngineOfflineError } from '@/hooks/useProxyContext' +import { Card, CardHeader, CardContent } from '@/components/ui/card' +import { Button } from '@/components/ui/button' +import { + Activity, + RefreshCw, + Circle, + ChevronDown, + ChevronRight, + Clock, + Database, + AlertCircle, + CheckCircle2, + XCircle +} from 'lucide-react' + +interface RequestLogEntry { + id: string + timestamp: string + method: string + path: string + model: string + provider: string + status: number + latencyMs: number + inputTokens: number + outputTokens: number + error?: string +} + +export function RequestMonitor() { + const { mgmtFetch, showToast, status, isMgmtLoading } = useProxyContext() + const [requests, setRequests] = useState([]) + const [isLive, setIsLive] = useState(false) + const [expandedRows, setExpandedRows] = useState>(new Set()) + const liveIntervalRef = useRef | null>(null) + const isRunning = status?.running ?? false + + const fetchRequests = useCallback(async () => { + try { + let data: RequestLogEntry[] = [] + if (window.pp_get_requests) { + data = await window.pp_get_requests() + } else { + const res = await mgmtFetch('/v0/management/requests') + data = res.requests || [] + } + // Sort by timestamp descending + setRequests([...data].sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime())) + } catch (e) { + if (!(e instanceof EngineOfflineError)) { + showToast(e instanceof Error ? e.message : String(e), 'error') + } + } + }, [mgmtFetch, showToast]) + + const toggleLive = () => { + if (isLive) { + setIsLive(false) + if (liveIntervalRef.current) { + clearInterval(liveIntervalRef.current) + liveIntervalRef.current = null + } + } else { + setIsLive(true) + fetchRequests() + liveIntervalRef.current = setInterval(() => { + fetchRequests() + }, 2000) + } + } + + const toggleRow = (id: string) => { + const newExpanded = new Set(expandedRows) + if (newExpanded.has(id)) { + newExpanded.delete(id) + } else { + newExpanded.add(id) + } + setExpandedRows(newExpanded) + } + + useEffect(() => { + fetchRequests() + return () => { + if (liveIntervalRef.current) { + clearInterval(liveIntervalRef.current) + } + } + }, [fetchRequests]) + + const getStatusColor = (code: number) => { + if (code >= 200 && code < 300) return 'text-[var(--status-online)]' + if (code >= 400 && code < 500) return 'text-[var(--status-warning)]' + if (code >= 500) return 'text-[var(--status-offline)]' + return 'text-[var(--text-muted)]' + } + + const getStatusIcon = (code: number) => { + if (code >= 200 && code < 300) return + if (code >= 400 && code < 500) return + if (code >= 500) return + return + } + + const formatTime = (ts: string) => { + const date = new Date(ts) + return date.toLocaleTimeString([], { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit' }) + } + + return ( + + +
+
+
+ + + REQUEST MONITOR + +
+ {isMgmtLoading && } + + +
+ +
+ +
+
+
+ + +
+ + + + + + + + + + + + + + {!isRunning ? ( + + + + ) : requests.length === 0 ? ( + + + + ) : ( + requests.map((req) => ( + + toggleRow(req.id)} + > + + + + + + + + + {expandedRows.has(req.id) && ( + + + + )} + + )) + )} + +
TIMEMODELPROVIDERSTATUSLATENCYTOKENS
+

⚠️ Engine Offline

+
+ No requests recorded yet. +
+ {formatTime(req.timestamp)} + + {req.model || '-'} + + + {req.provider?.toUpperCase() || 'UNKNOWN'} + + +
+ {getStatusIcon(req.status)} + {req.status} +
+
+ {req.latencyMs}ms + +
+ In: {req.inputTokens} + Out: {req.outputTokens} +
+
+ {expandedRows.has(req.id) ? ( + + ) : ( + + )} +
+
+
+
+ + Request Details +
+
+
+ Method: + {req.method} +
+
+ Path: + {req.path} +
+
+ ID: + {req.id} +
+
+
+
+
+ + Performance & Usage +
+
+
+ Latency: + {req.latencyMs}ms +
+
+ Total Tokens: + {req.inputTokens + req.outputTokens} +
+ {req.error && ( +
+
+ + ERROR +
+
{req.error}
+
+ )} +
+
+
+
+
+
+ +
+ {requests.length} requests tracked + + + {isLive ? 'LIVE POLLING ON' : 'LIVE POLLING OFF'} + + | + 2s INTERVAL + +
+
+ ) +}