|
| 1 | +# Database Integration Setup |
| 2 | + |
| 3 | +This document explains how to set up database integration for the alex-treBENCH frontend. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. **PostgreSQL Database**: The frontend requires a PostgreSQL database. SQLite is not supported for the frontend due to Next.js limitations. |
| 8 | + |
| 9 | +2. **Backend Data**: You need to have run benchmarks using the alex-treBENCH CLI tool to have data in the database. |
| 10 | + |
| 11 | +## Setup Steps |
| 12 | + |
| 13 | +### 1. Database Configuration |
| 14 | + |
| 15 | +The frontend connects to the same PostgreSQL database used by the CLI tool. |
| 16 | + |
| 17 | +#### Option A: Use Existing CLI Database |
| 18 | + |
| 19 | +If you're already using PostgreSQL with the CLI tool: |
| 20 | + |
| 21 | +```bash |
| 22 | +# Copy your existing DATABASE_URL from the main project |
| 23 | +cp ../.env frontend/.env.local |
| 24 | +``` |
| 25 | + |
| 26 | +#### Option B: Set up New PostgreSQL Database |
| 27 | + |
| 28 | +```bash |
| 29 | +# Copy the example environment file |
| 30 | +cp .env.local.example .env.local |
| 31 | + |
| 32 | +# Edit .env.local with your PostgreSQL credentials |
| 33 | +DATABASE_URL=postgresql://username:password@localhost:5432/alex_trebench |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Install Dependencies |
| 37 | + |
| 38 | +Dependencies are already installed if you ran `pnpm install` after the package.json updates. |
| 39 | + |
| 40 | +If needed: |
| 41 | +```bash |
| 42 | +pnpm install pg @types/pg |
| 43 | +``` |
| 44 | + |
| 45 | +### 3. Database Schema |
| 46 | + |
| 47 | +The frontend uses the existing database schema from the CLI tool: |
| 48 | + |
| 49 | +- `questions` - Jeopardy questions cache |
| 50 | +- `benchmark_runs` - Benchmark execution records |
| 51 | +- `benchmark_results` - Individual question results |
| 52 | +- `model_performance` - Aggregated performance metrics |
| 53 | + |
| 54 | +### 4. Run the Frontend |
| 55 | + |
| 56 | +```bash |
| 57 | +pnpm dev |
| 58 | +``` |
| 59 | + |
| 60 | +The frontend will be available at http://localhost:3000 |
| 61 | + |
| 62 | +## API Endpoints |
| 63 | + |
| 64 | +The frontend provides these API endpoints: |
| 65 | + |
| 66 | +- `GET /api/stats/summary` - Dashboard statistics |
| 67 | +- `GET /api/leaderboard` - Model performance leaderboard |
| 68 | + |
| 69 | +## Data Requirements |
| 70 | + |
| 71 | +For the frontend to display meaningful data, you need: |
| 72 | + |
| 73 | +1. **Questions Data**: Run `alex data init` to populate the questions table |
| 74 | +2. **Benchmark Data**: Run at least one benchmark with `alex benchmark run` |
| 75 | + |
| 76 | +Example CLI commands to populate data: |
| 77 | + |
| 78 | +```bash |
| 79 | +# Initialize questions database |
| 80 | +alex data init |
| 81 | + |
| 82 | +# Run a quick benchmark to generate performance data |
| 83 | +alex benchmark run --models gpt-3.5-turbo --size quick |
| 84 | + |
| 85 | +# Or run a larger benchmark for more comprehensive data |
| 86 | +alex benchmark run --models gpt-4,claude-3-haiku,gpt-3.5-turbo --size small |
| 87 | +``` |
| 88 | + |
| 89 | +## Troubleshooting |
| 90 | + |
| 91 | +### Database Connection Issues |
| 92 | + |
| 93 | +1. **Check DATABASE_URL**: Ensure it points to a valid PostgreSQL instance |
| 94 | +2. **Verify Credentials**: Test connection with `psql` command |
| 95 | +3. **Network Access**: Ensure PostgreSQL accepts connections from your host |
| 96 | + |
| 97 | +### No Data Showing |
| 98 | + |
| 99 | +1. **Check Tables**: Verify tables exist and contain data |
| 100 | +2. **Run Benchmarks**: Execute CLI benchmarks to populate performance data |
| 101 | +3. **Check API Responses**: Visit `/api/stats/summary` directly to see raw data |
| 102 | + |
| 103 | +### Performance Issues |
| 104 | + |
| 105 | +1. **Database Indexes**: Ensure proper indexing on frequently queried columns |
| 106 | +2. **Connection Pool**: Adjust pool settings in `lib/database.ts` if needed |
| 107 | +3. **Query Optimization**: Check slow query logs if performance degrades |
| 108 | + |
| 109 | +## Development Notes |
| 110 | + |
| 111 | +### Database Connection |
| 112 | + |
| 113 | +- Uses connection pooling for performance |
| 114 | +- Automatic connection management with cleanup |
| 115 | +- Error handling and reconnection logic |
| 116 | + |
| 117 | +### Type Safety |
| 118 | + |
| 119 | +- Full TypeScript support with database types |
| 120 | +- Interface definitions match SQLAlchemy models |
| 121 | +- Type-safe query functions |
| 122 | + |
| 123 | +### Error Handling |
| 124 | + |
| 125 | +- Graceful degradation when database unavailable |
| 126 | +- Fallback data for leaderboard in case of errors |
| 127 | +- User-friendly error messages with retry options |
| 128 | + |
| 129 | +## Production Deployment |
| 130 | + |
| 131 | +For production deployment: |
| 132 | + |
| 133 | +1. Set `NODE_ENV=production` |
| 134 | +2. Use proper PostgreSQL connection string with SSL |
| 135 | +3. Configure connection pool limits appropriately |
| 136 | +4. Set up database monitoring and alerting |
| 137 | +5. Implement backup strategies |
| 138 | + |
| 139 | +## Security Considerations |
| 140 | + |
| 141 | +- Never commit `.env.local` to version control |
| 142 | +- Use environment variables for database credentials |
| 143 | +- Enable SSL connections in production |
| 144 | +- Implement proper access controls on database |
0 commit comments