HH/apps/analytics/tasks.py
2026-03-28 14:03:56 +03:00

35 lines
1.1 KiB
Python

import logging
from celery import shared_task
from django.utils import timezone
from apps.organizations.models import Hospital
from .kpi_models import KPIReportType
from .kpi_service import KPICalculationService
logger = logging.getLogger(__name__)
@shared_task(bind=True, ignore_result=True)
def calculate_daily_kpis(self):
today = timezone.now()
year = today.year
month = today.month
hospitals = Hospital.objects.filter(status="active")
report_types = [rt[0] for rt in KPIReportType.choices]
for hospital in hospitals:
for report_type in report_types:
try:
KPICalculationService.generate_monthly_report(
report_type=report_type,
hospital=hospital,
year=year,
month=month,
)
logger.info(f"Daily KPI calculated: {report_type} for {hospital.name} ({year}-{month:02d})")
except Exception as e:
logger.exception(f"Failed daily KPI: {report_type} for {hospital.name}: {e}")