feat: cached solution to sync current date query

master
adriano 2025-06-16 00:16:06 -03:00
parent b0fa541fbc
commit 8222eb4b50
4 changed files with 33 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import os
import hashlib
import hashlib
from bson import json_util
from flask_restx import Resource
from flask_jwt_extended import jwt_required
@ -15,7 +15,8 @@ from app.docs.usage_models import (
model_price_update,
model_prices_query_params,
transcription_data_query_params, usage_ns)
from app.utils.current_date import is_current_date
from app.utils.hash_key import set_hash_key
TMP_DIR = '/tmp'
@ -40,13 +41,15 @@ class TranscriptionExport(Resource):
validated = TranscriptionRequest(**data)
# Cria um nome de arquivo baseado nos parâmetros (evita caracteres inválidos)
hash_key = hashlib.md5(f"{validated.company_id}_{validated.start_date}_{validated.end_date}_{validated.who}".encode()).hexdigest()
filename = f"transcription_{hash_key}.xlsx"
# Cria um nome de arquivo baseado nos parâmetros (evita caracteres inválidos)
hash_key = set_hash_key(f"{validated.company_id}_{validated.start_date}_{validated.end_date}_{validated.who}")
filename = f"transcription_{hash_key}.xlsx"
filepath = os.path.join(TMP_DIR, filename)
ignore_cache = is_current_date(validated.start_date, validated.end_date)
# Verifica se o arquivo já existe
if not os.path.exists(filepath):
if not os.path.exists(filepath) or ignore_cache:
# Gera o relatório e salva no caminho desejado
service = TranscriptionReportService(
validated.company_id,
@ -72,7 +75,7 @@ class TranscriptionExport(Resource):
download_name=os.path.basename(filepath),
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
@usage_ns.route('/data/trascription')
class TranscriptionUsageData(Resource):

View File

@ -6,12 +6,13 @@ from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.styles import Font, PatternFill
import pandas as pd
import os
import hashlib
import os
from typing import List, Dict, Any, Optional
from app.utils.mysql_query import execute_query
from app.utils.calc_api_usage import calculate_api_usage
from app.services.redis_service import cache_del, cache_get, cache_set
from app.utils.current_date import is_current_date
from app.utils.hash_key import set_hash_key
import json
class TranscriptionReportService:
@ -311,11 +312,13 @@ class TranscriptionReportService:
def _reportDataTotalCost(self):
hash_key = hashlib.md5(f"{self.company_id}_{self.start_date}_{self.end_date}".encode()).hexdigest()
sum_key = f"sum_total_cost_{hash_key}"
if data := cache_get(f'report_model_usage:total_cost:{sum_key}'):
return json.loads(data)
sum_key = set_hash_key(f"{self.company_id}_{self.start_date}_{self.end_date}")
ignore_cache = is_current_date(self.start_date, self.end_date)
if not ignore_cache:
if data := cache_get(f'report_model_usage:total_cost:{sum_key}'):
return json.loads(data)
self._fetch_mongo_data(all_data=True)
mysql_data = self._fetch_mysql_data(hit_report=True)
@ -331,10 +334,10 @@ class TranscriptionReportService:
'total_client_cost': total_client_cost
}
cache_set(f'report_model_usage:total_cost:{sum_key}', json.dumps(sum_total_cost))
cache_set(f'report_model_usage:total_cost:{sum_key}', json.dumps(sum_total_cost), 86400)
return sum_total_cost
def reportDataXLSX(self, hit_report: Optional[bool] = False) -> str:
self._fetch_mongo_data(all_data=True)

View File

@ -0,0 +1,6 @@
from datetime import datetime
def is_current_date(start_date: str, end_date: str):
today_str = datetime.utcnow().date().isoformat()
ignore_cache = (start_date == today_str or end_date == today_str)
return ignore_cache

View File

@ -0,0 +1,5 @@
import hashlib
def set_hash_key(key: str) -> str:
hash_key = hashlib.md5(f"{key}".encode()).hexdigest()
return hash_key