Skip to main content

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)

Configuration in config/app.php

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

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
  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