feat: use EIA as primary Brent crude source with FRED fallback
This commit is contained in:
@@ -40,9 +40,84 @@ class OilPriceService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Fetch the last 30 days of Brent crude prices from FRED and store them.
|
||||
* 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';
|
||||
|
||||
@@ -59,11 +134,11 @@ class OilPriceService
|
||||
if (! $response->successful()) {
|
||||
Log::error('OilPriceService: FRED request failed', ['status' => $response->status()]);
|
||||
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$rows = collect($response->json('observations') ?? [])
|
||||
->filter(fn (array $obs) => $obs['value'] !== '.') // FRED uses '.' for missing data
|
||||
->filter(fn (array $obs) => $obs['value'] !== '.')
|
||||
->map(fn (array $obs) => [
|
||||
'date' => $obs['date'],
|
||||
'price_usd' => (float) $obs['value'],
|
||||
@@ -73,12 +148,14 @@ class OilPriceService
|
||||
if (empty($rows)) {
|
||||
Log::warning('OilPriceService: no valid FRED observations returned');
|
||||
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
BrentPrice::upsert($rows, ['date'], ['price_usd']);
|
||||
return $rows;
|
||||
} catch (Throwable $e) {
|
||||
Log::error('OilPriceService: fetchBrentPrices failed', ['error' => $e->getMessage()]);
|
||||
Log::error('OilPriceService: fetchFromFred failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user