Skip to content

Commit 248e591

Browse files
authored
chore: update big query ingestion logs (#1210)
1 parent fe4ea3e commit 248e591

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

functions-python/big_query_ingestion/src/common/bq_data_transfer.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ def create_bigquery_dataset(self):
2929
dataset_ref = bigquery.DatasetReference(project_id, dataset_id)
3030
try:
3131
self.bigquery_client.get_dataset(dataset_ref)
32-
logging.info(f"Dataset {dataset_id} already exists.")
32+
logging.info("Dataset %s already exists.", dataset_id)
3333
except Exception:
3434
dataset = bigquery.Dataset(dataset_ref)
3535
dataset.location = dataset_location
3636
self.bigquery_client.create_dataset(dataset)
37-
logging.info(f"Created dataset {dataset_id}.")
37+
logging.info("Created dataset %s", dataset_id)
3838

3939
def create_bigquery_table(self):
4040
"""Creates a BigQuery table if it does not exist."""
@@ -43,7 +43,7 @@ def create_bigquery_table(self):
4343

4444
try:
4545
self.bigquery_client.get_table(table_ref)
46-
logging.info(f"Table {table_id} already exists.")
46+
logging.info("Table %s already exists.", table_id)
4747
except Exception:
4848
if self.schema_path is None:
4949
raise Exception("Schema path is not provided")
@@ -53,7 +53,10 @@ def create_bigquery_table(self):
5353
table = bigquery.Table(table_ref, schema=schema)
5454
table = self.bigquery_client.create_table(table)
5555
logging.info(
56-
f"Created table {table.project}.{table.dataset_id}.{table.table_id}"
56+
"Created table %s.%s.%s",
57+
table.project,
58+
table.dataset_id,
59+
table.table_id,
5760
)
5861

5962
def load_data_to_bigquery(self):
@@ -68,7 +71,7 @@ def load_data_to_bigquery(self):
6871
for blob in blobs:
6972
uri = f"gs://{bucket_name}/{blob.name}"
7073
source_uris.append(uri)
71-
logging.info(f"Found {len(source_uris)} files to load to BigQuery.")
74+
logging.info("Found %s files to load to BigQuery.", len(source_uris))
7275

7376
if len(source_uris) > 0:
7477
# Load the data to BigQuery
@@ -82,29 +85,35 @@ def load_data_to_bigquery(self):
8285
try:
8386
load_job.result() # Wait for the job to complete
8487
logging.info(
85-
f"Loaded {len(source_uris)} files into "
86-
f"{table_ref.project}.{table_ref.dataset_id}.{table_ref.table_id}"
88+
"Loaded %s files into %s.%s.%s.",
89+
len(source_uris),
90+
table_ref.project,
91+
table_ref.dataset_id,
92+
table_ref.table_id,
8793
)
8894
# If successful, delete the blobs
8995
for blob in blobs:
9096
blob.delete()
91-
logging.info(f"Deleted blob: {blob.name}")
97+
logging.debug("Deleted blob: %s", blob.name)
98+
logging.info("Deleted blobs")
9299
except Exception as e:
93-
logging.error(f"An error occurred while loading data to BigQuery: {e}")
100+
logging.error("An error occurred while loading data to BigQuery: %s", e)
94101
for error in load_job.errors:
95-
logging.error(f"Error: {error['message']}")
102+
logging.error("Error: %s", error["message"])
96103
if "location" in error:
97-
logging.error(f"Location: {error['location']}")
104+
logging.error("Location: %s", error["location"])
98105
if "reason" in error:
99-
logging.error(f"Reason: {error['reason']}")
106+
logging.error("Reason: %s", error["reason"])
100107

101108
def send_data_to_bigquery(self):
102109
"""Full process to send data to BigQuery."""
103110
try:
104111
self.create_bigquery_dataset()
105112
self.create_bigquery_table()
106113
self.load_data_to_bigquery()
107-
return "Data successfully loaded to BigQuery", 200
114+
msg = "Data successfully loaded to BigQuery"
115+
logging.info(msg)
116+
return msg, 200
108117
except Exception as e:
109-
logging.error(f"An error occurred: {e}")
118+
logging.error("An error occurred: %s", e)
110119
return f"Error while loading data: {e}", 500

functions-python/big_query_ingestion/src/main.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
import functions_framework
44

5-
from shared.helpers.logger import Logger
5+
from shared.helpers.logger import init_logger
66
from gbfs.gbfs_big_query_ingest import BigQueryDataTransferGBFS
77
from gtfs.gtfs_big_query_ingest import BigQueryDataTransferGTFS
88

9-
logging.basicConfig(level=logging.INFO)
9+
init_logger()
1010

1111

1212
@functions_framework.http
1313
def ingest_data_to_big_query_gtfs(_):
1414
"""Google Storage to Big Query data ingestion for GTFS data"""
15-
Logger.init_logger()
1615
logging.info("Function triggered")
1716
return BigQueryDataTransferGTFS().send_data_to_bigquery()
1817

1918

2019
@functions_framework.http
2120
def ingest_data_to_big_query_gbfs(_):
2221
"""Google Storage to Big Query data ingestion for GBFS data"""
23-
Logger.init_logger()
2422
logging.info("Function triggered")
2523
return BigQueryDataTransferGBFS().send_data_to_bigquery()

functions-python/big_query_ingestion/tests/test_main.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99

1010
class TestMain(unittest.TestCase):
1111
@patch("main.BigQueryDataTransferGTFS")
12-
@patch("shared.helpers.logger.Logger.init_logger")
13-
@patch("main.logging.info")
14-
def test_ingest_data_to_big_query_gtfs(
15-
self, mock_logging_info, mock_init_logger, mock_big_query_transfer_gtfs
16-
):
12+
def test_ingest_data_to_big_query_gtfs(self, mock_big_query_transfer_gtfs):
1713
mock_instance = mock_big_query_transfer_gtfs.return_value
1814
mock_instance.send_data_to_bigquery.return_value = (
1915
"Data successfully loaded to BigQuery",
@@ -22,17 +18,11 @@ def test_ingest_data_to_big_query_gtfs(
2218

2319
response = ingest_data_to_big_query_gtfs(None)
2420

25-
mock_init_logger.assert_called_once()
26-
mock_logging_info.assert_any_call("Function triggered")
2721
mock_instance.send_data_to_bigquery.assert_called_once()
2822
self.assertEqual(response, ("Data successfully loaded to BigQuery", 200))
2923

3024
@patch("main.BigQueryDataTransferGBFS")
31-
@patch("shared.helpers.logger.Logger.init_logger")
32-
@patch("main.logging.info")
33-
def test_ingest_data_to_big_query_gbfs(
34-
self, mock_logging_info, mock_init_logger, mock_biq_query_transfer_gbfs
35-
):
25+
def test_ingest_data_to_big_query_gbfs(self, mock_biq_query_transfer_gbfs):
3626
mock_instance = mock_biq_query_transfer_gbfs.return_value
3727
mock_instance.send_data_to_bigquery.return_value = (
3828
"Data successfully loaded to BigQuery",
@@ -41,7 +31,5 @@ def test_ingest_data_to_big_query_gbfs(
4131

4232
response = ingest_data_to_big_query_gbfs(None)
4333

44-
mock_init_logger.assert_called_once()
45-
mock_logging_info.assert_any_call("Function triggered")
4634
mock_instance.send_data_to_bigquery.assert_called_once()
4735
self.assertEqual(response, ("Data successfully loaded to BigQuery", 200))

0 commit comments

Comments
 (0)