Compare commits

...

2 Commits

Author SHA1 Message Date
ab4b5470b7 [charts] Async chart updates on demand again
All checks were successful
build & deploy / test (push) Successful in 1m43s
build & deploy / deploy (push) Successful in 23s
2026-03-23 16:16:11 -04:00
18e7af5052 [agent] Remind agents to respect pyproject 2026-03-23 16:13:59 -04:00
3 changed files with 27 additions and 34 deletions

View File

@ -11,3 +11,6 @@ methods are contained in either instance methods on instatiated data models, or
classmethods on the Django model class itself. When logic grows too complex,
helper functions should be pulled out into utils.py files and the model instance
ro class method should call the utility function.
Be sure to check pyproject.toml for project defaults. Specifically for black and
isort expectations.

View File

@ -5,24 +5,11 @@ from django.dispatch import receiver
from django.utils import timezone
from scrobbles.models import Scrobble
from scrobbles.tasks import update_charts_for_timestamp
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 +33,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 +41,4 @@ 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}")
update_charts_for_timestamp.delay(user.id, year, month, day, week)

View File

@ -3,6 +3,7 @@ import logging
from celery import shared_task
from charts.utils import (
build_charts_since,
build_daily_charts,
build_yearly_charts,
build_monthly_charts,
build_weekly_charts,
@ -50,9 +51,8 @@ def process_lastfm_import(import_id):
@shared_task
def process_tsv_import(import_id):
AudioScrobblerTSVImport = apps.get_model(
"scrobbles", "AudioscrobblerTSVImport"
)
model_path = "scrobbles.AudioscrobblerTSVImport"
AudioScrobblerTSVImport = apps.get_model(model_path)
tsv_import = AudioScrobblerTSVImport.objects.filter(id=import_id).first()
if not tsv_import:
logger.warn(f"AudioScrobblerTSVImport not found with id {import_id}")
@ -121,3 +121,22 @@ def rebuild_yearly_charts():
for user in User.objects.all():
build_yearly_charts(user, year, MEDIA_TYPES)
logger.info(f"Rebuilt yearly charts for {year}")
@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
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)
date_str = f"{year}-{month:02d}-{day:02d}"
logger.info(f"[charts] Updated charts for {user} on {date_str}")
except Exception as e:
logger.error(f"[charts] Failed to update charts: {e}")