- Fixed database relationships: LlmRequest now properly uses gateway_user_id instead of user_id - Updated Models: GatewayUser and LlmRequest relationships corrected - Removed User->llmRequests relationship (admin users don't have LLM requests) - Simplified Dashboard: Now shows Gateway User statistics instead of admin users - Removed obsolete Budgets management pages (budgets handled directly in gateway_users) - Removed User Budgets admin section (redundant with gateway_users management) - Fixed view errors: Added null-checks for user_id in keys views - Updated navigation: Removed Budget and User Budget links - Updated routes: Cleaned up unused BudgetController and UserManagementController routes - Simplified StatisticsService: Focus on gateway_users and basic metrics only
113 lines
2.8 KiB
PHP
113 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Illuminate\Auth\Authenticatable as AuthenticatableTrait;
|
|
|
|
class GatewayUser extends Model implements Authenticatable
|
|
{
|
|
use HasFactory, AuthenticatableTrait;
|
|
|
|
protected $table = 'gateway_users';
|
|
protected $primaryKey = 'user_id';
|
|
public $incrementing = false;
|
|
protected $keyType = 'string';
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'alias',
|
|
'monthly_budget_limit',
|
|
'current_month_spending',
|
|
'budget_alert_threshold',
|
|
'rate_limit_per_hour',
|
|
'blocked',
|
|
'metadata',
|
|
];
|
|
|
|
protected $casts = [
|
|
'metadata' => 'array',
|
|
'blocked' => 'boolean',
|
|
'monthly_budget_limit' => 'decimal:2',
|
|
'current_month_spending' => 'decimal:2',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
|
|
// Relationships
|
|
public function apiKeys()
|
|
{
|
|
return $this->hasMany(ApiKey::class, 'gateway_user_id', 'user_id');
|
|
}
|
|
|
|
public function credentials()
|
|
{
|
|
return $this->hasMany(GatewayUserCredential::class, 'user_id', 'user_id');
|
|
}
|
|
|
|
public function usageLogs()
|
|
{
|
|
return $this->hasMany(UsageLog::class, 'gateway_user_id', 'user_id');
|
|
}
|
|
|
|
public function llmRequests()
|
|
{
|
|
return $this->hasMany(LlmRequest::class, 'gateway_user_id', 'user_id');
|
|
}
|
|
|
|
// Scopes
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('blocked', false);
|
|
}
|
|
|
|
public function scopeBlocked($query)
|
|
{
|
|
return $query->where('blocked', true);
|
|
}
|
|
|
|
// Helper methods for budget management
|
|
public function isBlocked(): bool
|
|
{
|
|
return $this->blocked;
|
|
}
|
|
|
|
public function hasExceededBudget(): bool
|
|
{
|
|
if (!$this->monthly_budget_limit) {
|
|
return false;
|
|
}
|
|
return $this->current_month_spending >= $this->monthly_budget_limit;
|
|
}
|
|
|
|
public function incrementSpending(float $amount): void
|
|
{
|
|
$this->increment('current_month_spending', $amount);
|
|
}
|
|
|
|
public function resetMonthlySpending(): void
|
|
{
|
|
$this->update(['current_month_spending' => 0]);
|
|
}
|
|
|
|
public function getBudgetUsagePercentage(): ?float
|
|
{
|
|
if (!$this->monthly_budget_limit || $this->monthly_budget_limit == 0) {
|
|
return null;
|
|
}
|
|
return ($this->current_month_spending / $this->monthly_budget_limit) * 100;
|
|
}
|
|
|
|
public function shouldSendBudgetAlert(): bool
|
|
{
|
|
if (!$this->budget_alert_threshold || !$this->monthly_budget_limit) {
|
|
return false;
|
|
}
|
|
|
|
$percentage = $this->getBudgetUsagePercentage();
|
|
return $percentage !== null && $percentage >= $this->budget_alert_threshold;
|
|
}
|
|
}
|