Skip to content

Commit bb35112

Browse files
authored
chore: improve logs on update tasks functions (#1219)
1 parent 11b115b commit bb35112

File tree

10 files changed

+50
-41
lines changed

10 files changed

+50
-41
lines changed

api/src/shared/database/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def refresh_materialized_view(session: "Session", view_name: str) -> bool:
9696
session.execute(text(f"REFRESH MATERIALIZED VIEW CONCURRENTLY {view_name}"))
9797
return True
9898
except Exception as error:
99-
logging.error(f"Error raised while refreshing view: {error}")
99+
logging.error("Error raised while refreshing view: %s", error)
100100
return False
101101

102102

functions-python/backfill_dataset_service_date_range/src/main.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import functions_framework
44

5-
from shared.helpers.logger import Logger
5+
from shared.helpers.logger import init_logger
66

77
from shared.database.database import with_db_session, refresh_materialized_view
88

@@ -28,7 +28,7 @@
2828
env = os.getenv("ENV", "dev").lower()
2929
bucket_name = f"mobilitydata-datasets-{env}"
3030

31-
logging.basicConfig(level=logging.INFO)
31+
init_logger()
3232

3333

3434
def is_version_gte(target_version: str, version_field):
@@ -66,10 +66,10 @@ def backfill_datasets(session: "Session"):
6666
)
6767
).all()
6868

69-
logging.info(f"Found {len(datasets)} datasets to process.")
69+
logging.info("Found %s datasets to process.", len(datasets))
7070

7171
for dataset in datasets:
72-
logging.info(f"Processing gtfsdataset ID {dataset.stable_id}")
72+
logging.info("Processing gtfsdataset ID %s", dataset.stable_id)
7373
gtfsdataset_id = dataset.stable_id
7474
feed_stable_id = "-".join(gtfsdataset_id.split("-")[0:2])
7575
# Get the latest validation report for the dataset
@@ -81,7 +81,8 @@ def backfill_datasets(session: "Session"):
8181

8282
if not latest_validation_report:
8383
logging.info(
84-
f"Skipping gtfsdataset ID {gtfsdataset_id}: no validation reports found."
84+
"Skipping gtfsdataset ID %s: no validation reports found.",
85+
gtfsdataset_id,
8586
)
8687
continue
8788

@@ -90,7 +91,7 @@ def backfill_datasets(session: "Session"):
9091
try:
9192
# Download the JSON report
9293
blob_url = f"{feed_stable_id}/{gtfsdataset_id}/report_{latest_validation_report.validator_version}.json"
93-
logging.info("Blob URL: " + blob_url)
94+
logging.info("Blob URL: %s", blob_url)
9495
dataset_blob = bucket.blob(blob_url)
9596
if not dataset_blob.exists():
9697
logging.info("Blob not found, downloading from URL")
@@ -102,7 +103,9 @@ def backfill_datasets(session: "Session"):
102103
logging.info("Blob found, downloading from blob")
103104
json_data = json.loads(dataset_blob.download_as_string())
104105
except Exception as e:
105-
logging.error(f"Error downloading blob: {e} trying json report url")
106+
logging.error(
107+
"Error downloading blob trying json report url: %s", e
108+
)
106109
response = requests.get(json_report_url)
107110
response.raise_for_status()
108111
json_data = response.json()
@@ -133,7 +136,9 @@ def backfill_datasets(session: "Session"):
133136

134137
formatted_dates = f"{utc_service_start_date:%Y-%m-%d %H:%M} - {utc_service_end_date:%Y-%m-%d %H:%M}"
135138
logging.info(
136-
f"Updated gtfsdataset ID {gtfsdataset_id} with value: {formatted_dates}"
139+
"Updated gtfsdataset ID %s with value: ",
140+
gtfsdataset_id,
141+
formatted_dates,
137142
)
138143
total_changes_count += 1
139144
changes_count += 1
@@ -151,32 +156,31 @@ def backfill_datasets(session: "Session"):
151156

