feat(health): 趋势图升级为 ECharts 折线图 + 缓存 TTL 5分钟

This commit is contained in:
iven
2026-04-24 12:38:07 +08:00
parent 7b5b00fbac
commit a9861a0cde
2 changed files with 46 additions and 31 deletions

View File

@@ -1,14 +1,22 @@
import { create } from 'zustand';
import * as healthApi from '@/services/health';
interface CachedTrend {
data: { date: string; value: number }[];
cachedAt: number;
}
interface HealthState {
todaySummary: healthApi.TodaySummary | null;
trendData: Record<string, { date: string; value: number }[]>;
trendData: Record<string, CachedTrend>;
loading: boolean;
refreshToday: () => Promise<void>;
getTrend: (indicator: string, range: string) => Promise<{ date: string; value: number }[]>;
clearCache: () => void;
}
const CACHE_TTL = 5 * 60 * 1000; // 5 分钟
export const useHealthStore = create<HealthState>((set, get) => ({
todaySummary: null,
trendData: {},
@@ -27,15 +35,19 @@ export const useHealthStore = create<HealthState>((set, get) => ({
getTrend: async (indicator: string, range: string) => {
const cacheKey = `${indicator}_${range}`;
const cached = get().trendData[cacheKey];
if (cached) return cached;
if (cached && Date.now() - cached.cachedAt < CACHE_TTL) {
return cached.data;
}
try {
const resp = await healthApi.getTrend(indicator, range);
const points = resp.data_points || [];
set((s) => ({ trendData: { ...s.trendData, [cacheKey]: points } }));
set((s) => ({ trendData: { ...s.trendData, [cacheKey]: { data: points, cachedAt: Date.now() } } }));
return points;
} catch {
return [];
}
},
clearCache: () => set({ trendData: {}, todaySummary: null }),
}));