Return more data on TopStations endpoints

This commit is contained in:
Marcel Schwarz 2020-12-27 15:52:26 +01:00
parent 3cdfac30bc
commit 8947f9116e
2 changed files with 36 additions and 20 deletions

View File

@ -43,30 +43,44 @@ def get_dashboard(station_id):
def get_dashboard_to(station_id, start_date, end_date): def get_dashboard_to(station_id, start_date, end_date):
query = """ query = """
SELECT SELECT
u.start_station_name AS startStationName, topPoints.*,
u.end_station_name AS endStationName, b.lat AS stationLat,
count(*) AS number, b.lon AS stationLon
round(avg(u.duration)) AS avgDuration FROM (
FROM usage_stats u SELECT
WHERE u.start_station_id = ? AND date(u.start_date, 'unixepoch') BETWEEN ? AND ? u.end_station_name AS stationName,
GROUP BY u.end_station_name u.end_station_id AS stationId,
ORDER BY number DESC count(*) AS number,
LIMIT 3""" round(avg(u.duration)) AS avgDuration
FROM usage_stats u
WHERE u.start_station_id = ? AND date(u.start_date, 'unixepoch') BETWEEN ? AND ?
GROUP BY u.end_station_name
ORDER BY number DESC
LIMIT 3
) as topPoints
JOIN bike_points b ON b.id_num = topPoints.stationId"""
return get_db_connection().execute(query, (station_id, start_date, end_date)).fetchall() return get_db_connection().execute(query, (station_id, start_date, end_date)).fetchall()
def get_dashboard_from(station_id, start_date, end_date): def get_dashboard_from(station_id, start_date, end_date):
query = """ query = """
SELECT SELECT
u.start_station_name AS startStationName, topPoints.*,
u.end_station_name AS endStationName, b.lat AS stationLat,
count(*) AS number, b.lon AS stationLon
round(avg(u.duration)) AS avgDuration FROM (
FROM usage_stats u SELECT
WHERE u.end_station_id = ? AND date(u.start_date, 'unixepoch') BETWEEN ? AND ? u.start_station_name AS stationName,
GROUP BY u.start_station_name u.start_station_id AS stationId,
ORDER BY number DESC count(*) AS number,
LIMIT 3""" round(avg(u.duration)) AS avgDuration
FROM usage_stats u
WHERE u.end_station_id = ? AND date(u.start_date, 'unixepoch') BETWEEN ? AND ?
GROUP BY u.start_station_name
ORDER BY number DESC
LIMIT 3
) as topPoints
JOIN bike_points b ON b.id_num = topPoints.stationId"""
return get_db_connection().execute(query, (station_id, start_date, end_date)).fetchall() return get_db_connection().execute(query, (station_id, start_date, end_date)).fetchall()

View File

@ -30,8 +30,10 @@ def get_general_dashboard(station_id: int):
class StationDashboardTopStationsEntry(BaseModel): class StationDashboardTopStationsEntry(BaseModel):
startStationName: str stationName: str
endStationName: str stationId: int
stationLat: float
stationLon: float
number: int number: int
avgDuration: int avgDuration: int