 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| OLD | NEW | 
|---|---|
| 1 # Copyright 2015 The LUCI Authors. All rights reserved. | 1 # Copyright 2015 The LUCI Authors. All rights reserved. | 
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 | 
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. | 
| 4 | 4 | 
| 5 """This module defines Swarming Server endpoints handlers.""" | 5 """This module defines Swarming Server endpoints handlers.""" | 
| 6 | 6 | 
| 7 import datetime | 7 import datetime | 
| 8 import logging | 8 import logging | 
| 9 | 9 | 
| 10 from google.appengine.api import datastore_errors | 10 from google.appengine.api import datastore_errors | 
| (...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 601 message_conversion.task_result_to_rpc( | 601 message_conversion.task_result_to_rpc( | 
| 602 r, request.include_performance_stats) | 602 r, request.include_performance_stats) | 
| 603 for r in items | 603 for r in items | 
| 604 ], | 604 ], | 
| 605 now=now) | 605 now=now) | 
| 606 | 606 | 
| 607 | 607 | 
| 608 @swarming_api.api_class(resource_name='bots', path='bots') | 608 @swarming_api.api_class(resource_name='bots', path='bots') | 
| 609 class SwarmingBotsService(remote.Service): | 609 class SwarmingBotsService(remote.Service): | 
| 610 """Bots-related API.""" | 610 """Bots-related API.""" | 
| 611 | |
| 612 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.
 | |
| 613 q = bot_management.BotInfo.query() | |
| 614 for d in request.dimensions: | |
| 615 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.
 | |
| 616 raise endpoints.BadRequestException('Invalid dimensions') | |
| 617 parts = d.split(':', 1) | |
| 618 if len(parts) != 2 or any(i.strip() != i or not i for i in parts): | |
| 619 raise endpoints.BadRequestException('Invalid dimensions') | |
| 620 q = q.filter(bot_management.BotInfo.dimensions_flat == d) | |
| 621 | |
| 622 if request.quarantined == swarming_rpcs.ThreeStateBool.TRUE: | |
| 623 q = q.filter(bot_management.BotInfo.quarantined == True) | |
| 624 elif request.quarantined == swarming_rpcs.ThreeStateBool.FALSE: | |
| 625 q = q.filter(bot_management.BotInfo.quarantined == False) | |
| 626 | |
| 627 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.
 | |
