60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
import logging
|
|
|
|
from charts.utils import build_all_historical_charts, rebuild_all_charts
|
|
from django.core.management.base import BaseCommand
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Build missing charts for all users"
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
"--user-id",
|
|
type=int,
|
|
help="Build charts for a specific user only",
|
|
)
|
|
parser.add_argument(
|
|
"--full-rebuild",
|
|
action="store_true",
|
|
help="Delete existing chart records and rebuild from scratch",
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
from django.contrib.auth import get_user_model
|
|
|
|
User = get_user_model()
|
|
full_rebuild = options["full_rebuild"]
|
|
|
|
if full_rebuild:
|
|
self.stdout.write("Full rebuild requested - will delete existing records")
|
|
|
|
if options["user_id"]:
|
|
user = User.objects.filter(id=options["user_id"]).first()
|
|
if not user:
|
|
self.stderr.write(
|
|
self.style.ERROR(f"User with id {options['user_id']} not found")
|
|
)
|
|
return
|
|
users = [user]
|
|
action = "Rebuilding" if full_rebuild else "Building charts for"
|
|
self.stdout.write(f"{action} user: {user}")
|
|
else:
|
|
users = User.objects.all()
|
|
action = "Rebuilding" if full_rebuild else "Building charts for"
|
|
self.stdout.write(f"{action} charts for {users.count()} users...")
|
|
|
|
for user in users:
|
|
try:
|
|
logger.info(f"Building all historical charts for {user}")
|
|
if full_rebuild:
|
|
rebuild_all_charts(user)
|
|
else:
|
|
build_all_historical_charts(user)
|
|
self.stdout.write(self.style.SUCCESS(f" OK {user}"))
|
|
except Exception as e:
|
|
self.stderr.write(f" FAILED {user}: {e}")
|
|
|
|
self.stdout.write(self.style.SUCCESS("Done!"))
|