Fix: Add limit to event query (#7108)

* Add limit to event query for fetching latest event with specified label and camera name

* Refactor the label_thumbnail function in frigate/http.py to simplify the event_query logic and improve code readability
This commit is contained in:
Sergey Krashevich 2023-07-14 14:30:47 +03:00 committed by GitHub
parent 5c12762cb5
commit 6ac36e8436
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -593,24 +593,14 @@ def timeline():
@bp.route("/<camera_name>/<label>/thumbnail.jpg") @bp.route("/<camera_name>/<label>/thumbnail.jpg")
def label_thumbnail(camera_name, label): def label_thumbnail(camera_name, label):
label = unquote(label) label = unquote(label)
if label == "any": event_query = Event.select(fn.MAX(Event.id)).where(Event.camera == camera_name)
event_query = ( if label != "any":
Event.select() event_query = event_query.where(Event.label == label)
.where(Event.camera == camera_name)
.order_by(Event.start_time.desc())
)
else:
event_query = (
Event.select()
.where(Event.camera == camera_name)
.where(Event.label == label)
.order_by(Event.start_time.desc())
)
try: try:
event = event_query.get() event = event_query.scalar()
return event_thumbnail(event.id, 60) return event_thumbnail(event, 60)
except DoesNotExist: except DoesNotExist:
frame = np.zeros((175, 175, 3), np.uint8) frame = np.zeros((175, 175, 3), np.uint8)
ret, jpg = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 70]) ret, jpg = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 70])