| 628 seconds=config.settings().bot_death_timeout_secs) | |
| 629 timeout = now - dt | |
| 630 if request.is_dead == swarming_rpcs.ThreeStateBool.TRUE: | |
| 631 q = q.filter(bot_management.BotInfo.last_seen_ts < timeout) | |
| 632 elif request.is_dead == swarming_rpcs.ThreeStateBool.FALSE: | |
| 633 q = q.filter(bot_management.BotInfo.last_seen_ts > timeout) | |
| 634 | |
| 635 return q | |
| 636 | |
| 637 | |
| 611 @gae_ts_mon.instrument_endpoint() | 638 @gae_ts_mon.instrument_endpoint() | 
| 612 @auth.endpoints_method( | 639 @auth.endpoints_method( | 
| 613 swarming_rpcs.BotsRequest, swarming_rpcs.BotList, | 640 swarming_rpcs.BotsRequest, swarming_rpcs.BotList, | 
| 614 http_method='GET') | 641 http_method='GET') | 
| 615 @auth.require(acl.is_privileged_user) | 642 @auth.require(acl.is_privileged_user) | 
| 616 def list(self, request): | 643 def list(self, request): | 
| 617 """Provides list of known bots. | 644 """Provides list of known bots. | 
| 618 | 645 | 
| 619 Deleted bots will not be listed. | 646 Deleted bots will not be listed. | 
| 620 """ | 647 """ | 
| 621 logging.info('%s', request) | 648 logging.info('%s', request) | 
| 622 now = utils.utcnow() | 649 now = utils.utcnow() | 
| 623 q = bot_management.BotInfo.query().order(bot_management.BotInfo.key) | 650 q = self._build_query(request, now) | 
| 624 for d in request.dimensions: | 651 | 
| 625 if not ':' in d: | |
| 626 raise endpoints.BadRequestException('Invalid dimensions') | |
| 627 parts = d.split(':', 1) | |
| 628 if len(parts) != 2 or any(i.strip() != i or not i for i in parts): | |
| 629 raise endpoints.BadRequestException('Invalid dimensions') | |
| 630 q = q.filter(bot_management.BotInfo.dimensions_flat == d) | |
| 631 bots, cursor = datastore_utils.fetch_page(q, request.limit, request.cursor) | 652 bots, cursor = datastore_utils.fetch_page(q, request.limit, request.cursor) | 
| 632 return swarming_rpcs.BotList( | 653 return swarming_rpcs.BotList( | 
| 633 cursor=cursor, | 654 cursor=cursor, | 
| 634 death_timeout=config.settings().bot_death_timeout_secs, | 655 death_timeout=config.settings().bot_death_timeout_secs, | 
| 635 items=[message_conversion.bot_info_to_rpc(bot, now) for bot in bots], | 656 items=[message_conversion.bot_info_to_rpc(bot, now) for bot in bots], | 
| 636 now=now) | 657 now=now) | 
| 637 | 658 | 
| 638 @gae_ts_mon.instrument_endpoint() | 659 @gae_ts_mon.instrument_endpoint() | 
| 639 @auth.endpoints_method( | 660 @auth.endpoints_method( | 
| 640 swarming_rpcs.BotsRequest, swarming_rpcs.BotsCount, | 661 swarming_rpcs.BotsRequest, swarming_rpcs.BotsCount, | 
| 641 http_method='GET') | 662 http_method='GET') | 
| 642 @auth.require(acl.is_privileged_user) | 663 @auth.require(acl.is_privileged_user) | 
| 643 def count(self, request): | 664 def count(self, request): | 
| 644 """Counts number of bots with given dimensions.""" | 665 """Counts number of bots with given dimensions.""" | 
| 645 logging.info('%s', request) | 666 logging.info('%s', request) | 
| 646 now = utils.utcnow() | 667 now = utils.utcnow() | 
| 647 q = bot_management.BotInfo.query() | 668 q = self._build_query(request, now) | 
| 648 for d in request.dimensions: | 669 | 
| 649 parts = d.split(':', 1) | |
| 650 if len(parts) != 2 or any(i.strip() != i or not i for i in parts): | |
| 651 raise endpoints.BadRequestException('Invalid dimensions: %s' % d) | |
| 652 q = q.filter(bot_management.BotInfo.dimensions_flat == d) | |
| 653 f_count = q.count_async() | 670 f_count = q.count_async() | 
| 654 dt = datetime.timedelta(seconds=config.settings().bot_death_timeout_secs) | 671 dt = datetime.timedelta(seconds=config.settings().bot_death_timeout_secs) | 
| 655 timeout = now - dt | 672 timeout = now - dt | 
| 656 f_dead = q.filter( | 673 f_dead = q.filter( | 
| 657 bot_management.BotInfo.last_seen_ts < timeout).count_async() | 674 bot_management.BotInfo.last_seen_ts < timeout).count_async() | 
| 658 f_quarantined = q.filter( | 675 f_quarantined = q.filter( | 
| 659 bot_management.BotInfo.quarantined == True).count_async() | 676 bot_management.BotInfo.quarantined == True).count_async() | 
| 660 f_busy = q.filter(bot_management.BotInfo.is_busy == True).count_async() | 677 f_busy = q.filter(bot_management.BotInfo.is_busy == True).count_async() | 
| 661 return swarming_rpcs.BotsCount( | 678 return swarming_rpcs.BotsCount( | 
| 662 count=f_count.get_result(), | 679 count=f_count.get_result(), | 
| (...skipping 20 matching lines...) Expand all Loading... | |
| 683 def get_routes(): | 700 def get_routes(): | 
| 684 return ( | 701 return ( | 
| 685 endpoints_webapp2.api_routes(SwarmingServerService) + | 702 endpoints_webapp2.api_routes(SwarmingServerService) + | 
| 686 endpoints_webapp2.api_routes(SwarmingTaskService) + | 703 endpoints_webapp2.api_routes(SwarmingTaskService) + | 
| 687 endpoints_webapp2.api_routes(SwarmingTasksService) + | 704 endpoints_webapp2.api_routes(SwarmingTasksService) + | 
| 688 endpoints_webapp2.api_routes(SwarmingBotService) + | 705 endpoints_webapp2.api_routes(SwarmingBotService) + | 
| 689 endpoints_webapp2.api_routes(SwarmingBotsService) + | 706 endpoints_webapp2.api_routes(SwarmingBotsService) + | 
| 690 # components.config endpoints for validation and configuring of luci-config | 707 # components.config endpoints for validation and configuring of luci-config | 
| 691 # service URL. | 708 # service URL. | 
| 692 endpoints_webapp2.api_routes(config.ConfigApi)) | 709 endpoints_webapp2.api_routes(config.ConfigApi)) | 
| OLD | NEW |