diff --git a/api/core/mean.py b/api/core/mean.py index aa16d67c0d51e02ae0260c657b589d58c710df14..3e45d9c122fff0410ec7f0bddb48c3338a41ca0e 100644 --- a/api/core/mean.py +++ b/api/core/mean.py @@ -56,7 +56,7 @@ class Mean: Params = None @staticmethod - def Aggregate(cursor: any, meanType: MeanType, ids: tuple, fromTime: str, toTime: str, coverage: int = 75, verificationFlag: int = 3, fraction: int = 10, addMetadata: bool = False, useInvalidValues: bool = True): + def Aggregate(cursor: any, meanType: MeanType, ids: tuple, fromTime: str, toTime: str, coverage: int = 75, verificationFlag: int = 3, fraction: int = 10, addMetadata: bool = False, useInvalidValues: bool = False): sql = "" params = {"ids": ids, "fromTime": fromTime, "toTime": toTime, "verificationFlag": verificationFlag, "coverage": coverage, "fraction": fraction, "useInvalidValues": useInvalidValues} diff --git a/api/endpoints/aggregation/routes.py b/api/endpoints/aggregation/routes.py new file mode 100644 index 0000000000000000000000000000000000000000..561f44b04869c9af2825e853cc4748bb0c7aa712 --- /dev/null +++ b/api/endpoints/aggregation/routes.py @@ -0,0 +1,40 @@ +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 +from core.mean import Mean, MeanType + + +router = APIRouter() + + +class FilterModel(BaseModel): + verification: Optional[int] = 3 + coverage: Optional[int] = 75 + meantype: MeanType + station: str + pollutant: str + fromtime: datetime + totime: datetime + + +@router.get("/aggregation/", tags=["aggregation"]) +async def aggregation(filter_model: FilterModel = Depends()): + with CursorFromPool() as cursor: + sql = """ + select sp.id + from sampling_points sp, eea_pollutants p, stations s + where 1=1 + and sp.station_id = s.id + and sp.pollutant = p.uri + and sp.private = false + 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}) + rows = cursor.fetchall() + sp_ids = tuple([f["id"] for f in rows]) + + return Mean.Aggregate(cursor, filter_model.meantype, sp_ids, filter_model.fromtime, filter_model.totime, filter_model.coverage, filter_model.verification, 3, True) diff --git a/api/endpoints/aqi/historical/routes.py b/api/endpoints/aqi/historical/routes.py index ab45530f83d0cccad70a3b4d1b274b3479e53df2..2f555936965792d43f2ccf9a43984b022dd706d8 100644 --- a/api/endpoints/aqi/historical/routes.py +++ b/api/endpoints/aqi/historical/routes.py @@ -71,7 +71,8 @@ async def aqi(filter_model: FilterModel = Depends()): "pollutant": mv["component"], "unit": mv["unit"], "value": mv["value"], - "meantype": MeanType(mv["meantype"]).name, + "meantype": mv["meantype"], + "meantype_name": MeanType(mv["meantype"]).name, "datetime": mv["datetime"], "aqi": idx, "lng": mv["lng"], diff --git a/api/endpoints/aqi/utd/routes.py b/api/endpoints/aqi/utd/routes.py index c6d29736643471f01d91b68969f18603f5adaf6a..aadb41b969c7046dfad34b569a00edf4705b4e0a 100644 --- a/api/endpoints/aqi/utd/routes.py +++ b/api/endpoints/aqi/utd/routes.py @@ -73,7 +73,8 @@ async def aqi(filter_model: FilterModel = Depends()): "pollutant": mv["component"], "unit": mv["unit"], "value": mv["value"], - "meantype": MeanType(mv["meantype"]).name, + "meantype": mv["meantype"], + "meantype_name": MeanType(mv["meantype"]).name, "datetime": mv["datetime"], "aqi": idx, "lng": mv["lng"], diff --git a/api/endpoints/endpoints.py b/api/endpoints/endpoints.py index 6127cf71a875c23ef13dc83dbc1e689a4159d7d9..9febdff9b25c83f86ef15bea50dd498082978503 100644 --- a/api/endpoints/endpoints.py +++ b/api/endpoints/endpoints.py @@ -1,7 +1,9 @@ from endpoints.aqi.utd.routes import router as aqi_utd_router from endpoints.aqi.historical.routes import router as aqi_historical_router -from endpoints.aqi.meta.routes import router as aqi_meta_router +from endpoints.lookups.aqi.routes import router as lookups_aqi_router +from endpoints.lookups.meantype.routes import router as lookups_meantype_router from endpoints.version.routes import router as version_router +from endpoints.aggregation.routes import router as aggregation_router class Endpoints: @@ -9,5 +11,7 @@ class Endpoints: def init_app(app): app.include_router(aqi_utd_router) app.include_router(aqi_historical_router) - app.include_router(aqi_meta_router) + app.include_router(lookups_aqi_router) + app.include_router(lookups_meantype_router) app.include_router(version_router) + app.include_router(aggregation_router) diff --git a/api/endpoints/aqi/meta/routes.py b/api/endpoints/lookups/aqi/routes.py similarity index 87% rename from api/endpoints/aqi/meta/routes.py rename to api/endpoints/lookups/aqi/routes.py index 8cebaedf78c7f723b0d144e40cefd3611168051c..37d25c007ed4a2d9d5234ca2420b66501589fcd0 100644 --- a/api/endpoints/aqi/meta/routes.py +++ b/api/endpoints/lookups/aqi/routes.py @@ -15,11 +15,11 @@ class FilterModel(BaseModel): pollutant: Optional[str] = None -@router.get("/aqi/meta/", tags=["aqi"]) +@router.get("/lookup/aqi/", tags=["lookup"]) async def aqi(filter_model: FilterModel = Depends()): with CursorFromPool() as cursor: sql = """ - select p.notation as pollutant, i.meantype as meantype_id, i.index as aqi, i.range_from, i.range_to + select p.notation as pollutant, i.meantype as meantype, i.index as aqi, i.range_from, i.range_to from aqi i, eea_pollutants p where i.pollutant = p.uri """ @@ -33,5 +33,5 @@ async def aqi(filter_model: FilterModel = Depends()): cursor.execute(sql, {"meantype": filter_model.meantype, "pollutant": filter_model.pollutant}) meta = cursor.fetchall() for o in meta: - o["meantype"] = MeanType(o["meantype_id"]).name + o["meantype_name"] = MeanType(o["meantype"]).name return meta diff --git a/api/endpoints/lookups/meantype/routes.py b/api/endpoints/lookups/meantype/routes.py new file mode 100644 index 0000000000000000000000000000000000000000..cfb953780e6bbcf5bb15e69fee224112f6b5214a --- /dev/null +++ b/api/endpoints/lookups/meantype/routes.py @@ -0,0 +1,12 @@ +from fastapi import APIRouter +from core.mean import Mean, MeanType + +router = APIRouter() + + +@router.get("/lookup/meantype", tags=["lookup"]) +async def version(): + meantypes = [] + for m in MeanType: + meantypes.append({"meantype": m, "meantype_name": m.name}) + return meantypes diff --git a/api/endpoints/version/routes.py b/api/endpoints/version/routes.py index 3cac3506bca019e0c49d3fef5e39cddb94ad8654..1f6c7d717c052bda8b23c5c63f7e26799837debe 100644 --- a/api/endpoints/version/routes.py +++ b/api/endpoints/version/routes.py @@ -6,4 +6,4 @@ router = APIRouter() @router.get("/version", tags=["version"]) async def version(): - return {"version": "1.0.0"} + return {"version": "1.0.2"}