[charts] Async creating of new charts

This commit is contained in:
2026-03-23 12:11:05 -04:00
parent 881450eb8d
commit 44c6e014f5
3 changed files with 46 additions and 35 deletions

View File

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

View File

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

View File

@ -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}")