Skip to content

Commit

Permalink
handle empty influx response better
Browse files Browse the repository at this point in the history
  • Loading branch information
robinklaassen committed Nov 20, 2024
1 parent 4907c69 commit 50acf4b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion aid/provide/influx_trains.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class InfluxTrainProvider(BaseProvider):
# TODO better name, changing from postgres to influx kind of on the fly now

def get_trains(self, start: datetime, end: datetime) -> pd.DataFrame:
def get_trains(self, start: datetime, end: datetime) -> pd.DataFrame | None:
"""
Get train data from InfluxDB as a data frame.
Expand Down Expand Up @@ -45,6 +45,9 @@ def get_trains(self, start: datetime, end: datetime) -> pd.DataFrame:
query_api = self._influx_client.query_api()
df = query_api.query_data_frame(query=query, use_extension_dtypes=True)

if df.empty:
return None

time_query_done = perf_counter()
logger.debug("Influx query time", duration=time_query_done - time_start)

Expand All @@ -67,6 +70,8 @@ def get_trains_as_records(self, start: datetime, end: datetime) -> list[TrainRec
Get train data from InfluxDB as a list of pydantic records.
"""
gdf = self.get_trains(start, end)
if gdf is None:
return []

output = [
TrainRecord(
Expand Down
5 changes: 4 additions & 1 deletion aid/provide/routers/trains.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TypeAlias

import pandas as pd
from fastapi import APIRouter, Security
from fastapi import APIRouter, Security, HTTPException
from pydantic import BaseModel

from aid.constants import DEFAULT_TIMEZONE
Expand Down Expand Up @@ -79,6 +79,9 @@ def get_pivoted_data(start: datetime | None = None, end: datetime | None = None)
end = end or datetime.now(DEFAULT_TIMEZONE)
start = start or end - timedelta(seconds=10)
df = InfluxTrainProvider().get_trains(start, end)
if df is None:
raise HTTPException(status_code=204) # no content

df = df.rename(
columns={
"_time": "timestamp",
Expand Down

0 comments on commit 50acf4b

Please sign in to comment.