mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
Add a Product Insights tab backed by a suite of price-analysis calculators and a redesigned price history chart. - Insights calculators: listing-history and daily best-price series builders, price stats, percentile, deal score, distribution, drop events, store showdown, seasonality, availability timeline, and target tracker - ProductInsights aggregator service composing the calculators - Price history chart: best-price and target datasets, best-price line honours the unit/retail filter, horizontal scroll on mobile - Rename the History tab to Insights with consistent heading styling - Feature and unit test coverage, plus review-feedback fixes
39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Insights;
|
|
|
|
use App\Dto\Insights\TargetTrackerData;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class TargetTrackerCalculator
|
|
{
|
|
/**
|
|
* @param Collection<string, float> $series
|
|
*/
|
|
public function calculate(Collection $series, float $current, ?float $target): ?TargetTrackerData
|
|
{
|
|
if ($target === null || $target <= 0 || $series->isEmpty()) {
|
|
return null;
|
|
}
|
|
|
|
$high = (float) $series->max();
|
|
$range = $high - $target;
|
|
|
|
$progress = $range > 0
|
|
? (int) round(max(0, min(1, ($high - $current) / $range)) * 100)
|
|
: ($current <= $target ? 100 : 0);
|
|
|
|
$hits = $series->filter(fn ($v): bool => (float) $v <= $target);
|
|
|
|
return new TargetTrackerData(
|
|
target: $target,
|
|
current: $current,
|
|
gap: round($current - $target, 2),
|
|
progressPercent: $progress,
|
|
hitCount: $hits->count(),
|
|
hitPercent: (int) round($hits->count() / $series->count() * 100),
|
|
lastHitDate: $hits->keys()->last(),
|
|
);
|
|
}
|
|
}
|