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 am getting an error because when function is triggers - looks like it passes something to it and it’s causing an error because the def is just ().
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?
- I want to write logs once I run the function in order to know if the function started well and when it finish, also know how to write if something it is wrong, I read that I can do it with google.cloud.logging :Google Cloud Functions Python Logging issue
But I don't know how to implement this logs in my function, I don't have to much experience in this