diff --git a/app/Http/Livewire/Chat/PublicChat.php b/app/Http/Livewire/Chat/PublicChat.php index 1a90d67..07af6dd 100755 --- a/app/Http/Livewire/Chat/PublicChat.php +++ b/app/Http/Livewire/Chat/PublicChat.php @@ -906,7 +906,6 @@ class PublicChat extends Component $promos = Promociones::where('visible', 'true') ->where(fn ($q) => $q->whereNull('fecha_limite')->orWhere('fecha_limite', '>=', now())) - ->whereHas('tarifaPromo', fn ($q) => $q->where('rol_id', $rolId)) ->with(['tarifaPromo' => fn ($q) => $q->where('rol_id', $rolId)]) ->get(); diff --git a/app/Http/Livewire/Chat/ShowAiLogs.php b/app/Http/Livewire/Chat/ShowAiLogs.php old mode 100644 new mode 100755 index 7d75987..a57b7ac --- a/app/Http/Livewire/Chat/ShowAiLogs.php +++ b/app/Http/Livewire/Chat/ShowAiLogs.php @@ -3,6 +3,7 @@ namespace App\Http\Livewire\Chat; use App\Models\AiUsageLog; +use Carbon\Carbon; use Livewire\Component; use Livewire\WithPagination; @@ -10,43 +11,72 @@ class ShowAiLogs extends Component { use WithPagination; - public string $filtroServicio = ''; - public string $filtroCanal = ''; + public string $filtroServicio = ''; + public string $filtroCanal = ''; public string $filtroFechaDesde = ''; public string $filtroFechaHasta = ''; + public string $filtroMes = ''; - protected $queryString = ['filtroServicio', 'filtroCanal', 'filtroFechaDesde', 'filtroFechaHasta']; + protected $queryString = [ + 'filtroServicio', 'filtroCanal', + 'filtroFechaDesde', 'filtroFechaHasta', + 'filtroMes', + ]; - public function updatingFiltroServicio(): void { $this->resetPage(); } - public function updatingFiltroCanal(): void { $this->resetPage(); } + public function updatingFiltroServicio(): void { $this->resetPage(); } + public function updatingFiltroCanal(): void { $this->resetPage(); } public function updatingFiltroFechaDesde(): void { $this->resetPage(); } public function updatingFiltroFechaHasta(): void { $this->resetPage(); } + public function updatingFiltroMes(): void { $this->resetPage(); } - public function render() + public function updatedFiltroMes(): void { - $query = AiUsageLog::query()->latest(); + if ($this->filtroMes) { + $this->filtroFechaDesde = ''; + $this->filtroFechaHasta = ''; + } + } + public function updatedFiltroFechaDesde(): void + { + if ($this->filtroFechaDesde) $this->filtroMes = ''; + } + + public function updatedFiltroFechaHasta(): void + { + if ($this->filtroFechaHasta) $this->filtroMes = ''; + } + + private function applyFiltros($query): void + { if ($this->filtroServicio) { $query->where('servicio', $this->filtroServicio); } if ($this->filtroCanal) { $query->where('canal', $this->filtroCanal); } - if ($this->filtroFechaDesde) { - $query->whereDate('created_at', '>=', $this->filtroFechaDesde); - } - if ($this->filtroFechaHasta) { - $query->whereDate('created_at', '<=', $this->filtroFechaHasta); + if ($this->filtroMes) { + $inicio = Carbon::createFromFormat('Y-m', $this->filtroMes)->startOfMonth(); + $fin = $inicio->copy()->endOfMonth(); + $query->whereBetween('created_at', [$inicio, $fin]); + } else { + if ($this->filtroFechaDesde) { + $query->whereDate('created_at', '>=', $this->filtroFechaDesde); + } + if ($this->filtroFechaHasta) { + $query->whereDate('created_at', '<=', $this->filtroFechaHasta); + } } + } + public function render() + { + $query = AiUsageLog::query()->latest(); + $this->applyFiltros($query); $logs = $query->paginate(30); - // Resumen total (aplicando mismos filtros sin paginación) $resumenQuery = AiUsageLog::query(); - if ($this->filtroServicio) $resumenQuery->where('servicio', $this->filtroServicio); - if ($this->filtroCanal) $resumenQuery->where('canal', $this->filtroCanal); - if ($this->filtroFechaDesde) $resumenQuery->whereDate('created_at', '>=', $this->filtroFechaDesde); - if ($this->filtroFechaHasta) $resumenQuery->whereDate('created_at', '<=', $this->filtroFechaHasta); + $this->applyFiltros($resumenQuery); $resumen = $resumenQuery->selectRaw(" COUNT(*) as total_llamadas, @@ -56,9 +86,18 @@ class ShowAiLogs extends Component SUM(output_tokens) as total_output_tokens, SUM(audio_segundos) as total_segundos_whisper, SUM(CASE WHEN servicio = 'whisper' THEN costo ELSE 0 END) as costo_whisper, + SUM((COALESCE(input_tokens,0) + COALESCE(output_tokens,0)) / 1000000.0 * 20000) as costo_gemini, AVG(tiempo_ms) as avg_tiempo_ms ")->first(); - return view('livewire.chat.show-ai-logs', compact('logs', 'resumen')); + $meses = collect(range(0, 12))->map(function ($i) { + $d = now()->subMonths($i); + return [ + 'value' => $d->format('Y-m'), + 'label' => ucfirst($d->locale('es')->isoFormat('MMMM YYYY')), + ]; + }); + + return view('livewire.chat.show-ai-logs', compact('logs', 'resumen', 'meses')); } } diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index bb14b23..dd7dc86 100755 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -925,7 +925,6 @@ class TelegramBotService $promos = Promociones::where('visible', 'true') ->where(fn ($q) => $q->whereNull('fecha_limite')->orWhere('fecha_limite', '>=', now())) - ->whereHas('tarifaPromo', fn ($q) => $q->where('rol_id', $rolId)) ->with(['tarifaPromo' => fn ($q) => $q->where('rol_id', $rolId)]) ->get(); diff --git a/resources/views/livewire/chat/show-ai-logs.blade.php b/resources/views/livewire/chat/show-ai-logs.blade.php old mode 100644 new mode 100755 index 1cd7a4c..559d353 --- a/resources/views/livewire/chat/show-ai-logs.blade.php +++ b/resources/views/livewire/chat/show-ai-logs.blade.php @@ -6,7 +6,12 @@ {{-- Cards resumen --}} -
+ @php + $costoGemini = $resumen->costo_gemini ?? 0; + $costoWhisper = $resumen->costo_whisper ?? 0; + $costoTotal = $costoGemini + $costoWhisper; + @endphp +

Total llamadas

{{ number_format($resumen->total_llamadas ?? 0) }}

@@ -20,7 +25,7 @@

Tokens Gemini

{{ number_format(($resumen->total_input_tokens ?? 0) + ($resumen->total_output_tokens ?? 0)) }}

- Entrada: {{ number_format($resumen->total_input_tokens ?? 0) }} · Salida: {{ number_format($resumen->total_output_tokens ?? 0) }} + E: {{ number_format($resumen->total_input_tokens ?? 0) }} · S: {{ number_format($resumen->total_output_tokens ?? 0) }}

@@ -29,15 +34,41 @@

segundos transcritos

-

Costo Whisper

-

${{ number_format($resumen->costo_whisper ?? 0) }}

-

$1 por segundo · Tiempo prom: {{ number_format($resumen->avg_tiempo_ms ?? 0) }}ms

+

Costo Gemini

+

${{ number_format($costoGemini, 0, ',', '.') }}

+

$20.000 / millón tokens

+
+
+

Costo Total

+

${{ number_format($costoTotal, 0, ',', '.') }}

+

+ Whisper: ${{ number_format($costoWhisper, 0, ',', '.') }} + · Prom: {{ number_format($resumen->avg_tiempo_ms ?? 0) }}ms +

{{-- Filtros --}}
+
+ + +
+
ó rango:
+
+ + +
+
+ + +
-
- - -
-
- - -
- @@ -83,7 +106,7 @@ Tokens E/S Audio Tiempo - Costo + Costo COP Estado Detalle @@ -104,6 +127,9 @@ 'whisper' => 'Whisper', default => $log->servicio, }; + $costoFila = in_array($log->servicio, ['gemini_intent', 'gemini_vision']) + ? (($log->input_tokens ?? 0) + ($log->output_tokens ?? 0)) / 1000000 * 20000 + : ($log->costo ?? 0); @endphp @@ -132,9 +158,9 @@ @endif {{ number_format($log->tiempo_ms) }}ms - - @if($log->costo > 0) - ${{ number_format($log->costo) }} + + @if($costoFila > 0) + ${{ number_format($costoFila, 0, ',', '.') }} @else — @endif