Base emergency cleanup on the streams a camera is currently recording (#24022)

* gate emergency cleanup bandwidth on the streams a camera currently records

* settle bandwidth samples per stream instead of per camera

* fix mypy
This commit is contained in:
Josh Hawkins
2026-09-12 07:30:04 -06:00
committed by Nicolas Mowen
parent 8fe35ace3b
commit 3f20209339
2 changed files with 212 additions and 55 deletions
+101 -3
View File
@@ -36,7 +36,10 @@ class TestHttp(unittest.TestCase):
"front_door": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
{
"path": "rtsp://10.0.0.1:554/video",
"roles": ["detect", "record"],
}
]
},
"detect": {
@@ -44,6 +47,7 @@ class TestHttp(unittest.TestCase):
"width": 1920,
"fps": 5,
},
"record": {"enabled": True},
}
},
}
@@ -53,7 +57,10 @@ class TestHttp(unittest.TestCase):
"front_door": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
{
"path": "rtsp://10.0.0.1:554/video",
"roles": ["detect", "record"],
}
]
},
"detect": {
@@ -61,11 +68,15 @@ class TestHttp(unittest.TestCase):
"width": 1920,
"fps": 5,
},
"record": {"enabled": True},
},
"back_door": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.2:554/video", "roles": ["detect"]}
{
"path": "rtsp://10.0.0.2:554/video",
"roles": ["detect", "record"],
}
]
},
"detect": {
@@ -73,6 +84,7 @@ class TestHttp(unittest.TestCase):
"width": 1920,
"fps": 5,
},
"record": {"enabled": True},
},
},
}
@@ -326,6 +338,92 @@ class TestHttp(unittest.TestCase):
assert streams[STREAM_TYPE_SUB]["bandwidth"] is None
assert streams[STREAM_TYPE_MAIN]["bandwidth"] == 7200
def test_expected_bandwidth_follows_recording_config(self):
"""Only the streams a camera currently records count toward cleanup."""
config = FrigateConfig(**self.minimal_config)
storage = StorageMaintainer(config, MagicMock())
record = config.cameras["front_door"].record
time_keep = datetime.datetime.now().timestamp()
_insert_mock_recording(
"1234567.frontdoor",
os.path.join(self.test_dir, "main.tmp"),
time_keep,
time_keep + 10,
seg_size=20,
seg_dur=10,
)
_insert_mock_recording(
"1234568.frontdoor",
os.path.join(self.test_dir, "sub.tmp"),
time_keep,
time_keep + 10,
seg_size=2,
seg_dur=10,
stream_type=STREAM_TYPE_SUB,
)
storage.calculate_camera_bandwidth()
# sub segments are still on disk but the camera no longer records them
assert storage.expected_hourly_bandwidth() == 7200
record.sub.enabled = True
assert storage.expected_hourly_bandwidth() == 7920
record.enabled = False
assert storage.expected_hourly_bandwidth() == 0
def test_stream_stays_dirty_until_it_has_its_own_samples(self):
"""A stream enabled before its first segment keeps the camera dirty.
The record config update can be handled on a tick before ffmpeg has
written anything, so a camera-wide segment count would settle the camera
on the strength of the other stream's history and never measure the new
stream at all.
"""
config = FrigateConfig(**self.minimal_config)
storage = StorageMaintainer(config, MagicMock())
record = config.cameras["front_door"].record
stats = storage.camera_storage_stats
time_keep = datetime.datetime.now().timestamp()
for i in range(60):
_insert_mock_recording(
f"main_{i}.frontdoor",
os.path.join(self.test_dir, f"main_{i}.tmp"),
time_keep + i * 10,
time_keep + i * 10 + 10,
seg_size=20,
seg_dur=10,
)
storage.calculate_camera_bandwidth()
assert stats["front_door"]["needs_refresh"] is False
record.sub.enabled = True
stats["front_door"]["needs_refresh"] = True
storage.calculate_camera_bandwidth()
assert stats["front_door"]["needs_refresh"] is True
assert storage.expected_hourly_bandwidth() == 7200
for i in range(60):
_insert_mock_recording(
f"sub_{i}.frontdoor",
os.path.join(self.test_dir, f"sub_{i}.tmp"),
time_keep + 5000 + i * 10,
time_keep + 5000 + i * 10 + 10,
seg_size=2,
seg_dur=10,
stream_type=STREAM_TYPE_SUB,
)
storage.calculate_camera_bandwidth()
assert stats["front_door"]["needs_refresh"] is False
assert storage.expected_hourly_bandwidth() == 7920
def test_camera_usages_with_no_recordings(self):
"""A camera with no segments reports zero usage and no streams."""
config = FrigateConfig(**self.minimal_config)