Skip to content

Commit 9bbee47

Browse files
feat: on validation report, set service date range + backfill error handling (#930)
1 parent 19a0e97 commit 9bbee47

File tree

12 files changed

+76
-4
lines changed

12 files changed

+76
-4
lines changed

functions-python/backfill_dataset_service_date_range/src/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ def backfill_datasets(session: "Session"):
9494
response.raise_for_status()
9595
json_data = response.json()
9696
else:
97-
logging.info("Blob found, downloading from blob")
98-
json_data = json.loads(dataset_blob.download_as_string())
97+
try:
98+
logging.info("Blob found, downloading from blob")
99+
json_data = json.loads(dataset_blob.download_as_string())
100+
except Exception as e:
101+
logging.error(f"Error downloading blob: {e} trying json report url")
102+
response = requests.get(json_report_url)
103+
response.raise_for_status()
104+
json_data = response.json()
99105

100106
extracted_service_start_date = (
101107
json_data.get("summary", {})

functions-python/backfill_dataset_service_date_range/tests/test_backfill_dataset_service_date_range_main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,58 @@ def test_backfill_datasets(mock_get, mock_storage_client):
8383
mock_session.commit.assert_called_once()
8484

8585

86+
@patch("logging.error", autospec=True)
87+
@patch("google.cloud.storage.Client", autospec=True)
88+
@patch("requests.get")
89+
def test_backfill_datasets_error_commit(mock_get, mock_storage_client, mock_logger):
90+
# Mock the storage client and bucket
91+
mock_bucket = MagicMock()
92+
mock_client_instance = mock_storage_client.return_value
93+
mock_client_instance.bucket.return_value = mock_bucket
94+
mock_blob = MagicMock()
95+
mock_blob.exists.return_value = False
96+
mock_bucket.blob.return_value = mock_blob
97+
98+
mock_session = MagicMock()
99+
mock_dataset = Mock(spec=Gtfsdataset)
100+
mock_dataset.id = 1
101+
mock_dataset.stable_id = "mdb-392-202406181921"
102+
mock_dataset.service_date_range_end = None
103+
mock_dataset.service_date_range_start = None
104+
mock_dataset.validation_reports = [
105+
MagicMock(
106+
validator_version="6.0.0",
107+
validated_at="2022-01-01T00:00:00Z",
108+
json_report="http://example-2.com/report.json",
109+
)
110+
]
111+
112+
mock_query = MagicMock()
113+
mock_query.options.return_value = mock_query
114+
mock_query.filter.return_value = mock_query
115+
mock_query.all.return_value = [mock_dataset]
116+
mock_session.query.return_value = mock_query
117+
mock_session.commit.side_effect = Exception("Commit failed")
118+
119+
mock_response = Mock()
120+
mock_response.status_code = 200
121+
mock_response.json.return_value = {
122+
"summary": {
123+
"feedInfo": {
124+
"feedServiceWindowStart": "2023-01-01",
125+
"feedServiceWindowEnd": "2023-12-31",
126+
}
127+
}
128+
}
129+
mock_get.return_value = mock_response
130+
131+
try:
132+
backfill_datasets(mock_session)
133+
except Exception:
134+
mock_session.rollback.assert_called_once()
135+
mock_session.close.assert_called_once()
136+
137+
86138
@patch("google.cloud.storage.Client", autospec=True)
87139
@patch("requests.get")
88140
def test_backfill_datasets_no_validation_reports(mock_get, mock_storage_client):

functions-python/validation_report_processor/.coveragerc renamed to functions-python/process_validation_report/.coveragerc

File renamed without changes.

functions-python/validation_report_processor/.env.rename_me renamed to functions-python/process_validation_report/.env.rename_me

File renamed without changes.
File renamed without changes.

functions-python/validation_report_processor/function_config.json renamed to functions-python/process_validation_report/function_config.json

File renamed without changes.

functions-python/validation_report_processor/requirements.txt renamed to functions-python/process_validation_report/requirements.txt

File renamed without changes.

functions-python/validation_report_processor/requirements_dev.txt renamed to functions-python/process_validation_report/requirements_dev.txt

File renamed without changes.

functions-python/validation_report_processor/src/__init__.py renamed to functions-python/process_validation_report/src/__init__.py

File renamed without changes.

functions-python/validation_report_processor/src/main.py renamed to functions-python/process_validation_report/src/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ def generate_report_entities(
148148

149149
dataset = get_dataset(dataset_stable_id, session)
150150
dataset.validation_reports.append(validation_report_entity)
151+
152+
if (
153+
"summary" in json_report
154+
and "feedInfo" in json_report["summary"]
155+
and "feedServiceWindowStart" in json_report["summary"]["feedInfo"]
156+
and "feedServiceWindowEnd" in json_report["summary"]["feedInfo"]
157+
):
158+
dataset.service_date_range_start = json_report["summary"]["feedInfo"][
159+
"feedServiceWindowStart"
160+
]
161+
dataset.service_date_range_end = json_report["summary"]["feedInfo"][
162+
"feedServiceWindowEnd"
163+
]
164+
151165
for feature_name in json_report["summary"]["gtfsFeatures"]:
152166
feature = get_feature(feature_name, session)
153167
feature.validations.append(validation_report_entity)

0 commit comments

Comments
 (0)