-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquery_helper.py
More file actions
175 lines (144 loc) · 5.17 KB
/
query_helper.py
File metadata and controls
175 lines (144 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import logging
from datetime import datetime
from typing import Type
from sqlalchemy import and_
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.orm.query import Query
from shared.database_gen.sqlacodegen_models import (
Feed,
Gtfsrealtimefeed,
Gtfsfeed,
Gbfsfeed,
Gtfsdataset,
Validationreport,
)
feed_mapping = {"gtfs_rt": Gtfsrealtimefeed, "gtfs": Gtfsfeed, "gbfs": Gbfsfeed}
def get_model(data_type: str | None) -> Type[Feed]:
"""
Get the model based on the data type
"""
return feed_mapping.get(data_type, Feed)
def query_feed_by_stable_id(
session: Session, stable_id: str, data_type: str | None
) -> Gtfsrealtimefeed | Gtfsfeed | Gbfsfeed:
"""
Query the feed by stable id
"""
model = get_model(data_type)
return session.query(model).filter(model.stable_id == stable_id).first()
def get_eager_loading_options(model: Type[Feed]):
"""
Get the appropriate eager loading options based on the model type.
Args:
model: The SQLAlchemy model class
Returns:
List of joinedload options for the query
"""
if model == Gtfsrealtimefeed:
logging.info("Adding GTFS-RT specific eager loading")
return [
joinedload(Gtfsrealtimefeed.locations),
joinedload(Gtfsrealtimefeed.entitytypes),
joinedload(Gtfsrealtimefeed.gtfs_feeds),
joinedload(Gtfsrealtimefeed.externalids),
joinedload(Gtfsrealtimefeed.redirectingids),
]
elif model == Gtfsfeed:
logging.info("Adding GTFS specific eager loading")
return [
joinedload(Gtfsfeed.locations),
joinedload(Gtfsfeed.externalids),
joinedload(Gtfsfeed.redirectingids),
]
else:
logging.info("Adding base Feed eager loading")
return [
joinedload(Feed.locations),
joinedload(Feed.externalids),
joinedload(Feed.redirectingids),
joinedload(Gtfsrealtimefeed.entitytypes),
joinedload(Gtfsrealtimefeed.gtfs_feeds),
]
def get_feeds_query(
db_session: Session,
operation_status: str | None = None,
data_type: str | None = None,
limit: int | None = None,
offset: int | None = None,
) -> Query:
"""
Build a consolidated query for feeds with filtering options.
Args:
db_session: SQLAlchemy session
operation_status: Optional filter for operational status (wip or published)
data_type: Optional filter for feed type (gtfs or gtfs_rt)
limit: Maximum number of items to return
offset: Number of items to skip
Returns:
Query: SQLAlchemy query object
"""
try:
logging.info(
"Building query with params: data_type=%s, operation_status=%s",
data_type,
operation_status,
)
if data_type == "gtfs":
model = Gtfsfeed
elif data_type == "gtfs_rt":
model = Gtfsrealtimefeed # Force concrete model
else:
model = Feed
logging.info(f"Using concrete model: {model.__name__}")
conditions = []
if data_type is None:
conditions.append(model.data_type.in_(["gtfs", "gtfs_rt"]))
logging.info("Added filter to exclude gbfs feeds")
if operation_status:
conditions.append(model.operational_status == operation_status)
logging.info("Added operational_status filter: %s", operation_status)
query = db_session.query(model)
logging.info("Created base query with model %s", model.__name__)
eager_loading_options = get_eager_loading_options(model)
query = query.options(*eager_loading_options)
if conditions:
query = query.filter(and_(*conditions))
logging.info("Applied conditions: %s", conditions)
query = query.order_by(model.provider, model.stable_id)
if offset is not None:
query = query.offset(offset)
if limit is not None:
query = query.limit(limit)
logging.info("Generated SQL Query: %s", query)
return query
except Exception as e:
logging.error("Error building query: %s", str(e))
raise
def get_datasets_with_missing_reports_query(
db_session: Session,
filter_after: datetime | None = None,
) -> Query:
"""
Get datasets with missing validation reports.
Args:
db_session: SQLAlchemy session
filter_after: Optional date to filter datasets
Returns:
A SQLAlchemy query object for datasets with missing validation reports order by feed and dataset stable id.
"""
query = (
db_session.query(
Gtfsfeed.stable_id,
Gtfsdataset.stable_id,
)
.select_from(Gtfsfeed)
.join(Gtfsdataset, Gtfsdataset.feed_id == Gtfsfeed.id)
.outerjoin(Validationreport, Gtfsdataset.validation_reports)
.filter(Validationreport.id.is_(None))
)
if filter_after:
query = query.filter(Gtfsdataset.downloaded_at >= filter_after)
query = query.distinct(Gtfsfeed.stable_id, Gtfsdataset.stable_id).order_by(
Gtfsdataset.stable_id, Gtfsfeed.stable_id
)
return query