diff --git a/frigate/http.py b/frigate/http.py index 297b2f2e5..295c4ae67 100644 --- a/frigate/http.py +++ b/frigate/http.py @@ -1584,20 +1584,10 @@ def recordings_summary(camera_name): ) .where(Recordings.camera == camera_name) .group_by( - fn.strftime( - "%Y-%m-%d %H", - fn.datetime( - Recordings.start_time, "unixepoch", hour_modifier, minute_modifier - ), - ) + Recordings.start_time.cast("int") / 3600 ) .order_by( - fn.strftime( - "%Y-%m-%d H", - fn.datetime( - Recordings.start_time, "unixepoch", hour_modifier, minute_modifier - ), - ).desc() + Recordings.start_time.desc() ) .namedtuples() ) @@ -1614,12 +1604,7 @@ def recordings_summary(camera_name): ) .where(Event.camera == camera_name, Event.has_clip) .group_by( - fn.strftime( - "%Y-%m-%d %H", - fn.datetime( - Event.start_time, "unixepoch", hour_modifier, minute_modifier - ), - ), + Event.start_time.cast("int") / 3600 ) .namedtuples() ) diff --git a/frigate/storage.py b/frigate/storage.py index e21ba92c3..d7be876e2 100644 --- a/frigate/storage.py +++ b/frigate/storage.py @@ -35,7 +35,7 @@ class StorageMaintainer(threading.Thread): if self.camera_storage_stats.get(camera, {}).get("needs_refresh", True): self.camera_storage_stats[camera] = { "needs_refresh": ( - Recordings.select(fn.COUNT(Recordings.id)) + Recordings.select(fn.COUNT("*")) .where(Recordings.camera == camera, Recordings.segment_size > 0) .scalar() < 50 diff --git a/migrations/020_update_index_recordings.py b/migrations/020_update_index_recordings.py new file mode 100644 index 000000000..9d28ccea3 --- /dev/null +++ b/migrations/020_update_index_recordings.py @@ -0,0 +1,44 @@ +"""Peewee migrations -- 011_update_indexes.py. + +Some examples (model - class or model name):: + + > Model = migrator.orm['model_name'] # Return model in current state by name + + > migrator.sql(sql) # Run custom SQL + > migrator.python(func, *args, **kwargs) # Run python code + > migrator.create_model(Model) # Create a model (could be used as decorator) + > migrator.remove_model(model, cascade=True) # Remove a model + > migrator.add_fields(model, **fields) # Add fields to a model + > migrator.change_fields(model, **fields) # Change fields + > migrator.remove_fields(model, *field_names, cascade=True) + > migrator.rename_field(model, old_field_name, new_field_name) + > migrator.rename_table(model, new_table_name) + > migrator.add_index(model, *col_names, unique=False) + > migrator.drop_index(model, *col_names) + > migrator.add_not_null(model, *field_names) + > migrator.drop_not_null(model, *field_names) + > migrator.add_default(model, field_name, default) + +""" +import peewee as pw + +SQL = pw.SQL + + +def migrate(migrator, database, fake=False, **kwargs): + migrator.sql( + 'DROP INDEX recordings_end_time_start_time' + ) + migrator.sql( + 'CREATE INDEX "recordings_camera_start_time_end_time" ON "recordings" ("camera", "start_time" DESC, "end_time" DESC)' + ) + migrator.sql( + 'CREATE INDEX "recordings_api_recordings_summary" ON "recordings" ("camera", "start_time" DESC, "duration", "motion", "objects")' + ) + migrator.sql( + 'CREATE INDEX "recordings_start_time" ON "recordings" ("start_time")' + ) + + +def rollback(migrator, database, fake=False, **kwargs): + pass