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

Unified Diff: Tools/GardeningServer/alerts.py

Issue 633983002: Implemented retrieval of alerts history (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Refactored code and added tests Created 6 years, 2 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 | Tools/GardeningServer/alerts_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Tools/GardeningServer/alerts.py
diff --git a/Tools/GardeningServer/alerts.py b/Tools/GardeningServer/alerts.py
index 071dc7da47c6cf9f5e7f6756e47b44c06d733c75..f51a07b5549f1278ba8d1f95b91f7a5a1147da1d 100644
--- a/Tools/GardeningServer/alerts.py
+++ b/Tools/GardeningServer/alerts.py
@@ -10,6 +10,8 @@ import webapp2
import zlib
from google.appengine.api import memcache
+from google.appengine.api import users
+from google.appengine.datastore import datastore_query
from google.appengine.ext import ndb
LOGGER = logging.getLogger(__name__)
@@ -24,12 +26,13 @@ class DateTimeEncoder(json.JSONEncoder):
class AlertsJSON(ndb.Model):
+ key = ndb.StringProperty()
Z_DONOTUSE 2014/10/08 01:08:42 key implies uniqueness to me. How about calling th
Sergiy Byelozyorov 2014/10/08 12:50:26 Done.
json = ndb.BlobProperty(compressed=True)
date = ndb.DateTimeProperty(auto_now_add=True)
class AlertsHandler(webapp2.RequestHandler):
- MEMCACHE_ALERTS_KEY = 'alerts'
+ ALERTS_KEY = 'alerts'
# Has no 'response' member.
# pylint: disable=E1101
@@ -55,9 +58,9 @@ class AlertsHandler(webapp2.RequestHandler):
self.send_json_data(uncompressed)
def get(self):
- self.get_from_memcache(AlertsHandler.MEMCACHE_ALERTS_KEY)
+ self.get_from_memcache(AlertsHandler.ALERTS_KEY)
- def save_alerts_to_history(self, alerts):
+ def post_to_history(self, alerts_key, alerts):
last_entry = AlertsJSON.query().order(-AlertsJSON.date).get()
last_alerts = json.loads(last_entry.json) if last_entry else {}
@@ -71,7 +74,9 @@ class AlertsHandler(webapp2.RequestHandler):
return filtered_json
if alert_fields(last_alerts) != alert_fields(alerts):
- new_entry = AlertsJSON(json=self.generate_json_dump(alerts))
+ new_entry = AlertsJSON(
+ json=self.generate_json_dump(alerts),
+ key=alerts_key)
new_entry.put()
# Has no 'response' member.
@@ -95,16 +100,52 @@ class AlertsHandler(webapp2.RequestHandler):
return alerts
- def update_alerts(self, memcache_key):
+ def update_alerts(self, alerts_key):
alerts = self.parse_alerts(self.request.get('content'))
if alerts:
- self.post_to_memcache(memcache_key, alerts)
- self.save_alerts_to_history(alerts)
+ self.post_to_memcache(alerts_key, alerts)
+ self.post_to_history(alerts_key, alerts)
def post(self):
- self.update_alerts(AlertsHandler.MEMCACHE_ALERTS_KEY)
+ self.update_alerts(AlertsHandler.ALERTS_KEY)
+
+
+class AlertsHistory(webapp2.RequestHandler):
+ MAX_LIMIT_PER_PAGE = 100
+
+ def get(self):
+ alerts_query = AlertsJSON.query().order(-AlertsJSON.date)
+
+ # Return only public alerts for non-internal users.
+ user = users.get_current_user()
+ if not user or not user.email().endswith('@google.com'):
+ alerts_query = alerts_query.filter(
+ AlertsJSON.key == AlertsHandler.ALERTS_KEY)
+
+ cursor = self.request.get('cursor')
+ if cursor:
+ cursor = datastore_query.Cursor(urlsafe=cursor)
+
+ limit = int(self.request.get('limit', self.MAX_LIMIT_PER_PAGE))
+ limit = min(self.MAX_LIMIT_PER_PAGE, limit)
+
+ if cursor:
+ alerts, next_cursor, has_more = alerts_query.fetch_page(
+ limit, start_cursor=cursor)
+ else:
+ alerts, next_cursor, has_more = alerts_query.fetch_page(limit)
+
+ combined_json = {
+ 'has_more': has_more,
+ 'cursor': next_cursor.urlsafe() if next_cursor else '',
+ 'history': [json.loads(alert.json) for alert in alerts]
+ }
+
+ self.response.headers['Content-Type'] = 'application/json'
+ self.response.out.write(json.dumps(combined_json))
app = webapp2.WSGIApplication([
- ('/alerts', AlertsHandler)
+ ('/alerts', AlertsHandler),
+ ('/alerts-history', AlertsHistory)
])
« no previous file with comments | « no previous file | Tools/GardeningServer/alerts_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698