Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Fetches crashes and iterate over and process them.""" | |
| 6 | |
| 7 import os | |
| 8 | |
| 9 import remote_api | |
| 10 | |
| 11 _DEFAULT_BATCH_SIZE = 1000 | |
| 12 | |
| 13 | |
| 14 def GetCrashInfo(crash, crash_info_fields): | |
|
wrengr
2016/10/05 18:41:58
This seems very unclean. Why not just use the |cra
Sharu Jiang
2016/10/06 18:16:56
This is for saving memory especially for the batch
| |
| 15 """Gets crash_info_fields from crash object. Returns dict.""" | |
| 16 crash_info = {} | |
| 17 for field in crash_info_fields: | |
| 18 if hasattr(crash, field): | |
| 19 crash_info[field] = getattr(crash, field) | |
| 20 else: | |
| 21 crash_info[field] = None | |
| 22 crash_info['id'] = crash.key.id() | |
| 23 return crash_info | |
| 24 | |
| 25 | |
| 26 def IterateCrashes(query, | |
| 27 crash_info_fields, | |
| 28 callback_func, | |
|
wrengr
2016/10/05 18:41:58
See my note about callback functions vs generators
Sharu Jiang
2016/10/06 18:16:56
Acknowledged.
| |
| 29 filter_func=None, | |
| 30 batch_size=_DEFAULT_BATCH_SIZE, | |
| 31 batch_run=False): | |
| 32 """Pulls crashes and passes needed info to callback function. | |
| 33 | |
| 34 Args: | |
| 35 query (ndb.Query): The query to fetch crahes. | |
| 36 crash_info_fields (list): Field names of a crashto pass over to the | |
| 37 callback function. All supported fields are in class Testcase in | |
| 38 clusterfuzz/src/common/data_types.py. If a given field name is not | |
| 39 available, it is set to None. 'testcase_id' is always added by default | |
| 40 as an integer. | |
| 41 callback_func (function): A function with a dict as the single parameter. | |
| 42 For example: | |
| 43 callback(testcase_info): | |
| 44 print 'Testcase %d has fields:' % testcase_info['testcase_id'] | |
| 45 for name, value in testcase_info.iteritems(): | |
| 46 print ' %s: %s' % (name, value) | |
| 47 filter_func (function): A function that filters crashes. | |
| 48 batch_size (int): The number of crashes to query at one time. | |
| 49 batch_run (bool): If True, run callback_func on a batch of crashes, if | |
| 50 False, run callback_func on each crash. | |
| 51 | |
| 52 An exmaple is available in crash_printer/print_crash.py. | |
| 53 """ | |
| 54 remote_api.EnableRemoteApi(app_id=os.getenv('APP_ID')) | |
| 55 | |
| 56 cursor = None | |
| 57 while True: | |
| 58 crashes, next_cursor, more = query.fetch_page(batch_size, | |
| 59 start_cursor=cursor) | |
| 60 | |
| 61 if not more: | |
| 62 break | |
| 63 | |
| 64 if filter_func: | |
| 65 crashes = filter_func(crashes) | |
| 66 | |
| 67 crashes = [GetCrashInfo(crash, crash_info_fields) for crash in crashes] | |
| 68 | |
| 69 if batch_run: | |
| 70 callback_func(crashes) | |
| 71 else: | |
| 72 for crash in crashes: | |
| 73 callback_func(crash) | |
| 74 | |
| 75 cursor = next_cursor | |
| OLD | NEW |