# Admin Dashboard Guide

## Overview

The Admin Dashboard is a real-time control center for clinic administrators to monitor avatar call activity and system health. It updates automatically every 5 seconds.

## Access

```
http://localhost:3000/dashboard.html
```

## Features

### 1. Real-Time Statistics

**Displayed metrics:**
- **Total Calls** - All calls since system launch, with today's count
- **Total Duration** - Sum of all call durations in minutes
- **Avg Call Length** - Average call duration in seconds
- **Unique Patients** - Number of distinct patients who have called
- **Unprocessed Calls** - Calls waiting for EMR updates

### 2. Recent Calls Panel

**Shows the 10 most recent calls with:**
- Patient ID
- Timestamp
- Call duration (minutes and seconds)
- Number of messages in transcript
- Processing status (✓ Processed or ⏳ Pending)

**Click any call** to view full details including:
- Complete session information
- Call metadata
- Full transcript with speaker labels
- EMR update summary (if processed)

### 3. MCP FHIR Server Status

**Real-time monitoring of:**
- Server online/offline status
- Server URL
- Number of available tools
- List of FHIR operations (create, read, search, update)

**Status indicators:**
- 🟢 **Green "MCP Online"** - Server is healthy and responding
- 🔴 **Red "MCP Offline"** - Server is unreachable or has errors

### 4. Auto-Refresh

The dashboard automatically refreshes every **5 seconds** to provide real-time updates:
- Call statistics
- Recent call list
- MCP server status
- Last update timestamp

## Use Cases for Clinic Admins

### Daily Operations

1. **Monitor Call Volume**
   - Track how many patients are calling
   - Identify peak hours
   - Measure patient engagement

2. **Quality Assurance**
   - Review call transcripts
   - Ensure avatar is providing correct information
   - Identify training opportunities

3. **EMR Processing**
   - Track unprocessed calls
   - Verify EMR updates are completing
   - Follow up on failed updates

4. **System Health**
   - Monitor MCP server connectivity
   - Ensure FHIR operations are available
   - Quick troubleshooting

### Specific Workflows

#### Reviewing a Patient Call

1. Open dashboard: `http://localhost:3000/dashboard.html`
2. Find the patient in "Recent Calls"
3. Click the call entry
4. Review:
   - Call duration and timestamp
   - Full conversation transcript
   - EMR updates applied
5. Close modal when done

#### Monitoring System Health

1. Check the top-right status badge
2. **Green "MCP Online"** = Everything is working
3. **Red "MCP Offline"** = Check:
   - Is MCP server running?
   - Network connectivity
   - Server logs

#### Checking Unprocessed Calls

1. Look at "Unique Patients" card
2. Note the "X unprocessed" count
3. Click calls with "⏳ Pending" status
4. Investigate why EMR updates haven't completed

## API Endpoints

The dashboard uses three API endpoints:

### GET `/api/dashboard/stats`

Returns call statistics and recent calls.

**Response:**
```json
{
  "success": true,
  "timestamp": "2026-06-06T10:00:00.000000",
  "stats": {
    "total_calls": 5,
    "today_calls": 1,
    "total_duration_minutes": 8.9,
    "avg_duration_seconds": 107.0,
    "processed_calls": 2,
    "unprocessed_calls": 3,
    "unique_patients": 1
  },
  "recent_calls": [...]
}
```

### GET `/api/dashboard/mcp-status`

Returns MCP FHIR server status and available tools.

**Response:**
```json
{
  "success": true,
  "status": "online",
  "url": "http://localhost:8082/mcp",
  "tools_count": 4,
  "tools": [...],
  "timestamp": "2026-06-06T10:00:00.000000"
}
```

### GET `/api/dashboard/call-log/{log_file}`

Returns detailed information about a specific call.

**Response:**
```json
{
  "success": true,
  "log": {
    "session_id": "abc123",
    "patient_id": "69c2792581e99ae3d62b8954",
    "timestamp": "2026-06-06T10:00:00.000000",
    "duration_seconds": 120,
    "transcript": [...],
    "processed": true,
    "emr_update_summary": "..."
  }
}
```

## Troubleshooting

### Dashboard Won't Load

**Check:**
1. Is the web server running on port 3000?
   ```bash
   curl http://localhost:3000/dashboard.html
   ```
2. Is the API server running on port 8000?
   ```bash
   curl http://localhost:8000/health
   ```

### No Calls Showing

**Possible causes:**
1. No calls have been made yet - test with `test-avatar.html`
2. Call logs directory is empty
3. API server can't read `/call_logs/` directory

**Fix:**
```bash
ls -la /Users/tarun/Elume\ Agents/elume-mcp-fhir/call_logs/
```

### MCP Status Shows Offline

**Check:**
1. Is MCP server running?
   ```bash
   curl http://localhost:8082/health
   ```
2. Check API server logs:
   ```bash
   tail -f /tmp/api_server.log
   ```
3. Verify MCP URL in `.env` file

### Dashboard Not Updating

**Try:**
1. Hard refresh the page (Cmd+Shift+R or Ctrl+Shift+R)
2. Check browser console for errors (F12)
3. Verify API endpoints are responding:
   ```bash
   curl http://localhost:8000/api/dashboard/stats
   ```

## Technical Details

### Auto-Refresh Mechanism

The dashboard uses `setInterval` to refresh every 5 seconds:

```javascript
setInterval(refreshDashboard, 5000);
```

Both stats and MCP status are fetched in parallel using `Promise.all()` for efficiency.

### Data Flow

```
Dashboard (Browser)
    ↓ (every 5 seconds)
API Server (/api/dashboard/*)
    ↓
Call Logs (/call_logs/*.json) + MCP Server (http://localhost:8082)
    ↓
Dashboard UI Updated
```

### Performance

- **Lightweight**: No heavy dependencies, vanilla JavaScript
- **Efficient**: Parallel API calls
- **Responsive**: Updates within milliseconds
- **Low bandwidth**: Only fetches changed data

## Best Practices

### For Clinic Administrators

1. **Keep dashboard open during business hours** for real-time monitoring
2. **Review unprocessed calls** at end of day
3. **Check MCP status** before critical operations
4. **Monitor call patterns** to optimize staffing

### For System Operators

1. **Monitor logs** along with dashboard
2. **Set up alerts** for MCP offline status
3. **Archive old call logs** regularly
4. **Review dashboard performance** if refresh slows down

## Future Enhancements

Potential improvements:
- Email/SMS alerts for unprocessed calls
- Export call logs to CSV
- Filter calls by date range
- Search for specific patients
- Charts and graphs for call trends
- Integration with external alerting systems

## Support

**Dashboard Location:** `http://localhost:3000/dashboard.html`

**Server Logs:**
```bash
tail -f /tmp/api_server.log
```

**Call Logs Directory:**
```bash
/Users/tarun/Elume Agents/elume-mcp-fhir/call_logs/
```

## Summary

The Admin Dashboard provides clinic administrators with:
✅ Real-time call monitoring  
✅ Patient interaction insights  
✅ System health visibility  
✅ EMR processing status  
✅ Auto-updating every 5 seconds  
✅ Easy access to call transcripts  
✅ MCP server status monitoring  

It's a centralized control center that helps administrators stay informed and respond quickly to issues.
