diff --git a/vrobbler/apps/charts/tests/test_utils.py b/vrobbler/apps/charts/tests/test_utils.py index 4bd0364..c134c39 100644 --- a/vrobbler/apps/charts/tests/test_utils.py +++ b/vrobbler/apps/charts/tests/test_utils.py @@ -17,7 +17,6 @@ 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") @@ -133,7 +132,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, Artist + from music.models import Track track1 = scrobble_data["track"] track2 = Track.objects.create( @@ -183,7 +182,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, Artist + from music.models import Track track1 = scrobble_data["track"] track2 = Track.objects.create( @@ -228,7 +227,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, Artist + from music.models import Track track1 = scrobble_data["track"] track2 = Track.objects.create( diff --git a/vrobbler/apps/scrobbles/signals.py b/vrobbler/apps/scrobbles/signals.py index 9abba06..eb41f3c 100644 --- a/vrobbler/apps/scrobbles/signals.py +++ b/vrobbler/apps/scrobbles/signals.py @@ -9,20 +9,6 @@ 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: @@ -46,13 +32,6 @@ 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) @@ -61,13 +40,6 @@ def _update_charts_for_timestamp(user, ts): week = ts.isocalendar()[1] day = ts.day - 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}") + from scrobbles.tasks import update_charts_for_timestamp + + update_charts_for_timestamp.delay(user.id, year, month, day, week) diff --git a/vrobbler/apps/scrobbles/tasks.py b/vrobbler/apps/scrobbles/tasks.py index 5d3139d..fd01ea2 100644 --- a/vrobbler/apps/scrobbles/tasks.py +++ b/vrobbler/apps/scrobbles/tasks.py @@ -67,3 +67,43 @@ 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}")