 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..6fa9c852d5f22db4d3cb5f9570226c7997ee4f21 100644 | 
| --- a/appengine/swarming/handlers_endpoints.py | 
| +++ b/appengine/swarming/handlers_endpoints.py | 
| @@ -608,6 +608,33 @@ class SwarmingBotService(remote.Service): | 
| @swarming_api.api_class(resource_name='bots', path='bots') | 
| class SwarmingBotsService(remote.Service): | 
| """Bots-related API.""" | 
| + | 
| + def _build_query(self, request, now): | 
| 
M-A Ruel
2016/08/09 17:10:34
"""Returns a ndb.Query for BotInfo based on the re
 
kjlubick
2016/08/09 17:41:32
Done.
 | 
| + q = bot_management.BotInfo.query() | 
| + for d in request.dimensions: | 
| + if not ':' in d: | 
| 
M-A Ruel
2016/08/09 17:10:34
This check is not necessary, the len(parts) != 2 a
 
kjlubick
2016/08/09 17:41:32
Done.
 | 
| + 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) | 
| + | 
| + if request.quarantined == swarming_rpcs.ThreeStateBool.TRUE: | 
| + q = q.filter(bot_management.BotInfo.quarantined == True) | 
| + elif request.quarantined == swarming_rpcs.ThreeStateBool.FALSE: | 
| + q = q.filter(bot_management.BotInfo.quarantined == False) | 
| + | 
| + dt = datetime.timedelta( | 
| 
M-A Ruel
2016/08/09 17:10:34
according to line 617, this fits one line.
 
kjlubick
2016/08/09 17:41:32
Done.
 | 
| + seconds=config.settings().bot_death_timeout_secs) | 
| + timeout = now - dt | 
| + if request.is_dead == swarming_rpcs.ThreeStateBool.TRUE: | 
| + q = q.filter(bot_management.BotInfo.last_seen_ts < timeout) | 
| + elif request.is_dead == swarming_rpcs.ThreeStateBool.FALSE: | 
| + q = q.filter(bot_management.BotInfo.last_seen_ts > timeout) | 
| + | 
| + return q | 
| + | 
| + | 
| @gae_ts_mon.instrument_endpoint() | 
| @auth.endpoints_method( | 
| swarming_rpcs.BotsRequest, swarming_rpcs.BotList, | 
| @@ -620,14 +647,8 @@ 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 = self._build_query(request, now) | 
| + | 
| bots, cursor = datastore_utils.fetch_page(q, request.limit, request.cursor) | 
| return swarming_rpcs.BotList( | 
| cursor=cursor, | 
| @@ -644,12 +665,8 @@ class SwarmingBotsService(remote.Service): | 
| """Counts number of bots with given dimensions.""" | 
| 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) | 
| + q = self._build_query(request, now) | 
| + | 
| f_count = q.count_async() | 
| dt = datetime.timedelta(seconds=config.settings().bot_death_timeout_secs) | 
| timeout = now - dt |