Chromium Code Reviews| Index: appengine/swarming/handlers_endpoints.py |
| diff --git a/appengine/swarming/handlers_endpoints.py b/appengine/swarming/handlers_endpoints.py |
| index a001fe5492f21cacf0f5a481abe8f1fa077918fc..967209aa52e1c64e7e25980eb003d8ff5fd48eeb 100644 |
| --- a/appengine/swarming/handlers_endpoints.py |
| +++ b/appengine/swarming/handlers_endpoints.py |
| @@ -8,7 +8,9 @@ import logging |
| import os |
| from google.appengine.api import datastore_errors |
| +from google.appengine.api import memcache as gae_memcache |
|
M-A Ruel
2017/02/16 18:15:19
why rename?
kjlubick
2017/02/17 15:54:49
It was to be consistent with components/utils.py.
|
| from google.appengine.ext import ndb |
| + |
| import endpoints |
| import gae_ts_mon |
| from protorpc import messages |
| @@ -510,13 +512,25 @@ class SwarmingTasksService(remote.Service): |
| if not request.start: |
| raise endpoints.BadRequestException('start (as epoch) is required') |
| now = utils.utcnow() |
| + mem_key = self._memcache_key(request, now) |
| + count = gae_memcache.get(mem_key) |
|
M-A Ruel
2017/02/16 18:15:19
use a namespace, e.g. 'tasks_count'
|
| + if count is not None: |
| + return swarming_rpcs.TasksCount(count=count, now=now) |
| + |
| try: |
| count = self._query_from_request(request, 'created_ts').count() |
| + gae_memcache.add(mem_key, count, 120) |
|
M-A Ruel
2017/02/16 18:15:19
technically the entry is valid for a while since y
kjlubick
2017/02/17 15:54:49
Done
|
| except ValueError as e: |
| raise endpoints.BadRequestException( |
| 'Inappropriate filter for tasks/count: %s' % e) |
| return swarming_rpcs.TasksCount(count=count, now=now) |
| + def _memcache_key(self, request, now): |
| + # Floor now to minute to account for empty "end" |
| + end = request.end or now.replace(second=0, microsecond=0) |
| + request.tags.sort() |
| + return '%s|%s|%s|%s' % (request.tags, request.state, request.start, end) |
| + |
| def _query_from_request(self, request, sort=None): |
| """Returns a TaskResultSummary query.""" |
| start = message_conversion.epoch_to_datetime(request.start) |