#!/usr/bin/env python """ Test the ApexCharts fix by checking if charts can render without errors. This script verifies the template has the necessary fixes. """ import os import re def check_template_has_fixes(): """Check if the admin_evaluation template has the ApexCharts fixes""" print("=" * 60) print("CHECKING APEXCHARTS FIXES IN TEMPLATE") print("=" * 60) template_path = 'templates/dashboard/admin_evaluation.html' with open(template_path, 'r') as f: content = f.read() checks = { 'sanitizeSeriesData function': False, 'getSafeNumber function': False, 'hasValidData function': False, 'Enhanced sanitization (null, undefined, empty string)': False, 'Safe number extraction in initComplaintCharts': False, 'Safe number extraction in initInquiryCharts': False, 'Safe number extraction in resolution rate': False, 'Try-catch blocks for chart rendering': False, } # Check for sanitizeSeriesData function if 'function sanitizeSeriesData(series)' in content: checks['sanitizeSeriesData function'] = True print("\n✓ sanitizeSeriesData function found") # Check for enhanced sanitization if 'value === null || value === undefined || value === \'\' ||' in content: checks['Enhanced sanitization (null, undefined, empty string)'] = True print(" ✓ Enhanced sanitization handles null, undefined, empty strings") # Check for getSafeNumber function if 'function getSafeNumber(obj, path, defaultValue' in content: checks['getSafeNumber function'] = True print("\n✓ getSafeNumber function found") # Check for hasValidData function if 'function hasValidData(series)' in content: checks['hasValidData function'] = True print("✓ hasValidData function found") # Check for safe number extraction in initComplaintCharts if 'initComplaintCharts' in content and 'getSafeNumber(complaints,' in content: checks['Safe number extraction in initComplaintCharts'] = True print("\n✓ initComplaintCharts uses getSafeNumber") # Check for safe number extraction in initInquiryCharts if 'initInquiryCharts' in content and 'getSafeNumber(inquiries,' in content: checks['Safe number extraction in initInquiryCharts'] = True print("✓ initInquiryCharts uses getSafeNumber") # Check for safe number extraction in resolution rate if 'totalComplaints += getSafeNumber(complaints,' in content: checks['Safe number extraction in resolution rate'] = True print("✓ Resolution rate calculation uses getSafeNumber") # Check for try-catch blocks if content.count('try {') >= 6: # Should have at least 6 try-catch blocks (one per chart) checks['Try-catch blocks for chart rendering'] = True print("✓ Try-catch blocks present for error handling") print("\n" + "=" * 60) print("FIXES SUMMARY") print("=" * 60) all_passed = all(checks.values()) for check, passed in checks.items(): status = "✓ PASS" if passed else "✗ FAIL" print(f"{status}: {check}") print("\n" + "=" * 60) if all_passed: print("✅ ALL FIXES APPLIED SUCCESSFULLY!") print("\nThe template now includes:") print(" • Enhanced data sanitization that handles all invalid values") print(" • Safe number extraction from nested objects") print(" • Validation before rendering charts") print(" • Try-catch error handling for each chart") print(" • Proper default values for all aggregations") print("\nThis should fix the 't.put is not a function' error.") print("\nNext steps:") print(" 1. Start the development server: python manage.py runserver") print(" 2. Visit: http://127.0.0.1:8000/dashboard/admin-evaluation/") print(" 3. Check the browser console - charts should render without errors") return True else: print("❌ SOME FIXES MISSING") failed = [k for k, v in checks.items() if not v] print(f"\nMissing fixes: {', '.join(failed)}") return False def print_manual_testing_instructions(): """Print instructions for manual browser testing""" print("\n" + "=" * 60) print("MANUAL TESTING INSTRUCTIONS") print("=" * 60) print("\n1. Start the development server:") print(" python manage.py runserver") print("\n2. Open the admin evaluation page:") print(" http://127.0.0.1:8000/dashboard/admin-evaluation/") print("\n3. Log in as an admin user (if required)") print("\n4. Check the browser console (F12):") print(" • Look for 't.put is not a function' errors") print(" • Should see NO errors from ApexCharts") print(" • May see warnings about invalid values being replaced with 0") print("\n5. Verify charts are visible:") print(" • Complaint Source Breakdown (donut chart)") print(" • Complaint Status Distribution (bar chart)") print(" • Complaint Activation Time (bar chart)") print(" • Complaint Response Time (bar chart)") print(" • Inquiry Status Distribution (bar chart)") print(" • Inquiry Response Time (bar chart)") print("\n6. Check summary cards:") print(" • Total Staff") print(" • Total Complaints") print(" • Total Inquiries") print(" • Resolution Rate") print("\n7. Test filters:") print(" • Change date range") print(" • Select hospital/department (if available)") print(" • Charts should update without errors") print("\n" + "=" * 60) if __name__ == '__main__': print("\n" + "🔧" * 30) print("APEXCHARTS FIX VERIFICATION") print("🔧" * 30 + "\n") success = check_template_has_fixes() print_manual_testing_instructions() if success: print("\n✅ Template is ready for testing!") print("Please test in a browser to confirm the fix works.") else: print("\n⚠️ Some fixes are missing. Please review the template.")