本文整理汇总了Python中models.Report.get_or_insert方法的典型用法代码示例。如果您正苦于以下问题:Python Report.get_or_insert方法的具体用法?Python Report.get_or_insert怎么用?Python Report.get_or_insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Report
的用法示例。
在下文中一共展示了Report.get_or_insert方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_resource
# 需要导入模块: from models import Report [as 别名]
# 或者: from models.Report import get_or_insert [as 别名]
def process_resource(self, resource_entry):
"""Transform each ReportToProcess into a proper Report."""
# Load variables from stored entity
t = resource_entry.t
gbifdatasetid = resource_entry.gbifdatasetid
event = resource_entry.resource
logging.info("Processing %s" % gbifdatasetid)
# Extract useful information
number_of_records = event['records']
query_countries = [QueryCountry(**x)
for x in event['query_countries'].values()]
query_dates = [QueryDate(query_date=datetime.strptime(
x['query_date'],
'%Y-%m-%d'),
times=x['times'])
for x in event['query_dates'].values()]
query_terms = [QueryTerms(**x) for x in event['query_terms'].values()]
# Build report ID
report_id = "|".join([self.period, gbifdatasetid])
# Build dataset key
dataset_key = ndb.Key("Dataset", gbifdatasetid)
# Build period key
period_key = ndb.Key("Period", self.period)
# QC
sum_query_countries = 0
for i in event['query_countries'].values():
sum_query_countries += i['times']
sum_query_dates = 0
for i in event['query_dates'].values():
sum_query_dates += i['times']
sum_query_terms = 0
for i in event['query_terms'].values():
sum_query_terms += i['times']
if sum_query_countries != sum_query_dates or \
sum_query_countries != sum_query_terms or \
sum_query_dates != sum_query_terms:
logging.warning("WARNING: lengths of query entities keys list"
"do not match:")
logging.warning("Query countries: %d" % sum_query_countries)
logging.warning("Query dates: %d" % sum_query_dates)
logging.warning("Query terms: %d" % sum_query_terms)
number_of_events = max([sum_query_countries,
sum_query_countries,
sum_query_countries])
else:
number_of_events = sum_query_countries
# Get existing or create new Report entity
logging.info("Retrieving existing report or creating new one")
report = Report.get_or_insert(
report_id,
parent=period_key,
created=datetime.today(),
reported_period=period_key,
reported_resource=dataset_key,
searches=Search(
events=0,
records=0,
query_countries=[],
query_dates=[],
query_terms=[],
# status="in progress"
),
downloads=Download(
events=0,
records=0,
query_countries=[],
query_dates=[],
query_terms=[],
# status="in progress"
),
stored=False,
issue_sent=False
)
# Populate event data
logging.info("Storing %s data" % t)
if t == 'search':
report.searches.records = number_of_records
report.searches.events = number_of_events
report.searches.query_countries = query_countries
report.searches.query_dates = query_dates
report.searches.query_terms = query_terms
elif t == 'download':
report.downloads.records = number_of_records
report.downloads.events = number_of_events
report.downloads.query_countries = query_countries
report.downloads.query_dates = query_dates
#.........这里部分代码省略.........