From e3a88bfeeb5ca2672d711fa2f8c40dc0462c65ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Thu, 21 Nov 2024 15:29:10 +0000 Subject: [PATCH] Handle the case where the query returns no transparency data --- src/lvmapi/routers/transparency.py | 2 +- src/lvmapi/tools/transparency.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lvmapi/routers/transparency.py b/src/lvmapi/routers/transparency.py index 2ea6aca..2279f0a 100644 --- a/src/lvmapi/routers/transparency.py +++ b/src/lvmapi/routers/transparency.py @@ -47,7 +47,7 @@ class TransparencyData(BaseModel): zero_point: Annotated[float, Field(description="Zero-point value")] -@router.get("/", summary="Transparency measurements") +@router.get("", summary="Transparency measurements") async def route_get_transparency( start_time: Annotated[ float | None, diff --git a/src/lvmapi/tools/transparency.py b/src/lvmapi/tools/transparency.py index 174c871..f56d6f3 100644 --- a/src/lvmapi/tools/transparency.py +++ b/src/lvmapi/tools/transparency.py @@ -24,13 +24,22 @@ async def get_transparency(start_time: float, end_time: float): |> yield(name: "mean") """ + SCHEMA: dict[str, polars.DataType] = { + "time": polars.Datetime(time_unit="ms", time_zone="UTC"), + "telescope": polars.String(), + "zero_point": polars.Float32(), + } + data = await query_influxdb(query) + if len(data) == 0: + return polars.DataFrame(None, schema=SCHEMA) + # Clean up the dataframe. data = data.select( time=polars.col._time, telescope=polars.col._measurement.str.extract(r"lvm\.([a-z]+)\.guider"), zero_point=polars.col._value, - ) + ).cast(SCHEMA) # type: ignore return data