Skip to content

Commit

Permalink
Don't count events when events array empty
Browse files Browse the repository at this point in the history
  • Loading branch information
bairhys committed Jan 13, 2024
1 parent 7be80d8 commit 33eaea1
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions prometheus_frigate_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def collect(self):
yield storage_used

# count events

events = []
try:
# change url from stats to events
events_url = self.stats_url.replace('stats', 'events')
Expand All @@ -235,36 +235,44 @@ def collect(self):
logging.error("URLError while opening Frigate events url %s: %s", self.stats_url, e)
return

if not self.previous_event_id:
# ignore all previous events on startup
self.previous_event_id = events[0]['id']
self.previous_event_start_time = int(events[0]['start_time'])

camera_events = CounterMetricFamily('frigate_camera_events', 'Count of camera events since exporter started', labels=['camera', 'label'])
if len(events) > 0:
# events[0] is newest event, last element is oldest, don't need to sort

if not self.previous_event_id:
# ignore all previous events on startup, prometheus might have already counted them
self.previous_event_id = events[0]['id']
self.previous_event_start_time = int(events[0]['start_time'])

for event in events:
# break if event already counted
if event['id'] == self.previous_event_id:
break
for event in events:
# break if event already counted
if event['id'] == self.previous_event_id:
break

# break if event starts before previous event
if event['start_time'] < self.previous_event_start_time:
break

# count event in a dict
try:
cam = self.all_events[event['camera']]
# store counted events in a dict
try:
cam[event['label']] += 1
cam = self.all_events[event['camera']]
try:
cam[event['label']] += 1
except KeyError:
# create label dict if not exists
cam.update({event['label']: 1 })
except KeyError:
# create label dict if not exists
cam.update({event['label']: 1 })
except KeyError:
# create camera and label dict if not exists
self.all_events.update({event['camera']: {event['label'] : 1} })
# create camera and label dict if not exists
self.all_events.update({event['camera']: {event['label'] : 1} })

# don't recount events next time
self.previous_event_id = events[0]['id']
self.previous_event_start_time = int(events[0]['start_time'])

camera_events = CounterMetricFamily('frigate_camera_events', 'Count of camera events since exporter started', labels=['camera', 'label'])

for camera, cam_dict in self.all_events.items():
for label, label_value in cam_dict.items():
camera_events.add_metric([camera, label], label_value)

self.previous_event_id = events[0]['id']
self.previous_event_start_time = int(events[0]['start_time'])

yield camera_events

Expand Down

0 comments on commit 33eaea1

Please sign in to comment.