> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rocky.global/llms.txt
> Use this file to discover all available pages before exploring further.

# EXPORT OPTIMIZATION

# Participant Export Optimization

## Summary

Significant optimizations have been implemented for participant exports to prevent the server from slowing down with large volumes of data (30,000+ records).

## Optimizations Implemented

### 1. **Chunked Export**

* **File**: `app/Exports/ParticipantsExportOptimized.php`
* **Benefit**: Processes data in small batches (1000 records by default) instead of loading all records into memory
* **Implementation**: Uses `WithChunkReading` and `FromQuery` from Laravel Excel

### 2. **Background Processing**

* **File**: `app/Jobs/ProcessParticipantsExport.php`
* **Benefit**: For large exports (>5000 participants), processing happens in the background
* **Notification**: The user receives an email when the export is ready
* **Timeout**: 1 hour maximum processing time

### 3. **Validations and Limits**

* **File**: `app/Http/Middleware/ValidateExportRequest.php`
* **Maximum limit**: 100,000 participants per export
* **Immediate limit**: 5,000 participants (configurable)
* **Validation**: Required fields and event existence

### 4. **Query Optimization**

* **Specific select**: Only loads necessary fields
* **Filtering by event**: Reduces the volume of processed data
* **Indexes**: Optimized for queries by `evento_id`

### 5. **Automatic Cleanup**

* **Command**: `app/Console/Commands/CleanupExportFiles.php`
* **Schedule**: Runs daily
* **Retention**: Files are deleted after 7 days

## Configuration

### Environment Variables (.env)

```env theme={null}
# Limit for immediate processing (default: 5000)
EXPORT_IMMEDIATE_LIMIT=5000

# Chunk size for processing (default: 1000)
EXPORT_CHUNK_SIZE=1000

# Maximum participant limit (default: 100000)
EXPORT_MAX_PARTICIPANTS=100000
```

### Configuration in config/app.php

```php theme={null}
'export_immediate_limit' => env('EXPORT_IMMEDIATE_LIMIT', 5000),
'export_chunk_size' => env('EXPORT_CHUNK_SIZE', 1000),
'export_max_participants' => env('EXPORT_MAX_PARTICIPANTS', 100000),
```

## Operation Flow

### Small Exports (≤5,000 participants)

1. User clicks "Export"
2. Processed immediately
3. Direct download of the Excel file

### Large Exports (>5,000 participants)

1. User clicks "Export"
2. Notification is shown: "The export is being processed"
3. Job is dispatched to the background
4. User receives an email with the attached file when ready

## Files Modified

### Controllers

* `app/Http/Controllers/Eventos/EventosController.php`
  * Optimized `exportParticipants()` method
  * Decision logic between immediate/background processing

### Views

* `resources/views/eventos/participants/index.blade.php`
  * Updated JavaScript to handle JSON responses
  * Export status notifications

### Routes

* `routes/eventos.php`
  * `validate.export` middleware applied to the export route

### Middleware

* `app/Http/Middleware/ValidateExportRequest.php`
  * Validations for limits and required fields

## Artisan Commands

### File Cleanup

```bash theme={null}
# Clean up export files (manual)
php artisan exports:cleanup

# With options
php artisan exports:cleanup --days=7
```

### Automatic Scheduling

The command runs automatically every day at 00:00.

## Performance Benefits

1. **Memory**: 90% reduction in memory usage for large exports
2. **Response time**: Small exports remain immediate
3. **Scalability**: Support for up to 100,000 participants
4. **User experience**: Clear notifications of processing status
5. **Maintenance**: Automatic cleanup of temporary files

## Monitoring

### Logs

* Large exports are logged
* Processing errors are captured and reported

### Metrics

* Number of participants per export
* Processing time
* Files generated and deleted

## Security Considerations

1. **Strict limits**: Prevents DoS attacks
2. **Field validation**: Only allowed fields
3. **Automatic cleanup**: Temporary files are deleted
4. **Authentication**: Only authorized users can export

## Recommended Next Steps

1. **Monitoring**: Implement performance metrics
2. **Cache**: Consider caching for frequent queries
3. **Compression**: Compress large files before sending
4. **CDN**: Use a CDN for large file distribution
