From 60dd3dc7b1be915e44aedd2b06a35aec0fa4f236 Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko Date: Wed, 30 Oct 2024 13:11:56 +0200 Subject: [PATCH] feat(main.py): Add events endpoint Add initial implementation of events endpoint Signed-off-by: Denys Fedoryshchenko --- api/main.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/api/main.py b/api/main.py index d6994d19..9cb9f1bb 100644 --- a/api/main.py +++ b/api/main.py @@ -54,6 +54,7 @@ UserUpdate, UserGroup, ) +import traceback @asynccontextmanager @@ -166,6 +167,19 @@ async def invalid_id_exception_handler( ) +@app.exception_handler(Exception) +async def server_error(request: Request, error: Exception): + """ + Global exception handler for all exceptions + """ + print(f"Internal server error: {error} \n{traceback.format_exc()}") + return JSONResponse( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content={"detail": "Internal server error (exception)"}, + ) + + + @app.get('/') async def root(): """Root endpoint handler""" @@ -498,9 +512,44 @@ def _get_eventhistory(evdict): return evhist +# TBD: Restrict response by Pydantic model +@app.get('/events') +async def get_events(request: Request): + """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) + print(query_params) + resp = await db.find_by_attributes_nonpaginated(EventHistory, query_params) + print(resp) + 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,