 Chromium Code Reviews
 Chromium Code Reviews Issue 2220373003:
  Allow botlist API call to respond to quarantined: and is_dead:  (Closed) 
  Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@master
    
  
    Issue 2220373003:
  Allow botlist API call to respond to quarantined: and is_dead:  (Closed) 
  Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@master| Index: appengine/swarming/handlers_endpoints.py | 
| diff --git a/appengine/swarming/handlers_endpoints.py b/appengine/swarming/handlers_endpoints.py | 
| index 5ced064a782d16430703831503df9e22a64268ce..b4f4036b0f64ce2f9b957fd0da146eb1d6fd1ad3 100644 | 
| --- a/appengine/swarming/handlers_endpoints.py | 
| +++ b/appengine/swarming/handlers_endpoints.py | 
| @@ -608,6 +608,7 @@ class SwarmingBotService(remote.Service): | 
| @swarming_api.api_class(resource_name='bots', path='bots') | 
| class SwarmingBotsService(remote.Service): | 
| """Bots-related API.""" | 
| + | 
| @gae_ts_mon.instrument_endpoint() | 
| @auth.endpoints_method( | 
| swarming_rpcs.BotsRequest, swarming_rpcs.BotList, | 
| @@ -620,14 +621,14 @@ class SwarmingBotsService(remote.Service): | 
| """ | 
| logging.info('%s', request) | 
| now = utils.utcnow() | 
| - q = bot_management.BotInfo.query().order(bot_management.BotInfo.key) | 
| - for d in request.dimensions: | 
| - if not ':' in d: | 
| - raise endpoints.BadRequestException('Invalid dimensions') | 
| - parts = d.split(':', 1) | 
| - if len(parts) != 2 or any(i.strip() != i or not i for i in parts): | 
| - raise endpoints.BadRequestException('Invalid dimensions') | 
| - q = q.filter(bot_management.BotInfo.dimensions_flat == d) | 
| + q = bot_management.BotInfo.query() | 
| + try: | 
| + q = bot_management.filter_dimensions(q, request.dimensions) | 
| + q = bot_management.filter_availability(q, request.quarantined, | 
| 
M-A Ruel
2016/08/09 20:23:13
I don't like this kind of alignment, indent all ar
 
kjlubick
2016/08/09 20:44:44
Done.
 | 
| + request.is_dead, now) | 
| + except ValueError as e: | 
| + raise endpoints.BadRequestException('%s' % e) | 
| 
M-A Ruel
2016/08/09 20:23:13
str(e)
 
kjlubick
2016/08/09 20:44:44
Done.
 | 
| + | 
| bots, cursor = datastore_utils.fetch_page(q, request.limit, request.cursor) | 
| return swarming_rpcs.BotList( | 
| cursor=cursor, | 
| @@ -645,25 +646,51 @@ class SwarmingBotsService(remote.Service): | 
| logging.info('%s', request) | 
| now = utils.utcnow() | 
| q = bot_management.BotInfo.query() | 
| - for d in request.dimensions: | 
| - parts = d.split(':', 1) | 
| - if len(parts) != 2 or any(i.strip() != i or not i for i in parts): | 
| - raise endpoints.BadRequestException('Invalid dimensions: %s' % d) | 
| - q = q.filter(bot_management.BotInfo.dimensions_flat == d) | 
| - f_count = q.count_async() | 
| - dt = datetime.timedelta(seconds=config.settings().bot_death_timeout_secs) | 
| - timeout = now - dt | 
| - f_dead = q.filter( | 
| - bot_management.BotInfo.last_seen_ts < timeout).count_async() | 
| - f_quarantined = q.filter( | 
| - bot_management.BotInfo.quarantined == True).count_async() | 
| - f_busy = q.filter(bot_management.BotInfo.is_busy == True).count_async() | 
| - return swarming_rpcs.BotsCount( | 
| - count=f_count.get_result(), | 
| - quarantined=f_quarantined.get_result(), | 
| - dead=f_dead.get_result(), | 
| - busy=f_busy.get_result(), | 
| - now=now) | 
| + try: | 
| + q = bot_management.filter_dimensions(q, request.dimensions) | 
| + except ValueError as e: | 
| + raise endpoints.BadRequestException('%s' % e) | 
| + | 
| + quarantined = swarming_rpcs.to_bool(request.quarantined) | 
| + is_dead = swarming_rpcs.to_bool(request.is_dead) | 
| + if quarantined is None and is_dead is None: | 
| + f_count = q.count_async() | 
| + f_dead = (bot_management.filter_availability(q, None, True, now) | 
| + .count_async()) | 
| + f_quarantined = (bot_management.filter_availability(q, True, None, now) | 
| + .count_async()) | 
| + f_busy = q.filter(bot_management.BotInfo.is_busy == True).count_async() | 
| + return swarming_rpcs.BotsCount( | 
| + count=f_count.get_result(), | 
| + quarantined=f_quarantined.get_result(), | 
| + dead=f_dead.get_result(), | 
| + busy=f_busy.get_result(), | 
| + now=now) | 
| + elif quarantined or is_dead: | 
| + q = bot_management.filter_availability(q, quarantined, is_dead, now) | 
| + f_count = q.count_async() | 
| + f_dead = (bot_management.filter_availability(q, None, True, now) | 
| + .count_async()) | 
| + f_quarantined = (bot_management.filter_availability(q, True, None, now) | 
| + .count_async()) | 
| + # Dead and quarantined bots, by definition, are not busy. | 
| + return swarming_rpcs.BotsCount( | 
| + count=f_count.get_result(), | 
| + quarantined=f_quarantined.get_result(), | 
| + dead=f_dead.get_result(), | 
| + busy=0, | 
| + now=now) | 
| + else: # both quarantined and is_dead are false or None | 
| + f_count = (bot_management.filter_availability(q, quarantined, is_dead, | 
| + now).count_async()) | 
| + f_busy = q.filter(bot_management.BotInfo.is_busy == True).count_async() | 
| + return swarming_rpcs.BotsCount( | 
| + count=f_count.get_result(), | 
| + quarantined=0, | 
| + dead=0, | 
| + busy=f_busy.get_result(), | 
| + now=now) | 
| + | 
| @gae_ts_mon.instrument_endpoint() | 
| @auth.endpoints_method( |