From 718fd01763d7720ee2d0c2eed5a942dad7d5b2aa Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 8 Oct 2018 18:15:54 -0700 Subject: [PATCH] Add analytics sample + switch to static values in tests Change-Id: I0b78475a513db9d85cba2a17044680a7f6dae5db --- functions/firebase/main.py | 30 +++++++++++++++ functions/firebase/main_test.py | 66 ++++++++++++++++++++++----------- 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/functions/firebase/main.py b/functions/firebase/main.py index b0f79e76881d..0e24d93d748f 100644 --- a/functions/firebase/main.py +++ b/functions/firebase/main.py @@ -12,6 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +# [START functions_firebase_analytics] +from datetime import datetime +# [END functions_firebase_analytics] + # [START functions_firebase_rtdb] # [START functions_firebase_firestore] # [START functions_firebase_auth] @@ -69,3 +73,29 @@ def hello_auth(data, context): if 'email' in data: print('Email: %s' % data["email"]) # [END functions_firebase_auth] + + +# [START functions_firebase_analytics] +def hello_analytics(data, context): + print(data) + print(context) + """ Triggered by a Google Analytics for Firebase log event. + Args: + data (dict): The event payload. + context (google.cloud.functions.Context): Metadata for the event. + """ + trigger_resource = context.resource + print(f'Function triggered by the following event: {trigger_resource}') + + event = data["eventDim"][0] + print(f'Name: {event["name"]}') + + event_timestamp = int(event["timestampMicros"][:-6]) + print(f'Timestamp: {datetime.utcfromtimestamp(event_timestamp)}') + + user_obj = data["userDim"] + print(f'Device Model: {user_obj["deviceInfo"]["deviceModel"]}') + + geo_info = user_obj["geoInfo"] + print(f'Location: {geo_info["city"]}, {geo_info["country"]}') +# [END functions_firebase_analytics] diff --git a/functions/firebase/main_test.py b/functions/firebase/main_test.py index df30af461344..ff10bf672e60 100644 --- a/functions/firebase/main_test.py +++ b/functions/firebase/main_test.py @@ -15,8 +15,6 @@ from datetime import datetime import json -import uuid - import main @@ -25,61 +23,87 @@ class Context(object): def test_rtdb(capsys): - data_id = str(uuid.uuid4()) - resource_id = str(uuid.uuid4()) - data = { 'admin': True, - 'delta': {'id': data_id} + 'delta': {'id': 'my-data'} } context = Context() - context.resource = resource_id + context.resource = 'my-resource' main.hello_rtdb(data, context) out, _ = capsys.readouterr() - assert ('Function triggered by change to: %s' % resource_id) in out + assert 'Function triggered by change to: my-resource' in out assert 'Admin?: True' in out - assert data_id in out + assert 'my-data' in out def test_firestore(capsys): - resource_id = str(uuid.uuid4()) - context = Context() - context.resource = resource_id + context.resource = 'my-resource' data = { - 'oldValue': {'uuid': str(uuid.uuid4())}, - 'value': {'uuid': str(uuid.uuid4())} + 'oldValue': {'a': 1}, + 'value': {'b': 2} } main.hello_firestore(data, context) out, _ = capsys.readouterr() - assert ('Function triggered by change to: %s' % resource_id) in out + assert 'Function triggered by change to: my-resource' in out assert json.dumps(data['oldValue']) in out assert json.dumps(data['value']) in out def test_auth(capsys): - user_id = str(uuid.uuid4()) date_string = datetime.now().isoformat() - email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4()) data = { - 'uid': user_id, + 'uid': 'my-user', 'metadata': {'createdAt': date_string}, - 'email': email_string + 'email': 'me@example.com' } main.hello_auth(data, None) out, _ = capsys.readouterr() - assert user_id in out + assert 'Function triggered by creation/deletion of user: my-user' in out assert date_string in out - assert email_string in out + assert 'Email: me@example.com' in out + + +def test_analytics(capsys): + timestamp = int(datetime.utcnow().timestamp()) + + data = { + 'eventDim': [{ + 'name': 'my-event', + 'timestampMicros': f'{str(timestamp)}000000' + }], + 'userDim': { + 'deviceInfo': { + 'deviceModel': 'Pixel' + }, + 'geoInfo': { + 'city': 'London', + 'country': 'UK' + } + } + } + + context = Context() + context.resource = 'my-resource' + + main.hello_analytics(data, context) + + out, _ = capsys.readouterr() + + assert 'Function triggered by the following event: my-resource' in out + assert f'Timestamp: {datetime.utcfromtimestamp(timestamp)}' in out + assert 'Name: my-event' in out + assert 'Device Model: Pixel' in out + assert 'Location: London, UK' in out