Skip to content

Commit

Permalink
apply black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hernandezc1 committed Sep 8, 2024
1 parent 747414c commit d93dc85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion broker/cloud_functions/lvk/store_BigQuery/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ else # Deploy the Cloud Functions
--memory "${memory}" \
--trigger-topic "${store_bq_trigger_topic}" \
--set-env-vars TESTID="${testid}",SURVEY="${survey}",VERSIONTAG="${versiontag}",GCP_PROJECT="${GOOGLE_CLOUD_PROJECT}"
fi
fi
28 changes: 16 additions & 12 deletions broker/cloud_functions/lvk/store_BigQuery/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
TABLE = pittgoogle.Table.from_cloud(f"alerts_{VERSIONTAG}", survey=SURVEY, testid=TESTID)
TOPIC = pittgoogle.Topic.from_cloud("bigquery", survey=SURVEY, testid=TESTID, projectid=PROJECT_ID)


def run(event: dict, _context: functions_v1.context.Context) -> None:
"""Send alert data to various BigQuery tables.
Expand All @@ -35,25 +36,25 @@ def run(event: dict, _context: functions_v1.context.Context) -> None:
'context' is an unused argument in the function that is required
see https://cloud.google.com/functions/1stgendocs/writing/write-event-driven-functions#background-functions
"""

# decode the base64-encoded message data
decoded_data = base64.b64decode(event["data"])

# create a PubsubMessage-like object with the existing event dictionary
pubsub_message = pubsub_v1.types.PubsubMessage(
data=decoded_data,
attributes=event.get("attributes", {})
data=decoded_data, attributes=event.get("attributes", {})
)

# unpack the alert
alert = pittgoogle.Alert.from_msg(msg=pubsub_message,schema_name="default_schema")
alert = pittgoogle.Alert.from_msg(msg=pubsub_message, schema_name="default_schema")

# send the alert to BigQuery table
alert_table = insert_rows_alerts(alert)

# announce what's been done
TOPIC.publish(_create_outgoing_alert(alert, alert_table))


def insert_rows_alerts(alert: pittgoogle.alert.Alert):
"""Insert rows into the `alerts` table via the streaming API."""
# send to bigquery
Expand All @@ -69,14 +70,17 @@ def insert_rows_alerts(alert: pittgoogle.alert.Alert):

return table_dict

def _create_outgoing_alert(alert: pittgoogle.alert.Alert, table_dict: dict) -> pittgoogle.alert.Alert:

def _create_outgoing_alert(
alert: pittgoogle.alert.Alert, table_dict: dict
) -> pittgoogle.alert.Alert:
"""Create an announcement of the table storage operation to Pub/Sub."""
# collect attributes
attrs = {
**alert.attributes,
"alerts_table": table_dict['alerts_table'],
"alert_type": alert.dict['alert_type'],
"superevent_id": alert.dict['superevent_id'],
"alerts_table": table_dict["alerts_table"],
"alert_type": alert.dict["alert_type"],
"superevent_id": alert.dict["superevent_id"],
}

# set empty message body; everything is in the attributes
Expand All @@ -86,5 +90,5 @@ def _create_outgoing_alert(alert: pittgoogle.alert.Alert, table_dict: dict) -> p
alert_out = pittgoogle.Alert.from_dict(
payload=msg, attributes=attrs, schema_name="default_schema"
)
return alert_out

return alert_out
2 changes: 1 addition & 1 deletion broker/cloud_functions/lvk/store_BigQuery/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

google-cloud-functions
google-cloud-logging
pittgoogle-client>=0.3.11
pittgoogle-client>=0.3.11

0 comments on commit d93dc85

Please sign in to comment.