from django.core.management.base import BaseCommand from core.models import AppUser, Library, Channel, MediaItem, Airing, ScheduleTemplate from django.utils import timezone from datetime import timedelta class Command(BaseCommand): help = "Displays a beautifully formatted terminal dashboard of the current backend state." def get_color(self, text, code): """Helper to wrap string in bash color codes""" return f"\033[{code}m{text}\033[0m" def handle(self, *args, **options): # 1. Gather Aggregate Metrics total_users = AppUser.objects.count() total_libraries = Library.objects.count() total_channels = Channel.objects.count() total_media = MediaItem.objects.count() total_templates = ScheduleTemplate.objects.count() # Gather near-term Airing metrics now = timezone.now() tomorrow = now + timedelta(days=1) airings_today = Airing.objects.filter(starts_at__gte=now, starts_at__lt=tomorrow).count() airings_total = Airing.objects.count() self.stdout.write(self.get_color("\n=== PYTV Backend State Dashboard ===", "1;34")) self.stdout.write(f"\n{self.get_color('Core Metrics:', '1;36')}") self.stdout.write(f" Users: {self.get_color(str(total_users), '1;32')}") self.stdout.write(f" Libraries: {self.get_color(str(total_libraries), '1;32')}") self.stdout.write(f" Media Items: {self.get_color(str(total_media), '1;32')}") self.stdout.write(f" Channels: {self.get_color(str(total_channels), '1;32')}") self.stdout.write(f"\n{self.get_color('Scheduling Engine:', '1;36')}") self.stdout.write(f" Active Templates: {self.get_color(str(total_templates), '1;33')}") self.stdout.write(f" Airings (Next 24h): {self.get_color(str(airings_today), '1;33')}") self.stdout.write(f" Airings (Total): {self.get_color(str(airings_total), '1;33')}") # 2. Build Tree View of Channels self.stdout.write(f"\n{self.get_color('Channel Tree:', '1;36')}") channels = Channel.objects.prefetch_related('scheduletemplate_set').all() if not channels: self.stdout.write(" (No channels configured)") for c in channels: status_color = "1;32" if c.is_active else "1;31" status_text = "ACTIVE" if c.is_active else "INACTIVE" self.stdout.write(f"\n 📺 [{c.channel_number or '-'}] {c.name} ({self.get_color(status_text, status_color)})") # Show templates templates = c.scheduletemplate_set.filter(is_active=True).order_by('-priority') if not templates: self.stdout.write(" ⚠️ No active schedule templates.") else: for t in templates: blocks_count = t.scheduleblock_set.count() self.stdout.write(f" 📄 Template: {t.name} (Priority {t.priority}) -> {blocks_count} Blocks") self.stdout.write("\n")