-
Sabine Schröder authoredSabine Schröder authored
timeseries.py 4.34 KiB
"""
Simple test API for timeseries management
"""
from typing import List
from fastapi import APIRouter, Depends, HTTPException, Body
from sqlalchemy.orm import Session
from . import crud, schemas
from toardb.utils.database import ToarDbSession, get_db
from toardb.generic.models import RC_enum, RS_enum
from toardb.utils.utils import get_str_from_value
from toardb.contacts.crud import get_contact
router = APIRouter()
# plain views to post and get timeseries
#get all entries of table timeseries
@router.get('/timeseries/', response_model=List[schemas.Timeseries])
def get_all_timeseries(skip: int = 0, limit: int = None, db: Session = Depends(get_db)):
return crud.get_all_timeseries(db, skip=skip, limit=limit)
#get all metadata of one timeseries (known its ID)
@router.get('/timeseries/{timeseries_id}', response_model=schemas.Timeseries)
def get_timeseries(timeseries_id: int, db: Session = Depends(get_db)):
db_timeseries = crud.get_timeseries(db, timeseries_id=timeseries_id)
if db_timeseries is None:
raise HTTPException(status_code=404, detail="Timeseries not found.")
return db_timeseries
#get all metadata of one timeseries (known its ID)
@router.get('/timeseries/id/{timeseries_id}', response_model=schemas.Timeseries)
def get_timeseries2(timeseries_id: int, db: Session = Depends(get_db)):
db_timeseries = crud.get_timeseries(db, timeseries_id=timeseries_id)
if db_timeseries is None:
raise HTTPException(status_code=404, detail="Timeseries not found.")
return db_timeseries
#get all metadata of one timeseries (known its unique label)
@router.get('/timeseries/unique/', response_model=schemas.Timeseries)
def get_timeseries(station_id: int, variable_id: int, resource_provider: str , label: str='', db: Session = Depends(get_db)):
db_timeseries = crud.get_timeseries_by_unique_constraints(db, station_id=station_id,
variable_id=variable_id, resource_provider=resource_provider, label=label)
if db_timeseries is None:
raise HTTPException(status_code=404, detail="Timeseries not found.")
return db_timeseries
@router.get('/timeseries_changelog/{timeseries_id}', response_model=List[schemas.TimeseriesChangelog])
def get_timeseries_changelog(timeseries_id: int, db: Session = Depends(get_db)):
db_changelog = crud.get_timeseries_changelog(db, timeseries_id=timeseries_id)
return db_changelog
#some more gets to be tested:
#
#
#
#problems with Roles!!! prelimarily return TimeseriesPatch (instead of Timeseries!)
@router.post('/timeseries/', response_model=schemas.TimeseriesPatch)
def create_timeseries(timeseries: schemas.TimeseriesCreate = Body(..., embed = True), db: Session = Depends(get_db)):
#to be done! resource_provider!!!
resource_provider = ''
for role in timeseries.roles:
if role.role == 'ResourceProvider':
contact = get_contact(db, contact_id=role.contact_id)
resource_provider=contact.organisation.longname
db_timeseries = crud.get_timeseries_by_unique_constraints(db, station_id=timeseries.station_id,
variable_id=timeseries.variable_id, label=timeseries.label, resource_provider=resource_provider)
if db_timeseries:
raise HTTPException(status_code=400, detail="Timeseries already registered.")
db_timeseries=crud.create_timeseries(db=db, timeseries=timeseries)
return db_timeseries
@router.patch('/timeseries/{timeseries_id}', response_model=schemas.TimeseriesPatch)
def patch_timeseries(timeseries_id: int, description: str, timeseries: schemas.TimeseriesPatch = Body(..., embed = True), db: Session = Depends(get_db)):
db_timeseries = crud.get_timeseries(db, timeseries_id=timeseries_id)
if db_timeseries is None:
raise HTTPException(status_code=404, detail="Time series for patching not found.")
return crud.patch_timeseries(db=db, description=description, timeseries_id=db_timeseries.id, timeseries=timeseries)
@router.patch('/timeseries/delete_field/{timeseries_id}', response_model=schemas.TimeseriesPatch)
def patch_timeseries(timeseries_id: int, field: str, db: Session = Depends(get_db)):
db_timeseries = crud.get_timeseries(db, timeseries_id=timeseries_id)
if db_timeseries is None:
raise HTTPException(status_code=404, detail="Time series for deleting field not found.")
return crud.delete_timeseries_field(db=db, timeseries_id=db_timeseries.id, keyword=field)