|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Actions; |
| 4 | + |
| 5 | +use App\Models\AiModelDailyUsage; |
| 6 | +use Carbon\CarbonImmutable; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | +use Illuminate\Support\Facades\Cache; |
| 9 | +use Illuminate\Support\Str; |
| 10 | + |
| 11 | +class LlmUsageStatsAction |
| 12 | +{ |
| 13 | + public const VERSION_CACHE_KEY = 'llm_usage_version'; |
| 14 | + |
| 15 | + public const OTHER_MODEL = 'other'; |
| 16 | + |
| 17 | + /** |
| 18 | + * @return Collection<int, string> |
| 19 | + */ |
| 20 | + public function models(): Collection |
| 21 | + { |
| 22 | + return $this->remember('models', function () { |
| 23 | + return AiModelDailyUsage::query() |
| 24 | + ->whereNotNull('ai_model_id') |
| 25 | + ->distinct() |
| 26 | + ->orderBy('model') |
| 27 | + ->pluck('model'); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + public function hasOtherModels(): bool |
| 32 | + { |
| 33 | + return $this->remember('has_other_models', function () { |
| 34 | + return AiModelDailyUsage::query()->whereNull('ai_model_id')->exists(); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return Collection<int, string> |
| 40 | + */ |
| 41 | + public function years(): Collection |
| 42 | + { |
| 43 | + return $this->remember('years', function () { |
| 44 | + return AiModelDailyUsage::query() |
| 45 | + ->orderBy('date') |
| 46 | + ->pluck('date') |
| 47 | + ->map(fn ($date) => $date->format('Y')) |
| 48 | + ->unique() |
| 49 | + ->values(); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return Collection<int, array{label: string, prompt_tokens: int, completion_tokens: int, total_tokens: int, requests: int}> |
| 55 | + */ |
| 56 | + public function monthlyBreakdown(?string $year, ?string $month, ?string $model): Collection |
| 57 | + { |
| 58 | + $suffix = 'breakdown_'.($year ?? 'all').'_'.($month ?? 'all').'_'.($model ?? 'all'); |
| 59 | + |
| 60 | + return $this->remember($suffix, function () use ($year, $month, $model) { |
| 61 | + return AiModelDailyUsage::query() |
| 62 | + ->when($model === self::OTHER_MODEL, fn ($query) => $query->whereNull('ai_model_id')) |
| 63 | + ->when($model && $model !== self::OTHER_MODEL, fn ($query) => $query->where('model', $model)) |
| 64 | + ->when($year, fn ($query) => $query->whereYear('date', $year)) |
| 65 | + ->when($month, fn ($query) => $query->whereMonth('date', $month)) |
| 66 | + ->orderBy('date') |
| 67 | + ->get() |
| 68 | + ->groupBy(fn (AiModelDailyUsage $row) => $row->date->format('Y-m')) |
| 69 | + ->map(fn (Collection $rows, string $label) => [ |
| 70 | + 'label' => $label, |
| 71 | + 'prompt_tokens' => (int) $rows->sum('prompt_tokens'), |
| 72 | + 'completion_tokens' => (int) $rows->sum('completion_tokens'), |
| 73 | + 'total_tokens' => (int) $rows->sum('total_tokens'), |
| 74 | + 'requests' => (int) $rows->sum('requests'), |
| 75 | + ]) |
| 76 | + ->values(); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array{prompt_tokens: int, completion_tokens: int, total_tokens: int, requests: int} |
| 82 | + */ |
| 83 | + public function currentMonthSummary(?string $model = null): array |
| 84 | + { |
| 85 | + return $this->summary('month_summary', CarbonImmutable::now()->startOfMonth(), $model); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return array{prompt_tokens: int, completion_tokens: int, total_tokens: int, requests: int} |
| 90 | + */ |
| 91 | + public function currentYearSummary(?string $model = null): array |
| 92 | + { |
| 93 | + return $this->summary('year_summary', CarbonImmutable::now()->startOfYear(), $model); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @return array{prompt_tokens: int, completion_tokens: int, total_tokens: int, requests: int} |
| 98 | + */ |
| 99 | + public function totalSummary(?string $model = null): array |
| 100 | + { |
| 101 | + return $this->summary('total_summary', null, $model); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return array{prompt_tokens: int, completion_tokens: int, total_tokens: int, requests: int} |
| 106 | + */ |
| 107 | + private function summary(string $suffix, ?CarbonImmutable $from, ?string $model = null): array |
| 108 | + { |
| 109 | + return $this->remember($suffix.'_'.($model ?? 'all'), function () use ($from, $model) { |
| 110 | + $rows = AiModelDailyUsage::query() |
| 111 | + ->when($from, fn ($query) => $query->where('date', '>=', $from)) |
| 112 | + ->when($model === self::OTHER_MODEL, fn ($query) => $query->whereNull('ai_model_id')) |
| 113 | + ->when($model && $model !== self::OTHER_MODEL, fn ($query) => $query->where('model', $model)) |
| 114 | + ->get(); |
| 115 | + |
| 116 | + return [ |
| 117 | + 'prompt_tokens' => (int) $rows->sum('prompt_tokens'), |
| 118 | + 'completion_tokens' => (int) $rows->sum('completion_tokens'), |
| 119 | + 'total_tokens' => (int) $rows->sum('total_tokens'), |
| 120 | + 'requests' => (int) $rows->sum('requests'), |
| 121 | + ]; |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + private function remember(string $suffix, callable $callback): mixed |
| 126 | + { |
| 127 | + $version = Cache::get(self::VERSION_CACHE_KEY, 0); |
| 128 | + $key = Str::slug("llm_usage_{$version}_{$suffix}", '_'); |
| 129 | + |
| 130 | + return Cache::remember($key, now()->addHour(), $callback); |
| 131 | + } |
| 132 | +} |
0 commit comments