diff --git a/Memory.md b/Memory.md index 1a7a205..54372aa 100644 --- a/Memory.md +++ b/Memory.md @@ -1,5 +1,17 @@ # Omniledger 架构与开发记忆 (Memory) +## 新增 exchange_rates_history 数据库表,并接入极速数据 (Jisu API) 建立每天自动追加的汇率时间序列抓取引擎 (Task 63a) +- 在 `src/db/schema.ts` 中新增 `exchangeRatesHistory` 表定义:包含 `id` (uuid)、`fromCurrency`、`toCurrency` (固定 CNY)、`rate` (numeric(20,8) 高精度)、`fetchTime` (时间戳)、`createdAt`,支持 USD/CNY 与 HKD/CNY 双币种对的汇率历史追踪。 +- 执行 `drizzle-kit push` 将新表推送到 PostgreSQL 数据库,确保表结构生效。 +- 在 `app/api/cron/fetch-rates/route.ts` 创建 Next.js Route Handler (GET),专供定时任务调用。 +- **安全拦截:** 校验 `Authorization: Bearer ${process.env.CRON_SECRET}` 请求头,不匹配返回 401;若 `CRON_SECRET` 或 `JISU_API_KEY` 未配置则返回 500。 +- **极速数据 API 接入:** 并发请求 USD→CNY 与 HKD→CNY 的汇率接口 (`api.jisuapi.com/exchange/convert`),严格校验 `status === 0`,解析 `result.rate` 保持字符串形态入库。 +- **容错设计:** 使用 `Promise.allSettled` 并发处理两个币种请求,任一失败不会阻断另一个的入库;DB 插入失败单独 catch 记录日志但不中断流程。 +- **响应格式:** 返回 `{ success, timestamp, inserted, failed, details: { inserted: [{from,to,rate}], failed: [{from,error}] } }` 结构化 JSON。 +- 新增环境变量 `JISU_API_KEY`(需在 `.env` 中配置极速数据 API 密钥)。 + +## 升级价格抓取引擎,实现从 gtimg 原始报文中提取 Index 30 的真实行情日期,并针对 US/SH/HK 三种日期格式执行标准化 YYYY-MM-DD 转换 (Task 61e) + ## 升级价格抓取引擎,实现从 gtimg 原始报文中提取 Index 30 的真实行情日期,并针对 US/SH/HK 三种日期格式执行标准化 YYYY-MM-DD 转换 (Task 61e) - 在 `app/api/cron/fetch-prices/route.ts` 中新增 `parseMarketDate(rawString: string)` 核心工具函数,从腾讯财经 gtimg 原始响应(`~` 分隔)的 Index 30 提取真实交易日期。 - 支持三种市场日期格式标准化:A股 `20260430161416`(14位数字)→ `2026-04-30`;港股 `2026/04/30 16:08:24`(斜杠分隔)→ `2026-04-30`;美股 `2026-05-01 09:31:00`(空格分隔)→ `2026-05-01`。 diff --git a/app/api/cron/fetch-rates/route.ts b/app/api/cron/fetch-rates/route.ts new file mode 100644 index 0000000..07bf523 --- /dev/null +++ b/app/api/cron/fetch-rates/route.ts @@ -0,0 +1,147 @@ +import { NextResponse } from 'next/server'; +import { db } from '@/db'; +import { exchangeRatesHistory } from '@/db/schema'; +import { ProxyAgent, setGlobalDispatcher } from 'undici'; + +export const dynamic = 'force-dynamic'; +export const runtime = 'nodejs'; + +const CURRENCIES = [ + { from: 'USD', to: 'CNY' }, + { from: 'HKD', to: 'CNY' }, +]; + +const JISU_API_BASE = 'https://api.jisuapi.com/exchange/convert'; + +interface JisuResult { + from: string; + to: string; + rate: string; + updatetime: string; +} + +interface JisuResponse { + status: number; + msg: string; + result: JisuResult; +} + +async function fetchRate( + from: string, + to: string, + apikey: string +): Promise<{ from: string; to: string; rate: string; success: true } | { from: string; success: false; error: string }> { + const url = `${JISU_API_BASE}?appkey=${apikey}&from=${from}&to=${to}&amount=1`; + + try { + const res = await fetch(url, { cache: 'no-store' }); + + if (!res.ok) { + return { from, success: false, error: `HTTP ${res.status}` }; + } + + const data: JisuResponse = await res.json(); + + if (data.status !== 0) { + return { from, success: false, error: data.msg || `API status: ${data.status}` }; + } + + if (!data.result || !data.result.rate) { + return { from, success: false, error: 'Missing result.rate in response' }; + } + + return { + from, + to: data.result.to, + rate: data.result.rate, + success: true, + }; + } catch (err) { + console.error(`[ExchangeRate] Fetch ${from}/${to} failed:`, err); + return { from, success: false, error: (err as Error).message }; + } +} + +export async function GET(req: Request) { + const cronSecret = process.env.CRON_SECRET; + const authHeader = req.headers.get('Authorization'); + + if (!cronSecret) { + return NextResponse.json( + { error: 'CRON_SECRET not configured' }, + { status: 500 } + ); + } + + if (authHeader !== `Bearer ${cronSecret}`) { + return NextResponse.json( + { error: 'Unauthorized' }, + { status: 401 } + ); + } + + const jisuApiKey = process.env.JISU_API_KEY; + if (!jisuApiKey) { + return NextResponse.json( + { error: 'JISU_API_KEY not configured' }, + { status: 500 } + ); + } + + const proxyUrl = process.env.HTTPS_PROXY; + if (proxyUrl) { + const proxyAgent = new ProxyAgent(proxyUrl); + setGlobalDispatcher(proxyAgent); + } + + const fetchPromises = CURRENCIES.map(({ from, to }) => + fetchRate(from, to, jisuApiKey) + ); + + const results = await Promise.allSettled(fetchPromises); + + const inserted: Array<{ from: string; to: string; rate: string }> = []; + const failed: Array<{ from: string; error: string }> = []; + + for (let i = 0; i < results.length; i++) { + const result = results[i]; + const pair = CURRENCIES[i]; + + if (result.status === 'fulfilled') { + const res = result.value; + if (res.success) { + try { + await db.insert(exchangeRatesHistory).values({ + fromCurrency: res.from, + toCurrency: res.to, + rate: res.rate, + fetchTime: new Date(), + }); + inserted.push({ from: res.from, to: res.to, rate: res.rate }); + console.log(`[ExchangeRate] ${res.from}/${res.to} = ${res.rate} -> saved`); + } catch (dbErr) { + failed.push({ from: res.from, error: `DB insert failed: ${(dbErr as Error).message}` }); + console.error(`[ExchangeRate] DB insert failed for ${res.from}:`, dbErr); + } + } else { + const failRes = res as { from: string; success: false; error: string }; + failed.push({ from: failRes.from, error: failRes.error }); + console.error(`[ExchangeRate] API error for ${failRes.from}: ${failRes.error}`); + } + } else { + failed.push({ from: pair.from, error: result.reason?.message || 'Unknown error' }); + console.error(`[ExchangeRate] Promise rejected for ${pair.from}:`, result.reason); + } + } + + return NextResponse.json({ + success: true, + timestamp: new Date().toISOString(), + inserted: inserted.length, + failed: failed.length, + details: { + inserted, + failed, + }, + }); +} diff --git a/src/db/schema.ts b/src/db/schema.ts index b8b34ac..9d5e538 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -92,3 +92,15 @@ export const assetPricesHistory = pgTable("asset_prices_history", { }, (table) => [ uniqueIndex("asset_price_date_idx").on(table.assetId, table.date), ]); + +export const exchangeRatesHistory = pgTable("exchange_rates_history", { + id: uuid("id").primaryKey().$defaultFn(() => crypto.randomUUID()), + fromCurrency: varchar("from_currency", { length: 10 }).notNull(), + toCurrency: varchar("to_currency", { length: 10 }).notNull(), + rate: numeric("rate", { precision: 20, scale: 8 }).notNull(), + fetchTime: timestamp("fetch_time", { withTimezone: true, mode: "date" }) + .notNull(), + createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }) + .defaultNow() + .notNull(), +});