Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1775)

Unified Diff: appengine/swarming/handlers_endpoints.py

Issue 2697333002: Add caching for task counts (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/swarming/handlers_endpoints.py
diff --git a/appengine/swarming/handlers_endpoints.py b/appengine/swarming/handlers_endpoints.py
index a001fe5492f21cacf0f5a481abe8f1fa077918fc..6e7d5acc58d5cbc7dbf2f3d9472c00ff04f19ab9 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
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 = memcache.get(mem_key, namespace='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()
+ memcache.add(mem_key, count, 24*60*60, namespace='tasks_count')
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)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698