From 64b990d2480d62d0dfbb52fe547f8fd0ae2af1af Mon Sep 17 00:00:00 2001 From: adriano Date: Mon, 23 Jun 2025 14:28:43 -0300 Subject: [PATCH] feat: implementation of new report collumns --- backend/app/docs/biling_models.py | 2 +- backend/app/routes/usage_routes.py | 2 +- backend/app/services/report_service.py | 55 +++++++++++++-------- frontend/components/transcription-table.tsx | 34 ++++++++++++- 4 files changed, 70 insertions(+), 23 deletions(-) diff --git a/backend/app/docs/biling_models.py b/backend/app/docs/biling_models.py index f7e4c9b..bf0153e 100644 --- a/backend/app/docs/biling_models.py +++ b/backend/app/docs/biling_models.py @@ -1,6 +1,6 @@ from flask_restx import fields, Namespace -billing_ns = Namespace('billing', description='Authentication') +billing_ns = Namespace('billing', description='Product price') product_model = billing_ns.model('Product', { 'name': fields.String(required=True, description='Name of the product'), diff --git a/backend/app/routes/usage_routes.py b/backend/app/routes/usage_routes.py index bef5b1f..4962540 100644 --- a/backend/app/routes/usage_routes.py +++ b/backend/app/routes/usage_routes.py @@ -48,7 +48,7 @@ class TranscriptionExport(Resource): ignore_cache = is_current_date(validated.start_date, validated.end_date) - # Verifica se o arquivo já existe + # Verifica se o arquivo já existe e se a consulta é referente a data atual if not os.path.exists(filepath) or ignore_cache: # Gera o relatório e salva no caminho desejado service = TranscriptionReportService( diff --git a/backend/app/services/report_service.py b/backend/app/services/report_service.py index 95c3890..37929d4 100644 --- a/backend/app/services/report_service.py +++ b/backend/app/services/report_service.py @@ -64,7 +64,8 @@ class TranscriptionReportService: "_id": { "sessionId": "$sessionId", "type": "$pricing.type", - "product": "$product" + "product": "$product", + "provider": "$pricing.provider" }, "usage": {"$sum": {"$toDouble": "$usage"}}, "totalCost": {"$sum": {"$toDouble": "$total_cost"}}, @@ -84,21 +85,15 @@ class TranscriptionReportService: "usageByType": { "$push": { "k": "$_id.type", - "v": "$usage" + "v": { + "product": "$_id.product", + "type": "$_id.type", + "provider": "$_id.provider", + "usage": "$usage", + "usageCost": "$totalCost" + } } - }, - "costByType": { - "$push": { - "k": "$_id.type", - "v": "$totalCost" - } - }, - "usageByProduct": { - "$push": { - "k": "$_id.product", - "v": "$usage" - } - } + } } } @@ -108,9 +103,7 @@ class TranscriptionReportService: "firstCreatedAt": 1, "callerIds": 1, "totalCost": 1, - "usageByType": {"$arrayToObject": "$usageByType"}, - "costByType": {"$arrayToObject": "$costByType"}, - "usageByProduct": {"$arrayToObject": "$usageByProduct"} + "usageByType": {"$arrayToObject": "$usageByType"} } } @@ -138,6 +131,7 @@ class TranscriptionReportService: self.unique_ids = [doc["_id"] for doc in self.mongo_results] # print("=====> self.mongo_results: ", self.mongo_results) + # exit(1) # Pipeline para contagem total count_pipeline = [ @@ -190,8 +184,21 @@ class TranscriptionReportService: if rowMongo := next((m for m in self.mongo_results if m["_id"] == row["uniqueid"] ), None): row["custo_hit"] = f"{float(rowMongo["totalCost"])}" - row["qtd_token_input"] = rowMongo.get('usageByType', {}).get('input', 0) - row["qtd_token_output"] = rowMongo.get('usageByType', {}).get('output', 0) + + token_output = rowMongo.get('usageByType', {}).get('output', {}) + token_input = rowMongo.get('usageByType', {}).get('input',{}) + + row["qtd_token_input"] = token_input.get('usage', 0) + row["qtd_token_output"] = token_output.get('usage', 0) + row["total_cost_token"] = float(token_input.get('usageCost',0) + token_output.get('usageCost', 0)) + row["llm_provider"] = token_output.get('provider','unknown') + + tts = rowMongo.get('usageByType', {}).get('stt',{}) + row["tts_model"] = tts.get('product', 'unknown') + row["tts_provider"] = tts.get('provider', 'unknown') + row["tts_cost"] = tts.get('usageCost', 0) + row["tts_usage"] = tts.get('usage', 0) + row["total_min"] = f"{(int(row['total_billsec']) / 60):.2f}" self.client_price_row(products, row) @@ -259,6 +266,14 @@ class TranscriptionReportService: "custo_hit": "Custo HIT", "qtd_token_input": "Quantidade de tokens(input)", "qtd_token_output": "Quantidade de tokens(output)", + "total_cost_token": "Preço Final LLM", + "llm_provider": "Provider LLM", + + "tts_model": "TTS", + "tts_provider": "Provider TTS", + "tts_cost": "Preço Final TTS", + "tts_usage": "Segundos Transcritos", + "client_total_cost": "Custo Cliente", "client_price": "Preço Cliente por Minuto", "start_call": "Inicio", diff --git a/frontend/components/transcription-table.tsx b/frontend/components/transcription-table.tsx index 9f3354b..c28bbcb 100644 --- a/frontend/components/transcription-table.tsx +++ b/frontend/components/transcription-table.tsx @@ -56,7 +56,15 @@ interface HitTranscriptionData { total_billsec: number // Quantidade de segundos qtd_token_input: number // Quantidade de tokens(input) qtd_token_output: number // Quantidade de tokens(output) - total_min: number, + total_cost_token: number // Total token(input + output) + llm_provider: string // Provider llm + + // tts_model: string // Modelo de trascrição de audio para texto + tts_provider: string // Provedor do tts + tts_cost: number // Custo total dos minutos do audio em texto + tts_usage: number // Tempo total de trascrição do audio em segundos + + total_min: number, // Minutos de ligação custo_hit: string // Custo HIT client_total_cost: string // Custo Cliente client_price: string // Preço Cliente por Minuto @@ -471,6 +479,12 @@ export default function TranscriptionTable() { Fim Tokens (Input) Tokens (Output) + Preço Final LLM ($) + Provider LLM + {/* Model TTS */} + Provider TTS + Preço Final TTS ($) + Segundos Transcritos ) : ( <> @@ -517,6 +531,24 @@ export default function TranscriptionTable() { {formatDateTime((item as HitTranscriptionData).end_call || "-")} {(item as HitTranscriptionData).qtd_token_input || "-"} {(item as HitTranscriptionData).qtd_token_output || "-"} + + + {(item as HitTranscriptionData)?.total_cost_token + ? `$ ${Number((item as HitTranscriptionData).total_cost_token).toFixed(7)}` + : "-"} + + + {(item as HitTranscriptionData).llm_provider || "-"} + {/* {(item as HitTranscriptionData).tts_model || "-"} */} + {(item as HitTranscriptionData).tts_provider || "-"} + + + {(item as HitTranscriptionData)?.tts_cost + ? `$ ${Number((item as HitTranscriptionData).tts_cost).toFixed(2)}` + : "-"} + + + {(item as HitTranscriptionData).tts_usage || "-"} ) : ( <>