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(); } }