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