Skip to content
Snippets Groups Projects

added observation endpoint

Merged Christoffer Stoll requested to merge development into master
4 files
+ 130
1
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 61
0
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from typing import Optional
from core.database import CursorFromPool
from itertools import groupby
from datetime import timedelta, datetime
router = APIRouter()
class FilterModel(BaseModel):
station: str
pollutant: str
fromtime: datetime
totime: datetime
@router.get("/obs/historical/", tags=["obs"])
async def aqi(filter_model: FilterModel = Depends()):
with CursorFromPool() as cursor:
# Get observations
sql = """
select
sp.id as sampling_point_id,
s.name as station,
p.notation as pollutant,
u.notation as unit,
st_x(s.geom) as lng,
st_y(s.geom) as lat,
t.timestep,
o.begin_position as from_time,
o.end_position as to_time,
o.value,
o.verification_flag,
o.validation_flag
from
sampling_points sp,
eea_pollutants p,
eea_times t,
stations s,
eea_concentrations u,
observations o
where 1=1
and sp.id = o.sampling_point_id
and sp.station_id = s.id
and sp.pollutant = p.uri
and sp.concentration = u.id
and sp.timestep = t.id
and t.timestep <= 86400
and t.timestep > 1
and sp.private = false
and o.from_time >= %(fromTime)s
and o.from_time < %(toTime)s
and lower(s.name) = lower(%(sta)s)
and lower(p.notation) = lower(%(pollutant)s)
"""
cursor.execute(sql, {"sta": filter_model.station, "pollutant": filter_model.pollutant, "fromTime": filter_model.fromtime, "toTime": filter_model.totime})
return cursor.fetchall()
Loading