def purge_duplicates(self): duplicate_query = """with grouped_events as ( select id, label, camera, has_snapshot, has_clip, row_number() over ( partition by label, camera, round(start_time/5,0)*5 order by end_time-start_time desc ) as copy_number from event ) select distinct id, camera, has_snapshot, has_clip from grouped_events where copy_number > 1;""" duplicate_events = Event.raw(duplicate_query) for event in duplicate_events: logger.debug(f"Removing duplicate: {event.id}") media_name = f"{event.camera}-{event.id}" if event.has_snapshot: media_path = Path(f"{os.path.join(CLIPS_DIR, media_name)}.jpg") media_path.unlink(missing_ok=True) if event.has_clip: media_path = Path(f"{os.path.join(CLIPS_DIR, media_name)}.mp4") media_path.unlink(missing_ok=True) (Event.delete().where( Event.id << [event.id for event in duplicate_events]).execute())
def run(self): # only expire events every 5 minutes while not self.stop_event.wait(300): self.expire("clips") self.expire("snapshots") self.purge_duplicates() # drop events from db where has_clip and has_snapshot are false delete_query = Event.delete().where(Event.has_clip == False, Event.has_snapshot == False) delete_query.execute() logger.info(f"Exiting event cleanup...")
def run(self): counter = 0 while (True): if self.stop_event.is_set(): logger.info(f"Exiting event cleanup...") break # only expire events every 10 minutes, but check for stop events every 10 seconds time.sleep(10) counter = counter + 1 if counter < 60: continue counter = 0 self.expire('clips') self.expire('snapshots') # drop events from db where has_clip and has_snapshot are false delete_query = (Event.delete().where(Event.has_clip == False, Event.has_snapshot == False)) delete_query.execute()