Fix incorrect averaging of the segments so it correctly only uses the most recent segments

This commit is contained in:
Nicolas Mowen 2025-12-01 13:43:44 -07:00
parent cac29f96e7
commit e957f8d9f9

View File

@ -5,7 +5,7 @@ import shutil
import threading import threading
from pathlib import Path from pathlib import Path
from peewee import fn from peewee import SQL, fn
from frigate.config import FrigateConfig from frigate.config import FrigateConfig
from frigate.const import RECORD_DIR 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: try:
bandwidth = round( # Subquery to get last 100 segments, then average their bandwidth
Recordings.select(fn.AVG(bandwidth_equation)) last_100 = (
Recordings.select(bandwidth_equation.alias("bw"))
.where(Recordings.camera == camera, Recordings.segment_size > 0) .where(Recordings.camera == camera, Recordings.segment_size > 0)
.order_by(Recordings.start_time.desc())
.limit(100) .limit(100)
.scalar() .alias("recent")
)
bandwidth = round(
Recordings.select(fn.AVG(SQL("bw"))).from_(last_100).scalar()
* 3600, * 3600,
2, 2,
) )