Spaces:
Running
Running
| # Dashboard Module - Architecture Overview | |
| ## System Architecture | |
| ``` | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β Mobile App β | |
| β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β | |
| β β React Native β β Flutter β β iOS/Androidβ β | |
| β ββββββββ¬ββββββββ ββββββββ¬ββββββββ ββββββββ¬ββββββββ β | |
| β β β β β | |
| β ββββββββββββββββββββ΄βββββββββββββββββββ β | |
| β β β | |
| β Local Cache (30s) β | |
| β β β | |
| ββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ | |
| β | |
| HTTPS + JWT Token | |
| β | |
| ββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ | |
| β API Gateway / Load Balancer β | |
| ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ | |
| β | |
| ββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ | |
| β FastAPI Application β | |
| β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β | |
| β β Dashboard Router (/api/v1/dashboard/*) β β | |
| β β - GET /summary β β | |
| β β - POST /refresh β β | |
| β β - GET /bookings/stats β β | |
| β β - GET /earnings/stats β β | |
| β ββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ β | |
| β β β | |
| β ββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ β | |
| β β JWT Authentication Middleware β β | |
| β β - Verify token β β | |
| β β - Extract partner_id β β | |
| β ββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ β | |
| β β β | |
| β ββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ β | |
| β β Dashboard Service β β | |
| β β - get_summary() - Orchestrates all queries β β | |
| β β - Parallel async execution β β | |
| β ββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ β | |
| β β β | |
| βββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β | |
| βββββββββββββ΄ββββββββββββ | |
| β β | |
| βββββββββΌβββββββββ ββββββββββΌβββββββββ | |
| β Redis Cache β β PostgreSQL β | |
| β (60s TTL) β β Database β | |
| β β β β | |
| β Cache Key: β β Tables: β | |
| β dashboard: β β - orders β | |
| β summary: β β - wallets β | |
| β {partner_id} β β - transactions β | |
| ββββββββββββββββββ βββββββββββββββββββ | |
| ``` | |
| ## Request Flow | |
| ### 1. Cache Hit Scenario (95% of requests) | |
| ``` | |
| Mobile App | |
| β | |
| β 1. GET /dashboard/summary | |
| β Authorization: Bearer <token> | |
| βΌ | |
| FastAPI Router | |
| β | |
| β 2. Verify JWT token | |
| β Extract partner_id | |
| βΌ | |
| Cache Check | |
| β | |
| β 3. Check Redis | |
| β Key: dashboard:summary:{partner_id} | |
| βΌ | |
| Redis Cache | |
| β | |
| β 4. Cache HIT! β | |
| β Return cached data | |
| βΌ | |
| FastAPI Router | |
| β | |
| β 5. Return JSON response | |
| β Time: ~10-20ms | |
| βΌ | |
| Mobile App | |
| β | |
| β 6. Display dashboard | |
| ββββ Done! | |
| ``` | |
| ### 2. Cache Miss Scenario (5% of requests) | |
| ``` | |
| Mobile App | |
| β | |
| β 1. GET /dashboard/summary | |
| βΌ | |
| FastAPI Router | |
| β | |
| β 2. Verify JWT, extract partner_id | |
| βΌ | |
| Cache Check | |
| β | |
| β 3. Check Redis - MISS β | |
| βΌ | |
| Dashboard Service | |
| β | |
| β 4. Execute parallel queries: | |
| β βββββββββββββββββββββββββββ | |
| β β Query 1: Booking Stats ββββ | |
| β β Query 2: Earnings Stats ββββ€ | |
| β β Query 3: Rating Stats ββββ€ Parallel | |
| β β Query 4: Upcoming Apts ββββ€ Execution | |
| β β Query 5: Recent Ratings ββββ€ (asyncio.gather) | |
| β β Query 6: Special Offers ββββ | |
| β βββββββββββββββββββββββββββ | |
| βΌ | |
| PostgreSQL Database | |
| β | |
| β 5. Execute queries with indexes | |
| β Time: ~50-100ms total | |
| βΌ | |
| Dashboard Service | |
| β | |
| β 6. Aggregate results | |
| β Build DashboardSummary object | |
| βΌ | |
| Redis Cache | |
| β | |
| β 7. Cache result (60s TTL) | |
| β Key: dashboard:summary:{partner_id} | |
| βΌ | |
| FastAPI Router | |
| β | |
| β 8. Return JSON response | |
| β Time: ~100-200ms | |
| βΌ | |
| Mobile App | |
| β | |
| β 9. Display dashboard | |
| β Cache locally (30s) | |
| ββββ Done! | |
| ``` | |
| ## Data Flow Diagram | |
| ``` | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β Dashboard Summary β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ | |
| β β | |
| β ββββββββββββββββββ ββββββββββββββββββ ββββββββββββββββ β | |
| β β Booking Stats β β Earnings Stats β β Rating Stats β β | |
| β ββββββββββββββββββ€ ββββββββββββββββββ€ ββββββββββββββββ€ β | |
| β β Total: 150 β β Total: βΉ125K β β Avg: 4.5/5 β β | |
| β β Confirmed: 45 β β Month: βΉ15K β β Total: 120 β β | |
| β β Completed: 95 β β Pending: βΉ2.5K β β 5β : 80 β β | |
| β β Cancelled: 8 β β Wallet: βΉ8.5K β β 4β : 30 β β | |
| β β Pending: 2 β β β β 3β : 8 β β | |
| β ββββββββββββββββββ ββββββββββββββββββ ββββββββββββββββ β | |
| β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β | |
| β β Upcoming Appointments (5 max) β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β | |
| β β β’ SPA-2024-001 | βΉ2,500 | Approved | Jan 20, 2PM β β | |
| β β β’ SPA-2024-002 | βΉ3,200 | Packed | Jan 21, 10AM β β | |
| β β β’ SPA-2024-003 | βΉ1,800 | Approved | Jan 22, 3PM β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β | |
| β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β | |
| β β Recent Ratings (5 max) β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β | |
| β β β’ Jane Smith | 5β | "Excellent!" | Jan 15 β β | |
| β β β’ John Doe | 4β | "Good work" | Jan 14 β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β | |
| β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β | |
| β β Special Offers β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β | |
| β β β’ 20% off on spa packages | Valid until Jan 31 β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β | |
| β β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| ``` | |
| ## Database Query Strategy | |
| ### Booking Statistics Query | |
| ```sql | |
| SELECT | |
| COUNT(*) as total, | |
| SUM(CASE WHEN order_status = 'approved' THEN 1 ELSE 0 END) as confirmed, | |
| SUM(CASE WHEN order_status = 'delivered' THEN 1 ELSE 0 END) as completed, | |
| SUM(CASE WHEN order_status = 'cancelled' THEN 1 ELSE 0 END) as cancelled, | |
| SUM(CASE WHEN order_status IN ('draft', 'placed') THEN 1 ELSE 0 END) as pending | |
| FROM trans.spa_partner_orders | |
| WHERE partner_id = :partner_id; | |
| -- Uses index: idx_spa_orders_partner_status | |
| -- Execution time: ~10-20ms | |
| ``` | |
| ### Earnings Statistics Query | |
| ```sql | |
| -- Query 1: Order earnings | |
| SELECT | |
| COALESCE(SUM(CASE WHEN order_status = 'delivered' THEN net_amount ELSE 0 END), 0) as total, | |
| COALESCE(SUM(CASE WHEN order_status = 'delivered' AND created_at >= :month_start THEN net_amount ELSE 0 END), 0) as current_month, | |
| COALESCE(SUM(CASE WHEN order_status IN ('approved', 'packed', 'dispatched') AND payment_status != 'paid' THEN net_amount ELSE 0 END), 0) as pending | |
| FROM trans.spa_partner_orders | |
| WHERE partner_id = :partner_id; | |
| -- Query 2: Wallet balance | |
| SELECT encrypted_balance | |
| FROM trans.spa_wallets | |
| WHERE partner_id = :partner_id; | |
| -- Uses indexes: idx_spa_orders_partner_status, idx_spa_wallets_partner | |
| -- Execution time: ~20-30ms (parallel) | |
| ``` | |
| ### Upcoming Appointments Query | |
| ```sql | |
| SELECT order_id, order_number, net_amount, order_status, created_at | |
| FROM trans.spa_partner_orders | |
| WHERE partner_id = :partner_id | |
| AND order_status IN ('approved', 'packed', 'dispatched') | |
| ORDER BY created_at DESC | |
| LIMIT 5; | |
| -- Uses index: idx_spa_orders_partner_status_created | |
| -- Execution time: ~5-10ms | |
| ``` | |
| ## Caching Strategy | |
| ### Cache Layers | |
| ``` | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β Layer 1: Client-Side Cache (Mobile App) β | |
| β - Duration: 30 seconds β | |
| β - Storage: AsyncStorage / SharedPreferences β | |
| β - Benefit: Instant load, offline support β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β | |
| βΌ | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β Layer 2: Server-Side Cache (Redis) β | |
| β - Duration: 60 seconds β | |
| β - Storage: Redis in-memory β | |
| β - Benefit: Fast API response, reduced DB load β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β | |
| βΌ | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β Layer 3: Database (PostgreSQL) β | |
| β - Duration: Permanent β | |
| β - Storage: Disk with indexes β | |
| β - Benefit: Source of truth, complex queries β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| ``` | |
| ### Cache Invalidation | |
| ``` | |
| Event Occurs | |
| β | |
| β (Order created, status changed, payment received, etc.) | |
| βΌ | |
| Application Code | |
| β | |
| β 1. Perform operation | |
| β (create order, update status, etc.) | |
| βΌ | |
| Cache Invalidation | |
| β | |
| β 2. Call invalidate_dashboard_cache(partner_id) | |
| βΌ | |
| Redis | |
| β | |
| β 3. DELETE dashboard:summary:{partner_id} | |
| βΌ | |
| Next Request | |
| β | |
| β 4. Cache miss β Fetch fresh data | |
| β 5. Cache new data | |
| ββββ Fresh data served | |
| ``` | |
| ## Performance Characteristics | |
| ### Response Time Distribution | |
| ``` | |
| Without Caching: | |
| ββ Database Query: 50-100ms (70%) | |
| ββ Data Processing: 10-20ms (15%) | |
| ββ Network: 10-30ms (15%) | |
| ββ Total: 100-200ms | |
| With Redis Cache: | |
| ββ Cache Lookup: 1-5ms (40%) | |
| ββ Data Processing: 2-5ms (20%) | |
| ββ Network: 5-15ms (40%) | |
| ββ Total: 10-20ms | |
| Improvement: 5-10x faster | |
| ``` | |
| ### Scalability | |
| ``` | |
| Requests per Second (RPS) Capacity: | |
| Without Caching: | |
| ββ Single Server: ~50 RPS | |
| ββ Bottleneck: Database | |
| ββ Scaling: Vertical (bigger DB) | |
| With Redis Caching (95% hit rate): | |
| ββ Single Server: ~1000 RPS | |
| ββ Bottleneck: Network/CPU | |
| ββ Scaling: Horizontal (more servers) | |
| Improvement: 20x capacity increase | |
| ``` | |
| ## Error Handling Flow | |
| ``` | |
| Request Received | |
| β | |
| βΌ | |
| βββββββββββββββββββ | |
| β JWT Validation β | |
| ββββββ¬βββββββββββββ | |
| β | |
| ββ Invalid Token β 401 Unauthorized | |
| β | |
| βΌ | |
| βββββββββββββββββββ | |
| β Cache Check β | |
| ββββββ¬βββββββββββββ | |
| β | |
| ββ Cache Error β Log warning, continue to DB | |
| β | |
| βΌ | |
| βββββββββββββββββββ | |
| β Database Query β | |
| ββββββ¬βββββββββββββ | |
| β | |
| ββ DB Error β Return empty dashboard with error flag | |
| β | |
| βΌ | |
| βββββββββββββββββββ | |
| β Response β | |
| βββββββββββββββββββ | |
| ``` | |
| ## Monitoring Points | |
| ``` | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β Metrics Collection Points β | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ | |
| β β | |
| β 1. API Gateway β | |
| β - Request count β | |
| β - Response time β | |
| β - Error rate β | |
| β β | |
| β 2. Application β | |
| β - Cache hit/miss rate β | |
| β - Query execution time β | |
| β - Parallel query performance β | |
| β β | |
| β 3. Redis β | |
| β - Memory usage β | |
| β - Eviction count β | |
| β - Connection count β | |
| β β | |
| β 4. PostgreSQL β | |
| β - Query time β | |
| β - Connection pool usage β | |
| β - Index usage β | |
| β β | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| ``` | |
| ## Security Architecture | |
| ``` | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β Security Layers β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ | |
| β β | |
| β 1. Transport Layer β | |
| β β HTTPS/TLS encryption β | |
| β β Certificate validation β | |
| β β | |
| β 2. Authentication Layer β | |
| β β JWT token validation β | |
| β β Token expiry check β | |
| β β Signature verification β | |
| β β | |
| β 3. Authorization Layer β | |
| β β Partner ID extraction β | |
| β β Data isolation (partner_id filter) β | |
| β β No cross-partner access β | |
| β β | |
| β 4. Data Layer β | |
| β β Encrypted wallet balance β | |
| β β SQL injection prevention (ORM) β | |
| β β Prepared statements β | |
| β β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| ``` | |
| ## Summary | |
| The dashboard architecture is designed for: | |
| - **Performance**: <100ms response time with caching | |
| - **Scalability**: 1000+ RPS per server with Redis | |
| - **Reliability**: Graceful degradation on errors | |
| - **Security**: Multi-layer protection | |
| - **Maintainability**: Clean separation of concerns | |