fix: implemented solution to not found dadafor report

master
adriano 2025-09-01 16:33:53 -03:00
parent 07e897cd47
commit 3cc53ad3ee
3 changed files with 23 additions and 12 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)