From 0917025cac7cdf2ca1341ca1763c22a288fa7313 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Mon, 23 Mar 2026 15:54:14 -0400 Subject: [PATCH] [charts] Add periodic rebuilding of charts --- vrobbler/apps/scrobbles/tasks.py | 56 +++++++++++++++++++++++++++++++- vrobbler/settings.py | 12 +++++-- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/vrobbler/apps/scrobbles/tasks.py b/vrobbler/apps/scrobbles/tasks.py index 5d3139d..7dd5b6a 100644 --- a/vrobbler/apps/scrobbles/tasks.py +++ b/vrobbler/apps/scrobbles/tasks.py @@ -1,13 +1,32 @@ import logging from celery import shared_task -from charts.utils import build_charts_since +from charts.utils import ( + build_charts_since, + build_yearly_charts, + build_monthly_charts, + build_weekly_charts, +) from django.apps import apps from django.contrib.auth import get_user_model +from django.utils import timezone logger = logging.getLogger(__name__) User = get_user_model() +MEDIA_TYPES = [ + "artist", + "album", + "track", + "tv_series", + "video", + "podcast", + "board_game", + "trail", + "food", + "book", +] + @shared_task def process_retroarch_import(import_id): @@ -67,3 +86,38 @@ def build_charts_for_user(user_id): return logger.info(f"Building charts for {user}") build_charts_since(user) + + +@shared_task +def rebuild_weekly_charts(): + """Rebuild weekly charts for all users for the just-completed week.""" + now = timezone.now() + year, week, _ = now.isocalendar() + for user in User.objects.all(): + build_weekly_charts(user, year, week, MEDIA_TYPES) + logger.info(f"Rebuilt weekly charts for week {week}, {year}") + + +@shared_task +def rebuild_monthly_charts(): + """Rebuild monthly charts for all users for the just-completed month.""" + now = timezone.now() + year, month = now.year, now.month + if month == 1: + month = 12 + year -= 1 + else: + month -= 1 + for user in User.objects.all(): + build_monthly_charts(user, year, month, MEDIA_TYPES) + logger.info(f"Rebuilt monthly charts for {month}/{year}") + + +@shared_task +def rebuild_yearly_charts(): + """Rebuild yearly charts for all users for the just-completed year.""" + now = timezone.now() + year = now.year - 1 + for user in User.objects.all(): + build_yearly_charts(user, year, MEDIA_TYPES) + logger.info(f"Rebuilt yearly charts for {year}") diff --git a/vrobbler/settings.py b/vrobbler/settings.py index 3b18df6..491145a 100644 --- a/vrobbler/settings.py +++ b/vrobbler/settings.py @@ -121,10 +121,18 @@ CELERY_BEAT_SCHEDULE = { "task": "scrobbles.tasks.create_yesterdays_charts", "schedule": crontab(hour=0, minute=5), }, - "build-missing-charts-weekly": { - "task": "scrobbles.tasks.build_missing_charts_for_all_users", + "rebuild-weekly-charts": { + "task": "scrobbles.tasks.rebuild_weekly_charts", "schedule": crontab(hour=1, minute=0, day_of_week=0), }, + "rebuild-monthly-charts": { + "task": "scrobbles.tasks.rebuild_monthly_charts", + "schedule": crontab(hour=0, minute=30, day_of_month=1), + }, + "rebuild-yearly-charts": { + "task": "scrobbles.tasks.rebuild_yearly_charts", + "schedule": crontab(hour=0, minute=30, day_of_month=1, month_of_year=1), + }, } INSTALLED_APPS = [