Source code for bigquery.bigquery_edit_order_report_gateway
from exceptions.task_exception import BigQueryTableNotFoundError, BigQueryRowsNotSentError
[docs]class BigQueryEditOrderReportGateway:
[docs] def __init__(self, client, application_dependencies):
"""
Class to manage the big query executions for edit order notifications
Sets the client variable for use within the class
Args:
client (google.cloud.bigquery.client.Client): Initialised Bigquery Client Object
application_dependencies (util.application_dependencies.ApplicationDependencies): Application dependency obj
"""
self.client = client
self.application_dependencies = application_dependencies
[docs] def post_report_of_notifications(self, report_of_notification_process):
"""
This method will send the report generated from the notification process to bigquery
Args:
report_of_notification_process (list): List of dicts, where keys must match the table schema fields
Returns:
bool: True if the transaction completed successfully in and was added to big query
Raises:
BigQueryTableNotFoundError: If the report table name could not be found using big query client
BigQueryRowsNotSentError: If the rows to add into the notification report big query table were not inserted
Examples:
Example report_of_notification_process arg:
.. code-block:: python
[{"OrderId": "123456789", "RetailerCustomerId": "12345", "Sms": True, "Email": True,
"NotificationStatus": "Successfully sent communications",
"Timestamp": "2020-02-03 13:50:53"},
{"OrderId": "246835791", "RetailerCustomerId": "24680", "Sms": False, "Email": True,
"NotificationStatus": "Failed to send SMS",
"Timestamp": "2020-02-03 13:50:59"}]
"""
logger = self.application_dependencies.get_default_logger()
table = self.application_dependencies.get_report_table()
try:
table_obj = self.client.get_table(table)
except Exception as e:
logger.error(
"An Error occurred finding the Notification Report BigQuery table. Error: {error}".format(error=e))
raise BigQueryTableNotFoundError
try:
errors = self.client.insert_rows_json(table_obj, report_of_notification_process)
# By default, if any rows are invalid / wrong format for the big query schema it will fail and will not
# send any of the rows (can be changed in the function parameters)
except Exception as e:
logger.error(
"An Error occurred attempting to insert report rows to the Notification Report BigQuery table. Error: "
"{error}".format(error=e))
raise BigQueryRowsNotSentError
if errors:
logger.error(
"An Error occurred attempting to insert report rows to the Notification Report BigQuery table. "
"Error: {error}".format(error=errors))
raise BigQueryRowsNotSentError
logger.info("{rows} notification report orders sent to bigquery table.".format
(rows=len(report_of_notification_process)))
return True