Skip to content

Commit

Permalink
fix: added official to feed
Browse files Browse the repository at this point in the history
  • Loading branch information
cka-y committed Nov 25, 2024
1 parent da73718 commit f06c437
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 28 deletions.
32 changes: 4 additions & 28 deletions api/src/feeds/impl/feeds_api_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Union, TypeVar

from sqlalchemy import or_
from sqlalchemy import select, desc
from sqlalchemy import select
from sqlalchemy.orm import joinedload
from sqlalchemy.orm.query import Query

Expand All @@ -16,7 +16,6 @@
Validationreport,
t_location_with_translations_en,
Entitytype,
Officialstatushistory,
)
from feeds.filters.feed_filter import FeedFilter
from feeds.filters.gtfs_dataset_filter import GtfsDatasetFilter
Expand Down Expand Up @@ -83,29 +82,6 @@ def get_feed(
else:
raise_http_error(404, feed_not_found.format(id))

@staticmethod
def _get_latest_official_status_subquery(feed_query: Query, is_official: bool) -> Query:
"""Get the latest official status per feed using a subquery."""
# Subquery to get the latest official status per feed
latest_status_subquery = (
Database()
.get_query_model(Officialstatushistory)
.with_entities(
Officialstatushistory.feed_id,
Officialstatushistory.is_official,
Officialstatushistory.timestamp,
)
.distinct(Officialstatushistory.feed_id) # DISTINCT ON feed_id
.order_by(Officialstatushistory.feed_id, desc(Officialstatushistory.timestamp))
.subquery()
)

# Join with the main query and filter by is_official
return feed_query.join(
latest_status_subquery,
latest_status_subquery.c.feed_id == Feed.id,
).filter(latest_status_subquery.c.is_official == is_official)

def get_feeds(
self,
limit: int,
Expand All @@ -121,7 +97,7 @@ def get_feeds(
)
feed_query = feed_filter.filter(Database().get_query_model(Feed))
if is_official:
feed_query = self._get_latest_official_status_subquery(feed_query, is_official)
feed_query = feed_query.filter(Feed.official)
feed_query = feed_query.filter(Feed.data_type != "gbfs") # Filter out GBFS feeds
feed_query = feed_query.filter(
or_(
Expand Down Expand Up @@ -288,7 +264,7 @@ def get_gtfs_feeds(
.order_by(Gtfsfeed.provider, Gtfsfeed.stable_id)
)
if is_official:
feed_query = self._get_latest_official_status_subquery(feed_query, is_official)
feed_query = feed_query.filter(Feed.official)
feed_query = feed_query.limit(limit).offset(offset)
return self._get_response(feed_query, GtfsFeedImpl)

Expand Down Expand Up @@ -391,7 +367,7 @@ def get_gtfs_rt_feeds(
.order_by(Gtfsrealtimefeed.provider, Gtfsrealtimefeed.stable_id)
)
if is_official:
feed_query = self._get_latest_official_status_subquery(feed_query, is_official)
feed_query = feed_query.filter(Feed.official)
feed_query = feed_query.limit(limit).offset(offset)
return self._get_response(feed_query, GtfsRTFeedImpl)

Expand Down
1 change: 1 addition & 0 deletions liquibase/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
<include file="changes/feat_780.sql" relativeToChangelogFile="true"/>
<include file="changes/feat_741.sql" relativeToChangelogFile="true"/>
<include file="changes/feat_794.sql" relativeToChangelogFile="true"/>
<include file="changes/feat_794_2.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>
1 change: 1 addition & 0 deletions liquibase/changes/feat_794_2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE Feed ADD COLUMN official BOOLEAN DEFAULT NULL;

0 comments on commit f06c437

Please sign in to comment.