152157
except requests.RequestException as e:
153158
logging.error(
154-
f"Error downloading JSON for gtfsdataset ID {gtfsdataset_id}: {e}"
159+
"Error downloading JSON for gtfsdataset ID %s: %s", gtfsdataset_id, e
155160
)
156161
except json.JSONDecodeError as e:
157162
logging.error(
158-
f"Error parsing JSON for gtfsdataset ID {gtfsdataset_id}: {e}"
163+
"Error parsing JSON for gtfsdataset ID %s: %s", gtfsdataset_id, e
159164
)
160165
except Exception as e:
161-
logging.error(f"Error processing gtfsdataset ID {gtfsdataset_id}: {e}")
166+
logging.error("Error processing gtfsdataset ID %s: %s", gtfsdataset_id, e)
162167

163168
try:
164169
session.commit()
165170
logging.info("Database changes committed.")
166171
session.close()
167172
return total_changes_count
168173
except Exception as e:
169-
logging.error("Error committing changes:", e)
174+
logging.error("Error committing changes: %s", e)
170175
session.rollback()
171176
session.close()
172-
raise Exception(f"Error creating dataset: {e}")
177+
raise Exception("Error creating dataset: %s", e)
173178

174179

175180
@functions_framework.http
176181
@with_db_session
177182
def backfill_dataset_service_date_range(_, db_session: Session):
178183
"""Fills gtfs dataset service date range from the latest validation report."""
179-
Logger.init_logger()
180184
change_count = 0
181185
try:
182186
logging.info("Database session started.")
@@ -186,4 +190,6 @@ def backfill_dataset_service_date_range(_, db_session: Session):
186190
logging.error(f"Error setting the datasets service date range values: {error}")
187191
return f"Error setting the datasets service date range values: {error}", 500
188192

189-
return f"Script executed successfully. {change_count} datasets updated", 200
193+
result = f"Script executed successfully. {change_count} datasets updated"
194+
logging.info(result)
195+
return result, 200

