""" Analytics admin """ from django.contrib import admin from django.utils.html import format_html from .models import KPI, KPIValue @admin.register(KPI) class KPIAdmin(admin.ModelAdmin): """KPI admin""" list_display = [ 'name', 'category', 'unit', 'target_value', 'warning_threshold', 'critical_threshold', 'is_active' ] list_filter = ['category', 'is_active'] search_fields = ['name', 'name_ar', 'description'] ordering = ['category', 'name'] fieldsets = ( (None, { 'fields': ('name', 'name_ar', 'description', 'category') }), ('Measurement', { 'fields': ('unit', 'calculation_method') }), ('Thresholds', { 'fields': ('target_value', 'warning_threshold', 'critical_threshold') }), ('Configuration', { 'fields': ('is_active',) }), ('Metadata', { 'fields': ('created_at', 'updated_at') }), ) readonly_fields = ['created_at', 'updated_at'] @admin.register(KPIValue) class KPIValueAdmin(admin.ModelAdmin): """KPI value admin""" list_display = [ 'kpi', 'hospital', 'department', 'value', 'status_badge', 'period_type', 'period_end' ] list_filter = ['status', 'period_type', 'kpi__category', 'hospital', 'period_end'] search_fields = ['kpi__name'] ordering = ['-period_end'] date_hierarchy = 'period_end' fieldsets = ( ('KPI', { 'fields': ('kpi',) }), ('Scope', { 'fields': ('hospital', 'department') }), ('Value', { 'fields': ('value', 'status') }), ('Period', { 'fields': ('period_type', 'period_start', 'period_end') }), ('Metadata', { 'fields': ('metadata', 'created_at', 'updated_at'), 'classes': ('collapse',) }), ) readonly_fields = ['created_at', 'updated_at'] def get_queryset(self, request): qs = super().get_queryset(request) return qs.select_related('kpi', 'hospital', 'department') def status_badge(self, obj): """Display status with badge""" colors = { 'on_target': 'success', 'warning': 'warning', 'critical': 'danger', } color = colors.get(obj.status, 'secondary') return format_html( '{}', color, obj.get_status_display() ) status_badge.short_description = 'Status'