Files
laravel-llm-gateway/laravel-app/app/Models/RateLimit.php
wtrinkl 6573e15ba4 Add complete Laravel LLM Gateway implementation
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
2025-11-18 22:18:36 +01:00

75 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class RateLimit extends Model
{
protected $fillable = [
'user_id',
'requests_per_minute',
'requests_per_hour',
'requests_per_day',
'current_minute_count',
'current_hour_count',
'current_day_count',
'minute_started_at',
'hour_started_at',
'day_started_at',
'is_rate_limited',
'rate_limit_expires_at',
];
protected $casts = [
'requests_per_minute' => 'integer',
'requests_per_hour' => 'integer',
'requests_per_day' => 'integer',
'current_minute_count' => 'integer',
'current_hour_count' => 'integer',
'current_day_count' => 'integer',
'minute_started_at' => 'datetime',
'hour_started_at' => 'datetime',
'day_started_at' => 'datetime',
'is_rate_limited' => 'boolean',
'rate_limit_expires_at' => 'datetime',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function isMinuteLimitExceeded(): bool
{
if ($this->minute_started_at->lt(now()->subMinute())) {
return false; // Period expired, should be reset
}
return $this->current_minute_count >= $this->requests_per_minute;
}
public function isHourLimitExceeded(): bool
{
if ($this->hour_started_at->lt(now()->subHour())) {
return false; // Period expired, should be reset
}
return $this->current_hour_count >= $this->requests_per_hour;
}
public function isDayLimitExceeded(): bool
{
if ($this->day_started_at->lt(now()->subDay())) {
return false; // Period expired, should be reset
}
return $this->current_day_count >= $this->requests_per_day;
}
public function isAnyLimitExceeded(): bool
{
return $this->isMinuteLimitExceeded()
|| $this->isHourLimitExceeded()
|| $this->isDayLimitExceeded();
}
}