functions-python/backfill_dataset_service_date_range/tests/test_backfill_dataset_service_date_range_main.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,9 @@ def test_backfill_datasets_service_date_range_swap(mock_get, mock_storage_client
164164
mock_session.commit.assert_called_once()
165165

166166

167-
@patch("logging.error", autospec=True)
168167
@patch("google.cloud.storage.Client", autospec=True)
169168
@patch("requests.get")
170-
def test_backfill_datasets_error_commit(mock_get, mock_storage_client, mock_logger):
169+
def test_backfill_datasets_error_commit(mock_get, mock_storage_client):
171170
# Mock the storage client and bucket
172171
mock_bucket = MagicMock()
173172
mock_client_instance = mock_storage_client.return_value
@@ -412,9 +411,8 @@ def test_backfill_datasets_fail_to_get_validation_report(mock_get, mock_storage_
412411
mock_session.commit.assert_called_once()
413412

414413

415-
@patch("main.Logger", autospec=True)
416414
@patch("main.backfill_datasets")
417-
def test_backfill_dataset_service_date_range(mock_backfill_datasets, mock_logger):
415+
def test_backfill_dataset_service_date_range(mock_backfill_datasets):
418416
mock_backfill_datasets.return_value = 5
419417

420418
with patch.dict(os.environ, {"FEEDS_DATABASE_URL": default_db_url}):
@@ -425,11 +423,8 @@ def test_backfill_dataset_service_date_range(mock_backfill_datasets, mock_logger
425423
assert status_code == 200
426424

427425

428-
@patch("main.Logger", autospec=True)
429426
@patch("main.backfill_datasets")
430-
def test_backfill_dataset_service_date_range_error_raised(
431-
mock_backfill_datasets, mock_logger
432-
):
427+
def test_backfill_dataset_service_date_range_error_raised(mock_backfill_datasets):
433428
mock_backfill_datasets.side_effect = Exception("Mocked exception")
434429

435430
with patch.dict(os.environ, {"FEEDS_DATABASE_URL": default_db_url}):

functions-python/helpers/feed_status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def get_filters(status: str):
7070
.update({Feed.status: status}, synchronize_session=False)
7171
)
7272
except Exception as e:
73-
logging.error(f"Error updating feed statuses: {e}")
73+
logging.error("Error updating feed statuses: %s", e)
7474
raise Exception(f"Error updating feed statuses: {e}")
7575

7676
try:

functions-python/tasks_executor/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ This directory contains Google Cloud Functions used as a single point of access
55
## Usage
66
The function receive the following payload:
77
```
8-
{
8+
{
99
"task": "string", # [required] Name of the task to execute
1010
"payload": { } [optional] Payload to pass to the task
11-
}
12-
"payload": {
11+
}
12+
```
13+
14+
Example:
15+
```json
16+
{
17+
"task": "rebuild_missing_validation_reports",
18+
"payload": {
1319
"dry_run": true,
1420
"filter_after_in_days": 14,
1521
"filter_statuses": ["active", "inactive", "future"]

functions-python/tasks_executor/src/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import flask
2020
import functions_framework
21-
21+
from shared.helpers.logger import init_logger
2222
from tasks.validation_reports.rebuild_missing_validation_reports import (
2323
rebuild_missing_validation_reports_handler,
2424
)
2525

26+
27+
init_logger()
2628
LIST_COMMAND: Final[str] = "list"
2729
tasks = {
2830
"list_tasks": {

functions-python/tasks_executor/src/tasks/validation_reports/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
This task generates the missing reports in the GTFS datasets.
44
The reports are generated using the _gtfs_validator_ GCP workflow.
55

6+
## Task ID
7+
Use task Id: `rebuild_missing_validation_reports`
8+
69
## Usage
710
The function receive the following payload:
811
```
@@ -13,13 +16,13 @@ The function receive the following payload:
1316
}
1417
```
1518
Example:
16-
``
19+
```
1720
{
1821
"dry_run": true,
1922
"filter_after_in_days": 14,
2023
"filter_statuses": ["active", "inactive", "future"]
2124
}
22-
`````
25+
```
2326

2427
# GCP environment variables
2528
The function uses the following environment variables:

functions-python/tasks_executor/src/tasks/validation_reports/rebuild_missing_validation_reports.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
from shared.helpers.query_helper import get_datasets_with_missing_reports_query
3131
from shared.helpers.validation_report.validation_report_update import execute_workflows
3232

33-
logging.basicConfig(level=logging.INFO)
34-
3533
QUERY_LIMIT: Final[int] = 100
3634

3735

@@ -130,7 +128,7 @@ def rebuild_missing_validation_reports(
130128
if dry_run
131129
else "Rebuild missing validation reports task executed successfully."
132130
)
133-
return {
131+
result = {
134132
"message": message,
135133
"total_processed": total_processed,
136134
"params": {
@@ -141,6 +139,8 @@ def rebuild_missing_validation_reports(
141139
"validator_endpoint": validator_endpoint,
142140
},
143141
}
142+
logging.info(result)
143+
return result
144144

145145

146146
def get_parameters(payload):

functions-python/update_feed_status/src/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import logging
22
import functions_framework
3-
from shared.helpers.logger import Logger
3+
from shared.helpers.logger import init_logger
44
from shared.helpers.feed_status import update_feed_statuses_query
55
from shared.database.database import with_db_session
66

7-
logging.basicConfig(level=logging.INFO)
7+
init_logger()
88

99

1010
@with_db_session
1111
@functions_framework.http
1212
def update_feed_status(_, db_session):
1313
"""Updates the Feed status based on the latets dataset service date range."""
14-
Logger.init_logger()
1514
try:
1615
logging.info("Database session started.")
1716
diff_counts = update_feed_statuses_query(db_session, [])

functions-python/update_feed_status/tests/test_update_feed_status_main.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ def test_update_feed_status_failed_query():
108108
assert str(e) == "Error updating feed statuses: Mocked exception"
109109

110110

111-
@patch("main.Logger", autospec=True)
112111
@patch("main.update_feed_statuses_query")
113-
def test_updated_feed_status(mock_update_query, mock_logger):
112+
def test_updated_feed_status(mock_update_query):
114113
return_value = {"active": 5}
115114
mock_update_query.return_value = return_value
116115

@@ -122,9 +121,8 @@ def test_updated_feed_status(mock_update_query, mock_logger):
122121
assert status_code == 200
123122

124123

125-
@patch("main.Logger", autospec=True)
126124
@patch("main.update_feed_statuses_query")
127-
def test_updated_feed_status_error_raised(mock_update_query, mock_logger):
125+
def test_updated_feed_status_error_raised(mock_update_query):
128126
mock_update_query.side_effect = Exception("Mocked exception")
129127

130128
with patch.dict(os.environ, {"FEEDS_DATABASE_URL": default_db_url}):

0 commit comments

Comments
 (0)