|
| 1 | +# Architectural Patterns |
| 2 | + |
| 3 | +This document describes architectural patterns used consistently across this codebase. |
| 4 | + |
| 5 | +## API Design Patterns |
| 6 | + |
| 7 | +### Route Structure |
| 8 | +All Next.js API routes export explicit HTTP method handlers. See: |
| 9 | +- `codebenders-dashboard/app/api/analyze/route.ts:82-87` |
| 10 | +- `codebenders-dashboard/app/api/dashboard/kpis/route.ts:22` |
| 11 | +- `codebenders-dashboard/app/api/execute-sql/route.ts:33` |
| 12 | + |
| 13 | +### Error Response Standardization |
| 14 | +Consistent error structure: `{ error: string, details?: string }` with appropriate HTTP status codes (400 for bad requests, 404 for not found, 500 for server errors). See: |
| 15 | +- `codebenders-dashboard/app/api/dashboard/kpis/route.ts:54-59` |
| 16 | +- `codebenders-dashboard/app/api/execute-sql/route.ts:72-78` |
| 17 | +- `codebenders-dashboard/app/api/analyze/route.ts:212-218` |
| 18 | + |
| 19 | +### Console Logging with Prefixes |
| 20 | +Debug logs use module prefixes for traceability (e.g., `[analyze]`, `[v0]`). See: |
| 21 | +- `codebenders-dashboard/app/api/analyze/route.ts:83-211` |
| 22 | +- `codebenders-dashboard/lib/query-executor.ts:11-72` |
| 23 | + |
| 24 | +## Database Access Patterns |
| 25 | + |
| 26 | +### Connection Pooling (TypeScript) |
| 27 | +Lazy-initialized singleton pg Pool prevents connection exhaustion: |
| 28 | +- `codebenders-dashboard/lib/db.ts` - `getPool()` singleton |
| 29 | + |
| 30 | +Key config: `max: 10`, pool error handler registered on init |
| 31 | + |
| 32 | +### Connection Pooling (Python) |
| 33 | +psycopg2 with connection pooling via SQLAlchemy: |
| 34 | +- `operations/db_utils.py` - `get_connection()`, `get_sqlalchemy_engine()` |
| 35 | +- `operations/db_config.py` - Centralized DB_CONFIG |
| 36 | + |
| 37 | +### Parameterized Queries |
| 38 | +All dynamic queries use `$1`, `$2` placeholders (Postgres style) with params arrays to prevent SQL injection: |
| 39 | +- `codebenders-dashboard/app/api/dashboard/readiness/route.ts:47-96` |
| 40 | + |
| 41 | +### Bulk Data Insertion |
| 42 | +Chunked DataFrame insertion (1000 records/batch) with progress tracking: |
| 43 | +- `operations/db_utils.py:49-117` - `save_dataframe_to_db()` |
| 44 | + |
| 45 | +## React/Next.js Patterns |
| 46 | + |
| 47 | +### Independent State Variables |
| 48 | +Multiple `useState` hooks for different data domains instead of single state object: |
| 49 | +- `codebenders-dashboard/app/page.tsx:51-58` |
| 50 | +- `codebenders-dashboard/app/query/page.tsx:27-32` |
| 51 | + |
| 52 | +### Parallel Data Fetching |
| 53 | +`Promise.all()` for concurrent API calls: |
| 54 | +- `codebenders-dashboard/app/page.tsx:67-81` |
| 55 | + |
| 56 | +### Component Loading States |
| 57 | +Three-state rendering pattern: loading skeleton → error message → content: |
| 58 | +- `codebenders-dashboard/components/kpi-card.tsx:19-59` |
| 59 | +- `codebenders-dashboard/components/risk-alert-chart.tsx:47-84` |
| 60 | + |
| 61 | +## ML Pipeline Patterns |
| 62 | + |
| 63 | +### Feature Engineering Pipeline |
| 64 | +Sequential stages: data loading → feature engineering → preprocessing → training → evaluation → storage: |
| 65 | +- `ai_model/complete_ml_pipeline.py:91-179` - Target variable calculation |
| 66 | +- `ai_model/complete_ml_pipeline.py:189-223` - Feature set definitions |
| 67 | + |
| 68 | +### Preprocessing with Label Encoding |
| 69 | +Centralized preprocessing: median imputation for numeric, "Unknown" for categorical, LabelEncoder for object types: |
| 70 | +- `ai_model/complete_ml_pipeline.py:232-256` - `preprocess_features()` |
| 71 | + |
| 72 | +### Model Performance Tracking |
| 73 | +Metrics saved to `ml_model_performance` table after each training run: |
| 74 | +- `operations/db_utils.py:159-210` - `save_model_performance()` |
| 75 | + |
| 76 | +## Component Patterns |
| 77 | + |
| 78 | +### TypeScript Props Interfaces |
| 79 | +All components define explicit prop interfaces with optional loading/error fields: |
| 80 | +- `codebenders-dashboard/components/kpi-card.tsx:5-16` |
| 81 | +- `codebenders-dashboard/components/risk-alert-chart.tsx:13-17` |
| 82 | + |
| 83 | +### Chart Color Mapping |
| 84 | +Centralized color dictionaries mapping semantic values to hex colors: |
| 85 | +- `codebenders-dashboard/components/risk-alert-chart.tsx:19-24` |
| 86 | +- `codebenders-dashboard/components/retention-risk-chart.tsx:19-24` |
| 87 | + |
| 88 | +Colors: LOW=#22c55e (green), MODERATE=#eab308 (yellow), HIGH=#f97316 (orange), URGENT=#ef4444 (red) |
| 89 | + |
| 90 | +### Multi-Format Export |
| 91 | +Single component handles CSV, JSON, Markdown exports via `downloadFile()` utility: |
| 92 | +- `codebenders-dashboard/components/export-button.tsx:25-209` |
| 93 | + |
| 94 | +## Configuration Patterns |
| 95 | + |
| 96 | +### Environment Variable Hierarchy |
| 97 | +ENV vars with fallback defaults for development: |
| 98 | +- `codebenders-dashboard/app/api/dashboard/readiness/route.ts:4-10` |
| 99 | + |
| 100 | +### Schema Configuration Constants |
| 101 | +Database schema metadata as constants for multi-institution support: |
| 102 | +- `codebenders-dashboard/app/api/analyze/route.ts:22-79` - SCHEMA_INFO |
| 103 | +- `codebenders-dashboard/lib/prompt-analyzer.ts:4-28` - SCHEMA_CONFIG |
| 104 | + |
| 105 | +## Error Handling Patterns |
| 106 | + |
| 107 | +### Try-Catch with Typed Errors |
| 108 | +All API routes wrap operations in try-catch, return structured errors with stack traces logged: |
| 109 | +- `codebenders-dashboard/app/api/analyze/route.ts:209-220` |
| 110 | +- `codebenders-dashboard/app/api/execute-sql/route.ts:65-79` |
| 111 | + |
| 112 | +### Python Status Reporting |
| 113 | +Visual status indicators with `print()` statements: |
| 114 | +- `operations/db_utils.py` - Uses `✓` for success, `✗` for failure |
| 115 | +- Section headers with `"=" * 80` |
| 116 | + |
| 117 | +## Data Transformation Patterns |
| 118 | + |
| 119 | +### JSON Field Parsing |
| 120 | +Parse JSON columns from DB, aggregate values, handle parse errors gracefully: |
| 121 | +- `codebenders-dashboard/app/api/dashboard/readiness/route.ts:119-157` |
| 122 | + |
| 123 | +### Query Plan to SQL Conversion |
| 124 | +Semantic query plans translated to SQL using schema-aware column mapping: |
| 125 | +- `codebenders-dashboard/lib/prompt-analyzer.ts:30-174` |
0 commit comments