Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sample for commit stats #241

Merged
merged 10 commits into from Feb 26, 2021
Merged
44 changes: 44 additions & 0 deletions samples/samples/snippets.py
Expand Up @@ -24,6 +24,7 @@
import base64
import datetime
import decimal
import logging

from google.cloud import spanner
from google.cloud.spanner_v1 import param_types
Expand Down Expand Up @@ -969,6 +970,46 @@ def insert_singers(transaction):
# [END spanner_dml_standard_insert]


# [START spanner_get_commit_stats]
def log_commit_stats(instance_id, database_id):
"""Inserts sample data using DML and displays the commit statistics. """
# By default, commit statistics are logged via stdout at level Info.
# This sample uses a custom logger to access the commit statistics.
class CommitStatsSampleLogger(logging.Logger):

def __init__(self):
larkee marked this conversation as resolved.
Show resolved Hide resolved
self._last_commit_stats = None

def info(self, msg, *args, **kwargs):
if kwargs['extras'] and 'commitStats' in kwargs['extras']:
self._last_commit_stats = kwargs['extras']['commitStats']
super().self.info(msg)

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id, logger=CommitStatsSampleLogger())
database.log_commit_stats = True

def insert_singers(transaction):
row_ct = transaction.execute_update(
"INSERT Singers (SingerId, FirstName, LastName) "
" VALUES (110, 'Virginia', 'Watson')"
)

print("{} record(s) inserted.".format(row_ct))

database.run_in_transaction(insert_singers)
commit_stats = database.logger._last_commit_stats
larkee marked this conversation as resolved.
Show resolved Hide resolved
print(
"{} mutation(s) in transaction.".format(
commit_stats.mutation_count
)
)


larkee marked this conversation as resolved.
Show resolved Hide resolved
# [END spanner_get_commit_stats]


def update_data_with_dml(instance_id, database_id):
"""Updates sample data from the database using a DML statement. """
# [START spanner_dml_standard_update]
Expand Down Expand Up @@ -1710,6 +1751,7 @@ def create_client_with_query_options(instance_id, database_id):
"query_nested_struct_field", help=query_nested_struct_field.__doc__
)
subparsers.add_parser("insert_data_with_dml", help=insert_data_with_dml.__doc__)
subparsers.add_parser("log_commit_stats", help=log_commit_stats.__doc__)
subparsers.add_parser("update_data_with_dml", help=update_data_with_dml.__doc__)
subparsers.add_parser("delete_data_with_dml", help=delete_data_with_dml.__doc__)
subparsers.add_parser(
Expand Down Expand Up @@ -1820,6 +1862,8 @@ def create_client_with_query_options(instance_id, database_id):
query_nested_struct_field(args.instance_id, args.database_id)
elif args.command == "insert_data_with_dml":
insert_data_with_dml(args.instance_id, args.database_id)
elif args.command == "log_commit_stats":
log_commit_stats(args.instance_id, args.database_id)
elif args.command == "update_data_with_dml":
update_data_with_dml(args.instance_id, args.database_id)
elif args.command == "delete_data_with_dml":
Expand Down
7 changes: 7 additions & 0 deletions samples/samples/snippets_test.py
Expand Up @@ -236,6 +236,13 @@ def test_insert_data_with_dml(capsys):
assert "1 record(s) inserted." in out


def test_log_commit_stats(capsys):
snippets.log_commit_stats(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert "1 record(s) inserted." in out
assert "3 mutations(s) in transaction." in out


def test_update_data_with_dml(capsys):
snippets.update_data_with_dml(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
Expand Down