Skip to content

Commit 0c7dd5d

Browse files
committed
events: Add filtering by id and ids
Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent 6c21b82 commit 0c7dd5d

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

api/main.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ async def get_events(request: Request):
435435
- kind: Event kind to filter events
436436
- state: Event state to filter events
437437
- result: Event result to filter events
438+
- id / ids: Event document id(s) to filter events
438439
- recursive: Retrieve node together with event
439440
This API endpoint is under development and may change in future.
440441
"""
@@ -446,6 +447,39 @@ async def get_events(request: Request):
446447
state = query_params.pop('state', None)
447448
result = query_params.pop('result', None)
448449
from_ts = query_params.pop('from', None)
450+
# Support filtering by MongoDB _id.
451+
# Accept `id=<hex>` for a single id or `ids=a,b,c` for multiple ids.
452+
# Using `id` as query param is safe here because we remove it from the
453+
# filter before passing to Mongo.
454+
event_id = query_params.pop('id', None)
455+
event_ids = query_params.pop('ids', None)
456+
if event_id and event_ids:
457+
raise HTTPException(
458+
status_code=status.HTTP_400_BAD_REQUEST,
459+
detail="Provide either id or ids, not both"
460+
)
461+
if event_id:
462+
try:
463+
query_params['_id'] = ObjectId(event_id)
464+
except (errors.InvalidId, TypeError) as exc:
465+
raise HTTPException(
466+
status_code=status.HTTP_400_BAD_REQUEST,
467+
detail="Invalid id format"
468+
) from exc
469+
elif event_ids:
470+
try:
471+
ids_list = [ObjectId(x.strip()) for x in event_ids.split(',') if x.strip()]
472+
except (errors.InvalidId, TypeError) as exc:
473+
raise HTTPException(
474+
status_code=status.HTTP_400_BAD_REQUEST,
475+
detail="Invalid ids format"
476+
) from exc
477+
if not ids_list:
478+
raise HTTPException(
479+
status_code=status.HTTP_400_BAD_REQUEST,
480+
detail="ids must contain at least one id"
481+
)
482+
query_params['_id'] = {'$in': ids_list}
449483
if from_ts:
450484
if isinstance(from_ts, str):
451485
from_ts = datetime.fromisoformat(from_ts)

doc/events-migration.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ each event document but do not affect existing queries:
102102
Existing queries filtering on `timestamp`, `data.kind`, `data.state`, etc.
103103
continue to work unchanged.
104104

105+
Supported query parameters for `/events`:
106+
107+
| Parameter | Type | Description |
108+
|-----------|------|-------------|
109+
| `from` | string (ISO timestamp) | Return events with `timestamp` greater than this value. |
110+
| `kind` | string | Filter by `data.kind` (e.g., `job`, `node`). |
111+
| `state` | string | Filter by `data.state`. |
112+
| `result` | string | Filter by `data.result`. |
113+
| `limit` | integer | Maximum number of events to return. |
114+
| `recursive` | bool | Attach related node info to each event (max `limit` 1000). |
115+
| `id` | string (Mongo ObjectId) | Filter by a single event document id. |
116+
| `ids` | string (comma‑separated ObjectIds) | Filter by multiple event document ids. |
117+
105118
## Subscriber ID Guidelines
106119

107120
The `subscriber_id` should be:

0 commit comments

Comments
 (0)