# Enhanced User Management System

> Enhanced the Control Center user management page to provide comprehensive user activity metrics, profile image support, and detailed storage analytics.

- Skill: `tools-only/enhanced-user-management-system` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add tools-only/enhanced-user-management-system`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tools-only/enhanced-user-management-system/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tools-only (https://skillmd.com/u/tools-only)
- Updated: 2026-09-29
- Page: https://skillmd.com/skills/tools-only/enhanced-user-management-system

---

# Enhanced User Management System

**Version:** 0.230.018  
**Fixed/Implemented in version:** 0.230.018

## Overview

Enhanced the Control Center user management page to provide comprehensive user activity metrics, profile image support, and detailed storage analytics. This update transforms the basic user listing into a powerful administrative dashboard with rich user insights.

## Key Features

### 1. Profile Image Integration
- **Profile Image Display**: Extracts and displays user profile images from `user_settings.profileImage`
- **Base64 Support**: Handles base64-encoded profile images stored in user settings
- **Fallback Handling**: Gracefully handles users without profile images

### 2. Enhanced Chat Metrics
- **Total Conversations**: Lifetime conversation count per user
- **Total Messages**: Aggregate message count across all conversations
- **3-Month Activity**: Recent conversation activity (last 90 days)
- **Estimated Size**: Storage size estimation based on message count (~500 chars per message)

### 3. Comprehensive Document Metrics
- **Total Documents**: Count of documents in user's personal workspace
- **Storage Size**: Actual file storage consumption
- **AI Search Size**: Index size for enhanced citation features
- **Feature Status**: Shows if personal workspace and enhanced citation are enabled

### 4. Detailed Activity Timestamps
- **Last Chat Activity**: Most recent conversation activity
- **Last Document Activity**: Most recent document upload/modification
- **Last Login**: Most recent user login timestamp

## Technical Implementation

### Enhanced Data Structure

```python
enhanced_user = {
    'id': user.get('id'),
    'email': user.get('email', ''),
    'display_name': user.get('display_name', ''),
    'profile_image': user.get('settings', {}).get('profileImage'),
    'activity': {
        'last_login': timestamp,
        'last_chat_activity': timestamp,
        'last_document_activity': timestamp,
        'chat_metrics': {
            'total_conversations': count,
            'total_messages': count,
            'chat_volume_3m': count,
            'estimated_size': bytes
        },
        'document_metrics': {
            'personal_workspace_enabled': boolean,
            'enhanced_citation_enabled': boolean,
            'total_documents': count,
            'total_storage_size': bytes,
            'ai_search_size': bytes
        }
    }
}
```

### Database Queries

#### Chat Metrics Query
```sql
-- Total conversations
SELECT VALUE COUNT(1) FROM c WHERE c.user_id = @user_id

-- Total messages
SELECT VALUE SUM(c.message_count) FROM c 
WHERE c.user_id = @user_id AND c.message_count != null

-- Recent activity (3 months)
SELECT VALUE COUNT(1) FROM c 
WHERE c.user_id = @user_id AND c.last_updated >= @three_months_ago
```

#### Document Metrics Query
```sql
-- Comprehensive document metrics
SELECT 
    COUNT(1) as total_count,
    SUM(c.file_size) as total_storage_size,
    MAX(c.upload_date) as last_upload_date
FROM c WHERE c.user_id = @user_id

-- AI Search size (when enhanced citation enabled)
SELECT SUM(c.processed_content_size) as ai_search_size
FROM c WHERE c.user_id = @user_id AND c.processed_content_size != null
```

## Display Format Examples

### Chat Column Display
```
Conversations: 45, Messages: 892, Size: 435KB
```

### Documents Column Display
```
Docs: 15, Storage: 2.3MB, AI Search: 1.9MB (Personal Workspace)
```

### Last Activity Display
```
Chat: 2025-10-01 14:30
Docs: 2025-09-28 09:15
```

## Feature Flags Integration

The system checks user settings for feature enablement:

- **Personal Workspace**: `settings.enable_personal_workspace`
- **Enhanced Citation**: `settings.enable_enhanced_citation`

These flags affect:
- Document metrics collection
- AI search size calculation
- Display formatting and indicators

## Storage Size Calculations

### Chat Size Estimation
- **Formula**: `total_messages * 500 characters`
- **Rationale**: Average message length including metadata
- **Display**: Converted to KB/MB for readability

### Document Storage
- **Actual Size**: Sum of `file_size` fields from document records
- **AI Search Size**: Sum of `processed_content_size` or 80% of total storage as fallback
- **Display**: Converted to MB with 1 decimal precision

## Performance Considerations

### Optimized Queries
- Uses aggregate functions (`COUNT`, `SUM`, `MAX`) to minimize data transfer
- Implements proper indexing on `user_id` and timestamp fields
- Batches multiple metrics into single queries where possible

### Caching Strategy
- User activity data can be cached for short periods (5-10 minutes)
- Profile images are extracted once per session
- Feature flags are read from user settings without additional queries

## Error Handling

### Graceful Degradation
- Missing profile images default to `null`
- Failed metric queries default to zero values
- Database connection issues fall back to basic user data
- Comprehensive logging for debugging

### Exception Management
```python
try:
    # Enhanced metrics collection
    enhanced_metrics = collect_user_metrics(user_id)
