Skip to content

Commit

Permalink
Add agg by line ref to gtfs_ride_agg/group_by (#29)
Browse files Browse the repository at this point in the history
* added

* Added route short & long name

* Removed limit, offset
  • Loading branch information
ShayAdler committed Jun 28, 2023
1 parent 91db2d2 commit 27c0b93
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions open_bus_stride_api/routers/gtfs_rides_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class GtfsRidesAggGroupByPydanticModel(pydantic.BaseModel):
gtfs_route_hour: typing.Optional[datetime.date]
operator_ref: typing.Optional[int]
day_of_week: typing.Optional[str]
line_ref: typing.Optional[int]
route_short_name: typing.Optional[str]
route_long_name: typing.Optional[str]
total_routes: int
total_planned_rides: int
total_actual_rides: int
Expand All @@ -37,7 +40,7 @@ class GtfsRidesAggGroupByPydanticModel(pydantic.BaseModel):
PYDANTIC_MODEL = GtfsRidesAggPydanticModel
GROUP_BY_PYDANTIC_MODEL = GtfsRidesAggGroupByPydanticModel
DEFAULT_LIMIT = 1000
ALLOWED_GROUP_BY_FIELDS = ['gtfs_route_date', 'gtfs_route_hour', 'operator_ref', 'day_of_week']
ALLOWED_GROUP_BY_FIELDS = ['gtfs_route_date', 'gtfs_route_hour', 'operator_ref', 'day_of_week', 'line_ref']


@common.router_list(router, TAG, PYDANTIC_MODEL, WHAT_PLURAL)
Expand All @@ -58,9 +61,10 @@ def list_(limit: int = common.param_limit(default_limit=DEFAULT_LIMIT),
from gtfs_rides_agg_by_hour agg, gtfs_route rt
where
agg.gtfs_route_id = rt.id
and date_trunc('day', agg.gtfs_route_hour) >= :date_from
and date_trunc('day', agg.gtfs_route_hour) <= :date_to
and agg.gtfs_route_hour >= :date_from
and agg.gtfs_route_hour <= :date_to
"""
date_to = datetime.datetime.combine(date_to, datetime.time(23, 59, 59))
sql_params = {
'date_from': date_from,
'date_to': date_to,
Expand Down Expand Up @@ -90,14 +94,17 @@ def group_by_(date_from: datetime.date = common.doc_param('date', filter_type='d
for fieldname in group_by:
if fieldname == 'gtfs_route_hour':
full_fieldname = 'agg.gtfs_route_hour'
elif fieldname == 'gtfs_route_date':
full_fieldname = "date_trunc('day', agg.gtfs_route_hour)"
elif fieldname == 'day_of_week':
full_fieldname = "trim(lower(to_char(agg.gtfs_route_hour, 'DAY')))"
elif fieldname == 'line_ref':
full_fieldname = f'rt.{fieldname}'
select_fields.append(f'json_agg(distinct rt.route_short_name)::text as route_short_name')
select_fields.append(f'json_agg(distinct rt.route_long_name)::text as route_long_name')
else:
full_fieldname = f'rt.{fieldname}'
select_fields.append(f'{full_fieldname} as {fieldname}')
group_by_fields.append(full_fieldname)

sql = dedent(f"""
select
{', '.join(select_fields)},
Expand All @@ -107,9 +114,10 @@ def group_by_(date_from: datetime.date = common.doc_param('date', filter_type='d
from gtfs_rides_agg_by_hour agg, gtfs_route rt
where
agg.gtfs_route_id = rt.id
and date_trunc('day', agg.gtfs_route_hour) >= :date_from
and date_trunc('day', agg.gtfs_route_hour) <= :date_to
and agg.gtfs_route_hour >= :date_from
and agg.gtfs_route_hour <= :date_to
""")
date_to = datetime.datetime.combine(date_to, datetime.time(23, 59, 59))
sql_params = {
'date_from': date_from,
'date_to': date_to,
Expand All @@ -124,4 +132,6 @@ def group_by_(date_from: datetime.date = common.doc_param('date', filter_type='d

sql += f" group by {', '.join(group_by_fields)}"

print(sql)

return sql_route.list_(sql, sql_params, None, None, None, None, None, True)

0 comments on commit 27c0b93

Please sign in to comment.