Fix: Add Decimal JSON serialization handler for analytics charts

parent b4bc6fb6
......@@ -23,6 +23,7 @@ Why did the programmer quit his job? Because he didn't get arrays!
Token Usage Analytics module for AISBF.
"""
import json
from decimal import Decimal
import csv
import io
from typing import Dict, List, Optional, Any
......@@ -1113,7 +1114,13 @@ class Analytics:
'recommendations': self.get_optimization_recommendations()
}
return json.dumps(data, indent=2)
# Handle Decimal values from MySQL for JSON serialization
def decimal_default(obj):
if isinstance(obj, Decimal):
return float(obj)
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
return json.dumps(data, indent=2, default=decimal_default)
def export_to_csv(
self,
......
from decimal import Decimal
"""
Copyleft (C) 2026 Stefy Lanza <stefy@nexlab.net>
......@@ -2228,7 +2229,12 @@ async def dashboard_analytics(
"session": request.session,
"is_admin": is_admin,
"provider_stats": provider_stats,
"token_over_time": json.dumps(token_over_time),
# Handle Decimal values from MySQL for JSON serialization
def decimal_default(obj):
if isinstance(obj, Decimal):
return int(obj)
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
"token_over_time": json.dumps(token_over_time, default=decimal_default),
"model_performance": model_performance,
"cost_overview": cost_overview,
"recommendations": recommendations,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment