- Remove old any-llm related files (Dockerfile, config.yml, web/, setup-laravel.sh) - Update README.md with new Laravel LLM Gateway documentation - Keep docker-compose.yml with laravel-llm container names - Clean project structure for Laravel-only implementation
182 lines
6.1 KiB
PHP
182 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\LlmRequest;
|
|
use App\Models\User;
|
|
use App\Models\UserProviderCredential;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class StatisticsService
|
|
{
|
|
/**
|
|
* Get dashboard overview statistics
|
|
*/
|
|
public function getDashboardStats(): array
|
|
{
|
|
return [
|
|
'total_users' => User::count(),
|
|
'active_credentials' => UserProviderCredential::where('is_active', true)->count(),
|
|
'total_requests_today' => LlmRequest::whereDate('created_at', today())->count(),
|
|
'total_spend_today' => LlmRequest::whereDate('created_at', today())->sum('total_cost') ?? 0,
|
|
'total_tokens_today' => LlmRequest::whereDate('created_at', today())->sum('total_tokens') ?? 0,
|
|
'total_spend_month' => LlmRequest::whereMonth('created_at', now()->month)
|
|
->whereYear('created_at', now()->year)
|
|
->sum('total_cost') ?? 0,
|
|
'total_requests_month' => LlmRequest::whereMonth('created_at', now()->month)
|
|
->whereYear('created_at', now()->year)
|
|
->count(),
|
|
'avg_cost_per_request' => LlmRequest::whereMonth('created_at', now()->month)
|
|
->whereYear('created_at', now()->year)
|
|
->avg('total_cost') ?? 0,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get usage breakdown by provider
|
|
*/
|
|
public function getUsageByProvider(int $days = 30)
|
|
{
|
|
return LlmRequest::selectRaw('provider, COUNT(*) as count, SUM(total_cost) as total_cost, SUM(total_tokens) as total_tokens')
|
|
->where('created_at', '>=', now()->subDays($days))
|
|
->where('status', 'success')
|
|
->groupBy('provider')
|
|
->orderByDesc('count')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get usage breakdown by model
|
|
*/
|
|
public function getUsageByModel(int $days = 30)
|
|
{
|
|
return LlmRequest::selectRaw('model, provider, COUNT(*) as count, SUM(total_tokens) as tokens, SUM(total_cost) as total_cost')
|
|
->where('created_at', '>=', now()->subDays($days))
|
|
->where('status', 'success')
|
|
->groupBy('model', 'provider')
|
|
->orderByDesc('count')
|
|
->limit(10)
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get daily usage chart data
|
|
*/
|
|
public function getDailyUsageChart(int $days = 30)
|
|
{
|
|
return LlmRequest::selectRaw('DATE(created_at) as date, COUNT(*) as requests, SUM(total_cost) as cost, SUM(total_tokens) as tokens')
|
|
->where('created_at', '>=', now()->subDays($days))
|
|
->where('status', 'success')
|
|
->groupBy('date')
|
|
->orderBy('date')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get top users by spend
|
|
*/
|
|
public function getTopUsers(int $limit = 10)
|
|
{
|
|
return User::select('users.*')
|
|
->withCount('llmRequests')
|
|
->withSum('llmRequests as total_cost', 'total_cost')
|
|
->withSum('llmRequests as total_tokens', 'total_tokens')
|
|
->orderByDesc('total_cost')
|
|
->limit($limit)
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get recent activity
|
|
*/
|
|
public function getRecentActivity(int $limit = 20)
|
|
{
|
|
return LlmRequest::with('user')
|
|
->orderByDesc('created_at')
|
|
->limit($limit)
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get user statistics
|
|
*/
|
|
public function getUserStatistics(int $userId, int $days = 30)
|
|
{
|
|
return LlmRequest::where('user_id', $userId)
|
|
->where('created_at', '>=', now()->subDays($days))
|
|
->where('status', 'success')
|
|
->selectRaw('
|
|
COUNT(*) as total_requests,
|
|
SUM(prompt_tokens) as total_prompt_tokens,
|
|
SUM(completion_tokens) as total_completion_tokens,
|
|
SUM(total_tokens) as total_tokens,
|
|
SUM(total_cost) as total_cost,
|
|
AVG(total_tokens) as avg_tokens_per_request,
|
|
AVG(total_cost) as avg_cost_per_request,
|
|
AVG(response_time_ms) as avg_response_time_ms
|
|
')
|
|
->first();
|
|
}
|
|
|
|
/**
|
|
* Get provider usage over time
|
|
*/
|
|
public function getProviderUsageOverTime(int $days = 30)
|
|
{
|
|
return LlmRequest::selectRaw('DATE(created_at) as date, provider, COUNT(*) as count, SUM(total_cost) as cost')
|
|
->where('created_at', '>=', now()->subDays($days))
|
|
->where('status', 'success')
|
|
->groupBy('date', 'provider')
|
|
->orderBy('date')
|
|
->get()
|
|
->groupBy('provider');
|
|
}
|
|
|
|
/**
|
|
* Get cost trends
|
|
*/
|
|
public function getCostTrends(int $days = 30)
|
|
{
|
|
$data = LlmRequest::selectRaw('
|
|
DATE(created_at) as date,
|
|
SUM(total_cost) as daily_cost,
|
|
AVG(total_cost) as avg_request_cost,
|
|
COUNT(*) as request_count
|
|
')
|
|
->where('created_at', '>=', now()->subDays($days))
|
|
->where('status', 'success')
|
|
->groupBy('date')
|
|
->orderBy('date')
|
|
->get();
|
|
|
|
return [
|
|
'daily_data' => $data,
|
|
'total_cost' => $data->sum('daily_cost'),
|
|
'avg_daily_cost' => $data->avg('daily_cost'),
|
|
'total_requests' => $data->sum('request_count'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get error statistics
|
|
*/
|
|
public function getErrorStats(int $days = 30)
|
|
{
|
|
return [
|
|
'total_errors' => LlmRequest::where('created_at', '>=', now()->subDays($days))
|
|
->where('status', '!=', 'success')
|
|
->count(),
|
|
'errors_by_status' => LlmRequest::selectRaw('status, COUNT(*) as count')
|
|
->where('created_at', '>=', now()->subDays($days))
|
|
->where('status', '!=', 'success')
|
|
->groupBy('status')
|
|
->get(),
|
|
'errors_by_provider' => LlmRequest::selectRaw('provider, COUNT(*) as count')
|
|
->where('created_at', '>=', now()->subDays($days))
|
|
->where('status', '!=', 'success')
|
|
->groupBy('provider')
|
|
->get(),
|
|
];
|
|
}
|
|
}
|