Add zeroes for timeFrames if they do not exist to dashboards time endpoint

This commit is contained in:
Marcel Schwarz 2020-12-26 15:20:05 +01:00
parent 7a85fe933c
commit 3cdfac30bc

View File

@ -1,4 +1,4 @@
import datetime
from datetime import date, datetime, time, timedelta
from typing import Optional, List
from fastapi import APIRouter, HTTPException
@ -9,7 +9,7 @@ import api_database
router = APIRouter(prefix="/dashboard/{station_id}", tags=["dashboard", "local"])
def validate_daterange(start_date: datetime.date, end_date: datetime.date):
def validate_daterange(start_date: date, end_date: date):
days_requested = (end_date - start_date).days
if days_requested < 0:
raise HTTPException(status_code=400, detail="Requested date-range is negative")
@ -20,8 +20,8 @@ class StationDashboard(BaseModel):
commonName: Optional[str]
lat: Optional[float]
lon: Optional[float]
maxEndDate: Optional[datetime.date]
maxStartDate: Optional[datetime.date]
maxEndDate: Optional[date]
maxStartDate: Optional[date]
@router.get("/", response_model=StationDashboard)
@ -37,13 +37,13 @@ class StationDashboardTopStationsEntry(BaseModel):
@router.get("/to", response_model=List[StationDashboardTopStationsEntry])
def get_to_dashboard_for_station(station_id: int, start_date: datetime.date, end_date: datetime.date):
def get_to_dashboard_for_station(station_id: int, start_date: date, end_date: date):
validate_daterange(start_date, end_date)
return api_database.get_dashboard_to(station_id, start_date, end_date)
@router.get("/from", response_model=List[StationDashboardTopStationsEntry])
def get_from_dashboard_for_station(station_id: int, start_date: datetime.date, end_date: datetime.date):
def get_from_dashboard_for_station(station_id: int, start_date: date, end_date: date):
validate_daterange(start_date, end_date)
return api_database.get_dashboard_from(station_id, start_date, end_date)
@ -54,7 +54,7 @@ class StationDashboardDurationGroup(BaseModel):
@router.get("/duration", response_model=List[StationDashboardDurationGroup])
def get_duration_dashboard_for_station(station_id: int, start_date: datetime.date, end_date: datetime.date):
def get_duration_dashboard_for_station(station_id: int, start_date: date, end_date: date):
validate_daterange(start_date, end_date)
db_data = api_database.get_dashboard_duration(station_id, start_date, end_date)
ret_val = []
@ -78,6 +78,18 @@ class StationDashboardTimeGroup(BaseModel):
@router.get("/time", response_model=List[StationDashboardTimeGroup])
def get_time_dashboard_for_station(station_id: int, start_date: datetime.date, end_date: datetime.date):
def get_time_dashboard_for_station(station_id: int, start_date: date, end_date: date):
validate_daterange(start_date, end_date)
return api_database.get_dashboard_time(station_id, start_date, end_date)
db_data = api_database.get_dashboard_time(station_id, start_date, end_date)
ret_val = []
init_date = datetime.combine(date.today(), time(0, 0))
for i in range(144):
curr_interval = (init_date + timedelta(minutes=10 * i)).strftime("%H:%M")
search_interval = list(filter(lambda x: x['timeFrame'] == curr_interval, db_data))
if search_interval:
ret_val.append(search_interval.pop())
else:
ret_val.append({'timeFrame': curr_interval, 'number': 0, 'avgDuration': 0})
return ret_val