[charts] Async chart updates on demand again
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user