Files
stonks-oracle/infra/migrations/024_trend_history.sql
T
Celes Renata 7c589353f8 fix: blank company charts + competitor GUIDs instead of tickers
Trend charts blank:
- trend_windows uses upsert (1 row per ticker/window), so charts had
  at most 1 data point. Added trend_history table (migration 024) that
  appends every snapshot. New /api/trends/history endpoint serves the
  time series. Frontend now uses useTrendHistory for charts and
  useTrends for the latest summary card.

Competitor GUIDs:
- list_competitors query returned raw company_b_id UUIDs without
  joining companies table. Added LEFT JOIN with CASE to resolve the
  other company's ticker and legal_name. Updated Pydantic model to
  include enriched fields. Frontend fallback changed from truncated
  UUID to ticker/legal_name/Unknown.
2026-04-17 00:42:55 +00:00

38 lines
1.5 KiB
SQL

-- Trend history table for time-series charting.
-- trend_windows stores the latest snapshot per (entity, window) via upsert.
-- trend_history stores every snapshot so the frontend can plot trend evolution.
CREATE TABLE IF NOT EXISTS trend_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
entity_type VARCHAR(50) NOT NULL DEFAULT 'company',
entity_id VARCHAR(100) NOT NULL,
"window" VARCHAR(20) NOT NULL,
trend_direction VARCHAR(20) NOT NULL DEFAULT 'neutral',
trend_strength FLOAT DEFAULT 0.5,
confidence FLOAT DEFAULT 0.5,
contradiction_score FLOAT DEFAULT 0.0,
dominant_catalysts JSONB DEFAULT '[]',
material_risks JSONB DEFAULT '[]',
generated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_trend_history_lookup
ON trend_history (entity_id, "window", generated_at DESC);
CREATE INDEX IF NOT EXISTS idx_trend_history_generated
ON trend_history (generated_at DESC);
-- Seed history from existing trend_windows so charts aren't empty
-- on first deploy. This gives at least one data point per ticker/window.
INSERT INTO trend_history (
entity_type, entity_id, "window", trend_direction,
trend_strength, confidence, contradiction_score,
dominant_catalysts, material_risks, generated_at
)
SELECT
entity_type, entity_id, "window", trend_direction,
trend_strength, confidence, contradiction_score,
dominant_catalysts, material_risks, generated_at
FROM trend_windows
ON CONFLICT DO NOTHING;