From e36a0726c8661786e9037291a45276ae42219200 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Mon, 23 Mar 2026 12:35:52 -0400 Subject: [PATCH] Revert "[charts] Async creating of new charts" This reverts commit 44c6e014f55dff071b589833e8a5bbcb6025ec6f. --- vrobbler/apps/charts/tests/test_utils.py | 7 +++-- vrobbler/apps/scrobbles/signals.py | 34 ++++++++++++++++++-- vrobbler/apps/scrobbles/tasks.py | 40 ------------------------ 3 files changed, 35 insertions(+), 46 deletions(-) diff --git a/vrobbler/apps/charts/tests/test_utils.py b/vrobbler/apps/charts/tests/test_utils.py index c134c39..4bd0364 100644 --- a/vrobbler/apps/charts/tests/test_utils.py +++ b/vrobbler/apps/charts/tests/test_utils.py @@ -17,6 +17,7 @@ def user(db): @pytest.fixture def scrobble_data(user, db): + from scrobbles.models import Scrobble from music.models import Track, Artist, Album artist = Artist.objects.create(name="Test Artist") @@ -132,7 +133,7 @@ def test_build_yearly_charts_creates_yearly_record(user, scrobble_data): @pytest.mark.django_db def test_build_charts_ranks_by_count(user, scrobble_data): from scrobbles.models import Scrobble - from music.models import Track + from music.models import Track, Artist track1 = scrobble_data["track"] track2 = Track.objects.create( @@ -182,7 +183,7 @@ def test_build_charts_ranks_by_count(user, scrobble_data): @pytest.mark.django_db def test_build_daily_charts_deletes_existing_daily_record(user, scrobble_data): from scrobbles.models import Scrobble - from music.models import Track + from music.models import Track, Artist track1 = scrobble_data["track"] track2 = Track.objects.create( @@ -227,7 +228,7 @@ def test_build_daily_charts_deletes_existing_daily_record(user, scrobble_data): @pytest.mark.django_db def test_build_monthly_charts_deletes_existing_monthly_record(user, scrobble_data): from scrobbles.models import Scrobble - from music.models import Track + from music.models import Track, Artist track1 = scrobble_data["track"] track2 = Track.objects.create( diff --git a/vrobbler/apps/scrobbles/signals.py b/vrobbler/apps/scrobbles/signals.py index eb41f3c..9abba06 100644 --- a/vrobbler/apps/scrobbles/signals.py +++ b/vrobbler/apps/scrobbles/signals.py @@ -9,6 +9,20 @@ from scrobbles.models import Scrobble logger = logging.getLogger(__name__) +MEDIA_TYPES = [ + "artist", + "album", + "track", + "tv_series", + "video", + "podcast", + "board_game", + "trail", + "food", + "book", +] + + @receiver(post_save, sender=Scrobble) def update_charts_on_scrobble_complete(sender, instance, **kwargs): if not instance.played_to_completion: @@ -32,6 +46,13 @@ def _update_charts_for_timestamp(user, ts): if ts is None: return + from charts.utils import ( + build_daily_charts, + build_monthly_charts, + build_weekly_charts, + build_yearly_charts, + ) + if timezone.is_naive(ts): ts = timezone.make_aware(ts) @@ -40,6 +61,13 @@ def _update_charts_for_timestamp(user, ts): week = ts.isocalendar()[1] day = ts.day - from scrobbles.tasks import update_charts_for_timestamp - - update_charts_for_timestamp.delay(user.id, year, month, day, week) + try: + build_daily_charts(user, year, month, day, MEDIA_TYPES) + build_weekly_charts(user, year, week, MEDIA_TYPES) + build_monthly_charts(user, year, month, MEDIA_TYPES) + build_yearly_charts(user, year, MEDIA_TYPES) + logger.info( + f"[charts] Updated charts for {user} on {year}-{month:02d}-{day:02d}" + ) + except Exception as e: + logger.error(f"[charts] Failed to update charts: {e}") diff --git a/vrobbler/apps/scrobbles/tasks.py b/vrobbler/apps/scrobbles/tasks.py index fd01ea2..5d3139d 100644 --- a/vrobbler/apps/scrobbles/tasks.py +++ b/vrobbler/apps/scrobbles/tasks.py @@ -67,43 +67,3 @@ def build_charts_for_user(user_id): return logger.info(f"Building charts for {user}") build_charts_since(user) - - -@shared_task -def update_charts_for_timestamp(user_id, year, month, day, week): - """Update charts for a specific time period.""" - user = User.objects.filter(id=user_id).first() - if not user: - logger.error(f"User with id {user_id} not found") - return - - from charts.utils import ( - build_daily_charts, - build_monthly_charts, - build_weekly_charts, - build_yearly_charts, - ) - - MEDIA_TYPES = [ - "artist", - "album", - "track", - "tv_series", - "video", - "podcast", - "board_game", - "trail", - "food", - "book", - ] - - try: - build_daily_charts(user, year, month, day, MEDIA_TYPES) - build_weekly_charts(user, year, week, MEDIA_TYPES) - build_monthly_charts(user, year, month, MEDIA_TYPES) - build_yearly_charts(user, year, MEDIA_TYPES) - logger.info( - f"[charts] Updated charts for {user} on {year}-{month:02d}-{day:02d}" - ) - except Exception as e: - logger.error(f"[charts] Failed to update charts: {e}")