check every overlapping review item before dropping a recording (#24728)

Recording retention around review items applied only the first review item, in start order, whose pre and post capture window covered a segment. When an alert and a detection were close together, a quiet segment covered by both was dropped by the detection's motion mode without checking the alert's all mode. The loop also stopped at a review item that started too late to reach the segment, even when a later alert with a longer pre_capture did reach it. Both the recording maintainer and cleanup now check every review item whose window covers the segment and keep it if any of their retain modes would.
This commit is contained in:
Josh Hawkins
2026-10-23 06:22:47 -06:00
committed by GitHub
parent f385065981
commit a85526187e
4 changed files with 298 additions and 40 deletions
+27 -23
View File
@@ -198,52 +198,56 @@ class RecordingCleanup(threading.Thread):
deleted_recordings = set()
kept_recordings: list[tuple[float, float]] = []
for recording in recordings:
keep = False
mode = None
# Now look for a reason to keep this recording segment
modes: set[RetainModeEnum] = set()
# Now look for every reason to keep this recording segment
for idx in range(review_start, len(reviews)):
review = reviews[idx]
severity = review.severity
pre_capture = config.record.get_review_pre_capture(severity)
post_capture = config.record.get_review_post_capture(severity)
# if the review starts in the future, stop checking reviews
# and let this recording segment expire
if review.start_time - pre_capture > recording.end_time:
keep = False
# stop once no later review's pre capture can reach this segment
if (
review.start_time - config.record.event_pre_capture
> recording.end_time
):
break
# if the review is in progress or ends after the recording starts, keep it
# and stop looking at reviews
if review.start_time - pre_capture > recording.end_time:
continue
# collect every review that is in progress or ends after the
# recording starts, since their windows can overlap
if (
review.end_time is None
or review.end_time + post_capture >= recording.start_time
):
keep = True
mode = (
modes.add(
alerts_retain_mode
if review.severity == "alert"
else detections_retain_mode
)
break
# if the review ends before this recording segment starts, skip
# this review and check the next review for an overlap.
# since the review and recordings are sorted, we can skip review
# that end before the previous recording segment started on future segments
if review.end_time + post_capture < recording.start_time:
# it on future segments too. reviews and recordings are sorted,
# but an earlier review with a longer post capture may still
# overlap, so only skip while nothing has overlapped yet
elif not modes:
review_start = idx
# Delete recordings outside of the retention window or based on the retention mode
if (
not keep
# Delete recordings unless an overlapping review's retention mode keeps them
if not any(
mode == RetainModeEnum.all
or (
mode == RetainModeEnum.motion
and recording.motion == 0
and recording.objects == 0
and recording.dBFS == 0
and (
recording.motion != 0
or recording.objects != 0
or recording.dBFS != 0
)
)
or (mode == RetainModeEnum.active_objects and recording.objects == 0)
or (mode == RetainModeEnum.active_objects and recording.objects != 0)
for mode in modes
):
recording_path = Path(recording.path)
recording_path.unlink(missing_ok=True)
+18 -17
View File
@@ -759,39 +759,40 @@ class RecordingMaintainer(threading.Thread):
# we fell through the continuous / motion check, so we need to check the review items
# if the cached segment overlaps with the review items:
overlaps = False
modes: set[RetainModeEnum] = set()
for review in reviews:
severity = SeverityEnum[review.severity]
# if the review item starts in the future, stop checking review items
# and remove this segment
# stop once no later review item's pre capture can reach this segment
if (
review.start_time - record_config.event_pre_capture
) > end_time.timestamp():
break
if (
review.start_time - record_config.get_review_pre_capture(severity)
) > end_time.timestamp():
overlaps = False
break
continue
# if the review item is in progress or ends after the recording starts, keep it
# and stop looking at review items
# collect every review item that is in progress or ends after the
# recording starts, since their windows can overlap
if (
review.end_time is None
or (review.end_time + record_config.get_review_post_capture(severity))
>= start_time.timestamp()
):
overlaps = True
break
if overlaps:
record_mode = (
alerts_retain_mode
if review.severity == "alert"
else detections_retain_mode
)
modes.add(
alerts_retain_mode
if review.severity == "alert"
else detections_retain_mode
)
if modes:
if segment_stats is None:
segment_stats = self.segment_stats(camera, start_time, end_time)
if not segment_stats.should_discard_segment(record_mode):
# keep the segment if any overlapping review item's mode keeps it
if any(not segment_stats.should_discard_segment(m) for m in modes):
# move from cache to recordings immediately
return await self.move_segment(
camera,