11import datetime as dt
22from typing import Annotated
33
4- from fastapi import APIRouter , Depends , Query
4+ from fastapi import APIRouter , Depends , Form , Query
5+ from fastapi .responses import StreamingResponse
56from geoalchemy2 import WKTElement
7+ import pandas as pd
68from sqlalchemy .orm import Session
79
810from aross_stations_db .api .dependencies import get_db_session
911from aross_stations_db .api .v1 .output import (
1012 TimeseriesJsonElement ,
1113 timeseries_query_results_to_json ,
14+ timeseries_query_results_to_bar_plot_buffer ,
1215)
1316from aross_stations_db .db .query import timeseries_query
1417
15- router = APIRouter ()
18+ from loguru import logger
1619
20+ router = APIRouter ()
1721
1822@router .get ("/monthly" )
1923def get_monthly_timeseries (
@@ -22,8 +26,58 @@ def get_monthly_timeseries(
2226 start : Annotated [dt .datetime , Query (description = "ISO-format timestamp" )],
2327 end : Annotated [dt .datetime , Query (description = "ISO-format timestamp" )],
2428 polygon : Annotated [str | None , WKTElement , Query (description = "WKT shape" )] = None ,
29+ stations : Annotated [list [str ], Query (description = "List of station identifiers" )] = [],
2530) -> list [TimeseriesJsonElement ]:
2631 """Get a monthly timeseries of events matching query parameters."""
27- query = timeseries_query (db = db , start = start , end = end , polygon = polygon )
32+ logger .debug (f"STATIONS: { stations } " )
33+ query = timeseries_query (db = db , start = start , end = end , polygon = polygon , stations = stations )
2834
2935 return timeseries_query_results_to_json (query .all ())
36+
37+
38+ @router .post ("/monthly" )
39+ def post_monthly_timeseries (
40+ db : Annotated [Session , Depends (get_db_session )],
41+ * ,
42+ start : Annotated [dt .datetime , Form (description = "ISO-format timestamp" )],
43+ end : Annotated [dt .datetime , Form (description = "ISO-format timestamp" )],
44+ polygon : Annotated [str | None , WKTElement , Form (description = "WKT shape" )] = None ,
45+ stations : Annotated [list [str ], Form (description = "List of station identifiers" )] = [],
46+ ) -> list [TimeseriesJsonElement ]:
47+ """Get a monthly timeseries of events matching query parameters."""
48+ logger .debug (f"STATIONS: { stations } " )
49+ query = timeseries_query (db = db , start = start , end = end , polygon = polygon , stations = stations )
50+
51+ return timeseries_query_results_to_json (query .all ())
52+
53+
54+ @router .get ("/monthly/png" )
55+ def get_monthly_timeseries_png (
56+ db : Annotated [Session , Depends (get_db_session )],
57+ * ,
58+ start : Annotated [dt .datetime , Query (description = "ISO-format timestamp" )],
59+ end : Annotated [dt .datetime , Query (description = "ISO-format timestamp" )],
60+ polygon : Annotated [str | None , WKTElement , Query (description = "WKT shape" )] = None ,
61+ stations : Annotated [list [str ], Query (Description = "List of station identifiers" )] = [],
62+ ) -> StreamingResponse :
63+ """Get a monthly timeseries image plot of events matching query parameters."""
64+ query = timeseries_query (db = db , start = start , end = end , polygon = polygon , stations = stations )
65+ buffer = timeseries_query_results_to_bar_plot_buffer (query , start , end , 'png' )
66+
67+ return StreamingResponse (buffer , media_type = "image/png" )
68+
69+
70+ @router .post ("/monthly/png" )
71+ def post_monthly_timeseries_png (
72+ db : Annotated [Session , Depends (get_db_session )],
73+ * ,
74+ start : Annotated [dt .datetime , Form (description = "ISO-format timestamp" )],
75+ end : Annotated [dt .datetime , Form (description = "ISO-format timestamp" )],
76+ polygon : Annotated [str | None , WKTElement , Form (description = "WKT shape" )] = None ,
77+ stations : Annotated [list [str ], Form (Description = "List of station identifiers" )] = [],
78+ ) -> StreamingResponse :
79+ """Get a monthly timeseries image plot of events matching query parameters."""
80+ query = timeseries_query (db = db , start = start , end = end , polygon = polygon , stations = stations )
81+ buffer = timeseries_query_results_to_bar_plot_buffer (query , start , end , 'png' )
82+
83+ return StreamingResponse (buffer , media_type = "image/png" )
0 commit comments