Skip to content

Commit cbb6cff

Browse files
committed
Merge branch 'main' into 1024-load-tests-are-failing-with-gcp-token-message
2 parents baa08f9 + 3e6d4ad commit cbb6cff

File tree

4 files changed

+37
-66
lines changed

4 files changed

+37
-66
lines changed

api/src/feeds/impl/feeds_api_impl.py

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
Gtfsfeed,
1515
Gtfsrealtimefeed,
1616
Location,
17-
Validationreport,
1817
Entitytype,
1918
)
2019
from shared.feed_filters.feed_filter import FeedFilter
@@ -129,27 +128,11 @@ def get_gtfs_feed(self, id: str, db_session: Session) -> GtfsFeed:
129128
raise_http_error(404, gtfs_feed_not_found.format(id))
130129

131130
@staticmethod
132-
def _get_gtfs_feed(stable_id: str, db_session: Session) -> Optional[Gtfsfeed]:
133-
results = (
134-
FeedFilter(
135-
stable_id=stable_id,
136-
status=None,
137-
provider__ilike=None,
138-
producer_url__ilike=None,
139-
)
140-
.filter(db_session.query(Gtfsfeed))
141-
.filter(
142-
or_(
143-
Gtfsfeed.operational_status == "published",
144-
not is_user_email_restricted(), # Allow all feeds to be returned if the user is not restricted
145-
)
146-
)
147-
.options(
148-
joinedload(Gtfsfeed.gtfsdatasets)
149-
.joinedload(Gtfsdataset.validation_reports)
150-
.joinedload(Validationreport.notices),
151-
*get_joinedload_options(),
152-
)
131+
def _get_gtfs_feed(
132+
stable_id: str, db_session: Session, include_options_for_joinedload: bool = True
133+
) -> Optional[Gtfsfeed]:
134+
results = get_gtfs_feeds_query(
135+
db_session=db_session, stable_id=stable_id, include_options_for_joinedload=include_options_for_joinedload
153136
).all()
154137
if len(results) == 0:
155138
return None
@@ -173,22 +156,7 @@ def get_gtfs_feed_datasets(
173156
raise_http_validation_error(invalid_date_message.format("downloaded_after"))
174157

175158
# First make sure the feed exists. If not it's an error 404
176-
feed = (
177-
FeedFilter(
178-
stable_id=gtfs_feed_id,
179-
status=None,
180-
provider__ilike=None,
181-
producer_url__ilike=None,
182-
)
183-
.filter(Database().get_query_model(db_session, Gtfsfeed))
184-
.filter(
185-
or_(
186-
Feed.operational_status == "published",
187-
not is_user_email_restricted(), # Allow all feeds to be returned if the user is not restricted
188-
)
189-
)
190-
.first()
191-
)
159+
feed = self._get_gtfs_feed(gtfs_feed_id, db_session, include_options_for_joinedload=False)
192160

193161
if not feed:
194162
raise_http_error(404, f"Feed with id {gtfs_feed_id} not found")

api/src/shared/common/db_utils.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@
2525

2626

2727
def get_gtfs_feeds_query(
28-
limit: int | None,
29-
offset: int | None,
30-
provider: str | None,
31-
producer_url: str | None,
32-
country_code: str | None,
33-
subdivision_name: str | None,
34-
municipality: str | None,
35-
dataset_latitudes: str | None,
36-
dataset_longitudes: str | None,
37-
bounding_filter_method: str | None,
28+
db_session: Session,
29+
stable_id: str | None = None,
30+
limit: int | None = None,
31+
offset: int | None = None,
32+
provider: str | None = None,
33+
producer_url: str | None = None,
34+
country_code: str | None = None,
35+
subdivision_name: str | None = None,
36+
municipality: str | None = None,
37+
dataset_latitudes: str | None = None,
38+
dataset_longitudes: str | None = None,
39+
bounding_filter_method: str | None = None,
3840
is_official: bool = False,
3941
include_wip: bool = False,
40-
db_session: Session = None,
42+
include_options_for_joinedload: bool = True,
4143
) -> Query[any]:
4244
"""Get the DB query to use to retrieve the GTFS feeds.."""
4345
gtfs_feed_filter = GtfsFeedFilter(
44-
stable_id=None,
46+
stable_id=stable_id,
4547
provider__ilike=provider,
4648
producer_url__ilike=producer_url,
4749
location=LocationFilter(
@@ -65,12 +67,13 @@ def get_gtfs_feeds_query(
6567
if not include_wip:
6668
feed_query = feed_query.filter(Gtfsfeed.operational_status == "published")
6769

68-
feed_query = feed_query.options(
69-
contains_eager(Gtfsfeed.gtfsdatasets)
70-
.joinedload(Gtfsdataset.validation_reports)
71-
.joinedload(Validationreport.notices),
72-
*get_joinedload_options(),
73-
).order_by(Gtfsfeed.provider, Gtfsfeed.stable_id)
70+
if include_options_for_joinedload:
71+
feed_query = feed_query.options(
72+
contains_eager(Gtfsfeed.gtfsdatasets)
73+
.joinedload(Gtfsdataset.validation_reports)
74+
.joinedload(Validationreport.notices),
75+
*get_joinedload_options(),
76+
).order_by(Gtfsfeed.provider, Gtfsfeed.stable_id)
7477
if is_official:
7578
feed_query = feed_query.filter(Feed.official)
7679
feed_query = feed_query.limit(limit).offset(offset)
@@ -100,9 +103,11 @@ def get_all_gtfs_feeds(
100103
stable_ids = (f.stable_id for f in batch)
101104
yield from (
102105
db_session.query(Gtfsfeed)
106+
.outerjoin(Gtfsfeed.gtfsdatasets)
103107
.filter(Gtfsfeed.stable_id.in_(stable_ids))
108+
.filter((Gtfsdataset.latest) | (Gtfsdataset.id == None)) # noqa: E711
104109
.options(
105-
joinedload(Gtfsfeed.gtfsdatasets)
110+
contains_eager(Gtfsfeed.gtfsdatasets)
106111
.joinedload(Gtfsdataset.validation_reports)
107112
.joinedload(Validationreport.features),
108113
*get_joinedload_options(),

functions-python/export_csv/src/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,16 @@ def get_feed_csv_data(feed: Feed):
251251
redirect_ids = []
252252
redirect_comments = []
253253
# Add concatenated redirect IDs
254-
if feed.redirectingids:
255-
for redirect in feed.redirectingids:
254+
sorted_redirects = sorted(feed.redirectingids, key=lambda x: x.target.stable_id)
255+
if sorted_redirects:
256+
for redirect in sorted_redirects:
256257
if redirect and redirect.target and redirect.target.stable_id:
257258
stripped_id = redirect.target.stable_id.strip()
258259
if stripped_id:
259260
redirect_ids.append(stripped_id)
260261
redirect_comment = redirect.redirect_comment or ""
261262
redirect_comments.append(redirect_comment)
262-
263+
redirect_ids = sorted(redirect_ids)
263264
redirect_ids_str = "|".join(redirect_ids)
264265
redirect_comments_str = "|".join(redirect_comments)
265266

functions-python/export_csv/tests/conftest.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ def populate_database():
4141
- 2 active
4242
- 1 inactive
4343
- 2 deprecated
44-
- 5 GTFS Realtime feeds
45-
- 4 3TFS rt datasets, with 1 of them inactive
44+
- 3 GTFS datasets
45+
- 4 GTFS Realtime feeds, with 1 of them inactive
4646
"""
47-
clean_testing_db()
4847
session = get_testing_session()
4948
fake = Faker()
5049

@@ -138,7 +137,7 @@ def populate_database():
138137
.all()
139138
)
140139

141-
# the first 2 datasets are for the first feed
140+
# the first 2 datasets are for the first active feed, one dataset is for the second active feed
142141
for i in range(1, 4):
143142
feed_index = 0 if i in [1, 2] else 1
144143
wkt_polygon = "POLYGON((-18 -9, -18 9, 18 9, 18 -9, -18 -9))"
@@ -176,8 +175,6 @@ def populate_database():
176175
active_gtfs_feeds[0].locations = locations
177176
active_gtfs_feeds[1].locations = locations
178177

179-
# active_gtfs_feeds[0].gtfsdatasets.append() = gtfs_datasets
180-
181178
vp_entitytype = session.query(Entitytype).filter_by(name="vp").first()
182179
if not vp_entitytype:
183180
vp_entitytype = Entitytype(name="vp")

0 commit comments

Comments
 (0)