diff --git a/frontend/app/live/page.tsx b/frontend/app/live/page.tsx index 6021649..0e07515 100644 --- a/frontend/app/live/page.tsx +++ b/frontend/app/live/page.tsx @@ -1,19 +1,12 @@ "use client"; import { useEffect, useState, useCallback } from "react"; +import { api } from "@/lib/api"; import { LineChart, Line, XAxis, YAxis, Tooltip, Legend, ResponsiveContainer, ReferenceLine, CartesianGrid } from "recharts"; -interface Snapshot { - ts: number; - btc_rate: number; - eth_rate: number; - btc_price: number; - eth_price: number; -} - interface ChartPoint { time: string; btcRate: number; @@ -30,9 +23,8 @@ export default function LivePage() { const fetchSnapshots = useCallback(async () => { try { - const r = await fetch(`/api/snapshots?hours=${hours}&limit=3600`); - const json = await r.json(); - const rows: Snapshot[] = json.data || []; + const json = await api.snapshots(hours, 3600); + const rows = json.data || []; setCount(json.count || 0); // 降采样:每30条取1条,避免图表过密 const step = Math.max(1, Math.floor(rows.length / 300)); diff --git a/frontend/lib/api.ts b/frontend/lib/api.ts index 5781ef9..9b8db43 100644 --- a/frontend/lib/api.ts +++ b/frontend/lib/api.ts @@ -37,6 +37,33 @@ export interface StatsResponse { combo: { mean7d: number; annualized: number }; } +export interface SignalHistoryItem { + id: number; + symbol: string; + rate: number; + annualized: number; + sent_at: string; + message: string; +} + +export interface SignalsHistoryResponse { + items: SignalHistoryItem[]; +} + +export interface SnapshotItem { + ts: number; + btc_rate: number; + eth_rate: number; + btc_price: number; + eth_price: number; +} + +export interface SnapshotsResponse { + count: number; + hours: number; + data: SnapshotItem[]; +} + async function fetchAPI(path: string): Promise { const res = await fetch(`${API_BASE}${path}`, { cache: "no-store" }); if (!res.ok) throw new Error(`API error ${res.status}`); @@ -48,4 +75,7 @@ export const api = { history: () => fetchAPI("/api/history"), stats: () => fetchAPI("/api/stats"), health: () => fetchAPI<{ status: string }>("/api/health"), + signalsHistory: () => fetchAPI("/api/signals/history"), + snapshots: (hours = 24, limit = 5000) => + fetchAPI(`/api/snapshots?hours=${hours}&limit=${limit}`), };