Files
laravel-llm-gateway/laravel-app/app/Services/StatisticsService.php
wtrinkl b1363aeab9 Initial commit: Any-LLM Gateway with Laravel Admin Interface
- Any-LLM Gateway setup with Docker Compose
- Laravel 11 admin interface with Livewire
- Dashboard with usage statistics and charts
- Gateway Users management with budget tracking
- API Keys management with revocation
- Budget templates with assignment
- Usage Logs with filtering and CSV export
- Model Pricing management with calculator
- PostgreSQL database integration
- Complete authentication system for admins
2025-11-16 12:38:05 +01:00

106 lines
3.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\UsageLog;
use App\Models\GatewayUser;
use Illuminate\Support\Facades\DB;
class StatisticsService
{
/**
* Get dashboard overview statistics
*/
public function getDashboardStats()
{
return [
'total_users' => GatewayUser::count(),
'active_users' => GatewayUser::active()->count(),
'blocked_users' => GatewayUser::blocked()->count(),
'total_requests_today' => UsageLog::today()->count(),
'total_spend_today' => UsageLog::today()->sum('cost') ?? 0,
'total_tokens_today' => UsageLog::today()->sum('total_tokens') ?? 0,
'total_spend_month' => UsageLog::whereMonth('timestamp', now()->month)->sum('cost') ?? 0,
'total_requests_month' => UsageLog::whereMonth('timestamp', now()->month)->count(),
];
}
/**
* Get usage breakdown by provider
*/
public function getUsageByProvider($days = 30)
{
return UsageLog::selectRaw('provider, COUNT(*) as count, SUM(cost) as total_cost')
->where('timestamp', '>=', now()->subDays($days))
->groupBy('provider')
->orderByDesc('count')
->get();
}
/**
* Get usage breakdown by model
*/
public function getUsageByModel($days = 30)
{
return UsageLog::selectRaw('model, COUNT(*) as count, SUM(total_tokens) as tokens, SUM(cost) as total_cost')
->where('timestamp', '>=', now()->subDays($days))
->groupBy('model')
->orderByDesc('count')
->limit(10)
->get();
}
/**
* Get daily usage chart data
*/
public function getDailyUsageChart($days = 30)
{
return UsageLog::selectRaw('DATE(timestamp) as date, COUNT(*) as requests, SUM(cost) as cost, SUM(total_tokens) as tokens')
->where('timestamp', '>=', now()->subDays($days))
->groupBy('date')
->orderBy('date')
->get();
}
/**
* Get top users by spend
*/
public function getTopUsers($limit = 10)
{
return GatewayUser::withCount('usageLogs')
->withSum('usageLogs', 'cost')
->orderByDesc('usage_logs_sum_cost')
->limit($limit)
->get();
}
/**
* Get recent activity
*/
public function getRecentActivity($limit = 20)
{
return UsageLog::with(['gatewayUser', 'apiKey'])
->orderByDesc('timestamp')
->limit($limit)
->get();
}
/**
* Get user statistics
*/
public function getUserStatistics($userId, $days = 30)
{
return UsageLog::where('user_id', $userId)
->where('timestamp', '>=', now()->subDays($days))
->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(cost) as total_cost,
AVG(total_tokens) as avg_tokens_per_request
')
->first();
}
}