From 9fc93c72a065301a37df770a82d63cbe0c87bdb9 Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Sat, 21 Oct 2023 11:53:33 -0400 Subject: [PATCH] more consistent use of iterators in select queries (#8258) --- frigate/events/cleanup.py | 42 ++++++++++++++++++++++-------------- frigate/http.py | 13 +++++++---- frigate/record/cleanup.py | 23 +++++++++++++------- frigate/record/maintainer.py | 2 ++ frigate/storage.py | 41 ++++++++++++++++++++++------------- 5 files changed, 78 insertions(+), 43 deletions(-) diff --git a/frigate/events/cleanup.py b/frigate/events/cleanup.py index 1f1fb5f76..faba73979 100644 --- a/frigate/events/cleanup.py +++ b/frigate/events/cleanup.py @@ -83,14 +83,19 @@ class EventCleanup(threading.Thread): datetime.datetime.now() - datetime.timedelta(days=expire_days) ).timestamp() # grab all events after specific time - expired_events = Event.select( - Event.id, - Event.camera, - ).where( - Event.camera.not_in(self.camera_keys), - Event.start_time < expire_after, - Event.label == event.label, - Event.retain_indefinitely == False, + expired_events = ( + Event.select( + Event.id, + Event.camera, + ) + .where( + Event.camera.not_in(self.camera_keys), + Event.start_time < expire_after, + Event.label == event.label, + Event.retain_indefinitely == False, + ) + .namedtuples() + .iterator() ) # delete the media from disk for event in expired_events: @@ -136,14 +141,19 @@ class EventCleanup(threading.Thread): datetime.datetime.now() - datetime.timedelta(days=expire_days) ).timestamp() # grab all events after specific time - expired_events = Event.select( - Event.id, - Event.camera, - ).where( - Event.camera == name, - Event.start_time < expire_after, - Event.label == event.label, - Event.retain_indefinitely == False, + expired_events = ( + Event.select( + Event.id, + Event.camera, + ) + .where( + Event.camera == name, + Event.start_time < expire_after, + Event.label == event.label, + Event.retain_indefinitely == False, + ) + .namedtuples() + .iterator() ) # delete the grabbed clips from disk diff --git a/frigate/http.py b/frigate/http.py index 42d9c1610..0061d338f 100644 --- a/frigate/http.py +++ b/frigate/http.py @@ -958,9 +958,10 @@ def events(): .order_by(Event.start_time.desc()) .limit(limit) .dicts() + .iterator() ) - return jsonify([e for e in events]) + return jsonify(list(events)) @bp.route("/events//