Files
pricebuddy/app/Services/Insights/StoreShowdownCalculator.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

68 lines
2.2 KiB
PHP

<?php
namespace App\Services\Insights;
use App\Dto\Insights\ListingHistory;
use App\Dto\Insights\StoreShowdownData;
use Illuminate\Support\Collection;
class StoreShowdownCalculator
{
/**
* @param Collection<int, ListingHistory> $listings
* @return Collection<int, StoreShowdownData>
*/
public function calculate(Collection $listings): Collection
{
if ($listings->isEmpty()) {
return collect();
}
$dates = collect();
foreach ($listings as $listing) {
$dates = $dates->merge($listing->history->keys());
}
$dates = $dates->unique()->values();
$total = $dates->count();
$wins = array_fill(0, $listings->count(), 0);
foreach ($dates as $date) {
$best = null;
$bestIndex = null;
foreach ($listings as $i => $listing) {
$price = $listing->history->get($date);
if ($price === null) {
continue;
}
$price = (float) $price;
if ($best === null || $price < $best) {
$best = $price;
$bestIndex = $i;
}
}
if ($bestIndex !== null) {
$wins[$bestIndex]++;
}
}
$currents = $listings->map(fn (ListingHistory $l): float => $l->history->isEmpty() ? INF : (float) $l->history->last());
$minCurrent = $currents->min();
return $listings
->map(function (ListingHistory $listing, int $i) use ($wins, $total, $minCurrent): StoreShowdownData {
$current = $listing->history->isEmpty() ? 0.0 : (float) $listing->history->last();
return new StoreShowdownData(
storeName: $listing->storeName,
currentPrice: $current,
isAvailable: ! $listing->availability->isUnavailable(),
winRate: $total > 0 ? round($wins[$i] / $total, 4) : 0.0,
isCheapestToday: $current > 0 && abs($current - $minCurrent) < 0.005,
);
})
->sortBy('currentPrice')
->values();
}
}