# Call Analytics Dashboard - Backend API

Flask-based REST API for the Call Analytics Dashboard. Provides endpoints for managing calls, businesses, and analytics data.

![Python](https://img.shields.io/badge/Python-3.8+-blue) ![Flask](https://img.shields.io/badge/Flask-3.0-green) ![MySQL](https://img.shields.io/badge/MySQL-8.0-orange)

## Features

### 📊 **Business Management**
- List all businesses with call counts
- Get detailed business information
- Multi-tenant support with dynamic table routing

### 📞 **Call Management**
- Get all calls with filtering (status, intent, date range)
- Get specific call details with transcripts
- Search calls by multiple fields
- Recent calls retrieval
- Webhook support for call updates

### 📈 **Analytics**
- Call statistics (totals, averages, success rates)
- Sentiment distribution analysis
- Sales intent distribution
- Trend analysis over time
- Agent performance metrics
- Top keywords extraction

### 🔄 **Data Export**
- Export calls to JSON
- Export calls to CSV
- Filtered data export

## API Endpoints

### Health & Status
```
GET  /health                      # Health check
```

### Business Management
```
GET  /list-businesses             # List all businesses
GET  /businesses/<bid>/info       # Get business details
```

### Call Management
```
GET  /calls/<bid>                 # Get calls with filtering
GET  /calls/<bid>/<callid>        # Get call details
GET  /calls/<bid>/recent          # Get recent calls
POST /calls/search                # Search calls
POST /queue-calls/<bid>           # Queue calls for processing
POST /process-calls/<bid>         # Process calls directly
```

### Analytics
```
GET  /analytics/<bid>/stats       # Overall statistics
GET  /analytics/<bid>/sentiment   # Sentiment distribution
GET  /analytics/<bid>/intent      # Intent distribution
GET  /analytics/<bid>/trends      # Trends over time
GET  /analytics/<bid>/agents      # Agent performance
GET  /analytics/<bid>/keywords    # Top keywords
```

### Data Export
```
GET  /export/<bid>/calls          # Export calls (JSON/CSV)
```

### Webhooks
```
POST /webhook/call-update         # Receive call updates
POST /webhook/conversation-summary # Receive summaries
```

## Database Schema

### Tables Used

**Per Business (dynamic tables):**
- `{bid}_calls` - Main call records
- `{bid}_sarvamresponse` - Transcription data
- `{bid}_call_history` - Conversation summaries

**Call Record Fields:**
```
id, bid, callid, fileUrl, status, agent_name, duration,
customer_id, customer_name, call_type, call_date, call_time,
call_status, keywords, sentiments, emotions, summary,
sand_logic_response, sales_intent, transcripts,
customer_details, remarks, created_at, updated_at
```

**Status Codes:**
- `0` - Unprocessed
- `1` - Transcribed
- `2` - Analyzed
- `3` - Message Sent

## Installation

### Prerequisites
- Python 3.8+
- MySQL 8.0+
- Access to voicebot_cluster database

### Setup Steps

1. **Create virtual environment:**
```bash
cd dashboard-backend
python3 -m venv venv
source venv/bin/activate  # Linux/Mac
# or
venv\Scripts\activate  # Windows
```

2. **Install dependencies:**
```bash
pip install -r requirements.txt
```

3. **Configure environment:**
```bash
cp .env.example .env
# Edit .env with your database credentials
```

4. **Update .env file:**
```env
DB_HOST=10.0.0.109
DB_USER=admin
DB_PASSWORD=your-password
DB_NAME=voicebot_cluster
```

5. **Run the application:**
```bash
python app.py
```

API will be available at: **http://localhost:5000**

## Configuration

### Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `FLASK_ENV` | Environment (development/production) | development |
| `FLASK_DEBUG` | Debug mode | True |
| `FLASK_HOST` | Server host | 0.0.0.0 |
| `FLASK_PORT` | Server port | 5000 |
| `DB_HOST` | Database host | 10.0.0.109 |
| `DB_PORT` | Database port | 3306 |
| `DB_USER` | Database user | admin |
| `DB_PASSWORD` | Database password | - |
| `DB_NAME` | Database name | voicebot_cluster |
| `CORS_ORIGINS` | Allowed CORS origins | localhost:5173 |

## Project Structure

```
dashboard-backend/
├── app.py              # Main Flask application
├── config.py           # Configuration classes
├── db_handler.py       # Database operations
├── analytics.py        # Analytics service
├── requirements.txt    # Python dependencies
├── .env.example        # Environment template
├── .env                # Your config (don't commit)
└── README.md          # This file
```

## API Usage Examples

### Get All Businesses
```bash
curl http://localhost:5000/list-businesses
```

Response:
```json
[
  {
    "bid": "6840",
    "name": "Business 6840",
    "totalCalls": 125,
    "callsTable": "6840_calls"
  }
]
```

### Get Calls with Filtering
```bash
# All calls
curl "http://localhost:5000/calls/6840"

# Filter by status
curl "http://localhost:5000/calls/6840?status=3"

# Filter by date range
curl "http://localhost:5000/calls/6840?date_from=2024-11-01&date_to=2024-11-30"

# Pagination
curl "http://localhost:5000/calls/6840?limit=50&offset=0"
```

### Get Call Details
```bash
curl http://localhost:5000/calls/6840/CALL-2024-001
```

### Search Calls
```bash
curl -X POST http://localhost:5000/calls/search \
  -H "Content-Type: application/json" \
  -d '{
    "bid": "6840",
    "query": "product inquiry",
    "limit": 50
  }'
```

### Get Analytics
```bash
# Overall statistics
curl "http://localhost:5000/analytics/6840/stats"

# Sentiment distribution
curl "http://localhost:5000/analytics/6840/sentiment"

# Intent distribution
curl "http://localhost:5000/analytics/6840/intent"

# Trends (last 7 days)
curl "http://localhost:5000/analytics/6840/trends?days=7"

# Agent performance
curl "http://localhost:5000/analytics/6840/agents"

# Top keywords
curl "http://localhost:5000/analytics/6840/keywords?limit=20"
```

### Export Data
```bash
# Export as JSON
curl "http://localhost:5000/export/6840/calls"

# Export as CSV
curl "http://localhost:5000/export/6840/calls?format=csv" > calls.csv
```

## Production Deployment

### Using Gunicorn

```bash
# Install gunicorn (already in requirements.txt)
pip install gunicorn

# Run with 4 workers
gunicorn -w 4 -b 0.0.0.0:5000 app:app

# With gevent for async support
gunicorn -w 4 -k gevent -b 0.0.0.0:5000 app:app
```

### Using Systemd Service

Create `/etc/systemd/system/dashboard-api.service`:

```ini
[Unit]
Description=Call Analytics Dashboard API
After=network.target

[Service]
User=www-data
WorkingDirectory=/var/www/html/sara/dashboard-backend
Environment="PATH=/var/www/html/sara/dashboard-backend/venv/bin"
ExecStart=/var/www/html/sara/dashboard-backend/venv/bin/gunicorn \
    -w 4 -k gevent -b 0.0.0.0:5000 app:app

[Install]
WantedBy=multi-user.target
```

Enable and start:
```bash
sudo systemctl enable dashboard-api
sudo systemctl start dashboard-api
sudo systemctl status dashboard-api
```

### Nginx Reverse Proxy

Add to nginx config:

```nginx
server {
    listen 80;
    server_name dashboard-api.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

## Error Handling

All endpoints return consistent error responses:

```json
{
  "error": "Error message",
  "message": "Human-readable description"
}
```

HTTP Status Codes:
- `200` - Success
- `400` - Bad Request
- `404` - Not Found
- `500` - Internal Server Error

## Logging

Logs are written to:
- Console (stdout)
- File: `dashboard_api.log` (if configured)

Log format:
```
2024-11-25 10:30:45 - db_handler - INFO - Database connection established
```

## Security Considerations

1. **Environment Variables**: Never commit `.env` file
2. **CORS**: Configure allowed origins properly
3. **SQL Injection**: Uses parameterized queries
4. **Rate Limiting**: Enable in production
5. **Authentication**: Add JWT/OAuth if needed

## Troubleshooting

### Database Connection Errors
```bash
# Test database connection
mysql -h 10.0.0.109 -u admin -p voicebot_cluster
```

### Port Already in Use
```bash
# Kill process on port 5000
lsof -ti:5000 | xargs kill -9
```

### Module Not Found
```bash
# Reinstall dependencies
pip install -r requirements.txt
```

### CORS Errors
Update CORS_ORIGINS in `.env`:
```env
CORS_ORIGINS=http://localhost:5173,https://yourdomain.com
```

## Performance Optimization

### Enable Caching
```env
CACHE_ENABLED=True
CACHE_TYPE=redis
CACHE_REDIS_HOST=localhost
```

### Database Indexing
Recommended indexes:
```sql
ALTER TABLE 6840_calls ADD INDEX idx_status (status);
ALTER TABLE 6840_calls ADD INDEX idx_call_date (call_date);
ALTER TABLE 6840_calls ADD INDEX idx_sales_intent (sales_intent);
ALTER TABLE 6840_calls ADD INDEX idx_callid (callid);
```

### Connection Pooling
Uses PyMySQL connection pooling automatically.

## Integration with Frontend

Update frontend API URL in `dashboard-frontend/src/services/api.js`:

```javascript
const API_BASE_URL = 'http://your-server:5000';
```

## Testing

### Manual Testing
```bash
# Health check
curl http://localhost:5000/health

# Test endpoints
curl http://localhost:5000/list-businesses
curl "http://localhost:5000/calls/6840?limit=5"
```

### Load Testing (with Apache Bench)
```bash
ab -n 1000 -c 10 http://localhost:5000/health
```

## Monitoring

### Health Check Endpoint
```bash
# Continuous monitoring
watch -n 5 'curl -s http://localhost:5000/health'
```

### Logs Monitoring
```bash
tail -f dashboard_api.log
```

## Future Enhancements

- [ ] JWT Authentication
- [ ] Role-based access control
- [ ] WebSocket support for real-time updates
- [ ] Redis caching layer
- [ ] API rate limiting
- [ ] Comprehensive test suite
- [ ] API documentation (Swagger/OpenAPI)
- [ ] Data validation with Marshmallow
- [ ] Background task queue (Celery)
- [ ] Metrics and monitoring (Prometheus)

## License

Part of the Post Call Analysis System.

## Support

For issues or questions, contact the development team.

---

**Built with Flask + PyMySQL + Python 3.8+**
