From cb39b639974f860c57db716d252fabbc2ea6dcf2 Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Mon, 11 May 2026 17:31:41 +0000 Subject: [PATCH] fix: drastically reduce false-positive consumption anomaly banners MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two changes: 1. Skip prediction when expected_qty=0 — model says 'should be finished' but user simply restocked or consumed less. Not actionable. 2. Raise 'more than expected' threshold to 400% (was 30%). Having more than expected almost always means a restock the model doesn't know about yet — only truly extreme cases (>4x) are flagged. 'Less than expected' stays at 30% (still actionable: unregistered use). --- api/index.php | 14 +++++++++++++- index.html | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/api/index.php b/api/index.php index c511728..194e892 100644 --- a/api/index.php +++ b/api/index.php @@ -2166,9 +2166,21 @@ function getConsumptionPredictions(PDO $db): void { // Flag if deviation > 30% and absolute diff > meaningful threshold $deviation = abs($actualQty - $expectedQty); $threshold = max($dailyRate * 3, 0.5); // at least 3 days worth or 0.5 units + + // If expected = 0 and actual > 0, the model simply thinks the product + // should have been used up by now. This is NOT an anomaly — the user + // either restocked (not yet tracked) or consumed less than usual. + // Only flag "less" direction when expected = 0 (actual ran out faster). + if ($expectedQty <= 0 && $actualQty >= 0) continue; + $pctDev = $expectedQty > 0 ? ($deviation / $expectedQty) : ($actualQty > 0 ? 1 : 0); - if ($pctDev > 0.30 && $deviation > $threshold) { + // "more than expected" is almost always a restock the model doesn't know about yet. + // Only flag it at very high deviation (>400%) to catch truly impossible values. + // "less than expected" is more actionable: user may have consumed without registering. + $flagThreshold = ($actualQty > $expectedQty) ? 4.0 : 0.30; + + if ($pctDev > $flagThreshold && $deviation > $threshold) { $unit = $item['unit']; // Format expected/actual in human units if ($unit === 'conf' && $item['default_quantity'] > 0 && $item['package_unit']) { diff --git a/index.html b/index.html index b3c5ad5..e3566fe 100644 --- a/index.html +++ b/index.html @@ -11,7 +11,7 @@ EverShelf - + @@ -1469,6 +1469,6 @@ - +