Skip to content

Commit

Permalink
feat(main.py): Add events endpoint
Browse files Browse the repository at this point in the history
Add initial implementation of events endpoint

Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
  • Loading branch information
nuclearcat committed Oct 30, 2024
1 parent 7c0b59a commit fefa528
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,43 @@ def _get_eventhistory(evdict):
return evhist


# TBD: Restrict response by Pydantic model
@app.get('/events')
async def get_events(request: Request) -> JSONResponse:
"""Get all the events if no request parameters have passed.
Format: [{event1}, {event2}, ...] or if recursive is set to true,
then we add to each event the node information.
Get all the matching events otherwise.
Query parameters can be used to filter the events:
- limit: Number of events to return
- from: Start timestamp (unix epoch) to filter events
- kind: Event kind to filter events
- recursive: Retrieve node together with event
This API endpoint is under development and may change in future.
"""
metrics.add('http_requests_total', 1)
query_params = dict(request.query_params)
recursive = query_params.pop('recursive', None)
limit = query_params.pop('limit', None)
kind = query_params.pop('kind', None)
from_ts = query_params.pop('from', None)
if from_ts:
query_params['timestamp'] = {'$gte': int(from_ts)}
if kind:
query_params['data.kind'] = kind
if limit:
query_params['limit'] = int(limit)
resp = await db.find_by_attributes(EventHistory, query_params)
if recursive:
for item in resp.items:
node = await db.find_by_id(Node, item['data']['id'])
item['node'] = node

return JSONResponse(content=resp)


# -----------------------------------------------------------------------------
# Nodes

def _get_node_event_data(operation, node, is_hierarchy=False):
return {
'op': operation,
Expand Down

0 comments on commit fefa528

Please sign in to comment.