refactor: extract 6 prediction signals into Signal classes
The 803-line NationalFuelPredictionService had six private compute*Signal methods, a private linearRegression helper, and a private disabledSignal shape factory all crammed together. Each signal is now an independently testable class. - App\Services\Prediction\Signals\Signal — interface - App\Services\Prediction\Signals\SignalContext — input value object (FuelType + optional lat/lng + hasCoordinates() helper) - App\Services\Prediction\Signals\AbstractSignal — shared disabledSignal() and linearRegression() helpers - TrendSignal, DayOfWeekSignal, BrandBehaviourSignal, StickinessSignal, RegionalMomentumSignal, OilSignal — one class each, extending AbstractSignal NationalFuelPredictionService receives the 6 signal classes via constructor injection and orchestrates them. The lat/lng null-guard for regional momentum now lives inside RegionalMomentumSignal::compute() so the coordinator no longer branches on coordinate presence. Aggregation, weekly summary, and reasoning helpers stay in the service for now — they are coupled to the public predict() output shape and are candidates for a follow-up extraction once a stable API is locked in. Service: 803 → 414 lines. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
61
app/Services/Prediction/Signals/BrandBehaviourSignal.php
Normal file
61
app/Services/Prediction/Signals/BrandBehaviourSignal.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Prediction\Signals;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
final class BrandBehaviourSignal extends AbstractSignal
|
||||
{
|
||||
public function compute(SignalContext $context): array
|
||||
{
|
||||
$rows = DB::table('station_prices')
|
||||
->join('stations', 'station_prices.station_id', '=', 'stations.node_id')
|
||||
->where('station_prices.fuel_type', $context->fuelType->value)
|
||||
->where('station_prices.price_effective_at', '>=', now()->subDays(7))
|
||||
->selectRaw('stations.is_supermarket, DATE(station_prices.price_effective_at) as day, AVG(station_prices.price_pence) as avg_price')
|
||||
->groupBy('stations.is_supermarket', 'day')
|
||||
->orderBy('day')
|
||||
->get();
|
||||
|
||||
$supermarket = $rows->where('is_supermarket', 1)->values();
|
||||
$major = $rows->where('is_supermarket', 0)->values();
|
||||
|
||||
if ($supermarket->count() < 2 || $major->count() < 2) {
|
||||
return $this->disabledSignal('Insufficient brand data for comparison');
|
||||
}
|
||||
|
||||
$supermarketSlope = $this->linearRegression($supermarket->pluck('avg_price')->map(fn ($v) => (float) $v / 100)->values()->all())['slope'];
|
||||
$majorSlope = $this->linearRegression($major->pluck('avg_price')->map(fn ($v) => (float) $v / 100)->values()->all())['slope'];
|
||||
|
||||
$divergence = round(abs($supermarketSlope - $majorSlope) * 7, 1);
|
||||
$supermarketChange = round($supermarketSlope * 7, 1);
|
||||
$majorChange = round($majorSlope * 7, 1);
|
||||
|
||||
if ($divergence < 1.0) {
|
||||
return [
|
||||
'score' => 0.0,
|
||||
'confidence' => 0.5,
|
||||
'direction' => 'stable',
|
||||
'detail' => 'Supermarkets and majors moving in sync.',
|
||||
'data_points' => $rows->count(),
|
||||
'enabled' => true,
|
||||
];
|
||||
}
|
||||
|
||||
$leaderChange = abs($supermarketChange) > abs($majorChange) ? $supermarketChange : $majorChange;
|
||||
$direction = $leaderChange > 0 ? 'up' : 'down';
|
||||
$leader = abs($supermarketChange) > abs($majorChange) ? 'Supermarkets' : 'Majors';
|
||||
$follower = $leader === 'Supermarkets' ? 'majors' : 'supermarkets';
|
||||
$leaderAbs = abs($leaderChange);
|
||||
$followerChange = $leader === 'Supermarkets' ? abs($majorChange) : abs($supermarketChange);
|
||||
|
||||
return [
|
||||
'score' => $direction === 'up' ? 1.0 : -1.0,
|
||||
'confidence' => min(1.0, $divergence / 5.0),
|
||||
'direction' => $direction,
|
||||
'detail' => "{$leader} ".($leaderChange > 0 ? 'rose' : 'fell')." {$leaderAbs}p vs {$follower} {$followerChange}p (divergence: {$divergence}p). Expect {$follower} to follow.",
|
||||
'data_points' => $rows->count(),
|
||||
'enabled' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user