Skip to content

Commit

Permalink
purge duplicate events during cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeblackshear committed Feb 20, 2021
1 parent ce90ae3 commit 165ca8f
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions frigate/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,38 @@ def expire(self, media):
Event.label == l.label)
)
update_query.execute()

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(f"{os.path.join(CLIPS_DIR, media_name)}.jpg")
media.unlink(missing_ok=True)
if event.has_clip:
media = Path(f"{os.path.join(CLIPS_DIR, media_name)}.mp4")
media.unlink(missing_ok=True)

(Event.delete()
.where( Event.id << [event.id for event in duplicate_events] )
.execute())

def run(self):
counter = 0
Expand All @@ -319,15 +351,16 @@ def run(self):
logger.info(f"Exiting event cleanup...")
break

# only expire events every 10 minutes, but check for stop events every 10 seconds
# only expire events every 5 minutes, but check for stop events every 10 seconds
time.sleep(10)
counter = counter + 1
if counter < 60:
if counter < 30:
continue
counter = 0

self.expire('clips')
self.expire('snapshots')
self.purge_duplicates()

# drop events from db where has_clip and has_snapshot are false
delete_query = (
Expand Down

0 comments on commit 165ca8f

Please sign in to comment.