Compare commits
4 Commits
4220b1b86a
...
aec547cd86
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aec547cd86 | ||
|
|
486f0e689c | ||
|
|
1a0381265e | ||
|
|
a7ee9f4557 |
@@ -68,3 +68,10 @@ FUELALERT_API_KEY=
|
||||
|
||||
FRED_API_KEY=
|
||||
EIA_API_KEY= # US EIA Open Data API key — register free at eia.gov/opendata
|
||||
|
||||
STRIPE_PRICE_BASIC_MONTHLY=
|
||||
STRIPE_PRICE_BASIC_ANNUAL=
|
||||
STRIPE_PRICE_PLUS_MONTHLY=
|
||||
STRIPE_PRICE_PLUS_ANNUAL=
|
||||
STRIPE_PRICE_PRO_MONTHLY=
|
||||
STRIPE_PRICE_PRO_ANNUAL=
|
||||
|
||||
37
app/Console/Commands/FetchOilPrices.php
Normal file
37
app/Console/Commands/FetchOilPrices.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\BrentPriceFetcher;
|
||||
use App\Services\BrentPriceSources\BrentPriceFetchException;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
#[Signature('oil:fetch')]
|
||||
#[Description('Fetch latest Brent crude prices (EIA primary, FRED fallback)')]
|
||||
class FetchOilPrices extends Command
|
||||
{
|
||||
public function handle(BrentPriceFetcher $fetcher): int
|
||||
{
|
||||
try {
|
||||
$fetcher->fetchFromEia();
|
||||
$this->info('Fetched Brent prices from EIA.');
|
||||
|
||||
return self::SUCCESS;
|
||||
} catch (BrentPriceFetchException $e) {
|
||||
$this->warn('EIA fetch failed: '.$e->getMessage().'. Trying FRED...');
|
||||
}
|
||||
|
||||
try {
|
||||
$fetcher->fetchFromFred();
|
||||
$this->info('Fetched Brent prices from FRED.');
|
||||
|
||||
return self::SUCCESS;
|
||||
} catch (BrentPriceFetchException $e) {
|
||||
$this->error('Both EIA and FRED failed: '.$e->getMessage());
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,26 +2,37 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\OilPriceService;
|
||||
use App\Services\BrentPricePredictor;
|
||||
use Illuminate\Console\Command;
|
||||
use Throwable;
|
||||
|
||||
class PredictOilPrices extends Command
|
||||
{
|
||||
protected $signature = 'oil:predict {--fetch : Fetch latest FRED prices before predicting}';
|
||||
protected $signature = 'oil:predict {--force : Generate even if the latest price already has a prediction}';
|
||||
|
||||
protected $description = 'Generate a Brent crude oil price direction prediction';
|
||||
|
||||
public function handle(OilPriceService $service): int
|
||||
public function handle(BrentPricePredictor $predictor): int
|
||||
{
|
||||
try {
|
||||
if ($this->option('fetch')) {
|
||||
$this->info('Fetching latest Brent crude prices from FRED...');
|
||||
$service->fetchBrentPrices();
|
||||
$latest = $predictor->latestPrice();
|
||||
|
||||
if ($latest?->prediction_generated_at !== null && ! $this->option('force')) {
|
||||
$message = sprintf(
|
||||
'Prediction already generated for %s at %s.',
|
||||
$latest->date->toDateString(),
|
||||
$latest->prediction_generated_at->toDateTimeString(),
|
||||
);
|
||||
|
||||
if (! $this->confirm($message.' Run again anyway?', default: false)) {
|
||||
$this->info('Skipped.');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Generating prediction...');
|
||||
$prediction = $service->generatePrediction();
|
||||
$prediction = $predictor->generatePrediction();
|
||||
|
||||
if ($prediction === null) {
|
||||
$this->error('Could not generate a prediction — not enough price data.');
|
||||
|
||||
@@ -4,7 +4,10 @@ namespace App\Filament\Resources\BrentPriceResource\Pages;
|
||||
|
||||
use App\Filament\Resources\BrentPriceResource;
|
||||
use App\Filament\Widgets\BrentPriceChartWidget;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
|
||||
class ListBrentPrices extends ListRecords
|
||||
{
|
||||
@@ -12,7 +15,30 @@ class ListBrentPrices extends ListRecords
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [];
|
||||
return [
|
||||
Action::make('fetchPrices')
|
||||
->label('Fetch Prices Now')
|
||||
->icon('heroicon-o-arrow-down-tray')
|
||||
->requiresConfirmation()
|
||||
->modalHeading('Fetch latest Brent prices?')
|
||||
->modalDescription('Pulls the latest Brent crude prices from EIA (falls back to FRED if EIA is unavailable).')
|
||||
->action(function () {
|
||||
$result = Artisan::call('oil:fetch');
|
||||
|
||||
if ($result === 0) {
|
||||
Notification::make()
|
||||
->title('Brent prices fetched successfully')
|
||||
->success()
|
||||
->send();
|
||||
} else {
|
||||
Notification::make()
|
||||
->title('Fetch failed')
|
||||
->body('Both EIA and FRED failed. Check API Logs for details.')
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getHeaderWidgets(): array
|
||||
|
||||
@@ -20,9 +20,9 @@ class ListOilPredictions extends ListRecords
|
||||
->icon('heroicon-o-cpu-chip')
|
||||
->requiresConfirmation()
|
||||
->modalHeading('Run oil price prediction?')
|
||||
->modalDescription('This will fetch the latest FRED prices and generate a new prediction. May take a few seconds.')
|
||||
->modalDescription('Generates a new prediction from the stored Brent prices. Runs even if a prediction already exists for the latest price.')
|
||||
->action(function () {
|
||||
$result = Artisan::call('oil:predict', ['--fetch' => true]);
|
||||
$result = Artisan::call('oil:predict', ['--force' => true]);
|
||||
|
||||
if ($result === 0) {
|
||||
Notification::make()
|
||||
|
||||
@@ -6,9 +6,8 @@ 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;
|
||||
|
||||
#[Fillable(['date', 'price_usd'])]
|
||||
#[Fillable(['date', 'price_usd', 'prediction_generated_at'])]
|
||||
class BrentPrice extends Model
|
||||
{
|
||||
/** @use HasFactory<BrentPriceFactory> */
|
||||
@@ -27,6 +26,7 @@ class BrentPrice extends Model
|
||||
return [
|
||||
'date' => 'date',
|
||||
'price_usd' => 'decimal:2',
|
||||
'prediction_generated_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
44
app/Services/BrentPriceFetcher.php
Normal file
44
app/Services/BrentPriceFetcher.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\BrentPrice;
|
||||
use App\Services\BrentPriceSources\BrentPriceFetchException;
|
||||
use App\Services\BrentPriceSources\EiaBrentPriceSource;
|
||||
use App\Services\BrentPriceSources\FredBrentPriceSource;
|
||||
|
||||
final readonly class BrentPriceFetcher
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EiaBrentPriceSource $eia,
|
||||
private readonly FredBrentPriceSource $fred,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Fetch from EIA and persist. Throws on failure.
|
||||
*/
|
||||
public function fetchFromEia(): void
|
||||
{
|
||||
$rows = $this->eia->fetch();
|
||||
|
||||
if ($rows === null) {
|
||||
throw new BrentPriceFetchException('EIA fetch returned no data');
|
||||
}
|
||||
|
||||
BrentPrice::upsert($rows, ['date'], ['price_usd']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch from FRED and persist. Throws on failure.
|
||||
*/
|
||||
public function fetchFromFred(): void
|
||||
{
|
||||
$rows = $this->fred->fetch();
|
||||
|
||||
if ($rows === null) {
|
||||
throw new BrentPriceFetchException('FRED fetch returned no data');
|
||||
}
|
||||
|
||||
BrentPrice::upsert($rows, ['date'], ['price_usd']);
|
||||
}
|
||||
}
|
||||
@@ -8,91 +8,40 @@ use App\Models\BrentPrice;
|
||||
use App\Models\PricePrediction;
|
||||
use App\Services\LlmPrediction\OilPredictionProvider;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class OilPriceService
|
||||
final class BrentPricePredictor
|
||||
{
|
||||
/**
|
||||
* Decay factor for EWMA. Higher = more weight on recent prices.
|
||||
*/
|
||||
private const float EWMA_ALPHA = 0.3;
|
||||
|
||||
/**
|
||||
* Minimum % change in EWMA to be considered rising/falling.
|
||||
*/
|
||||
private const float EWMA_THRESHOLD_PCT = 1.5;
|
||||
|
||||
/**
|
||||
* EWMA confidence is capped lower than LLM — it's a simpler model.
|
||||
*/
|
||||
private const int EWMA_MAX_CONFIDENCE = 65;
|
||||
|
||||
/**
|
||||
* Minimum price rows needed before EWMA is meaningful.
|
||||
*/
|
||||
private const int EWMA_MIN_ROWS = 14;
|
||||
|
||||
public function __construct(
|
||||
private readonly ApiLogger $apiLogger,
|
||||
private readonly OilPredictionProvider $provider,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Fetch the last 30 days of Brent crude prices from FRED and store them.
|
||||
* Return the latest BrentPrice row, or null if none exists.
|
||||
*/
|
||||
public function fetchBrentPrices(): void
|
||||
public function latestPrice(): ?BrentPrice
|
||||
{
|
||||
$url = 'https://api.stlouisfed.org/fred/series/observations';
|
||||
|
||||
try {
|
||||
$response = $this->apiLogger->send('fred', 'GET', $url, fn () => Http::timeout(10)
|
||||
->get($url, [
|
||||
'series_id' => 'DCOILBRENTEU',
|
||||
'api_key' => config('services.fred.api_key'),
|
||||
'sort_order' => 'desc',
|
||||
'limit' => 30,
|
||||
'file_type' => 'json',
|
||||
]));
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::error('OilPriceService: FRED request failed', ['status' => $response->status()]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$rows = collect($response->json('observations') ?? [])
|
||||
->filter(fn (array $obs) => $obs['value'] !== '.') // FRED uses '.' for missing data
|
||||
->map(fn (array $obs) => [
|
||||
'date' => $obs['date'],
|
||||
'price_usd' => (float) $obs['value'],
|
||||
])
|
||||
->all();
|
||||
|
||||
if (empty($rows)) {
|
||||
Log::warning('OilPriceService: no valid FRED observations returned');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
BrentPrice::upsert($rows, ['date'], ['price_usd']);
|
||||
} catch (Throwable $e) {
|
||||
Log::error('OilPriceService: fetchBrentPrices failed', ['error' => $e->getMessage()]);
|
||||
}
|
||||
return BrentPrice::orderBy('date', 'desc')->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate predictions from all available sources and store each one.
|
||||
* EWMA always runs. LLM provider runs and returns null if not configured.
|
||||
* Returns the highest-confidence prediction (LLM preferred over EWMA).
|
||||
* Generate EWMA + LLM predictions, store them, and flag the latest
|
||||
* brent_prices row as having a prediction generated.
|
||||
*/
|
||||
public function generatePrediction(): ?PricePrediction
|
||||
{
|
||||
$prices = BrentPrice::orderBy('date', 'desc')->limit(30)->get();
|
||||
|
||||
if ($prices->count() < self::EWMA_MIN_ROWS) {
|
||||
Log::warning('OilPriceService: not enough price data to generate prediction', [
|
||||
Log::warning('BrentPricePredictor: not enough price data', [
|
||||
'rows' => $prices->count(),
|
||||
]);
|
||||
|
||||
@@ -111,13 +60,15 @@ class OilPriceService
|
||||
PricePrediction::create($llm->toArray());
|
||||
}
|
||||
|
||||
return $llm ?? $ewma;
|
||||
$result = $llm ?? $ewma;
|
||||
|
||||
if ($result !== null) {
|
||||
$prices->first()->forceFill(['prediction_generated_at' => now()])->save();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Option A — EWMA-based trend extrapolation. Used as fallback when LLM is unavailable.
|
||||
* Compares the 3-day EWMA against the 7-day EWMA to detect direction.
|
||||
*/
|
||||
public function generateEwmaPrediction(Collection $prices): ?PricePrediction
|
||||
{
|
||||
$chronological = $prices->sortBy('date')->pluck('price_usd')->values()->all();
|
||||
@@ -162,9 +113,7 @@ class OilPriceService
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute Exponential Weighted Moving Average for a series of prices.
|
||||
*
|
||||
* @param float[] $prices Chronological order (oldest first)
|
||||
* @param float[] $prices Chronological (oldest first).
|
||||
*/
|
||||
private function computeEwma(array $prices): float
|
||||
{
|
||||
@@ -177,10 +126,6 @@ class OilPriceService
|
||||
return round($ema, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a % change magnitude to a 0–EWMA_MAX_CONFIDENCE confidence score.
|
||||
* 1.5% → ~30, 3% → ~50, 5%+ → 65.
|
||||
*/
|
||||
private function ewmaConfidence(float $changePct): int
|
||||
{
|
||||
$scaled = min($changePct / 5.0, 1.0) * self::EWMA_MAX_CONFIDENCE;
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\BrentPriceSources;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
final class BrentPriceFetchException extends RuntimeException {}
|
||||
60
app/Services/BrentPriceSources/EiaBrentPriceSource.php
Normal file
60
app/Services/BrentPriceSources/EiaBrentPriceSource.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\BrentPriceSources;
|
||||
|
||||
use App\Services\ApiLogger;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
final class EiaBrentPriceSource
|
||||
{
|
||||
private const string URL = 'https://api.eia.gov/v2/petroleum/pri/spt/data/';
|
||||
|
||||
public function __construct(private readonly ApiLogger $apiLogger) {}
|
||||
|
||||
/**
|
||||
* @return array{date: string, price_usd: float}[]|null
|
||||
*/
|
||||
public function fetch(): ?array
|
||||
{
|
||||
try {
|
||||
$response = $this->apiLogger->send('eia', 'GET', self::URL, fn () => Http::timeout(10)
|
||||
->get(self::URL, [
|
||||
'api_key' => config('services.eia.api_key'),
|
||||
'frequency' => 'daily',
|
||||
'data[0]' => 'value',
|
||||
'facets[series][]' => 'RBRTE',
|
||||
'sort[0][column]' => 'period',
|
||||
'sort[0][direction]' => 'desc',
|
||||
'length' => 30,
|
||||
]));
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::error('EiaBrentPriceSource: request failed', ['status' => $response->status()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$rows = collect($response->json('response.data') ?? [])
|
||||
->filter(fn (array $row) => ($row['value'] ?? '.') !== '.')
|
||||
->map(fn (array $row) => [
|
||||
'date' => $row['period'],
|
||||
'price_usd' => (float) $row['value'],
|
||||
])
|
||||
->all();
|
||||
|
||||
if ($rows === []) {
|
||||
Log::warning('EiaBrentPriceSource: no valid observations returned');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $rows;
|
||||
} catch (Throwable $e) {
|
||||
Log::error('EiaBrentPriceSource: fetch failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
58
app/Services/BrentPriceSources/FredBrentPriceSource.php
Normal file
58
app/Services/BrentPriceSources/FredBrentPriceSource.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\BrentPriceSources;
|
||||
|
||||
use App\Services\ApiLogger;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
final class FredBrentPriceSource
|
||||
{
|
||||
private const string URL = 'https://api.stlouisfed.org/fred/series/observations';
|
||||
|
||||
public function __construct(private readonly ApiLogger $apiLogger) {}
|
||||
|
||||
/**
|
||||
* @return array{date: string, price_usd: float}[]|null
|
||||
*/
|
||||
public function fetch(): ?array
|
||||
{
|
||||
try {
|
||||
$response = $this->apiLogger->send('fred', 'GET', self::URL, fn () => Http::timeout(10)
|
||||
->get(self::URL, [
|
||||
'series_id' => 'DCOILBRENTEU',
|
||||
'api_key' => config('services.fred.api_key'),
|
||||
'sort_order' => 'desc',
|
||||
'limit' => 30,
|
||||
'file_type' => 'json',
|
||||
]));
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::error('FredBrentPriceSource: request failed', ['status' => $response->status()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$rows = collect($response->json('observations') ?? [])
|
||||
->filter(fn (array $obs) => $obs['value'] !== '.')
|
||||
->map(fn (array $obs) => [
|
||||
'date' => $obs['date'],
|
||||
'price_usd' => (float) $obs['value'],
|
||||
])
|
||||
->all();
|
||||
|
||||
if ($rows === []) {
|
||||
Log::warning('FredBrentPriceSource: no valid observations returned');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $rows;
|
||||
} catch (Throwable $e) {
|
||||
Log::error('FredBrentPriceSource: fetch failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,9 +74,18 @@ return [
|
||||
|
||||
'stripe' => [
|
||||
'prices' => [
|
||||
'basic' => env('STRIPE_PRICE_BASIC'),
|
||||
'plus' => env('STRIPE_PRICE_PLUS'),
|
||||
'pro' => env('STRIPE_PRICE_PRO'),
|
||||
'basic' => [
|
||||
'monthly' => env('STRIPE_PRICE_BASIC_MONTHLY'),
|
||||
'annual' => env('STRIPE_PRICE_BASIC_ANNUAL'),
|
||||
],
|
||||
'plus' => [
|
||||
'monthly' => env('STRIPE_PRICE_PLUS_MONTHLY'),
|
||||
'annual' => env('STRIPE_PRICE_PLUS_ANNUAL'),
|
||||
],
|
||||
'pro' => [
|
||||
'monthly' => env('STRIPE_PRICE_PRO_MONTHLY'),
|
||||
'annual' => env('STRIPE_PRICE_PRO_ANNUAL'),
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('brent_prices', function (Blueprint $table) {
|
||||
$table->timestamp('prediction_generated_at')->nullable()->after('price_usd');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('brent_prices', function (Blueprint $table) {
|
||||
$table->dropColumn('prediction_generated_at');
|
||||
});
|
||||
}
|
||||
};
|
||||
354
docs/superpowers/plans/2026-04-14-eia-brent-price-fallback.md
Normal file
354
docs/superpowers/plans/2026-04-14-eia-brent-price-fallback.md
Normal file
@@ -0,0 +1,354 @@
|
||||
# EIA Brent Price Source — Primary with FRED Fallback Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Replace FRED as the primary Brent crude data source with EIA's API, keeping FRED as a silent fallback.
|
||||
|
||||
**Architecture:** `fetchBrentPrices()` is split into two private methods — `fetchFromEia()` and `fetchFromFred()` — each returning a mapped `array|null`. The public method tries EIA first; on `null`, warns and tries FRED; on second `null`, logs an error and returns. The upsert call is shared and runs once.
|
||||
|
||||
**Tech Stack:** Laravel `Http` facade, `BrentPrice::upsert()`, `config/services.php`, Pest + `Http::fake()`
|
||||
|
||||
---
|
||||
|
||||
## Files
|
||||
|
||||
| Action | File |
|
||||
|--------|------|
|
||||
| Modify | `config/services.php` |
|
||||
| Modify | `.env.example` |
|
||||
| Modify | `app/Services/OilPriceService.php` |
|
||||
| Modify | `tests/Unit/Services/OilPriceServiceTest.php` |
|
||||
|
||||
---
|
||||
|
||||
## Task 1: Add EIA config key
|
||||
|
||||
**Files:**
|
||||
- Modify: `config/services.php`
|
||||
- Modify: `.env.example`
|
||||
|
||||
- [ ] **Step 1: Add EIA service config**
|
||||
|
||||
In `config/services.php`, add after the `'fred'` block:
|
||||
|
||||
```php
|
||||
'eia' => [
|
||||
'api_key' => env('EIA_API_KEY'),
|
||||
],
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Add EIA key to `.env.example`**
|
||||
|
||||
Add after the existing `FRED_API_KEY=` line:
|
||||
|
||||
```
|
||||
EIA_API_KEY= # US EIA Open Data API key — register free at eia.gov/opendata
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Add your real EIA key to `.env`**
|
||||
|
||||
Add after `FRED_API_KEY=`:
|
||||
|
||||
```
|
||||
EIA_API_KEY=your_key_here
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Commit**
|
||||
|
||||
```bash
|
||||
git add config/services.php .env.example
|
||||
git commit -m "config: add EIA API key for Brent crude price source"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 2: Write failing tests for EIA fetch behaviour
|
||||
|
||||
**Files:**
|
||||
- Modify: `tests/Unit/Services/OilPriceServiceTest.php`
|
||||
|
||||
- [ ] **Step 1: Replace the three existing FRED fetch tests with four new tests**
|
||||
|
||||
Replace the `// --- fetchBrentPrices ---` section (lines 22–69) with:
|
||||
|
||||
```php
|
||||
// --- fetchBrentPrices ---
|
||||
|
||||
it('fetches and stores brent prices from EIA when EIA succeeds', function (): void {
|
||||
Http::fake([
|
||||
'*/eia.gov/*' => Http::response([
|
||||
'response' => [
|
||||
'data' => [
|
||||
['period' => '2026-04-02', 'value' => '73.80'],
|
||||
['period' => '2026-04-01', 'value' => '75.10'],
|
||||
['period' => '2026-03-31', 'value' => '74.50'],
|
||||
],
|
||||
],
|
||||
]),
|
||||
'*/fred/*' => Http::response([], 500),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(3)
|
||||
->and(BrentPrice::find('2026-04-02')->price_usd)->toBe('73.80');
|
||||
Http::assertNothingSentTo('*fred*');
|
||||
});
|
||||
|
||||
it('falls back to FRED when EIA returns a 500', function (): void {
|
||||
Http::fake([
|
||||
'*/eia.gov/*' => Http::response([], 500),
|
||||
'*/fred/series/observations*' => Http::response([
|
||||
'observations' => [
|
||||
['date' => '2026-04-01', 'value' => '75.10'],
|
||||
['date' => '2026-04-02', 'value' => '73.80'],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(2);
|
||||
});
|
||||
|
||||
it('falls back to FRED when EIA returns empty data', function (): void {
|
||||
Http::fake([
|
||||
'*/eia.gov/*' => Http::response(['response' => ['data' => []]]),
|
||||
'*/fred/series/observations*' => Http::response([
|
||||
'observations' => [
|
||||
['date' => '2026-04-01', 'value' => '75.10'],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(1);
|
||||
});
|
||||
|
||||
it('stores no rows and logs error when both EIA and FRED fail', function (): void {
|
||||
Http::fake([
|
||||
'*/eia.gov/*' => Http::response([], 500),
|
||||
'*/fred/series/observations*' => Http::response([], 500),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(0);
|
||||
});
|
||||
|
||||
it('filters out EIA missing value markers', function (): void {
|
||||
Http::fake([
|
||||
'*/eia.gov/*' => Http::response([
|
||||
'response' => [
|
||||
'data' => [
|
||||
['period' => '2026-04-01', 'value' => '75.10'],
|
||||
['period' => '2026-04-02', 'value' => '.'],
|
||||
['period' => '2026-04-03', 'value' => '74.20'],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(2)
|
||||
->and(BrentPrice::find('2026-04-02'))->toBeNull();
|
||||
});
|
||||
|
||||
it('upserts existing brent price rows on refetch via EIA', function (): void {
|
||||
Http::fake([
|
||||
'*/eia.gov/*' => Http::sequence()
|
||||
->push(['response' => ['data' => [['period' => '2026-04-01', 'value' => '74.00']]]])
|
||||
->push(['response' => ['data' => [['period' => '2026-04-01', 'value' => '75.50']]]]),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(1)
|
||||
->and(BrentPrice::find('2026-04-01')->price_usd)->toBe('75.50');
|
||||
});
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run the new tests to confirm they fail**
|
||||
|
||||
```bash
|
||||
php artisan test --compact tests/Unit/Services/OilPriceServiceTest.php --timeout=10
|
||||
```
|
||||
|
||||
Expected: several FAIL — "no matching fake" or assertion errors (EIA endpoint not yet implemented).
|
||||
|
||||
---
|
||||
|
||||
## Task 3: Refactor `OilPriceService::fetchBrentPrices()`
|
||||
|
||||
**Files:**
|
||||
- Modify: `app/Services/OilPriceService.php`
|
||||
|
||||
- [ ] **Step 1: Replace `fetchBrentPrices()` and extract private methods**
|
||||
|
||||
Replace the entire `fetchBrentPrices()` method with:
|
||||
|
||||
```php
|
||||
/**
|
||||
* Fetch the last 30 days of Brent crude prices.
|
||||
* Tries EIA first; falls back to FRED if EIA is unavailable.
|
||||
*/
|
||||
public function fetchBrentPrices(): void
|
||||
{
|
||||
$rows = $this->fetchFromEia();
|
||||
|
||||
if ($rows === null) {
|
||||
Log::warning('OilPriceService: EIA fetch failed, falling back to FRED');
|
||||
$rows = $this->fetchFromFred();
|
||||
}
|
||||
|
||||
if ($rows === null) {
|
||||
Log::error('OilPriceService: both EIA and FRED fetch failed');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
BrentPrice::upsert($rows, ['date'], ['price_usd']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch Brent crude prices from the EIA Open Data API.
|
||||
* Returns mapped rows or null on any failure.
|
||||
*
|
||||
* @return array{date: string, price_usd: float}[]|null
|
||||
*/
|
||||
private function fetchFromEia(): ?array
|
||||
{
|
||||
$url = 'https://api.eia.gov/v2/petroleum/pri/spt/data/';
|
||||
|
||||
try {
|
||||
$response = $this->apiLogger->send('eia', 'GET', $url, fn () => Http::timeout(10)
|
||||
->get($url, [
|
||||
'api_key' => config('services.eia.api_key'),
|
||||
'frequency' => 'daily',
|
||||
'data[0]' => 'value',
|
||||
'facets[series][]' => 'RBRTE',
|
||||
'sort[0][column]' => 'period',
|
||||
'sort[0][direction]' => 'desc',
|
||||
'length' => 30,
|
||||
]));
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::error('OilPriceService: EIA request failed', ['status' => $response->status()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$rows = collect($response->json('response.data') ?? [])
|
||||
->filter(fn (array $row) => ($row['value'] ?? '.') !== '.')
|
||||
->map(fn (array $row) => [
|
||||
'date' => $row['period'],
|
||||
'price_usd' => (float) $row['value'],
|
||||
])
|
||||
->all();
|
||||
|
||||
if (empty($rows)) {
|
||||
Log::warning('OilPriceService: no valid EIA observations returned');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $rows;
|
||||
} catch (Throwable $e) {
|
||||
Log::error('OilPriceService: fetchFromEia failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch Brent crude prices from FRED (fallback).
|
||||
* Returns mapped rows or null on any failure.
|
||||
*
|
||||
* @return array{date: string, price_usd: float}[]|null
|
||||
*/
|
||||
private function fetchFromFred(): ?array
|
||||
{
|
||||
$url = 'https://api.stlouisfed.org/fred/series/observations';
|
||||
|
||||
try {
|
||||
$response = $this->apiLogger->send('fred', 'GET', $url, fn () => Http::timeout(10)
|
||||
->get($url, [
|
||||
'series_id' => 'DCOILBRENTEU',
|
||||
'api_key' => config('services.fred.api_key'),
|
||||
'sort_order' => 'desc',
|
||||
'limit' => 30,
|
||||
'file_type' => 'json',
|
||||
]));
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::error('OilPriceService: FRED request failed', ['status' => $response->status()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$rows = collect($response->json('observations') ?? [])
|
||||
->filter(fn (array $obs) => $obs['value'] !== '.')
|
||||
->map(fn (array $obs) => [
|
||||
'date' => $obs['date'],
|
||||
'price_usd' => (float) $obs['value'],
|
||||
])
|
||||
->all();
|
||||
|
||||
if (empty($rows)) {
|
||||
Log::warning('OilPriceService: no valid FRED observations returned');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $rows;
|
||||
} catch (Throwable $e) {
|
||||
Log::error('OilPriceService: fetchFromFred failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run Pint to fix formatting**
|
||||
|
||||
```bash
|
||||
vendor/bin/pint app/Services/OilPriceService.php --format agent
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Run the tests**
|
||||
|
||||
```bash
|
||||
php artisan test --compact tests/Unit/Services/OilPriceServiceTest.php --timeout=10
|
||||
```
|
||||
|
||||
Expected: all PASS.
|
||||
|
||||
- [ ] **Step 4: Commit**
|
||||
|
||||
```bash
|
||||
git add app/Services/OilPriceService.php tests/Unit/Services/OilPriceServiceTest.php
|
||||
git commit -m "feat: use EIA as primary Brent crude source with FRED fallback"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 4: Smoke-test the live fetch
|
||||
|
||||
- [ ] **Step 1: Run the command against the live APIs**
|
||||
|
||||
```bash
|
||||
php artisan oil:predict --fetch
|
||||
```
|
||||
|
||||
Expected output: starts with "Fetching latest Brent crude prices from FRED..." (existing message — acceptable), followed by a successful prediction line. No error output.
|
||||
|
||||
- [ ] **Step 2: Verify EIA data landed in the database**
|
||||
|
||||
```bash
|
||||
php artisan tinker --execute 'echo App\Models\BrentPrice::orderBy("date","desc")->value("date");'
|
||||
```
|
||||
|
||||
Expected: a date more recent than `2026-04-02` if EIA has newer data, otherwise `2026-04-02` (EIA and FRED may both be current to the same date — that is acceptable).
|
||||
@@ -0,0 +1,97 @@
|
||||
# EIA Brent Price Source — Primary with FRED Fallback
|
||||
|
||||
**Date:** 2026-04-14
|
||||
**Status:** Approved
|
||||
|
||||
## Problem
|
||||
|
||||
FRED (`DCOILBRENTEU`) publishes Brent crude prices with a lag on top of the EIA's own publication delay. As of 2026-04-14, FRED's most recent data is 2026-04-02 — 12 days stale. EIA is the original data source; FRED mirrors it. Using EIA directly removes the mirroring lag.
|
||||
|
||||
## Goal
|
||||
|
||||
Replace FRED as the primary source for `fetchBrentPrices()` with the EIA Open Data API, keeping FRED as a fallback so the system degrades gracefully if EIA is unavailable.
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
All changes are confined to `app/Services/OilPriceService.php` and supporting config/env files.
|
||||
|
||||
### `fetchBrentPrices()` — updated flow
|
||||
|
||||
```
|
||||
1. $rows = fetchFromEia()
|
||||
2. if ($rows === null):
|
||||
Log::warning('OilPriceService: EIA fetch failed, falling back to FRED')
|
||||
$rows = fetchFromFred()
|
||||
3. if ($rows === null):
|
||||
Log::error('OilPriceService: both EIA and FRED fetch failed')
|
||||
return
|
||||
4. BrentPrice::upsert($rows, ['date'], ['price_usd'])
|
||||
```
|
||||
|
||||
### `fetchFromEia(): ?array` (new private method)
|
||||
|
||||
- Endpoint: `GET https://api.eia.gov/v2/petroleum/pri/spt/data/`
|
||||
- Params: `api_key`, `frequency=daily`, `data[0]=value`, `facets[series][]=RBRTE`, `sort[0][column]=period`, `sort[0][direction]=desc`, `length=30`
|
||||
- Returns `null` on: HTTP error, exception, empty/missing `response.data` array
|
||||
- Maps each row: `['date' => $row['period'], 'price_usd' => (float) $row['value']]`
|
||||
- Filters rows where `value` is `'.'` (EIA uses same placeholder as FRED)
|
||||
|
||||
### `fetchFromFred(): ?array` (extracted private method)
|
||||
|
||||
- Existing FRED logic moved verbatim from the current `fetchBrentPrices()` body
|
||||
- Returns `null` on: HTTP error, exception, empty observations
|
||||
- Maps each row: `['date' => $obs['date'], 'price_usd' => (float) $obs['value']]`
|
||||
|
||||
---
|
||||
|
||||
## Configuration
|
||||
|
||||
### `config/services.php`
|
||||
|
||||
Add under existing entries:
|
||||
```php
|
||||
'eia' => [
|
||||
'api_key' => env('EIA_API_KEY'),
|
||||
],
|
||||
```
|
||||
|
||||
### `.env.example`
|
||||
|
||||
Add:
|
||||
```
|
||||
EIA_API_KEY= # US EIA Open Data API key — register free at eia.gov/opendata
|
||||
```
|
||||
|
||||
FRED key stays in `.env.example` (still used as fallback).
|
||||
|
||||
---
|
||||
|
||||
## Logging
|
||||
|
||||
| Event | Level | Message |
|
||||
|---|---|---|
|
||||
| EIA fetch succeeded | — | (no log — normal path) |
|
||||
| EIA fetch failed, FRED used | `warning` | `OilPriceService: EIA fetch failed, falling back to FRED` |
|
||||
| Both failed | `error` | `OilPriceService: both EIA and FRED fetch failed` |
|
||||
| No rows after filter | `warning` | `OilPriceService: no valid EIA observations returned` (existing pattern) |
|
||||
|
||||
---
|
||||
|
||||
## Tests
|
||||
|
||||
Existing `OilPriceService` fetch tests are updated (not replaced):
|
||||
|
||||
1. **EIA succeeds** — `Http::fake()` returns valid EIA response; assert upsert called with correct rows; assert FRED endpoint never called.
|
||||
2. **EIA fails, FRED succeeds** — EIA returns 500; assert FRED endpoint called; assert upsert called with FRED rows; assert warning logged.
|
||||
3. **Both fail** — both return 500; assert upsert never called; assert error logged.
|
||||
4. **EIA returns empty data** — `response.data` is `[]`; assert falls back to FRED.
|
||||
|
||||
---
|
||||
|
||||
## Out of Scope
|
||||
|
||||
- No changes to `generatePrediction()`, `PredictOilPrices` command, or scheduler
|
||||
- No new service classes or interfaces
|
||||
- No changes to `brent_prices` schema
|
||||
120
tests/Unit/Services/BrentPriceFetcherTest.php
Normal file
120
tests/Unit/Services/BrentPriceFetcherTest.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
use App\Models\BrentPrice;
|
||||
use App\Services\ApiLogger;
|
||||
use App\Services\BrentPriceFetcher;
|
||||
use App\Services\BrentPriceSources\BrentPriceFetchException;
|
||||
use App\Services\BrentPriceSources\EiaBrentPriceSource;
|
||||
use App\Services\BrentPriceSources\FredBrentPriceSource;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function (): void {
|
||||
Http::preventStrayRequests();
|
||||
$apiLogger = new ApiLogger;
|
||||
$this->fetcher = new BrentPriceFetcher(
|
||||
new EiaBrentPriceSource($apiLogger),
|
||||
new FredBrentPriceSource($apiLogger),
|
||||
);
|
||||
});
|
||||
|
||||
it('fetches and stores brent prices from EIA', function (): void {
|
||||
Http::fake([
|
||||
'*eia.gov/*' => Http::response([
|
||||
'response' => [
|
||||
'data' => [
|
||||
['period' => '2026-04-02', 'value' => '73.80'],
|
||||
['period' => '2026-04-01', 'value' => '75.10'],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->fetcher->fetchFromEia();
|
||||
|
||||
expect(BrentPrice::count())->toBe(2)
|
||||
->and(BrentPrice::find('2026-04-02')->price_usd)->toBe('73.80');
|
||||
});
|
||||
|
||||
it('throws when EIA returns a 500', function (): void {
|
||||
Http::fake(['*eia.gov/*' => Http::response([], 500)]);
|
||||
|
||||
$this->fetcher->fetchFromEia();
|
||||
})->throws(BrentPriceFetchException::class);
|
||||
|
||||
it('throws when EIA returns empty data', function (): void {
|
||||
Http::fake(['*eia.gov/*' => Http::response(['response' => ['data' => []]])]);
|
||||
|
||||
$this->fetcher->fetchFromEia();
|
||||
})->throws(BrentPriceFetchException::class);
|
||||
|
||||
it('filters out EIA missing value markers', function (): void {
|
||||
Http::fake([
|
||||
'*eia.gov/*' => Http::response([
|
||||
'response' => [
|
||||
'data' => [
|
||||
['period' => '2026-04-01', 'value' => '75.10'],
|
||||
['period' => '2026-04-02', 'value' => '.'],
|
||||
['period' => '2026-04-03', 'value' => '74.20'],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->fetcher->fetchFromEia();
|
||||
|
||||
expect(BrentPrice::count())->toBe(2)
|
||||
->and(BrentPrice::find('2026-04-02'))->toBeNull();
|
||||
});
|
||||
|
||||
it('fetches and stores brent prices from FRED', function (): void {
|
||||
Http::fake([
|
||||
'*/fred/series/observations*' => Http::response([
|
||||
'observations' => [
|
||||
['date' => '2026-04-01', 'value' => '75.10'],
|
||||
['date' => '2026-04-02', 'value' => '73.80'],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->fetcher->fetchFromFred();
|
||||
|
||||
expect(BrentPrice::count())->toBe(2);
|
||||
});
|
||||
|
||||
it('throws when FRED fails', function (): void {
|
||||
Http::fake(['*/fred/series/observations*' => Http::response([], 500)]);
|
||||
|
||||
$this->fetcher->fetchFromFred();
|
||||
})->throws(BrentPriceFetchException::class);
|
||||
|
||||
it('filters out FRED missing value markers', function (): void {
|
||||
Http::fake([
|
||||
'*/fred/series/observations*' => Http::response([
|
||||
'observations' => [
|
||||
['date' => '2026-04-01', 'value' => '75.10'],
|
||||
['date' => '2026-04-02', 'value' => '.'],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->fetcher->fetchFromFred();
|
||||
|
||||
expect(BrentPrice::count())->toBe(1);
|
||||
});
|
||||
|
||||
it('upserts existing rows on refetch', function (): void {
|
||||
Http::fake([
|
||||
'*eia.gov/*' => Http::sequence()
|
||||
->push(['response' => ['data' => [['period' => '2026-04-01', 'value' => '74.00']]]])
|
||||
->push(['response' => ['data' => [['period' => '2026-04-01', 'value' => '75.50']]]]),
|
||||
]);
|
||||
|
||||
$this->fetcher->fetchFromEia();
|
||||
$this->fetcher->fetchFromEia();
|
||||
|
||||
expect(BrentPrice::count())->toBe(1)
|
||||
->and(BrentPrice::find('2026-04-01')->price_usd)->toBe('75.50');
|
||||
});
|
||||
144
tests/Unit/Services/BrentPricePredictorTest.php
Normal file
144
tests/Unit/Services/BrentPricePredictorTest.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\PredictionSource;
|
||||
use App\Enums\TrendDirection;
|
||||
use App\Models\BrentPrice;
|
||||
use App\Models\PricePrediction;
|
||||
use App\Services\BrentPricePredictor;
|
||||
use App\Services\LlmPrediction\OilPredictionProvider;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->provider = Mockery::mock(OilPredictionProvider::class);
|
||||
$this->predictor = new BrentPricePredictor($this->provider);
|
||||
});
|
||||
|
||||
it('detects a rising trend when 3-day EWMA exceeds 7-day EWMA by threshold', function (): void {
|
||||
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
|
||||
'date' => now()->subDays(14 - $i)->toDateString(),
|
||||
'price_usd' => 70.0 + ($i * 2.0),
|
||||
]));
|
||||
|
||||
$prediction = $this->predictor->generateEwmaPrediction($prices);
|
||||
|
||||
expect($prediction->direction)->toBe(TrendDirection::Rising)
|
||||
->and($prediction->source)->toBe(PredictionSource::Ewma)
|
||||
->and($prediction->confidence)->toBeGreaterThan(0)
|
||||
->and($prediction->confidence)->toBeLessThanOrEqual(65);
|
||||
});
|
||||
|
||||
it('detects a falling trend when 3-day EWMA falls below 7-day EWMA by threshold', function (): void {
|
||||
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
|
||||
'date' => now()->subDays(14 - $i)->toDateString(),
|
||||
'price_usd' => 85.0 - ($i * 2.0),
|
||||
]));
|
||||
|
||||
$prediction = $this->predictor->generateEwmaPrediction($prices);
|
||||
|
||||
expect($prediction->direction)->toBe(TrendDirection::Falling);
|
||||
});
|
||||
|
||||
it('returns flat when price movement is within threshold', function (): void {
|
||||
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
|
||||
'date' => now()->subDays(14 - $i)->toDateString(),
|
||||
'price_usd' => 75.0 + (($i % 2 === 0) ? 0.1 : -0.1),
|
||||
]));
|
||||
|
||||
$prediction = $this->predictor->generateEwmaPrediction($prices);
|
||||
|
||||
expect($prediction->direction)->toBe(TrendDirection::Flat)
|
||||
->and($prediction->confidence)->toBe(50);
|
||||
});
|
||||
|
||||
it('returns null when fewer than 14 prices are available for EWMA', function (): void {
|
||||
$prices = collect(range(1, 10))->map(fn (int $i) => new BrentPrice([
|
||||
'date' => now()->subDays(10 - $i)->toDateString(),
|
||||
'price_usd' => 75.0,
|
||||
]));
|
||||
|
||||
expect($this->predictor->generateEwmaPrediction($prices))->toBeNull();
|
||||
});
|
||||
|
||||
it('stores both EWMA and LLM predictions when provider succeeds', function (): void {
|
||||
seedPrices(20);
|
||||
|
||||
$this->provider->shouldReceive('predict')->once()->andReturn(new PricePrediction([
|
||||
'predicted_for' => now()->toDateString(),
|
||||
'source' => PredictionSource::LlmWithContext,
|
||||
'direction' => TrendDirection::Rising,
|
||||
'confidence' => 70,
|
||||
'reasoning' => 'Trend is up.',
|
||||
'generated_at' => now(),
|
||||
]));
|
||||
|
||||
$prediction = $this->predictor->generatePrediction();
|
||||
|
||||
expect($prediction->source)->toBe(PredictionSource::LlmWithContext)
|
||||
->and(PricePrediction::count())->toBe(2);
|
||||
});
|
||||
|
||||
it('falls back to EWMA when provider returns null', function (): void {
|
||||
seedPrices(20, slope: 0.8);
|
||||
|
||||
$this->provider->shouldReceive('predict')->once()->andReturn(null);
|
||||
|
||||
$prediction = $this->predictor->generatePrediction();
|
||||
|
||||
expect($prediction->source)->toBe(PredictionSource::Ewma)
|
||||
->and(PricePrediction::count())->toBe(1);
|
||||
});
|
||||
|
||||
it('returns null when there is insufficient price data', function (): void {
|
||||
BrentPrice::insert([
|
||||
['date' => now()->subDays(2)->toDateString(), 'price_usd' => 75.0],
|
||||
['date' => now()->subDay()->toDateString(), 'price_usd' => 76.0],
|
||||
]);
|
||||
|
||||
$this->provider->shouldNotReceive('predict');
|
||||
|
||||
expect($this->predictor->generatePrediction())->toBeNull()
|
||||
->and(PricePrediction::count())->toBe(0);
|
||||
});
|
||||
|
||||
it('flags latest brent price as prediction generated on success', function (): void {
|
||||
seedPrices(20);
|
||||
|
||||
$this->provider->shouldReceive('predict')->once()->andReturn(null);
|
||||
|
||||
$this->predictor->generatePrediction();
|
||||
|
||||
$latest = BrentPrice::orderBy('date', 'desc')->first();
|
||||
|
||||
expect($latest->prediction_generated_at)->not->toBeNull();
|
||||
});
|
||||
|
||||
it('does not flag when prediction cannot be generated', function (): void {
|
||||
BrentPrice::insert([
|
||||
['date' => now()->subDay()->toDateString(), 'price_usd' => 75.0],
|
||||
]);
|
||||
|
||||
$this->provider->shouldNotReceive('predict');
|
||||
|
||||
$this->predictor->generatePrediction();
|
||||
|
||||
expect(BrentPrice::first()->prediction_generated_at)->toBeNull();
|
||||
});
|
||||
|
||||
it('returns the latest price row', function (): void {
|
||||
seedPrices(3);
|
||||
|
||||
expect($this->predictor->latestPrice())->not->toBeNull()
|
||||
->and($this->predictor->latestPrice()->date->toDateString())->toBe(now()->toDateString());
|
||||
});
|
||||
|
||||
function seedPrices(int $count, float $slope = 1.0): void
|
||||
{
|
||||
BrentPrice::insert(
|
||||
collect(range(1, $count))->map(fn (int $i) => [
|
||||
'date' => now()->subDays($count - $i)->toDateString(),
|
||||
'price_usd' => 75.0 + ($i * $slope),
|
||||
])->all()
|
||||
);
|
||||
}
|
||||
@@ -1,195 +0,0 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\PredictionSource;
|
||||
use App\Enums\TrendDirection;
|
||||
use App\Models\BrentPrice;
|
||||
use App\Models\PricePrediction;
|
||||
use App\Services\ApiLogger;
|
||||
use App\Services\LlmPrediction\OilPredictionProvider;
|
||||
use App\Services\OilPriceService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function (): void {
|
||||
Http::preventStrayRequests();
|
||||
$this->provider = Mockery::mock(OilPredictionProvider::class);
|
||||
$this->service = new OilPriceService(new ApiLogger, $this->provider);
|
||||
});
|
||||
|
||||
// --- fetchBrentPrices ---
|
||||
|
||||
it('fetches and stores brent prices from FRED', function (): void {
|
||||
Http::fake([
|
||||
'*/fred/series/observations*' => Http::response([
|
||||
'observations' => [
|
||||
['date' => '2026-03-31', 'value' => '74.50'],
|
||||
['date' => '2026-04-01', 'value' => '75.10'],
|
||||
['date' => '2026-04-02', 'value' => '73.80'],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(3)
|
||||
->and(BrentPrice::find('2026-04-02')->price_usd)->toBe('73.80');
|
||||
});
|
||||
|
||||
it('filters out FRED missing value markers', function (): void {
|
||||
Http::fake([
|
||||
'*/fred/series/observations*' => Http::response([
|
||||
'observations' => [
|
||||
['date' => '2026-04-01', 'value' => '75.10'],
|
||||
['date' => '2026-04-02', 'value' => '.'],
|
||||
['date' => '2026-04-03', 'value' => '74.20'],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(2)
|
||||
->and(BrentPrice::find('2026-04-02'))->toBeNull();
|
||||
});
|
||||
|
||||
it('upserts existing brent price rows on refetch', function (): void {
|
||||
Http::fake([
|
||||
'*/fred/series/observations*' => Http::sequence()
|
||||
->push(['observations' => [['date' => '2026-04-01', 'value' => '74.00']]])
|
||||
->push(['observations' => [['date' => '2026-04-01', 'value' => '75.50']]]),
|
||||
]);
|
||||
|
||||
$this->service->fetchBrentPrices();
|
||||
$this->service->fetchBrentPrices();
|
||||
|
||||
expect(BrentPrice::count())->toBe(1)
|
||||
->and(BrentPrice::find('2026-04-01')->price_usd)->toBe('75.50');
|
||||
});
|
||||
|
||||
// --- generateEwmaPrediction ---
|
||||
|
||||
it('detects a rising trend when 3-day EWMA exceeds 7-day EWMA by threshold', function (): void {
|
||||
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
|
||||
'date' => now()->subDays(14 - $i)->toDateString(),
|
||||
'price_usd' => 70.0 + ($i * 2.0),
|
||||
]));
|
||||
|
||||
$prediction = $this->service->generateEwmaPrediction($prices);
|
||||
|
||||
expect($prediction->direction)->toBe(TrendDirection::Rising)
|
||||
->and($prediction->source)->toBe(PredictionSource::Ewma)
|
||||
->and($prediction->confidence)->toBeGreaterThan(0)
|
||||
->and($prediction->confidence)->toBeLessThanOrEqual(65);
|
||||
});
|
||||
|
||||
it('detects a falling trend when 3-day EWMA falls below 7-day EWMA by threshold', function (): void {
|
||||
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
|
||||
'date' => now()->subDays(14 - $i)->toDateString(),
|
||||
'price_usd' => 85.0 - ($i * 2.0),
|
||||
]));
|
||||
|
||||
$prediction = $this->service->generateEwmaPrediction($prices);
|
||||
|
||||
expect($prediction->direction)->toBe(TrendDirection::Falling)
|
||||
->and($prediction->source)->toBe(PredictionSource::Ewma);
|
||||
});
|
||||
|
||||
it('returns flat when price movement is within threshold', function (): void {
|
||||
$prices = collect(range(1, 14))->map(fn (int $i) => new BrentPrice([
|
||||
'date' => now()->subDays(14 - $i)->toDateString(),
|
||||
'price_usd' => 75.0 + (($i % 2 === 0) ? 0.1 : -0.1),
|
||||
]));
|
||||
|
||||
$prediction = $this->service->generateEwmaPrediction($prices);
|
||||
|
||||
expect($prediction->direction)->toBe(TrendDirection::Flat)
|
||||
->and($prediction->confidence)->toBe(50);
|
||||
});
|
||||
|
||||
it('returns null when fewer than 14 prices are available for EWMA', function (): void {
|
||||
$prices = collect(range(1, 10))->map(fn (int $i) => new BrentPrice([
|
||||
'date' => now()->subDays(10 - $i)->toDateString(),
|
||||
'price_usd' => 75.0,
|
||||
]));
|
||||
|
||||
expect($this->service->generateEwmaPrediction($prices))->toBeNull();
|
||||
});
|
||||
|
||||
// --- generatePrediction (orchestrator) ---
|
||||
|
||||
it('stores both EWMA and LLM predictions when provider succeeds', function (): void {
|
||||
BrentPrice::insert(
|
||||
collect(range(1, 20))->map(fn (int $i) => [
|
||||
'date' => now()->subDays(20 - $i)->toDateString(),
|
||||
'price_usd' => 75.0 + $i,
|
||||
])->all()
|
||||
);
|
||||
|
||||
$this->provider->shouldReceive('predict')->once()->andReturn(new PricePrediction([
|
||||
'predicted_for' => now()->toDateString(),
|
||||
'source' => PredictionSource::LlmWithContext,
|
||||
'direction' => TrendDirection::Rising,
|
||||
'confidence' => 70,
|
||||
'reasoning' => 'Trend is up.',
|
||||
'generated_at' => now(),
|
||||
]));
|
||||
|
||||
$prediction = $this->service->generatePrediction();
|
||||
|
||||
expect($prediction->source)->toBe(PredictionSource::LlmWithContext)
|
||||
->and(PricePrediction::count())->toBe(2);
|
||||
});
|
||||
|
||||
it('returns LLM prediction when provider succeeds', function (): void {
|
||||
BrentPrice::insert(
|
||||
collect(range(1, 20))->map(fn (int $i) => [
|
||||
'date' => now()->subDays(20 - $i)->toDateString(),
|
||||
'price_usd' => 75.0 + $i,
|
||||
])->all()
|
||||
);
|
||||
|
||||
$llmPrediction = new PricePrediction([
|
||||
'predicted_for' => now()->toDateString(),
|
||||
'source' => PredictionSource::Llm,
|
||||
'direction' => TrendDirection::Rising,
|
||||
'confidence' => 65,
|
||||
'reasoning' => 'Rising trend.',
|
||||
'generated_at' => now(),
|
||||
]);
|
||||
|
||||
$this->provider->shouldReceive('predict')->once()->andReturn($llmPrediction);
|
||||
|
||||
$prediction = $this->service->generatePrediction();
|
||||
|
||||
expect($prediction->source)->toBe(PredictionSource::Llm);
|
||||
});
|
||||
|
||||
it('falls back to EWMA when provider returns null', function (): void {
|
||||
BrentPrice::insert(
|
||||
collect(range(1, 20))->map(fn (int $i) => [
|
||||
'date' => now()->subDays(20 - $i)->toDateString(),
|
||||
'price_usd' => 75.0 + ($i * 0.8),
|
||||
])->all()
|
||||
);
|
||||
|
||||
$this->provider->shouldReceive('predict')->once()->andReturn(null);
|
||||
|
||||
$prediction = $this->service->generatePrediction();
|
||||
|
||||
expect($prediction->source)->toBe(PredictionSource::Ewma)
|
||||
->and(PricePrediction::count())->toBe(1);
|
||||
});
|
||||
|
||||
it('returns null when there is insufficient price data', function (): void {
|
||||
BrentPrice::insert([
|
||||
['date' => now()->subDays(2)->toDateString(), 'price_usd' => 75.0],
|
||||
['date' => now()->subDay()->toDateString(), 'price_usd' => 76.0],
|
||||
]);
|
||||
|
||||
$this->provider->shouldNotReceive('predict');
|
||||
|
||||
expect($this->service->generatePrediction())->toBeNull()
|
||||
->and(PricePrediction::count())->toBe(0);
|
||||
});
|
||||
Reference in New Issue
Block a user