Skip to content

Commit db60faf

Browse files
committed
use stable_id instead of feed id
1 parent 82fe5fa commit db60faf

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

functions-python/gbfs_validator/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The message published by the batch function to the Pub/Sub topic follows this fo
3030

3131
### Functionality Details
3232

33-
- **`gbfs-validator-batch`**: Triggered per execution ID, this function iterates over all GBFS feeds, preparing and publishing individual messages to the Pub/Sub topic.
33+
- **`gbfs-validator-batch`**: Triggered per execution ID, when the request is a POST request with a JSON body containing `feed_stable_ids`, it publishes events related to only those feeds. Otherwise, it publishes avents of all feeds to the Pub/Sub topic.
3434
- **`gbfs-validator-pubsub`**: Triggered per feed, this function performs the following steps:
3535
1. **Access the autodiscovery URL and update versions**: The function accesses the autodiscovery URL to update the **GBFSVersions** table.
3636
2. **Measure latency and validate the feed**: For each version, the function measures the response latency and validates the feed. The validation summary is stored in GCP, and the total error count is extracted and saved in the **GBFSValidationReport**.
@@ -46,6 +46,13 @@ The `gbfs-validator-batch` function requires the following environment variables
4646
- **`PROJECT_ID`**: The Google Cloud Project ID used to construct the full topic path.
4747
- **`FEEDS_DATABASE_URL`**: The database connection string for accessing the GBFS feeds.
4848

49+
Optional request body parameters for the batch function:
50+
```json
51+
{
52+
"feed_stable_ids": ["feed_id_1", "feed_id_2"]
53+
}
54+
```
55+
4956
### Pub/Sub Function Environment Variables
5057

5158
The `gbfs-validator-pubsub` function requires the following environment variables:

functions-python/gbfs_validator/src/main.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def gbfs_validator_pubsub(cloud_event: CloudEvent):
104104
def gbfs_validator_batch(request, db_session: Session):
105105
"""
106106
HTTP Cloud Function to trigger the GBFS Validator function for multiple datasets.
107-
When the request is a POST request with a JSON body containing `feed_ids`,
107+
When the request is a POST request with a JSON body containing `feed_stable_ids`,
108108
it processes only those feeds. Otherwise, it processes all feeds in the database.
109109
@param _: The request object.
110110
@return: The response of the function.
@@ -116,19 +116,21 @@ def gbfs_validator_batch(request, db_session: Session):
116116
return "PUBSUB_TOPIC_NAME environment variable not set.", 500
117117

118118
try:
119-
feed_ids = None
119+
feed_stable_ids = None
120120
if request and request.method == "POST" and request.is_json:
121121
request_json = request.get_json()
122-
feed_ids = request_json.get("feed_ids") if request_json else None
122+
feed_stable_ids = (
123+
request_json.get("feed_stable_ids") if request_json else None
124+
)
123125
else:
124126
logging.info("Request body not provided or not a valid JSON.")
125127
except Exception as e:
126128
logging.error("Error parsing request body: %s", e)
127129
return "Invalid request body.", 400
128130

129131
try:
130-
if feed_ids:
131-
gbfs_feeds = fetch_gbfs_feeds_by_ids(db_session, feed_ids)
132+
if feed_stable_ids:
133+
gbfs_feeds = fetch_gbfs_feeds_by_stable_ids(db_session, feed_stable_ids)
132134
else:
133135
# Get all GBFS feeds from the database
134136
gbfs_feeds = fetch_all_gbfs_feeds(db_session)
@@ -168,11 +170,13 @@ def gbfs_validator_batch(request, db_session: Session):
168170
)
169171

170172

171-
def fetch_gbfs_feeds_by_ids(db_session, feed_ids):
172-
"""Fetch GBFS feeds by their IDs from the database."""
173+
def fetch_gbfs_feeds_by_stable_ids(db_session, feed_stable_ids):
174+
"""Fetch GBFS feeds by their IDs and not deprecated from the database"""
173175
gbfs_feeds = (
174176
db_session.query(Gbfsfeed)
175-
.filter(Gbfsfeed.id.in_(feed_ids), Gbfsfeed.status != "deprecated")
177+
.filter(
178+
Gbfsfeed.stable_id.in_(feed_stable_ids), Gbfsfeed.status != "deprecated"
179+
)
176180
.all()
177181
)
178182
return gbfs_feeds

functions-python/gbfs_validator/tests/test_gbfs_validator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def test_gbfs_validator_batch(
7575
):
7676
# Prepare mocks
7777
mock_session = MagicMock()
78-
# mock_database.return_value.start_db_session.return_value = mock_session
7978

8079
mock_publisher = MagicMock()
8180
mock_publisher_client.return_value = mock_publisher
@@ -187,13 +186,12 @@ def test_gbfs_validator_batch_publish_exception(
187186
},
188187
)
189188
@patch("main.pubsub_v1.PublisherClient")
190-
@patch("main.fetch_gbfs_feeds_by_ids")
191-
def test_gbfs_validator_batch_by_feed_ids(
192-
self, mock_fetch_gbfs_feeds_by_ids, mock_publisher_client
189+
@patch("main.fetch_gbfs_feeds_by_stable_ids")
190+
def test_gbfs_validator_batch_by_feed_stable_ids(
191+
self, fetch_gbfs_feeds_by_stable_ids, mock_publisher_client
193192
):
194193
# Prepare mocks
195194
mock_session = MagicMock()
196-
# mock_database.return_value.start_db_session.return_value = mock_session
197195

198196
mock_publisher = MagicMock()
199197
mock_publisher_client.return_value = mock_publisher
@@ -205,14 +203,16 @@ def test_gbfs_validator_batch_by_feed_ids(
205203
mock_feed.gbfsversions = [MagicMock(version="1.0")]
206204
mock_feed_2 = copy.deepcopy(mock_feed)
207205
mock_feed_2.gbfsversions = []
208-
mock_fetch_gbfs_feeds_by_ids.return_value = [mock_feed, mock_feed_2]
206+
fetch_gbfs_feeds_by_stable_ids.return_value = [mock_feed, mock_feed_2]
209207
request = MagicMock()
210208
request.method = "POST"
211209
request.is_json = True
212-
request.get_json.return_value = {"feed_ids": [mock_feed.id, mock_feed_2.id]}
210+
request.get_json.return_value = {
211+
"feed_stable_ids": [mock_feed.id, mock_feed_2.id]
212+
}
213213
# Call the function
214214
result = gbfs_validator_batch(request, db_session=mock_session)
215215
self.assertEqual(result[1], 200)
216216

217-
mock_fetch_gbfs_feeds_by_ids.assert_called_once()
217+
fetch_gbfs_feeds_by_stable_ids.assert_called_once()
218218
self.assertEqual(mock_publisher.publish.call_count, 2)

0 commit comments

Comments
 (0)