from django.core.management.base import BaseCommand from apps.farms.models import Cycle from apps.operations.services.kpi_rollups import refresh_kpi_source_fields class Command(BaseCommand): help = "Recompute KPI source fields and FCR/EEF for active cycles" def handle(self, *args, **options): count = 0 cycles = Cycle.objects.filter(status=Cycle.STATUS_ACTIVE) for cycle in cycles: written = refresh_kpi_source_fields(cycle) count += written self.stdout.write(f"cycle={cycle.pk} wrote {written} KPI row(s)") self.stdout.write(self.style.SUCCESS(f"Recomputed {count} KPI row(s)"))