Skip to content

Asynchronous Metrics Download in Admin Panel #1295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

saifrk
Copy link
Collaborator

@saifrk saifrk commented Mar 31, 2025

Implementation of the Download Metrics Button in Django Admin:

  1. Button addition:
    • Added a 'metrics' button using changelist_view method
  2. Task generation:
    • Triggered by button click, the download_metrics:
      • Generates a unique task_id (UUID).
      • Initiates an asynchronous Celery task generate_metrics.delay().
      • Displays a download link message
      • Redirects back to the collection list page.
  3. Cleanup mechanism:
    • At the start of generate_metrics task:
      • Scans for files named "metrics_*.csv" or "metrics.tmp" in the metrics directory.
      • Removes old files, keeping only those related to the current task_id.
  4. Background processing:
    • Asynchronous generate_metrics task:
      • Gathers metrics data about collections.
      • Creates a CSV file named metrics_{task_id}.csv in MEDIA_ROOT/metrics/.
      • Uses a .tmp file to indicate ongoing processing.
  5. File retrieval:
    • When downloading via the link:
      • get_metrics_file checks if the file exists and is substantial (>100 bytes).
      • Serves the file as a downloadable CSV if ready.
      • If still processing, displays status messages and offers retry option

Note: Currently there are 3 metrics that the script calculates, for all the 3 to run and a CSV to be written and downloaded, it takes over 15 mins. Thus it was necessary to have it run asynchronously for better user exp.

@saifrk saifrk changed the title Implement Asynchronous Metrics Download in Admin Panel Asynchronous Metrics Download in Admin Panel Mar 31, 2025
@saifrk saifrk requested a review from CarsonDavis March 31, 2025 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant