Python Cloud Functions添加日志以查看发生了什么

时间:2019-10-11 15:59:18

标签: python google-cloud-firestore google-cloud-functions

我正在使用Python处理存储在Firestore中的数据。我已经在本地计算机上创建了一个函数来更新Firestore中的数据,但是现在我想添加一个云功能,以便每天使用Pub / sub和云调度程序自动更新。

这是我正在使用的功能:

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)     

我想将其放在google函数中,这里有两个问题:

  1. 我收到一个错误消息是因为当函数被触发时-好像它传递了一些东西,并且由于def只是()而导致了错误。

我已阅读到我必须在函数中放入这两个变量:https://cloud.google.com/functions/docs/calling/pubsub?hl=en

def My_function (event, context):

我不知道该变量有什么用,我必须在函数中更改哪些才能使用此变量?

  1. 我想在运行该函数后编写日志,以便知道该函数是否启动正确以及何时完成,还想知道如果出现问题,该如何写,我读到可以使用google.cloud来完成。日志记录:Google Cloud Functions Python Logging issue

但是我不知道如何在我的函数中实现此日志,我对此没有太多经验

1 个答案:

答案 0 :(得分:2)

调用update_blanklabelcom__persons()的pubsub Cloud Function看起来像这样:

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")

您不需要使用eventcontext变量。它们提供有关调用该函数的pubsub事件的更多信息。要编写日志,可以使用print语句。您打印的所有内容都会显示在带有时间戳的Cloud Function日志中:

enter image description here

我从控制台as described here部署了此Cloud Function。这是requirements.txt

google-cloud-firestore>=1.4.0