except Exception as e:
    debug_print(f"Could not get metrics for user {user_id}: {e}")
    # Return basic user data with default values
    return basic_user_data
```

## API Response Format

### Enhanced User Object
```json
{
  "id": "user-123",
  "email": "user@microsoft.com",
  "display_name": "John Doe",
  "profile_image": "data:image/jpeg;base64,/9j/...",
  "activity": {
    "last_login": "2025-10-03T08:00:00Z",
    "last_chat_activity": "2025-10-01T14:30:00Z",
    "last_document_activity": "2025-09-28T09:15:00Z",
    "chat_metrics": {
      "total_conversations": 45,
      "total_messages": 892,
      "chat_volume_3m": 23,
      "estimated_size": 446000
    },
    "document_metrics": {
      "personal_workspace_enabled": true,
      "enhanced_citation_enabled": true,
      "total_documents": 15,
      "total_storage_size": 2457600,
      "ai_search_size": 1966080
    }
  }
}
```

## Frontend Integration

### Profile Image Display
```html
<img src="{user.profile_image}" alt="{user.display_name}" class="user-avatar" />
```

### Metrics Display Components
```html
<!-- Chat Metrics -->
<div class="chat-metrics">
  <span>Conversations: {chat_metrics.total_conversations}</span>
  <span>Messages: {chat_metrics.total_messages}</span>
  <span>Size: {formatBytes(chat_metrics.estimated_size)}</span>
</div>

<!-- Document Metrics -->
<div class="document-metrics">
  <span>Docs: {document_metrics.total_documents}</span>
  <span>Storage: {formatBytes(document_metrics.total_storage_size)}</span>
  {if document_metrics.enhanced_citation_enabled}
    <span>AI Search: {formatBytes(document_metrics.ai_search_size)}</span>
  {/if}
  {if document_metrics.personal_workspace_enabled}
    <span class="feature-indicator">(Personal Workspace)</span>
  {/if}
</div>
```

## Testing and Validation

### Functional Tests
- **test_enhanced_user_management.py**: Validates enhanced user data structure
- **Profile Image Extraction**: Tests base64 image handling
- **Metrics Calculation**: Validates chat and document metrics
- **Feature Flags**: Tests personal workspace and enhanced citation detection
- **Display Formatting**: Validates human-readable metric formatting

### Test Coverage
- ✅ Profile image extraction from user settings
- ✅ Chat metrics aggregation and calculation
- ✅ Document metrics with storage size computation
- ✅ Feature flag detection and display
- ✅ Error handling and graceful degradation
- ✅ Display format validation

## Migration and Deployment

### Database Requirements
- No schema changes required
- Uses existing `user_settings`, `conversations`, and `user_documents` containers
- Requires read access to user profile data

### Configuration Updates
- Version bump to 0.230.018
- Enhanced user management endpoint remains backward compatible
- New metrics are additive - existing frontend code continues to work

## Future Enhancements

### Potential Improvements
1. **Real-time Metrics**: WebSocket updates for live activity monitoring
2. **Historical Analytics**: Trend data over time periods
3. **Bulk Operations**: Enhanced bulk user management with metrics-based filtering
4. **Export Capabilities**: CSV export of user metrics for reporting
5. **Advanced Filtering**: Filter users by activity levels, storage usage, feature adoption

### Performance Optimizations
1. **Materialized Views**: Pre-computed metrics for large user bases
2. **Background Processing**: Asynchronous metric calculation for heavy workloads
3. **Selective Loading**: Load detailed metrics only when viewing specific users
4. **Caching Layer**: Redis caching for frequently accessed user metrics
