Window dashboard Money Saved to 30-day average

This commit is contained in:
prosolis
2026-06-20 11:57:54 -07:00
parent 2fd4019103
commit ed03494edf
5 changed files with 55 additions and 4 deletions

View File

@@ -1040,11 +1040,16 @@ func (s *Store) GetDashboardStats(ctx context.Context) (*DashboardStats, error)
`).Scan(&d.UnpricedCount); err != nil {
return nil, err
}
// "Money saved" compares each item's current best price against its recent
// market average. The average is windowed to the last 30 days so stale
// historical prices don't anchor the baseline; items whose only price
// history predates the window simply don't contribute an estimate.
rows, err := s.DB.QueryContext(ctx, `
SELECT i.best_price, AVG(p.price) AS avg_price
FROM items i
JOIN price_history p ON p.item_id = i.id
WHERE i.active = 1 AND i.best_price IS NOT NULL
AND p.polled_at >= datetime('now', '-30 day')
GROUP BY i.id
`)
if err != nil {