Revert "[charts] Async creating of new charts"

This reverts commit 44c6e014f5.
This commit is contained in:
2026-03-23 12:35:52 -04:00
parent 44c6e014f5
commit e36a0726c8
3 changed files with 35 additions and 46 deletions

View File

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

View File

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

View File

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