|
| 1 | +# WARP.md |
| 2 | + |
| 3 | +This file provides guidance to WARP (warp.dev) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Six Degrees of Wikipedia finds the shortest path between any two Wikipedia pages through their hyperlinks. The project consists of: |
| 8 | + |
| 9 | +- **Backend**: Python Flask API server (`sdow/`) with SQLite database containing Wikipedia links |
| 10 | +- **Frontend**: React/TypeScript website (`website/`) built with Vite |
| 11 | +- **Database**: Scripts to download, process, and build Wikipedia dump databases (`scripts/`) |
| 12 | +- **Infrastructure**: Configuration for production deployment with Nginx, Gunicorn, Supervisord |
| 13 | + |
| 14 | +## Development Environment Setup |
| 15 | + |
| 16 | +### Initial Setup (One-time) |
| 17 | +```bash |
| 18 | +# From repo root - create Python virtual environment and install dependencies |
| 19 | +virtualenv env |
| 20 | +source env/bin/activate |
| 21 | +pip install -r requirements.txt |
| 22 | + |
| 23 | +# Create mock database for development |
| 24 | +python scripts/create_mock_databases.py |
| 25 | + |
| 26 | +# Setup frontend dependencies |
| 27 | +cd website/ |
| 28 | +npm install |
| 29 | +``` |
| 30 | + |
| 31 | +### Development Server (Every Session) |
| 32 | +**Backend (Terminal 1):** |
| 33 | +```bash |
| 34 | +# From repo root |
| 35 | +source env/bin/activate |
| 36 | +cd sdow/ |
| 37 | +export FLASK_APP=server.py FLASK_DEBUG=1 |
| 38 | +flask run |
| 39 | +# Runs on http://localhost:5000 |
| 40 | +``` |
| 41 | + |
| 42 | +**Frontend (Terminal 2):** |
| 43 | +```bash |
| 44 | +# From website/ directory |
| 45 | +npm start |
| 46 | +# Runs on http://localhost:3000 |
| 47 | +``` |
| 48 | + |
| 49 | +## Core Development Commands |
| 50 | + |
| 51 | +### Backend (Python Flask API) |
| 52 | +```bash |
| 53 | +# Run server in debug mode |
| 54 | +cd sdow/ && export FLASK_APP=server.py FLASK_DEBUG=1 && flask run |
| 55 | + |
| 56 | +# Check Python code formatting |
| 57 | +pylint sdow/ |
| 58 | + |
| 59 | +# Format Python code (uses PEP 8 with 2-space indents, 100-char lines) |
| 60 | +autopep8 --in-place --recursive sdow/ |
| 61 | + |
| 62 | +# Query mock database directly |
| 63 | +litecli sdow/sdow.sqlite |
| 64 | +``` |
| 65 | + |
| 66 | +### Frontend (React/TypeScript) |
| 67 | +```bash |
| 68 | +# From website/ directory |
| 69 | +npm start # Development server (port 3000) |
| 70 | +npm run build # Production build |
| 71 | +npm run preview # Preview production build |
| 72 | +npm run lint # Run Prettier + ESLint |
| 73 | +npm run format # Auto-format code |
| 74 | +npm run analyze # Analyze bundle size |
| 75 | +npm run update-deps # Update dependencies (excludes react-router-dom) |
| 76 | + |
| 77 | +# Type checking |
| 78 | +npx tsc --noEmit |
| 79 | +``` |
| 80 | + |
| 81 | +### Database Operations |
| 82 | +```bash |
| 83 | +# Create mock development databases |
| 84 | +python scripts/create_mock_databases.py |
| 85 | + |
| 86 | +# Build full production database from Wikipedia dumps (takes hours/days) |
| 87 | +cd scripts/ && ./buildDatabase.sh |
| 88 | + |
| 89 | +# Build for specific Wikipedia dump date |
| 90 | +cd scripts/ && ./buildDatabase.sh 20231201 |
| 91 | + |
| 92 | +# Upload database to Google Cloud Storage |
| 93 | +cd scripts/ && ./uploadToGcs.sh |
| 94 | + |
| 95 | +# Backup searches database |
| 96 | +cd scripts/ && ./backupSearchesDatabase.sh |
| 97 | +``` |
| 98 | + |
| 99 | +## Architecture Overview |
| 100 | + |
| 101 | +### Bi-Directional Breadth-First Search Algorithm |
| 102 | +The core search algorithm (`sdow/breadth_first_search.py`) uses bi-directional BFS: |
| 103 | + |
| 104 | +1. **Dual Search**: Searches simultaneously from source and target pages |
| 105 | +2. **Adaptive Direction**: Chooses search direction based on link count (fewer outgoing/incoming links) |
| 106 | +3. **Optimal Strategy**: Forward search follows outgoing links, backward search follows incoming links |
| 107 | +4. **Path Construction**: When searches meet, reconstructs complete path through parent tracking |
| 108 | + |
| 109 | +### Database Schema |
| 110 | +- **pages**: `id`, `title`, `is_redirect` - All Wikipedia pages |
| 111 | +- **links**: `id`, `outgoing_links_count`, `incoming_links_count`, `outgoing_links`, `incoming_links` - Page link relationships stored as pipe-separated strings |
| 112 | +- **redirects**: `source_id`, `target_id` - Page redirects |
| 113 | +- **searches**: Search result logging with timing data |
| 114 | + |
| 115 | +### API Endpoints |
| 116 | +- `POST /paths` - Main search endpoint: `{"source": "Page A", "target": "Page B"}` |
| 117 | +- `GET /ok` - Health check |
| 118 | + |
| 119 | +### Data Flow |
| 120 | +1. Frontend sends search request to `/paths` with source/target page titles |
| 121 | +2. Backend resolves titles to page IDs, handling redirects |
| 122 | +3. Bi-directional BFS algorithm finds shortest paths |
| 123 | +4. Wikipedia API fetched for page metadata (titles, URLs, summaries) |
| 124 | +5. Results returned as JSON with paths (page ID arrays) and page data |
| 125 | +6. Frontend renders results as both list and D3.js graph visualization |
| 126 | + |
| 127 | +## Key Files and Patterns |
| 128 | + |
| 129 | +### Backend Structure |
| 130 | +- `server.py` - Flask app with CORS, compression, error handling |
| 131 | +- `database.py` - SQLite query abstraction and caching |
| 132 | +- `breadth_first_search.py` - Core pathfinding algorithm |
| 133 | +- `helpers.py` - Wikipedia API integration, error classes |
| 134 | + |
| 135 | +### Frontend Structure (see website/WARP.md) |
| 136 | +The frontend is a separate React/TypeScript application with its own WARP.md file containing detailed frontend-specific guidance. |
| 137 | + |
| 138 | +### Database Build Pipeline |
| 139 | +Wikipedia dump processing involves multiple stages: |
| 140 | +1. **Download**: Gets latest Wikipedia dumps (pages, links, redirects) via wget/torrent |
| 141 | +2. **Trim**: Filters to main namespace (0) articles only |
| 142 | +3. **Transform**: Converts titles to IDs, resolves redirects |
| 143 | +4. **Sort/Dedupe**: Optimizes link data for search performance |
| 144 | +5. **Import**: Creates SQLite database with proper indexes |
| 145 | + |
| 146 | +### Production Architecture |
| 147 | +- **Web Server**: Nginx reverse proxy |
| 148 | +- **App Server**: Gunicorn WSGI server running Flask app |
| 149 | +- **Process Management**: Supervisord for service monitoring |
| 150 | +- **Database**: SQLite files (~50GB+ for full Wikipedia) |
| 151 | +- **Deployment**: Google Cloud Platform with Firebase hosting for frontend |
| 152 | + |
| 153 | +## Development Patterns |
| 154 | + |
| 155 | +### Python Code Style |
| 156 | +- 2-space indentation (configured in .pylintrc, setup.cfg) |
| 157 | +- 100-120 character line limits |
| 158 | +- PEP 8 compliance with custom indent rules |
| 159 | + |
| 160 | +### Error Handling |
| 161 | +- Custom `InvalidRequest` exception class for user-facing errors |
| 162 | +- Comprehensive logging with Google Cloud Logging integration |
| 163 | +- Graceful degradation when Wikipedia API unavailable |
| 164 | + |
| 165 | +### Performance Considerations |
| 166 | +- **Link Storage**: Pipe-separated strings in TEXT fields for space efficiency |
| 167 | +- **Search Optimization**: Chooses BFS direction based on link density |
| 168 | +- **Database Indexes**: Optimized for title lookups and link count queries |
| 169 | +- **Caching**: Database connection pooling and query result caching |
| 170 | + |
| 171 | +## Mock vs Production Data |
| 172 | + |
| 173 | +**Development**: Uses `create_mock_databases.py` - creates ~35 mock Wikipedia pages with simple link relationships for testing the search algorithm. |
| 174 | + |
| 175 | +**Production**: Uses `buildDatabase.sh` - downloads and processes full Wikipedia dumps (~6M+ pages, ~150M+ links). Takes significant time and disk space (50GB+). |
0 commit comments