|
11 | 11 | import os |
12 | 12 | import re |
13 | 13 | from typing import List, Union, Optional |
| 14 | +from datetime import datetime |
14 | 15 | from contextlib import asynccontextmanager |
15 | 16 | from fastapi import ( |
16 | 17 | Depends, |
|
23 | 24 | Query, |
24 | 25 | Body, |
25 | 26 | ) |
| 27 | +from fastapi.encoders import jsonable_encoder |
26 | 28 | from fastapi.responses import JSONResponse, PlainTextResponse, FileResponse |
27 | 29 | from fastapi.security import OAuth2PasswordRequestForm |
28 | 30 | from fastapi_pagination import add_pagination |
@@ -452,9 +454,54 @@ def _get_eventhistory(evdict): |
452 | 454 | return evhist |
453 | 455 |
|
454 | 456 |
|
| 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 | + |
455 | 503 | # ----------------------------------------------------------------------------- |
456 | 504 | # Nodes |
457 | | - |
458 | 505 | def _get_node_event_data(operation, node, is_hierarchy=False): |
459 | 506 | return { |
460 | 507 | 'op': operation, |
|
0 commit comments