Reputation: 1334
I am working with data stored in firestore with Python. I have created a function in my local machine to update my data in firestore but now I want to add a cloud function to update automatically every day using Pub/sub and cloud scheduler.
this is the function I am using:
from google.cloud import firestore
from datetime import datetime, timedelta
db = firestore.Client.from_service_account_json('credentials/gc.json')
def update_collection__persons():
persons = db.collection(u'collection__persons')
#person_docs = persons.stream()
person_docs = [snapshot for snapshot in persons.stream()]
for person_doc in person_docs:
person_dict = person_doc.to_dict()
#last_updated = person_dict['last_updated']
#last_processed = person_dict['last_processed']
#dt_last_updated = datetime(1, 1, 1) + timedelta(microseconds=last_updated/10)
#dt_last_processed = datetime(1, 1, 1) + timedelta(microseconds=last_processed/10)
#if dt_last_processed < dt_last_updated:
orders = db.collection(u'collection__orders').where(u'email', u'==', person_dict['email'])
orders_docs = [snapshot for snapshot in orders.stream()]
sum_price = 0
count = 0
date_add_list = []
for order_doc in orders_docs:
order_dict = order_doc.to_dict()
sum_price += order_dict['total_price']
count +=1
date_add_list.append(order_dict['dateAdded'])
if count > 0:
data = {'metrics': {'LTV': sum_price,
'AOV': sum_price/count,
'Quantity_orders': count,
'first_order_date': min(date_add_list),
'last_order_date': max(date_add_list)},
'last_processed': int((datetime.utcnow() - datetime(1, 1, 1)).total_seconds() * 10000000),
'delta_last_updated_last_processed': 0}
db.collection(u'collection__persons').document(person_dict['email']).set(data, merge = True)
I want to put it in google functions, I have two questions here:
I have read that I have to put this two variables in my function: https://cloud.google.com/functions/docs/calling/pubsub?hl=en
def My_function (event, context):
I don't understand what I have to do with this variables, what I have to change in my function to work with this variables?
But I don't know how to implement this logs in my function, I don't have to much experience in this
Upvotes: 0
Views: 578
Reputation: 6854
A pubsub Cloud Function that calls update_blanklabelcom__persons()
would look something like this:
from google.cloud import firestore
from datetime import datetime, timedelta
# Service account provided by Cloud Function environment
db = firestore.Client()
def update_blanklabelcom__persons():
persons = db.collection(u'collection__persons')
person_docs = [snapshot for snapshot in persons.stream()]
for person_doc in person_docs:
person_dict = person_doc.to_dict()
orders = db.collection(u'collection__orders').where(u'email', u'==', person_dict['email'])
orders_docs = [snapshot for snapshot in orders.stream()]
sum_price = 0
count = 0
date_add_list = []
for order_doc in orders_docs:
order_dict = order_doc.to_dict()
sum_price += order_dict['total_price']
count +=1
date_add_list.append(order_dict['dateAdded'])
if count > 0:
data = {'metrics': {'LTV': sum_price,
'AOV': sum_price/count,
'Quantity_orders': count,
'first_order_date': min(date_add_list),
'last_order_date': max(date_add_list)},
'last_processed': int((datetime.utcnow() - datetime(1, 1, 1)).total_seconds() * 10000000),
'delta_last_updated_last_processed': 0}
db.collection(u'collection__persons').document(person_dict['email']).set(data, merge = True)
def persons_pubsub(event, context):
print("update blanklabel started")
update_blanklabelcom__persons()
print("update blanklabel done")
You don't need to use the event
and context
variables. They provide extra information about the pubsub event that called the function. To write logs, you can use print
statements. Anything you print will show up in the Cloud Function logs with a timestamp:
I deployed this Cloud Function from the console, as described here. Here's the requirements.txt
:
google-cloud-firestore>=1.4.0
Upvotes: 2