Skip to content

Commit 1d0fea5

Browse files
fixed the api fetchChartDataWithFilters
1 parent 552fc0e commit 1d0fea5

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/api/api/api_routes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import json
33
import logging
4+
import math
45
import os
56
from fastapi import APIRouter, Request
67
from fastapi.responses import JSONResponse, StreamingResponse
@@ -74,6 +75,17 @@ async def fetch_chart_data_with_filters(chart_filters: ChartFilters):
7475
"FetchChartDataWithFiltersSuccess",
7576
{"status": "success", "filters": chart_filters.model_dump()}
7677
)
78+
# Sanitize the response to handle NaN and Infinity values
79+
def sanitize(obj):
80+
if isinstance(obj, dict):
81+
return {k: sanitize(v) for k, v in obj.items()}
82+
elif isinstance(obj, list):
83+
return [sanitize(v) for v in obj]
84+
elif isinstance(obj, float) and (math.isnan(obj) or math.isinf(obj)):
85+
return None
86+
else:
87+
return obj
88+
response = sanitize(response)
7789
return JSONResponse(content=response)
7890
except Exception as e:
7991
logger.exception("Error in fetch_chart_data_with_filters: %s", str(e))

src/api/common/database/sqldb_service.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,10 @@ async def fetch_chart_data(chart_filters: ChartFilters = ''):
235235
# charts pt1
236236
nested_json1 = (
237237
df.groupby(['id', 'chart_name', 'chart_type']).apply(
238-
lambda x: x[['name', 'value', 'unit_of_measurement']].to_dict(orient='records'), include_groups=False).reset_index(
239-
name='chart_value')
238+
lambda x: x[['name', 'value', 'unit_of_measurement']].to_dict(orient='records'), include_groups=False).reset_index()
240239
)
240+
nested_json1.columns = ['id', 'chart_name', 'chart_type', 'chart_value']
241241
result1 = nested_json1.to_dict(orient='records')
242-
243242
sql_stmt = f'''SELECT TOP 1 WITH TIES
244243
mined_topic as name, 'TOPICS' as id, 'Trending Topics' as chart_name, 'table' as chart_type,
245244
lower(sentiment) as average_sentiment,
@@ -258,12 +257,17 @@ async def fetch_chart_data(chart_filters: ChartFilters = ''):
258257
df = pd.DataFrame(rows, columns=column_names)
259258

260259
# charts pt2
261-
nested_json2 = (
262-
df.groupby(['id', 'chart_name', 'chart_type']).apply(
263-
lambda x: x[['name', 'call_frequency', 'average_sentiment']].to_dict(orient='records'), include_groups=False).reset_index(
264-
name='chart_value')
265-
)
266-
result2 = nested_json2.to_dict(orient='records')
260+
if not df.empty:
261+
nested_json2 = (
262+
df.groupby(['id', 'chart_name', 'chart_type']).apply(
263+
lambda x: x[['name', 'call_frequency', 'average_sentiment']].to_dict(orient='records'),
264+
include_groups=False
265+
).reset_index()
266+
)
267+
nested_json2.columns = ['id', 'chart_name', 'chart_type', 'chart_value']
268+
result2 = nested_json2.to_dict(orient='records')
269+
else:
270+
result2 = []
267271

268272
where_clause = where_clause.replace('mined_topic', 'topic')
269273
sql_stmt = f'''select top 15 key_phrase as text,
@@ -293,15 +297,17 @@ async def fetch_chart_data(chart_filters: ChartFilters = ''):
293297

294298
df = df.head(15)
295299

296-
nested_json3 = (
297-
df.groupby(['id', 'chart_name', 'chart_type']).apply(
298-
lambda x: x[['text', 'size', 'average_sentiment']].to_dict(orient='records'), include_groups=False).reset_index(
299-
name='chart_value')
300-
)
301-
result3 = nested_json3.to_dict(orient='records')
300+
if not df.empty:
301+
nested_json3 = (
302+
df.groupby(['id', 'chart_name', 'chart_type']).apply(
303+
lambda x: x[['text', 'size', 'average_sentiment']].to_dict(orient='records'), include_groups=False).reset_index()
304+
)
305+
nested_json3.columns = ['id', 'chart_name', 'chart_type', 'chart_value']
306+
result3 = nested_json3.to_dict(orient='records')
307+
else:
308+
result3 = []
302309

303310
final_result = result1 + result2 + result3
304-
305311
return final_result
306312

307313
finally:

0 commit comments

Comments
 (0)