Audit items #17 and #21. #17 — DayOfWeekSignal and StickinessSignal each had their own isSqlite ternary picking between SQLite (strftime/julianday) and MySQL (DAYOFWEEK/DATEDIFF) date expressions. Centralised in App\Services\Prediction\Signals\DbDialect. #21 — ProfileValidationRules was a trait with one consumer (CreateNewUser); inlined the rules into the action and deleted the trait. Also dropped PasswordValidationRules::currentPasswordRules() which was unused. PasswordValidationRules trait stays (two consumers). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
51 lines
1.7 KiB
PHP
51 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Prediction\Signals;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class StickinessSignal extends AbstractSignal
|
|
{
|
|
public function compute(SignalContext $context): array
|
|
{
|
|
$diffExpr = DbDialect::maxMinDayDiffExpr('price_effective_at');
|
|
|
|
$rows = DB::table('station_prices')
|
|
->where('fuel_type', $context->fuelType->value)
|
|
->where('price_effective_at', '>=', now()->subDays(30))
|
|
->selectRaw("station_id, COUNT(*) as changes, {$diffExpr} as span_days")
|
|
->groupBy('station_id')
|
|
->having('changes', '>', 1)
|
|
->having('span_days', '>', 0)
|
|
->get();
|
|
|
|
if ($rows->count() < 10) {
|
|
return $this->disabledSignal('Insufficient stickiness data (need 10+ stations with price history)');
|
|
}
|
|
|
|
$avgHoldDays = $rows->avg(fn ($r) => $r->span_days / ($r->changes - 1));
|
|
$avgHoldDays = round((float) $avgHoldDays, 1);
|
|
|
|
$score = match (true) {
|
|
$avgHoldDays < 2 => -0.1,
|
|
$avgHoldDays > 5 => 0.1,
|
|
default => 0.0,
|
|
};
|
|
|
|
$detail = match (true) {
|
|
$avgHoldDays < 2 => "Volatile prices (avg hold: {$avgHoldDays} days) — harder to predict.",
|
|
$avgHoldDays > 5 => "Sticky prices (avg hold: {$avgHoldDays} days) — more predictable.",
|
|
default => "Normal hold period (avg: {$avgHoldDays} days).",
|
|
};
|
|
|
|
return [
|
|
'score' => $score,
|
|
'confidence' => min(1.0, $rows->count() / 200),
|
|
'direction' => 'stable',
|
|
'detail' => $detail,
|
|
'data_points' => $rows->count(),
|
|
'enabled' => true,
|
|
];
|
|
}
|
|
}
|