Skip to content

Commit

Permalink
Group by gtfs_route_date bugfix (#30)
Browse files Browse the repository at this point in the history
* Fixes

* Update ci.yml

* Update ci.yml

---------

Co-authored-by: Ori Hoch <[email protected]>
  • Loading branch information
ShayAdler and OriHoch authored Jul 10, 2023
1 parent d9c4f50 commit c4c479e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: CI
on:
push:
pull_request:
types: [synchronize, opened, reopened, ready_for_review]
jobs:
ci:
runs-on: ubuntu-20.04
Expand Down
18 changes: 7 additions & 11 deletions open_bus_stride_api/routers/gtfs_rides_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class GtfsRidesAggGroupByPydanticModel(pydantic.BaseModel):
GROUP_BY_PYDANTIC_MODEL = GtfsRidesAggGroupByPydanticModel
DEFAULT_LIMIT = 1000
ALLOWED_GROUP_BY_FIELDS = ['gtfs_route_date', 'gtfs_route_hour', 'operator_ref', 'day_of_week', 'line_ref']
AGG_VIEW_FIELDS = ['gtfs_route_date', 'gtfs_route_hour']


@common.router_list(router, TAG, PYDANTIC_MODEL, WHAT_PLURAL)
Expand All @@ -61,10 +62,9 @@ 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 agg.gtfs_route_hour >= :date_from
and agg.gtfs_route_hour <= :date_to
and agg.gtfs_route_date >= :date_from
and agg.gtfs_route_date <= :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 @@ -92,8 +92,8 @@ def group_by_(date_from: datetime.date = common.doc_param('date', filter_type='d
select_fields = []
group_by_fields = []
for fieldname in group_by:
if fieldname == 'gtfs_route_hour':
full_fieldname = 'agg.gtfs_route_hour'
if fieldname in AGG_VIEW_FIELDS:
full_fieldname = f'agg.{fieldname}'
elif fieldname == 'day_of_week':
full_fieldname = "trim(lower(to_char(agg.gtfs_route_hour, 'DAY')))"
elif fieldname == 'line_ref':
Expand All @@ -114,10 +114,9 @@ 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 agg.gtfs_route_hour >= :date_from
and agg.gtfs_route_hour <= :date_to
and agg.gtfs_route_date >= :date_from
and agg.gtfs_route_date <= :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 @@ -131,7 +130,4 @@ def group_by_(date_from: datetime.date = common.doc_param('date', filter_type='d
sql_params['exclude_hour_to'] = exclude_hours_to

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 c4c479e

Please sign in to comment.