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,
b.lon AS stationLon
FROM (
SELECT
u.end_station_name AS stationName,
u.end_station_id AS stationId,
count(*) AS number, count(*) AS number,
round(avg(u.duration)) AS avgDuration round(avg(u.duration)) AS avgDuration
FROM usage_stats u FROM usage_stats u
WHERE u.start_station_id = ? AND date(u.start_date, 'unixepoch') BETWEEN ? AND ? WHERE u.start_station_id = ? AND date(u.start_date, 'unixepoch') BETWEEN ? AND ?
GROUP BY u.end_station_name GROUP BY u.end_station_name
ORDER BY number DESC ORDER BY number DESC
LIMIT 3""" 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,
b.lon AS stationLon
FROM (
SELECT
u.start_station_name AS stationName,
u.start_station_id AS stationId,
count(*) AS number, count(*) AS number,
round(avg(u.duration)) AS avgDuration round(avg(u.duration)) AS avgDuration
FROM usage_stats u FROM usage_stats u
WHERE u.end_station_id = ? AND date(u.start_date, 'unixepoch') BETWEEN ? AND ? WHERE u.end_station_id = ? AND date(u.start_date, 'unixepoch') BETWEEN ? AND ?
GROUP BY u.start_station_name GROUP BY u.start_station_name
ORDER BY number DESC ORDER BY number DESC
LIMIT 3""" 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