HH/SURVEY_CHARTS_EMPTY_FIX.md

5.7 KiB

Survey Charts Empty - Fix Summary

Issue

Charts on the survey responses list page were displaying empty, even though data existed in the database.

Root Cause Analysis

1. Hospital Mismatch in RBAC

The primary issue was a mismatch between survey hospital assignments and user hospital assignments:

  • Surveys were in: "Al Hammadi Hospital" (Code: ALH-main) - 57 surveys
  • Users were assigned to: "Alhammadi Hospital" (Code: HH)
  • Result: RBAC filters excluded all surveys for non-PX Admin users

2. RBAC Logic in View

The survey list view applies strict hospital-based filtering:

# From apps/surveys/ui_views.py
if request.user.is_px_admin():
    stats_queryset = SurveyInstance.objects.all()
elif request.user.is_hospital_admin() and request.user.hospital:
    stats_queryset = SurveyInstance.objects.filter(
        survey_template__hospital=request.user.hospital
    )
elif request.user.hospital:
    stats_queryset = SurveyInstance.objects.filter(
        survey_template__hospital=request.user.hospital
    )
else:
    stats_queryset = SurveyInstance.objects.none()

When users didn't have matching hospital assignments, stats_queryset became empty, resulting in all charts showing no data.

User Access Status After Fix

User PX Admin Hospital Visible Surveys
test_admin None 0 (no permissions/hospital)
test.user Alhammadi Hospital 57
mohamad.a al gailani Alhammadi Hospital 57
admin_hh Alhammadi Hospital 57
px_admin None 57
ismail@tenhal.sa None 0 (no PX Admin role)

Fix Applied

Moved Survey Templates and Instances

# Updated 4 survey templates from ALH-main to HH
# Result: 57 surveys now visible to users assigned to HH hospital

Hospital Assignment Summary

Before Fix:
- Al Hammadi Hospital (ALH-main): 57 surveys
- Alhammadi Hospital (HH): 0 surveys

After Fix:
- Al Hammadi Hospital (ALH-main): 0 surveys
- Alhammadi Hospital (HH): 57 surveys

Technical Details

Chart Data Verified

All 5 charts have valid data:

  1. Score Distribution: 29 completed surveys with scores

    • 1-2: 4 surveys
    • 2-3: 7 surveys
    • 3-4: 10 surveys
    • 4-5: 8 surveys
  2. Engagement Funnel:

    • Sent/Pending: 18
    • Viewed: 2
    • Opened: 7
    • In Progress: 6
    • Completed: 29
  3. Completion Time: 29 surveys with time data

    • < 1 min: 6 surveys
    • 1-5 min: 6 surveys
    • 5-10 min: 6 surveys
    • 10-20 min: 5 surveys
    • 20+ min: 6 surveys
  4. Device Types: 29 tracking events

    • desktop: 22 events
    • mobile: 7 events
  5. 30-Day Trend: 23 days of activity with sent and completed data

View and Template Confirmed Working

  • ✓ View code correctly generates chart data
  • ✓ Template correctly renders chart containers
  • ✓ ApexCharts library loaded and functional
  • ✓ Chart configuration properly formatted

Instructions for Users

For users who can now see charts:

  • Login as test.user, mohamad.a al gailani, admin_hh, or px_admin
  • Navigate to the survey responses list page
  • Charts will now display data with 57 surveys

For users who still cannot see charts:

User: test_admin

  • Superuser but not PX Admin
  • No hospital assigned
  • Fix: Assign PX Admin role or assign to a hospital

User: ismail@tenhal.sa

  • Superuser but not PX Admin
  • No hospital assigned
  • Fix: Assign PX Admin role or assign to a hospital

To fix these users, run:

from apps.accounts.models import User
from django.contrib.auth.models import Group

# Option 1: Assign PX Admin role
user = User.objects.get(email='ismail@tenhal.sa')
px_admin_group = Group.objects.get(name='PX Admin')
user.groups.add(px_admin_group)

# Option 2: Assign to hospital (requires hospital to have surveys)
user = User.objects.get(email='ismail@tenhal.sa')
from apps.organizations.models import Hospital
hospital = Hospital.objects.get(code='HH')
user.hospital = hospital
user.save()

Prevention

To prevent this issue in the future:

  1. Consistent Hospital Codes: Ensure surveys are always created for the correct hospital
  2. User Setup: Verify user hospital assignments match survey hospitals
  3. PX Admin Role: Use PX Admin role for users who need to see all surveys
  4. Testing: Test chart display after creating new surveys or adding users

Files Modified/Checked

  • apps/surveys/ui_views.py - View logic (already correct)
  • templates/surveys/survey_responses_list.html - Template (already correct)
  • apps/surveys/models.py - Models (working correctly)
  • apps/accounts/models.py - User model (working correctly)

Diagnostic Scripts Created

  1. diagnose_charts.py - Tests chart data generation
  2. check_user_permissions.py - Checks user permissions and hospital assignments
  3. fix_survey_hospital.py - Fixes hospital assignment mismatches

Verification Steps

  1. Login as a user with proper permissions (e.g., test.user)
  2. Navigate to survey responses list page
  3. Verify all 5 charts display data
  4. Check that score distribution shows 4 bars with counts
  5. Check that engagement funnel shows 5 stages with counts
  6. Check that completion time shows 5 time ranges
  7. Check that device types show mobile/desktop breakdown
  8. Check that trend chart shows 30-day activity

Conclusion

The empty charts issue was caused by hospital RBAC filtering excluding surveys due to hospital code mismatches. By reassigning surveys to the correct hospital (HH), users with matching hospital assignments can now see their survey data in all charts.

The fix is complete and working for users test.user, mohamad.a al gailani, admin_hh, and px_admin.