feat: add HasFactory and factories for ApiLog, BrentPrice, PricePrediction
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,12 +2,17 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\ApiLogFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
#[Fillable(['service', 'method', 'url', 'status_code', 'duration_ms', 'error'])]
|
||||
class ApiLog extends Model
|
||||
{
|
||||
/** @use HasFactory<ApiLogFactory> */
|
||||
use HasFactory;
|
||||
|
||||
const null UPDATED_AT = null;
|
||||
|
||||
protected function casts(): array
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\BrentPriceFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
@@ -13,6 +15,9 @@ use Illuminate\Support\Carbon;
|
||||
#[Fillable(['date', 'price_usd'])]
|
||||
class BrentPrice extends Model
|
||||
{
|
||||
/** @use HasFactory<BrentPriceFactory> */
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $primaryKey = 'date';
|
||||
|
||||
@@ -4,7 +4,9 @@ namespace App\Models;
|
||||
|
||||
use App\Enums\PredictionSource;
|
||||
use App\Enums\TrendDirection;
|
||||
use Database\Factories\PricePredictionFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
@@ -20,6 +22,9 @@ use Illuminate\Support\Carbon;
|
||||
#[Fillable(['predicted_for', 'source', 'direction', 'confidence', 'reasoning', 'generated_at'])]
|
||||
class PricePrediction extends Model
|
||||
{
|
||||
/** @use HasFactory<PricePredictionFactory> */
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected function casts(): array
|
||||
|
||||
31
database/factories/ApiLogFactory.php
Normal file
31
database/factories/ApiLogFactory.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\ApiLog;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<ApiLog> */
|
||||
class ApiLogFactory extends Factory
|
||||
{
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'service' => fake()->randomElement(['fuel_finder', 'fred', 'anthropic', 'postcodes_io']),
|
||||
'method' => fake()->randomElement(['GET', 'POST']),
|
||||
'url' => fake()->url(),
|
||||
'status_code' => fake()->randomElement([200, 200, 200, 401, 429, 500]),
|
||||
'duration_ms' => fake()->numberBetween(50, 2000),
|
||||
'error' => null,
|
||||
'created_at' => fake()->dateTimeBetween('-7 days'),
|
||||
];
|
||||
}
|
||||
|
||||
public function failed(): static
|
||||
{
|
||||
return $this->state([
|
||||
'status_code' => fake()->randomElement([400, 401, 403, 429, 500, 503]),
|
||||
'error' => fake()->sentence(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
27
database/factories/BrentPriceFactory.php
Normal file
27
database/factories/BrentPriceFactory.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\BrentPrice;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<BrentPrice> */
|
||||
class BrentPriceFactory extends Factory
|
||||
{
|
||||
/** @var array<int, string> */
|
||||
private static array $usedDates = [];
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
do {
|
||||
$date = fake()->dateTimeBetween('-60 days')->format('Y-m-d');
|
||||
} while (in_array($date, self::$usedDates, true));
|
||||
|
||||
self::$usedDates[] = $date;
|
||||
|
||||
return [
|
||||
'date' => $date,
|
||||
'price_usd' => fake()->randomFloat(2, 65, 95),
|
||||
];
|
||||
}
|
||||
}
|
||||
34
database/factories/PricePredictionFactory.php
Normal file
34
database/factories/PricePredictionFactory.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\PredictionSource;
|
||||
use App\Enums\TrendDirection;
|
||||
use App\Models\PricePrediction;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<PricePrediction> */
|
||||
class PricePredictionFactory extends Factory
|
||||
{
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'predicted_for' => fake()->dateTimeBetween('-30 days')->format('Y-m-d'),
|
||||
'source' => fake()->randomElement(PredictionSource::cases()),
|
||||
'direction' => fake()->randomElement(TrendDirection::cases()),
|
||||
'confidence' => fake()->numberBetween(40, 85),
|
||||
'reasoning' => fake()->sentence(12),
|
||||
'generated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
public function llm(): static
|
||||
{
|
||||
return $this->state(['source' => PredictionSource::Llm]);
|
||||
}
|
||||
|
||||
public function ewma(): static
|
||||
{
|
||||
return $this->state(['source' => PredictionSource::Ewma]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user