HH/COMMAND_CENTER_FIX_SUMMARY.md

4.0 KiB

Command Center Error Fix

Problem

When visiting the Command Center page, users encountered a 500 Internal Server Error with the following traceback:

django.core.exceptions.FieldError: Unsupported lookup 'survey_instance' for UUIDField or join on the field not permitted.

The error occurred in apps/analytics/services/analytics_service.py at line 530 in the _get_department_performance method.

Root Cause

The issue was an incorrect Django ORM annotation path in the _get_department_performance method. The code was trying to access survey data through an invalid relationship path:

# INCORRECT (caused the error)
departments = queryset.annotate(
    avg_survey_score=Avg('journey_stages__journey_instance__surveys__total_score'),
    survey_count=Count('journey_stages__journey_instance__surveys')
)

This path was incorrect because:

  1. SurveyInstance links to PatientJourneyInstance (not to PatientJourneyStageInstance)
  2. PatientJourneyInstance has a direct department field
  3. The path 'journey_stages__journey_instance__surveys' doesn't exist in the model relationships

Solution

Fixed the annotation path to use the correct relationship:

# CORRECT (fixed)
departments = queryset.annotate(
    avg_survey_score=Avg(
        'journey_instances__surveys__total_score',
        filter=Q(journey_instances__surveys__status='completed',
                journey_instances__surveys__completed_at__gte=start_date,
                journey_instances__surveys__completed_at__lte=end_date)
    ),
    survey_count=Count(
        'journey_instances__surveys',
        filter=Q(journey_instances__surveys__status='completed',
                journey_instances__surveys__completed_at__gte=start_date,
                journey_instances__surveys__completed_at__lte=end_date)
    )
).filter(survey_count__gt=0).order_by('-avg_survey_score')[:10]

Key Changes:

  1. Changed path from 'journey_stages__journey_instance__surveys' to 'journey_instances__surveys'
  2. Added proper filters to only count completed surveys within the date range
  3. This matches the actual model relationships:
    • DepartmentPatientJourneyInstance (via journey_instances related name)
    • PatientJourneyInstanceSurveyInstance (via surveys related name)

Model Relationships

# From SurveyInstance model
journey_instance = models.ForeignKey(
    'journeys.PatientJourneyInstance',
    on_delete=models.CASCADE,
    null=True,
    blank=True,
    related_name='surveys'
)

# From PatientJourneyInstance model
department = models.ForeignKey(
    'organizations.Department',
    on_delete=models.SET_NULL,
    null=True,
    blank=True,
    related_name='journey_instances'
)

Testing

Created and ran test script (test_command_center_fix.py) to verify the fix:

============================================================
Testing Command Center Fix
============================================================
Testing department performance chart...
Using user: admin_hh (Is admin: False)
✓ SUCCESS: Got department performance data
  Chart type: bar
  Number of departments: 0
  Labels: []
  Series data: [{'name': 'Average Score', 'data': []}]

Testing get_chart_data method...
✓ SUCCESS: Got chart data via get_chart_data
  Chart type: bar
  Number of departments: 0

============================================================
✓ ALL TESTS PASSED!
============================================================

Files Modified

  • apps/analytics/services/analytics_service.py - Fixed the _get_department_performance method (line 525-544)

Additional Actions

  • Cleared Django cache to ensure the fix takes effect immediately
  • Created test script to verify the fix works correctly

Result

The Command Center page should now load without errors. The department performance chart will display data for departments that have completed surveys within the specified date range. If no departments have completed surveys in the date range, the chart will show empty data (which is expected behavior).