From 3cc53ad3eed612cee6b6c196cc1a43a74c7bb47a Mon Sep 17 00:00:00 2001 From: adriano Date: Mon, 1 Sep 2025 16:33:53 -0300 Subject: [PATCH] fix: implemented solution to not found dadafor report --- backend/app/errors/handlers.py | 3 +-- backend/app/routes/usage_routes.py | 5 ++++- backend/app/services/report_service.py | 27 +++++++++++++++++--------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/backend/app/errors/handlers.py b/backend/app/errors/handlers.py index 114988c..f06c445 100644 --- a/backend/app/errors/handlers.py +++ b/backend/app/errors/handlers.py @@ -1,8 +1,7 @@ import traceback from flask import jsonify from werkzeug.exceptions import HTTPException -from pydantic import ValidationError -import traceback +from pydantic import ValidationError from bson.errors import InvalidId diff --git a/backend/app/routes/usage_routes.py b/backend/app/routes/usage_routes.py index 36fde75..6c007d7 100644 --- a/backend/app/routes/usage_routes.py +++ b/backend/app/routes/usage_routes.py @@ -62,9 +62,12 @@ class TranscriptionExport(Resource): generated_path = service.reportDataXLSX(hit_report=True) else: generated_path = service.reportDataXLSX() + + if not generated_path: + return {"success": False, "message": "No data found"}, 404 if not os.path.exists(generated_path): - return {"error": "File generation failed"}, 500 + return {"success": False, "message": "File generation failed"}, 500 # Move o arquivo gerado para o TMP os.rename(generated_path, filepath) diff --git a/backend/app/services/report_service.py b/backend/app/services/report_service.py index 37929d4..8c22f9a 100644 --- a/backend/app/services/report_service.py +++ b/backend/app/services/report_service.py @@ -128,10 +128,7 @@ class TranscriptionReportService: # Executa agregação principal self.mongo_results = list(collection.aggregate(pipeline)) - self.unique_ids = [doc["_id"] for doc in self.mongo_results] - - # print("=====> self.mongo_results: ", self.mongo_results) - # exit(1) + self.unique_ids = [doc["_id"] for doc in self.mongo_results] # Pipeline para contagem total count_pipeline = [ @@ -143,7 +140,7 @@ class TranscriptionReportService: {"$count": "total"} ] count_result = list(collection.aggregate(count_pipeline)) - total = count_result[0]["total"] if count_result else 0 + total = count_result[0]["total"] if count_result else 0 return { "total": total, @@ -155,9 +152,9 @@ class TranscriptionReportService: def _fetch_mysql_data(self, hit_report: Optional[bool] = False)-> List[Dict[str, Any]]: - collection = self.mongo_client["billing-api"]["api_products"] + collection = self.mongo_client["billing-api"]["api_products"] - products = list(collection.find({})) + products = list(collection.find({})) sql = f"""SELECT uniqueid, @@ -176,7 +173,7 @@ class TranscriptionReportService: uniqueid IN {tuple(self.unique_ids)} GROUP BY uniqueid, src, dst;""" - rows = execute_query(self.company_id, sql) + rows = execute_query(self.company_id, sql) if hit_report: for row in rows: @@ -355,7 +352,10 @@ class TranscriptionReportService: def reportDataXLSX(self, hit_report: Optional[bool] = False) -> str: - self._fetch_mongo_data(all_data=True) + mongo_data = self._fetch_mongo_data(all_data=True) + + if mongo_data["total"] == 0: + return if hit_report: mysql_data = self._fetch_mysql_data(hit_report=True) @@ -367,6 +367,15 @@ class TranscriptionReportService: def reportData(self, page: int = 1, page_size: int = 20, hit_report: Optional[bool] = False) -> Dict[str, Any]: mongo_data = self._fetch_mongo_data(page=page, page_size=page_size) + + if mongo_data["total"] == 0: + return { + "pagination": mongo_data, + "data": [], + "cost": { + + } + } if hit_report: mysql_data = self._fetch_mysql_data(hit_report=True)