From e66f47bdf69bab9c12134f151ee3a5a9d7f2d896 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Tue, 3 Sep 2024 08:49:50 -0600 Subject: [PATCH] Cleanup after recordings refactor (#13518) * Cleanup after recordings refactor * Fix default retention mode * Formatting * Fix review item query * Default review retention --- frigate/config.py | 9 +++++++-- frigate/events/external.py | 8 ++------ frigate/record/cleanup.py | 32 +++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index 20433e644..ef3375ad0 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -301,13 +301,18 @@ class RecordRetainConfig(FrigateBaseModel): mode: RetainModeEnum = Field(default=RetainModeEnum.all, title="Retain mode.") +class ReviewRetainConfig(FrigateBaseModel): + days: float = Field(default=10, title="Default retention period.") + mode: RetainModeEnum = Field(default=RetainModeEnum.motion, title="Retain mode.") + + class EventsConfig(FrigateBaseModel): pre_capture: int = Field( default=5, title="Seconds to retain before event starts.", le=MAX_PRE_CAPTURE ) post_capture: int = Field(default=5, title="Seconds to retain after event ends.") - retain: RecordRetainConfig = Field( - default_factory=RecordRetainConfig, title="Event retention settings." + retain: ReviewRetainConfig = Field( + default_factory=ReviewRetainConfig, title="Event retention settings." ) diff --git a/frigate/events/external.py b/frigate/events/external.py index 00f7cee4f..edfb757a0 100644 --- a/frigate/events/external.py +++ b/frigate/events/external.py @@ -57,11 +57,7 @@ class ExternalEventProcessor: thumbnail = self._write_images( camera_config, label, event_id, draw, snapshot_frame ) - end = ( - now + duration + camera_config.record.events.post_capture - if duration is not None - else None - ) + end = now + duration if duration is not None else None self.event_sender.publish( ( @@ -74,7 +70,7 @@ class ExternalEventProcessor: "sub_label": sub_label, "score": score, "camera": camera, - "start_time": now - camera_config.record.events.pre_capture, + "start_time": now, "end_time": end, "thumbnail": thumbnail, "has_clip": camera_config.record.enabled and include_recording, diff --git a/frigate/record/cleanup.py b/frigate/record/cleanup.py index 9f00fb50e..7703da8ea 100644 --- a/frigate/record/cleanup.py +++ b/frigate/record/cleanup.py @@ -5,9 +5,11 @@ import itertools import logging import os import threading +from functools import reduce from multiprocessing.synchronize import Event as MpEvent from pathlib import Path +from peewee import operator from playhouse.sqlite_ext import SqliteExtDatabase from frigate.config import CameraConfig, FrigateConfig, RetainModeEnum @@ -71,17 +73,26 @@ class RecordingCleanup(threading.Thread): ).timestamp() expired_reviews: ReviewSegment = ( ReviewSegment.select(ReviewSegment.id) + .where(ReviewSegment.camera == "front_cam") .where( - ReviewSegment.camera == config.name - and ( - ( - ReviewSegment.severity == "alert" - and ReviewSegment.end_time < alert_expire_date - ) - or ( - ReviewSegment.severity == "detection" - and ReviewSegment.end_time < detection_expire_date - ) + reduce( + operator.or_, + [ + reduce( + operator.and_, + [ + (ReviewSegment.severity == "alert"), + (ReviewSegment.end_time < alert_expire_date), + ], + ), + reduce( + operator.and_, + [ + (ReviewSegment.severity == "detection"), + (ReviewSegment.end_time < detection_expire_date), + ], + ), + ], ) ) .namedtuples() @@ -89,7 +100,6 @@ class RecordingCleanup(threading.Thread): max_deletes = 100000 deleted_reviews_list = list(map(lambda x: x[0], expired_reviews)) - logger.info(f"the list is {deleted_reviews_list}") for i in range(0, len(deleted_reviews_list), max_deletes): ReviewSegment.delete().where( ReviewSegment.id << deleted_reviews_list[i : i + max_deletes]