fix: add SignalHistoryItem and snapshots types to api.ts

This commit is contained in:
root 2026-02-27 05:52:57 +00:00
parent f8201b3d8e
commit c6801e061c
2 changed files with 33 additions and 11 deletions

View File

@ -1,19 +1,12 @@
"use client"; "use client";
import { useEffect, useState, useCallback } from "react"; import { useEffect, useState, useCallback } from "react";
import { api } from "@/lib/api";
import { import {
LineChart, Line, XAxis, YAxis, Tooltip, Legend, LineChart, Line, XAxis, YAxis, Tooltip, Legend,
ResponsiveContainer, ReferenceLine, CartesianGrid ResponsiveContainer, ReferenceLine, CartesianGrid
} from "recharts"; } from "recharts";
interface Snapshot {
ts: number;
btc_rate: number;
eth_rate: number;
btc_price: number;
eth_price: number;
}
interface ChartPoint { interface ChartPoint {
time: string; time: string;
btcRate: number; btcRate: number;
@ -30,9 +23,8 @@ export default function LivePage() {
const fetchSnapshots = useCallback(async () => { const fetchSnapshots = useCallback(async () => {
try { try {
const r = await fetch(`/api/snapshots?hours=${hours}&limit=3600`); const json = await api.snapshots(hours, 3600);
const json = await r.json(); const rows = json.data || [];
const rows: Snapshot[] = json.data || [];
setCount(json.count || 0); setCount(json.count || 0);
// 降采样每30条取1条避免图表过密 // 降采样每30条取1条避免图表过密
const step = Math.max(1, Math.floor(rows.length / 300)); const step = Math.max(1, Math.floor(rows.length / 300));

View File

@ -37,6 +37,33 @@ export interface StatsResponse {
combo: { mean7d: number; annualized: number }; 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<T>(path: string): Promise<T> { async function fetchAPI<T>(path: string): Promise<T> {
const res = await fetch(`${API_BASE}${path}`, { cache: "no-store" }); const res = await fetch(`${API_BASE}${path}`, { cache: "no-store" });
if (!res.ok) throw new Error(`API error ${res.status}`); if (!res.ok) throw new Error(`API error ${res.status}`);
@ -48,4 +75,7 @@ export const api = {
history: () => fetchAPI<HistoryResponse>("/api/history"), history: () => fetchAPI<HistoryResponse>("/api/history"),
stats: () => fetchAPI<StatsResponse>("/api/stats"), stats: () => fetchAPI<StatsResponse>("/api/stats"),
health: () => fetchAPI<{ status: string }>("/api/health"), health: () => fetchAPI<{ status: string }>("/api/health"),
signalsHistory: () => fetchAPI<SignalsHistoryResponse>("/api/signals/history"),
snapshots: (hours = 24, limit = 5000) =>
fetchAPI<SnapshotsResponse>(`/api/snapshots?hours=${hours}&limit=${limit}`),
}; };