api/tracking: simple data model and route for querying tracks
This commit is contained in:
parent
8ee3c8d015
commit
d3850cce2e
2 changed files with 22 additions and 4 deletions
|
|
@ -2,22 +2,39 @@
|
||||||
|
|
||||||
"""basic (generic) routes and views for tracks (records)"""
|
"""basic (generic) routes and views for tracks (records)"""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
import transaction
|
import transaction
|
||||||
from typing import Annotated
|
from typing import Annotated, Any, Self
|
||||||
|
|
||||||
from scopes.api import deps
|
from scopes.api import deps
|
||||||
from scopes.storage import tracking
|
from scopes.storage import tracking
|
||||||
from scopes.storage.common import Storage
|
from scopes.storage.common import Storage
|
||||||
|
|
||||||
|
class Track(BaseModel):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create(cls, obj: Any) -> Self:
|
||||||
|
return cls(**obj.asDict())
|
||||||
|
|
||||||
|
uid: str = None
|
||||||
|
head: dict[str, str]
|
||||||
|
data: dict[str, Any]
|
||||||
|
timeStamp: datetime = None
|
||||||
|
|
||||||
|
# routes
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.get('/tracks')
|
type Storage = Annotated[Storage, Depends(deps.storage)]
|
||||||
async def get_tracks(storage: Annotated[Storage, Depends(deps.storage)]):
|
|
||||||
|
@router.get('/tracks', response_model=list[Track])
|
||||||
|
async def get_tracks(storage: Storage):
|
||||||
tracks = storage.create(tracking.Container)
|
tracks = storage.create(tracking.Container)
|
||||||
result = [tr.asDict() for tr in tracks.query()]
|
result = [tr.asDict() for tr in tracks.query()]
|
||||||
#result = [Track(tr) for tr in tracks.query()]
|
#result = [Track.create(tr) for tr in tracks.query()]
|
||||||
transaction.commit()
|
transaction.commit()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ def test_tracking(storage, caplog):
|
||||||
tracks = clt.get('/tracks').json()
|
tracks = clt.get('/tracks').json()
|
||||||
assert len(tracks) == 1
|
assert len(tracks) == 1
|
||||||
tr = tracks[0]
|
tr = tracks[0]
|
||||||
|
assert tr['timeStamp'].startswith('202')
|
||||||
tr['timeStamp'] = '...'
|
tr['timeStamp'] = '...'
|
||||||
assert tr == {'uid': 'rec-1', 'head': {'taskId': 't01', 'userName': 'john'},
|
assert tr == {'uid': 'rec-1', 'head': {'taskId': 't01', 'userName': 'john'},
|
||||||
'data': {'activity': 'testing'}, 'timeStamp': '...'}
|
'data': {'activity': 'testing'}, 'timeStamp': '...'}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue