[charts] Add periodic rebuilding of charts
This commit is contained in:
@ -1,13 +1,32 @@
|
||||
import logging
|
||||
|
||||
from celery import shared_task
|
||||
from charts.utils import build_charts_since
|
||||
from charts.utils import (
|
||||
build_charts_since,
|
||||
build_yearly_charts,
|
||||
build_monthly_charts,
|
||||
build_weekly_charts,
|
||||
)
|
||||
from django.apps import apps
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.utils import timezone
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
User = get_user_model()
|
||||
|
||||
MEDIA_TYPES = [
|
||||
"artist",
|
||||
"album",
|
||||
"track",
|
||||
"tv_series",
|
||||
"video",
|
||||
"podcast",
|
||||
"board_game",
|
||||
"trail",
|
||||
"food",
|
||||
"book",
|
||||
]
|
||||
|
||||
|
||||
@shared_task
|
||||
def process_retroarch_import(import_id):
|
||||
@ -67,3 +86,38 @@ def build_charts_for_user(user_id):
|
||||
return
|
||||
logger.info(f"Building charts for {user}")
|
||||
build_charts_since(user)
|
||||
|
||||
|
||||
@shared_task
|
||||
def rebuild_weekly_charts():
|
||||
"""Rebuild weekly charts for all users for the just-completed week."""
|
||||
now = timezone.now()
|
||||
year, week, _ = now.isocalendar()
|
||||
for user in User.objects.all():
|
||||
build_weekly_charts(user, year, week, MEDIA_TYPES)
|
||||
logger.info(f"Rebuilt weekly charts for week {week}, {year}")
|
||||
|
||||
|
||||
@shared_task
|
||||
def rebuild_monthly_charts():
|
||||
"""Rebuild monthly charts for all users for the just-completed month."""
|
||||
now = timezone.now()
|
||||
year, month = now.year, now.month
|
||||
if month == 1:
|
||||
month = 12
|
||||
year -= 1
|
||||
else:
|
||||
month -= 1
|
||||
for user in User.objects.all():
|
||||
build_monthly_charts(user, year, month, MEDIA_TYPES)
|
||||
logger.info(f"Rebuilt monthly charts for {month}/{year}")
|
||||
|
||||
|
||||
@shared_task
|
||||
def rebuild_yearly_charts():
|
||||
"""Rebuild yearly charts for all users for the just-completed year."""
|
||||
now = timezone.now()
|
||||
year = now.year - 1
|
||||
for user in User.objects.all():
|
||||
build_yearly_charts(user, year, MEDIA_TYPES)
|
||||
logger.info(f"Rebuilt yearly charts for {year}")
|
||||
|
||||
@ -121,10 +121,18 @@ CELERY_BEAT_SCHEDULE = {
|
||||
"task": "scrobbles.tasks.create_yesterdays_charts",
|
||||
"schedule": crontab(hour=0, minute=5),
|
||||
},
|
||||
"build-missing-charts-weekly": {
|
||||
"task": "scrobbles.tasks.build_missing_charts_for_all_users",
|
||||
"rebuild-weekly-charts": {
|
||||
"task": "scrobbles.tasks.rebuild_weekly_charts",
|
||||
"schedule": crontab(hour=1, minute=0, day_of_week=0),
|
||||
},
|
||||
"rebuild-monthly-charts": {
|
||||
"task": "scrobbles.tasks.rebuild_monthly_charts",
|
||||
"schedule": crontab(hour=0, minute=30, day_of_month=1),
|
||||
},
|
||||
"rebuild-yearly-charts": {
|
||||
"task": "scrobbles.tasks.rebuild_yearly_charts",
|
||||
"schedule": crontab(hour=0, minute=30, day_of_month=1, month_of_year=1),
|
||||
},
|
||||
}
|
||||
|
||||
INSTALLED_APPS = [
|
||||
|
||||
Reference in New Issue
Block a user