trading-app/src/lib/diagnostics.ts
David 469e46d306 Fix double executions, missed deposits, holidays; AI pot deposits; faster market data
- Orders, position closes, card answers and deposits are claimed atomically
  (one tick at a time, one evening at a time); cash updated in SQL
- Each tick step is isolated: an AI or Yahoo failure no longer stops the rest
- Missed monthly deposits are caught up; Trade Republic holidays handled
- Gap opens fill at the open (target included); limit orders valid one
  trading day; stale sell orders cancelled; old cards expire (PEA transfer kept)
- News queries bounded to 48 h, news older than 30 days purged
- AI sim pot receives the same monthly deposit as the real account;
  performance measured on money contributed vs S&P 500 with the same flows;
  reset action
- Yahoo data cached in the database (stale-while-revalidate); the order
  engine still requires fresh quotes; client router keeps tabs for 60 s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 14:54:17 +01:00

37 lines
1.5 KiB
TypeScript

import "server-only";
import { and, desc, gte, isNotNull } from "drizzle-orm";
import { aiEnabled, getAiSpend } from "./ai";
import { getDb, schema } from "./db";
import type { RunLog } from "./jobs";
import { getKv } from "./settings";
/** Ce qu'il faut savoir pour comprendre pourquoi l'IA trade (ou pas). */
export async function getDiagnostics() {
const db = await getDb();
const since48h = new Date(Date.now() - 48 * 3_600_000);
const since7d = new Date(Date.now() - 7 * 86_400_000);
const [news, triaged, signals, spend, lastTick, lastEvening] = await Promise.all([
db.select({ id: schema.news.id }).from(schema.news).where(gte(schema.news.publishedAt, since48h)),
db
.select({ triage: schema.news.triage })
.from(schema.news)
.where(and(isNotNull(schema.news.triagedAt), gte(schema.news.publishedAt, since48h))),
db.select().from(schema.signals).where(gte(schema.signals.createdAt, since7d)).orderBy(desc(schema.signals.createdAt)).limit(10),
getAiSpend(),
getKv<RunLog>("lastTickLog"),
getKv<RunLog>("lastEveningLog"),
]);
const scored = triaged.filter((t) => t.triage && t.triage.reason !== "Non évaluée.");
return {
aiOn: aiEnabled(),
cronConfigured: Boolean(process.env.CRON_SECRET),
news48h: news.length,
triagedTotal: triaged.length,
positiveStrong: scored.filter((t) => t.triage!.direction === "positive" && t.triage!.relevance >= 6).length,
signals,
spend,
lastTick,
lastEvening,
};
}