From e957f8d9f9db977ac67430e9296d22c9c427b508 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Mon, 1 Dec 2025 13:43:44 -0700 Subject: [PATCH] Fix incorrect averaging of the segments so it correctly only uses the most recent segments --- frigate/storage.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/frigate/storage.py b/frigate/storage.py index ee11cf7a9..feabe06ff 100644 --- a/frigate/storage.py +++ b/frigate/storage.py @@ -5,7 +5,7 @@ import shutil import threading from pathlib import Path -from peewee import fn +from peewee import SQL, fn from frigate.config import FrigateConfig from frigate.const import RECORD_DIR @@ -44,13 +44,19 @@ class StorageMaintainer(threading.Thread): ) } - # calculate MB/hr + # calculate MB/hr from last 100 segments try: - bandwidth = round( - Recordings.select(fn.AVG(bandwidth_equation)) + # Subquery to get last 100 segments, then average their bandwidth + last_100 = ( + Recordings.select(bandwidth_equation.alias("bw")) .where(Recordings.camera == camera, Recordings.segment_size > 0) + .order_by(Recordings.start_time.desc()) .limit(100) - .scalar() + .alias("recent") + ) + + bandwidth = round( + Recordings.select(fn.AVG(SQL("bw"))).from_(last_100).scalar() * 3600, 2, )