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/AbstractSignal.php
Normal file
61
app/Services/Prediction/Signals/AbstractSignal.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Prediction\Signals;
|
||||
|
||||
abstract class AbstractSignal implements Signal
|
||||
{
|
||||
/** @return array{score: 0.0, confidence: 0.0, direction: 'stable', detail: string, data_points: 0, enabled: false} */
|
||||
protected function disabledSignal(string $detail): array
|
||||
{
|
||||
return [
|
||||
'score' => 0.0,
|
||||
'confidence' => 0.0,
|
||||
'direction' => 'stable',
|
||||
'detail' => $detail,
|
||||
'data_points' => 0,
|
||||
'enabled' => false,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Least-squares linear regression. x = array index, y = value.
|
||||
*
|
||||
* @param float[] $values
|
||||
* @return array{slope: float, r_squared: float}
|
||||
*/
|
||||
protected function linearRegression(array $values): array
|
||||
{
|
||||
$n = count($values);
|
||||
|
||||
if ($n < 2) {
|
||||
return ['slope' => 0.0, 'r_squared' => 0.0];
|
||||
}
|
||||
|
||||
$xMean = ($n - 1) / 2.0;
|
||||
$yMean = array_sum($values) / $n;
|
||||
|
||||
$numerator = 0.0;
|
||||
$denominator = 0.0;
|
||||
|
||||
foreach ($values as $i => $y) {
|
||||
$x = $i - $xMean;
|
||||
$numerator += $x * ($y - $yMean);
|
||||
$denominator += $x * $x;
|
||||
}
|
||||
|
||||
$slope = $denominator > 0.0 ? $numerator / $denominator : 0.0;
|
||||
|
||||
$ssRes = 0.0;
|
||||
$ssTot = 0.0;
|
||||
|
||||
foreach ($values as $i => $y) {
|
||||
$predicted = $yMean + $slope * ($i - $xMean);
|
||||
$ssRes += ($y - $predicted) ** 2;
|
||||
$ssTot += ($y - $yMean) ** 2;
|
||||
}
|
||||
|
||||
$rSquared = $ssTot > 0.0 ? max(0.0, 1.0 - ($ssRes / $ssTot)) : 0.0;
|
||||
|
||||
return ['slope' => $slope, 'r_squared' => $rSquared];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user