Core Features: - Multi-provider support (OpenAI, Anthropic, DeepSeek, Gemini, Mistral) - Provider service architecture with abstract base class - Dynamic model discovery from provider APIs - Encrypted per-user provider credentials storage Admin Interface: - Complete admin panel with Livewire components - User management with CRUD operations - API key management with testing capabilities - Budget system with limits and reset schedules - Usage logs with filtering and CSV export - Model pricing management with cost calculator - Dashboard with Chart.js visualizations Database Schema: - MariaDB migrations for all tables - User provider credentials (encrypted) - LLM request logging - Budget tracking and rate limiting - Model pricing configuration API Implementation: - OpenAI-compatible endpoints - Budget checking middleware - Rate limit enforcement - Request logging jobs - Cost calculation service Testing: - Unit tests for all provider services - Provider factory tests - Cost calculator tests Documentation: - Admin user seeder - Model pricing seeder - Configuration files
70 lines
1.9 KiB
PHP
70 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class UserBudget extends Model
|
|
{
|
|
protected $fillable = [
|
|
'user_id',
|
|
'monthly_limit',
|
|
'daily_limit',
|
|
'current_month_spending',
|
|
'current_day_spending',
|
|
'month_started_at',
|
|
'day_started_at',
|
|
'alert_threshold_percentage',
|
|
'last_alert_sent_at',
|
|
'is_budget_exceeded',
|
|
'is_active',
|
|
];
|
|
|
|
protected $casts = [
|
|
'monthly_limit' => 'decimal:2',
|
|
'daily_limit' => 'decimal:2',
|
|
'current_month_spending' => 'decimal:2',
|
|
'current_day_spending' => 'decimal:2',
|
|
'month_started_at' => 'date',
|
|
'day_started_at' => 'date',
|
|
'alert_threshold_percentage' => 'integer',
|
|
'last_alert_sent_at' => 'datetime',
|
|
'is_budget_exceeded' => 'boolean',
|
|
'is_active' => 'boolean',
|
|
];
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function getRemainingMonthlyBudget(): float
|
|
{
|
|
return max(0, $this->monthly_limit - $this->current_month_spending);
|
|
}
|
|
|
|
public function getRemainingDailyBudget(): ?float
|
|
{
|
|
if (!$this->daily_limit) {
|
|
return null;
|
|
}
|
|
return max(0, $this->daily_limit - $this->current_day_spending);
|
|
}
|
|
|
|
public function getMonthlyUsagePercentage(): float
|
|
{
|
|
if ($this->monthly_limit == 0) {
|
|
return 0;
|
|
}
|
|
return ($this->current_month_spending / $this->monthly_limit) * 100;
|
|
}
|
|
|
|
public function shouldSendAlert(): bool
|
|
{
|
|
$percentage = $this->getMonthlyUsagePercentage();
|
|
return $percentage >= $this->alert_threshold_percentage
|
|
&& (!$this->last_alert_sent_at || $this->last_alert_sent_at->lt(now()->subHours(24)));
|
|
}
|
|
}
|