Files
pricebuddy/app/Services/Insights/DistributionCalculator.php
Jeremy d1a8f1d009 feat: product insights tab
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
2026-06-18 23:13:14 +10:00

54 lines
1.5 KiB
PHP

<?php
namespace App\Services\Insights;
use App\Dto\Insights\DistributionData;
use Illuminate\Support\Collection;
class DistributionCalculator
{
/**
* @param Collection<string, float> $series
*/
public function calculate(Collection $series, float $current, int $binCount = 8): DistributionData
{
if ($series->isEmpty()) {
return new DistributionData([], 0);
}
$low = (float) $series->min();
$high = (float) $series->max();
$total = $series->count();
if ($high <= $low) {
return new DistributionData(
[['min' => $low, 'max' => $high, 'count' => $total, 'isCurrent' => true]],
100,
);
}
$width = ($high - $low) / $binCount;
$bins = [];
for ($i = 0; $i < $binCount; $i++) {
$bins[$i] = [
'min' => round($low + $i * $width, 2),
'max' => round($low + ($i + 1) * $width, 2),
'count' => 0,
'isCurrent' => false,
];
}
$index = fn (float $value): int => max(0, min($binCount - 1, (int) floor(($value - $low) / $width)));
foreach ($series as $value) {
$bins[$index((float) $value)]['count']++;
}
$currentIndex = $index($current);
$bins[$currentIndex]['isCurrent'] = true;
$percent = (int) round($bins[$currentIndex]['count'] / $total * 100);
return new DistributionData(array_values($bins), $percent);
}
}