- Fix model_pricing table references (model_id -> model, display_name -> model)
- Fix price columns (output_price_per_1k -> output_price_per_million)
- Add price conversion (per_million / 1000 = per_1k) in all API responses
- Add whereNotNull('model') filters to exclude invalid entries
- Add getModelDisplayName() helper method to all controllers
- Fix AccountController to use gateway_users budget fields directly
- Remove Budget model dependencies from AccountController
- Add custom Scramble server URL configuration for API docs
- Create ScrambleServiceProvider to set correct /api prefix
- Add migration to rename user_id to gateway_user_id in llm_requests
- Add custom ApiGuard for gateway_users authentication
- Update all API controllers: AccountController, ModelController, PricingController, ProviderController
All API endpoints now working correctly:
- GET /api/account
- GET /api/models
- GET /api/pricing
- GET /api/providers/{provider}
89 lines
2.0 KiB
PHP
89 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class UsageLog extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'usage_logs';
|
|
protected $primaryKey = 'request_id';
|
|
public $incrementing = false;
|
|
protected $keyType = 'string';
|
|
public $timestamps = false;
|
|
|
|
protected $fillable = [
|
|
'request_id',
|
|
'gateway_user_id', // Changed from user_id
|
|
'api_key',
|
|
'model',
|
|
'provider',
|
|
'endpoint',
|
|
'prompt_tokens',
|
|
'completion_tokens',
|
|
'total_tokens',
|
|
'cost',
|
|
'status',
|
|
'error_message',
|
|
'timestamp',
|
|
'metadata',
|
|
'request_payload',
|
|
'response_payload',
|
|
'response_time_ms',
|
|
'ip_address',
|
|
'user_agent',
|
|
];
|
|
|
|
protected $casts = [
|
|
'prompt_tokens' => 'integer',
|
|
'completion_tokens' => 'integer',
|
|
'total_tokens' => 'integer',
|
|
'cost' => 'decimal:6',
|
|
'timestamp' => 'datetime',
|
|
'metadata' => 'array',
|
|
'request_payload' => 'array',
|
|
'response_payload' => 'array',
|
|
'response_time_ms' => 'integer',
|
|
];
|
|
|
|
// Relationships
|
|
public function gatewayUser()
|
|
{
|
|
return $this->belongsTo(GatewayUser::class, 'gateway_user_id', 'user_id');
|
|
}
|
|
|
|
public function apiKey()
|
|
{
|
|
return $this->belongsTo(ApiKey::class, 'api_key', 'token');
|
|
}
|
|
|
|
// Scopes
|
|
public function scopeSuccess($query)
|
|
{
|
|
return $query->where('status', 'success');
|
|
}
|
|
|
|
public function scopeFailed($query)
|
|
{
|
|
return $query->where('status', 'failed');
|
|
}
|
|
|
|
public function scopeRecent($query, $days = 7)
|
|
{
|
|
return $query->where('timestamp', '>=', now()->subDays($days));
|
|
}
|
|
|
|
public function scopeByProvider($query, string $provider)
|
|
{
|
|
return $query->where('provider', $provider);
|
|
}
|
|
|
|
public function scopeByModel($query, string $model)
|
|
{
|
|
return $query->where('model', $model);
|
|
}
|
|
}
|