┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Flask API │ ──▶ │ Session Manager │ ──▶ │ SQLite Store │
└─────────────────┘ └─────────────────┘ └─────────────────┘
-
API Layer (
app.py
)- Handles HTTP requests and responses
- Implements input validation
- Routes requests to appropriate handlers
- Manages response formatting
-
Session Manager (
session_manager.py
)- Core business logic implementation
- Maintains in-memory session state
- Handles conversation flow
- Manages user context
-
Database Layer (
db_handler.py
)- Handles SQLite operations
- Manages session persistence
- Implements data access patterns
- Handles connection pooling
- Request received at
/chat
endpoint - Request validation
- Session lookup/creation
- Business logic processing
- State persistence
- Response generation
CREATE TABLE sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_phone TEXT UNIQUE NOT NULL,
current_state TEXT NOT NULL,
user_name TEXT,
favorite_song TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE conversation_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER,
message TEXT NOT NULL,
sender TEXT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (session_id) REFERENCES sessions(id)
);
┌─────────────┐
│ INITIAL │
└──────┬──────┘
│ hello
▼
┌──────────────────┐
│ AWAITING_NAME │
└─────────┬────────┘
│ name provided
▼
┌───────────────────────┐
│ AWAITING_SONG │
└───────────┬───────────┘
│ song provided
▼
┌─────────────────┐
│ COMPLETE │
└─────────────────┘
-
Input Validation
- Phone number format
- Message presence
- State transitions
-
Database Errors
- Connection issues
- Constraint violations
- Transaction management
-
Session Management
- Invalid state transitions
- Session timeouts
- Concurrent access
-
Input Sanitization
- SQL injection prevention
- XSS protection
- Input length limits
-
Rate Limiting
- Per-user limits
- Global API limits
- Burst protection
-
Data Protection
- Phone number hashing
- Sensitive data encryption
- Session isolation
-
Database
- Connection pooling
- Indexed queries
- Periodic cleanup
-
Memory Management
- Session cache
- LRU eviction
- Memory limits
-
Response Time
- Async processing
- Request queuing
- Load balancing
-
Application Metrics
- Request latency
- Error rates
- Session counts
-
System Metrics
- CPU usage
- Memory usage
- Disk I/O
-
Business Metrics
- Active users
- Completion rates
- Session duration
-
Horizontal Scaling
- Container orchestration
- Load balancing
- Session stickiness
-
Database Scaling
- Read replicas
- Sharding
- Backup strategy
-
Cache Strategy
- Redis integration
- Cache invalidation
- Distribution