# sleepingarhat/tianxi-racing architecture

> Architecture diagram of the GitHub repository sleepingarhat/tianxi-racing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sleepingarhat/tianxi-racing
- Repository: https://github.com/sleepingarhat/tianxi-racing

## Overview

Tianxi Database is a Hong Kong horse-racing data platform: it scrapes HKJC and related sources into reusable racing datasets, with a backend API and prediction/analysis services. The main workflow is source collection, structured ingestion and integrity checking, database/API serving, then analysis and race-day picks with a lock-time freeze. The README also describes Elo ratings, odds, profiles, membership, alerts, and research/backtesting. Edges are limited to relationships stated in the README or shown in excerpts/index references; several backend and scraper subsystems are represented by their supported paths without inferred call chains.

Main entry points: [`backend/src/index.ts`](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/index.ts) (Backend API), [`backend/src/types.ts`](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/types.ts) (D1 racing store), [`backend/src/routes/analyze.ts`](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/analyze.ts) (Race analysis), [`backend/src/lib/freeze-ledger.ts`](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/freeze-ledger.ts) (Prediction freeze)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_collect["Data Collection"]
  node_scrapers["Racing scrapers"]
  node_specialized["Specialized scrapers"]
  node_ingest["Backend ingestion"]
  node_horse_profiles["Horse profiles"]
end

subgraph group_store["Data and API"]
  node_csv[("CSV data archive")]
  node_d1[("D1 racing store<br/>[types.ts]")]
  node_api["Backend API<br/>[index.ts]"]
  node_race_data["Race and entity APIs<br/>[meetings.ts]"]
end

subgraph group_predict["Ratings and Picks"]
  node_elo["Elo calculation<br/>[engine_v12.ts]"]
  node_elo_driver["Elo batch driver<br/>[compute_v12.ts]"]
  node_analysis["Race analysis<br/>[analyze.ts]"]
end

subgraph group_operate["Race-Day Operations"]
  node_freeze["Prediction freeze<br/>[freeze-ledger.ts]"]
  node_recovery["Freeze recovery<br/>[freeze-recovery.ts]"]
  node_lock["Lock window<br/>[lock-window.ts]"]
  node_odds["Odds collection<br/>[odds-collection.ts]"]
  node_health["Engine health<br/>[engine-health.ts]"]
  node_members["Membership and access<br/>[membership.ts]"]
  node_alerts["Notifications and watch<br/>[notify.ts]"]
end

subgraph group_research["Research and Integrity"]
  node_audit["Integrity audit"]
  node_backtest["Upcoming prediction"]
  node_profile_audit["Profile coverage"]
  node_research_models["Model research"]
end

node_hkjc(("HKJC sources"))
node_client(("Downstream clients"))

node_hkjc -->|"scrape"| node_scrapers
node_scrapers -->|"produce CSV"| node_csv
node_hkjc -->|"collect racing data"| node_specialized
node_specialized -.->|"produce CSV"| node_csv
node_hkjc -->|"fetch racecards"| node_ingest
node_ingest -->|"ingest data"| node_d1
node_csv -.->|"audit archive"| node_audit
node_csv -->|"fetch CSV"| node_client
node_d1 -->|"read and write"| node_api
node_api -->|"serve API"| node_client
node_elo_driver -.->|"compute ratings"| node_elo
node_d1 -.->|"read race data"| node_elo_driver
node_analysis -->|"query race data"| node_d1
node_api -->|"route analysis"| node_analysis
node_backtest -.->|"evaluate predictions"| node_research_models
node_analysis -->|"record frozen picks"| node_freeze
node_freeze -.->|"check lock"| node_lock
node_freeze -->|"recover originals"| node_recovery
node_api -->|"build health"| node_health
node_health -->|"check meeting lock"| node_lock
node_health -->|"read freshness"| node_odds
node_api -->|"route race APIs"| node_race_data
node_api -.->|"route membership"| node_members
node_api -.->|"route notifications"| node_alerts
node_hkjc -.->|"scrape profiles"| node_horse_profiles
node_horse_profiles -.->|"measure coverage"| node_profile_audit
node_api -->|"publish racing data"| node_client

