Skip to content

Commit b14cf1f

Browse files
committed
feat(main.py): Add events endpoint
Add initial implementation of events endpoint Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent d467990 commit b14cf1f

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

api/main.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import re
1313
from typing import List, Union, Optional
14+
from datetime import datetime
1415
from contextlib import asynccontextmanager
1516
from fastapi import (
1617
Depends,
@@ -23,6 +24,7 @@
2324
Query,
2425
Body,
2526
)
27+
from fastapi.encoders import jsonable_encoder
2628
from fastapi.responses import JSONResponse, PlainTextResponse, FileResponse
2729
from fastapi.security import OAuth2PasswordRequestForm
2830
from fastapi_pagination import add_pagination
@@ -452,9 +454,54 @@ def _get_eventhistory(evdict):
452454
return evhist
453455

454456

457+
# TBD: Restrict response by Pydantic model
458+
@app.get('/events')
459+
async def get_events(request: Request):
460+
"""Get all the events if no request parameters have passed.
461+
Format: [{event1}, {event2}, ...] or if recursive is set to true,
462+
then we add to each event the node information.
463+
Get all the matching events otherwise.
464+
Query parameters can be used to filter the events:
465+
- limit: Number of events to return
466+
- from: Start timestamp (unix epoch) to filter events
467+
- kind: Event kind to filter events
468+
- state: Event state to filter events
469+
- recursive: Retrieve node together with event
470+
This API endpoint is under development and may change in future.
471+
"""
472+
metrics.add('http_requests_total', 1)
473+
query_params = dict(request.query_params)
474+
recursive = query_params.pop('recursive', None)
475+
limit = query_params.pop('limit', None)
476+
kind = query_params.pop('kind', None)
477+
state = query_params.pop('state', None)
478+
from_ts = query_params.pop('from', None)
479+
if from_ts:
480+
if isinstance(from_ts, str):
481+
from_ts = datetime.fromisoformat(from_ts)
482+
query_params['timestamp'] = {'$gt': from_ts}
483+
if kind:
484+
query_params['data.kind'] = kind
485+
if state:
486+
query_params['data.state'] = state
487+
if limit:
488+
query_params['limit'] = int(limit)
489+
resp = await db.find_by_attributes_nonpaginated(EventHistory, query_params)
490+
resp_list = []
491+
for item in resp:
492+
item['id'] = str(item['_id'])
493+
item.pop('_id')
494+
if recursive:
495+
node = await db.find_by_id(Node, item['data']['id'])
496+
if node:
497+
item['node'] = node
498+
resp_list.append(item)
499+
json_comp = jsonable_encoder(resp_list)
500+
return JSONResponse(content=json_comp)
501+
502+
455503
# -----------------------------------------------------------------------------
456504
# Nodes
457-
458505
def _get_node_event_data(operation, node, is_hierarchy=False):
459506
return {
460507
'op': operation,

0 commit comments

Comments
 (0)