Insert recordings as bulk instead of individually.

This commit is contained in:
Nick Mowen 2023-07-20 11:09:24 -06:00
parent 5966414691
commit e759fe4a55

View File

@ -125,6 +125,7 @@ class RecordingMaintainer(threading.Thread):
self.end_time_cache.pop(cache_path, None) self.end_time_cache.pop(cache_path, None)
grouped_recordings[camera] = grouped_recordings[camera][-keep_count:] grouped_recordings[camera] = grouped_recordings[camera][-keep_count:]
tasks = []
for camera, recordings in grouped_recordings.items(): for camera, recordings in grouped_recordings.items():
# clear out all the object recording info for old frames # clear out all the object recording info for old frames
while ( while (
@ -155,10 +156,13 @@ class RecordingMaintainer(threading.Thread):
.order_by(Event.start_time) .order_by(Event.start_time)
) )
await asyncio.gather( tasks.extend(
*(self.validate_and_move_segment(camera, events, r) for r in recordings) [self.validate_and_move_segment(camera, events, r) for r in recordings]
) )
recordings_to_insert: list[Optional[Recordings]] = await asyncio.gather(*tasks)
Recordings.insert_many([r for r in recordings_to_insert if r is not None])
async def validate_and_move_segment( async def validate_and_move_segment(
self, camera: str, events: Event, recording: dict[str, any] self, camera: str, events: Event, recording: dict[str, any]
) -> None: ) -> None:
@ -225,7 +229,7 @@ class RecordingMaintainer(threading.Thread):
if overlaps: if overlaps:
record_mode = self.config.cameras[camera].record.events.retain.mode record_mode = self.config.cameras[camera].record.events.retain.mode
# move from cache to recordings immediately # move from cache to recordings immediately
self.store_segment( return self.move_segment(
camera, camera,
start_time, start_time,
end_time, end_time,
@ -247,7 +251,7 @@ class RecordingMaintainer(threading.Thread):
# else retain days includes this segment # else retain days includes this segment
else: else:
record_mode = self.config.cameras[camera].record.retain.mode record_mode = self.config.cameras[camera].record.retain.mode
self.store_segment( return self.move_segment(
camera, start_time, end_time, duration, cache_path, record_mode camera, start_time, end_time, duration, cache_path, record_mode
) )
@ -290,7 +294,7 @@ class RecordingMaintainer(threading.Thread):
return SegmentInfo(motion_count, active_count, round(average_dBFS)) return SegmentInfo(motion_count, active_count, round(average_dBFS))
def store_segment( def move_segment(
self, self,
camera: str, camera: str,
start_time: datetime.datetime, start_time: datetime.datetime,
@ -298,7 +302,7 @@ class RecordingMaintainer(threading.Thread):
duration: float, duration: float,
cache_path: str, cache_path: str,
store_mode: RetainModeEnum, store_mode: RetainModeEnum,
) -> None: ) -> Optional[Recordings]:
segment_info = self.segment_stats(camera, start_time, end_time) segment_info = self.segment_stats(camera, start_time, end_time)
# check if the segment shouldn't be stored # check if the segment shouldn't be stored
@ -348,7 +352,7 @@ class RecordingMaintainer(threading.Thread):
if p.returncode != 0: if p.returncode != 0:
logger.error(f"Unable to convert {cache_path} to {file_path}") logger.error(f"Unable to convert {cache_path} to {file_path}")
logger.error(p.stderr) logger.error(p.stderr)
return return None
else: else:
logger.debug( logger.debug(
f"Copied {file_path} in {datetime.datetime.now().timestamp()-start_frame} seconds." f"Copied {file_path} in {datetime.datetime.now().timestamp()-start_frame} seconds."
@ -368,19 +372,20 @@ class RecordingMaintainer(threading.Thread):
rand_id = "".join( rand_id = "".join(
random.choices(string.ascii_lowercase + string.digits, k=6) random.choices(string.ascii_lowercase + string.digits, k=6)
) )
Recordings.create(
id=f"{start_time.timestamp()}-{rand_id}", return {
camera=camera, Recordings.id: f"{start_time.timestamp()}-{rand_id}",
path=file_path, Recordings.camera: camera,
start_time=start_time.timestamp(), Recordings.path: file_path,
end_time=end_time.timestamp(), Recordings.start_time: start_time.timestamp(),
duration=duration, Recordings.end_time: end_time.timestamp(),
motion=segment_info.motion_box_count, Recordings.duration: duration,
Recordings.motion: segment_info.motion_box_count,
# TODO: update this to store list of active objects at some point # TODO: update this to store list of active objects at some point
objects=segment_info.active_object_count, Recordings.objects: segment_info.active_object_count,
dBFS=segment_info.average_dBFS, Recordings.dBFS: segment_info.average_dBFS,
segment_size=segment_size, Recordings.segment_size: segment_size,
) }
except Exception as e: except Exception as e:
logger.error(f"Unable to store recording segment {cache_path}") logger.error(f"Unable to store recording segment {cache_path}")
Path(cache_path).unlink(missing_ok=True) Path(cache_path).unlink(missing_ok=True)
@ -388,10 +393,11 @@ class RecordingMaintainer(threading.Thread):
# clear end_time cache # clear end_time cache
self.end_time_cache.pop(cache_path, None) self.end_time_cache.pop(cache_path, None)
return None
def run(self) -> None: def run(self) -> None:
# Check for new files every 5 seconds # Check for new files every 5 seconds
wait_time = 5.0 wait_time = 0.0
while not self.stop_event.wait(wait_time): while not self.stop_event.wait(wait_time):
run_start = datetime.datetime.now().timestamp() run_start = datetime.datetime.now().timestamp()