click node_scrapers "https://github.com/sleepingarhat/tianxi-racing/blob/main/RacingData_Scraper.py"
click node_ingest "https://github.com/sleepingarhat/tianxi-racing/tree/main/backend/scripts/ingest"
click node_audit "https://github.com/sleepingarhat/tianxi-racing/blob/main/tools/data_integrity_audit.py"
click node_d1 "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/types.ts"
click node_api "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/index.ts"
click node_elo "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/scripts/elo/engine_v12.ts"
click node_elo_driver "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/scripts/elo/compute_v12.ts"
click node_analysis "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/analyze.ts"
click node_backtest "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/scripts/backtest/predict_upcoming.py"
click node_freeze "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/freeze-ledger.ts"
click node_recovery "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/freeze-recovery.ts"
click node_lock "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/lock-window.ts"
click node_odds "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/odds-collection.ts"
click node_health "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/engine-health.ts"
click node_members "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/membership.ts"
click node_alerts "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/notify.ts"
click node_race_data "https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/meetings.ts"
click node_horse_profiles "https://github.com/sleepingarhat/tianxi-racing/blob/main/HorseData_Scraper.py"
click node_profile_audit "https://github.com/sleepingarhat/tianxi-racing/blob/main/tools/horse_profile_coverage.py"
click node_research_models "https://github.com/sleepingarhat/tianxi-racing/tree/main/backend/scripts/backtest"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_scrapers,node_specialized,node_ingest,node_horse_profiles,node_client toneBlue
class node_csv,node_d1,node_api,node_race_data toneAmber
class node_elo,node_elo_driver,node_analysis toneMint
class node_freeze,node_recovery,node_lock,node_odds,node_health,node_members,node_alerts toneRose
class node_audit,node_backtest,node_profile_audit,node_research_models,node_hkjc toneIndigo
```

## Components

### Data Collection

- [Racing scrapers](https://github.com/sleepingarhat/tianxi-racing/blob/main/RacingData_Scraper.py): `RacingData_Scraper.py`
- **Specialized scrapers**
- [Backend ingestion](https://github.com/sleepingarhat/tianxi-racing/tree/main/backend/scripts/ingest): `backend/scripts/ingest`
- [Horse profiles](https://github.com/sleepingarhat/tianxi-racing/blob/main/HorseData_Scraper.py): `HorseData_Scraper.py`

### Data and API

- **CSV data archive**
- [D1 racing store](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/types.ts): `backend/src/types.ts`
- [Backend API](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/index.ts): `backend/src/index.ts`
- [Race and entity APIs](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/meetings.ts): `backend/src/routes/meetings.ts`

### Ratings and Picks

- [Elo calculation](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/scripts/elo/engine_v12.ts): `backend/scripts/elo/engine_v12.ts`
- [Elo batch driver](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/scripts/elo/compute_v12.ts): `backend/scripts/elo/compute_v12.ts`
- [Race analysis](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/analyze.ts): `backend/src/routes/analyze.ts`

### Race-Day Operations

- [Prediction freeze](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/freeze-ledger.ts): `backend/src/lib/freeze-ledger.ts`
- [Freeze recovery](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/freeze-recovery.ts): `backend/src/lib/freeze-recovery.ts`
- [Lock window](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/lock-window.ts): `backend/src/lib/lock-window.ts`
- [Odds collection](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/odds-collection.ts): `backend/src/lib/odds-collection.ts`
- [Engine health](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/lib/engine-health.ts): `backend/src/lib/engine-health.ts`
- [Membership and access](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/membership.ts): `backend/src/routes/membership.ts`
- [Notifications and watch](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/src/routes/notify.ts): `backend/src/routes/notify.ts`

### Research and Integrity

- [Integrity audit](https://github.com/sleepingarhat/tianxi-racing/blob/main/tools/data_integrity_audit.py): `tools/data_integrity_audit.py`
- [Upcoming prediction](https://github.com/sleepingarhat/tianxi-racing/blob/main/backend/scripts/backtest/predict_upcoming.py): `backend/scripts/backtest/predict_upcoming.py`
- [Profile coverage](https://github.com/sleepingarhat/tianxi-racing/blob/main/tools/horse_profile_coverage.py): `tools/horse_profile_coverage.py`
- [Model research](https://github.com/sleepingarhat/tianxi-racing/tree/main/backend/scripts/backtest): `backend/scripts/backtest`

### Other components

- **HKJC sources**
- **Downstream clients**

## Connections

- HKJC sources → Racing scrapers: scrape (evidence: `README.md`)
- Racing scrapers → CSV data archive: produce CSV (evidence: `README.md`)
- HKJC sources → Specialized scrapers: collect racing data (evidence: `README.md`)
- Specialized scrapers → CSV data archive: produce CSV
- HKJC sources → Backend ingestion: fetch racecards (evidence: `backend/scripts/ingest/lib/hkjc-racecard-query.graphql`)
- Backend ingestion → D1 racing store: ingest data (evidence: `README.md`)
- CSV data archive → Integrity audit: audit archive
- CSV data archive → Downstream clients: fetch CSV (evidence: `README.md`)
- D1 racing store → Backend API: read and write (evidence: `backend/src/index.ts`)
- Backend API → Downstream clients: serve API (evidence: `README.md`)
- Elo batch driver → Elo calculation: compute ratings
- D1 racing store → Elo batch driver: read race data
- Race analysis → D1 racing store: query race data (evidence: `backend/src/routes/analyze.ts`)
- Backend API → Race analysis: route analysis (evidence: `backend/src/index.ts`)
- Upcoming prediction → Model research: evaluate predictions (evidence: `backend/scripts/backtest/predict_upcoming.py`)
- Race analysis → Prediction freeze: record frozen picks (evidence: `backend/src/routes/analyze.ts`)
- Prediction freeze → Lock window: check lock (evidence: `backend/src/lib/freeze-ledger.ts`)
- Prediction freeze → Freeze recovery: recover originals (evidence: `backend/src/lib/freeze-ledger.ts`)
- Backend API → Engine health: build health (evidence: `backend/src/index.ts`)
- Engine health → Lock window: check meeting lock (evidence: `backend/src/lib/engine-health.ts`)
- Engine health → Odds collection: read freshness (evidence: `backend/src/lib/engine-health.ts`)
- Backend API → Race and entity APIs: route race APIs (evidence: `backend/src/index.ts`)
- Backend API → Membership and access: route membership (evidence: `backend/src/index.ts`)
- Backend API → Notifications and watch: route notifications (evidence: `backend/src/index.ts`)
- HKJC sources → Horse profiles: scrape profiles
- Horse profiles → Profile coverage: measure coverage
- Backend API → Downstream clients: publish racing data (evidence: `README.md`)
