- 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
135 lines
2.7 KiB
PHP
135 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class GatewayUser extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'users';
|
|
|
|
/**
|
|
* The primary key for the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $primaryKey = 'user_id';
|
|
|
|
/**
|
|
* Indicates if the IDs are auto-incrementing.
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $incrementing = false;
|
|
|
|
/**
|
|
* The data type of the primary key ID.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $keyType = 'string';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'user_id',
|
|
'alias',
|
|
'spend',
|
|
'budget_id',
|
|
'blocked',
|
|
'metadata',
|
|
'budget_started_at',
|
|
'next_budget_reset_at',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'spend' => 'double',
|
|
'blocked' => 'boolean',
|
|
'metadata' => 'array',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
'budget_started_at' => 'datetime',
|
|
'next_budget_reset_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the API keys for this user.
|
|
*/
|
|
public function apiKeys()
|
|
{
|
|
return $this->hasMany(ApiKey::class, 'user_id', 'user_id');
|
|
}
|
|
|
|
/**
|
|
* Get the usage logs for this user.
|
|
*/
|
|
public function usageLogs()
|
|
{
|
|
return $this->hasMany(UsageLog::class, 'user_id', 'user_id');
|
|
}
|
|
|
|
/**
|
|
* Get the budget for this user.
|
|
*/
|
|
public function budget()
|
|
{
|
|
return $this->belongsTo(Budget::class, 'budget_id', 'budget_id');
|
|
}
|
|
|
|
/**
|
|
* Scope a query to only include active users.
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('blocked', false);
|
|
}
|
|
|
|
/**
|
|
* Scope a query to only include blocked users.
|
|
*/
|
|
public function scopeBlocked($query)
|
|
{
|
|
return $query->where('blocked', true);
|
|
}
|
|
|
|
/**
|
|
* Get the formatted spend amount.
|
|
*/
|
|
public function getSpendFormattedAttribute()
|
|
{
|
|
return '$' . number_format($this->spend, 2);
|
|
}
|
|
|
|
/**
|
|
* Get the total number of requests.
|
|
*/
|
|
public function getTotalRequestsAttribute()
|
|
{
|
|
return $this->usageLogs()->count();
|
|
}
|
|
|
|
/**
|
|
* Get the total number of tokens used.
|
|
*/
|
|
public function getTotalTokensAttribute()
|
|
{
|
|
return $this->usageLogs()->sum('total_tokens');
|
|
}
|
|
}
|