OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import alerts_history | 5 import alerts_history |
6 import datetime_encoder | 6 import datetime_encoder |
7 import hashlib | 7 import hashlib |
8 import json | 8 import json |
9 import logging | 9 import logging |
10 import webapp2 | 10 import webapp2 |
11 import zlib | 11 import zlib |
12 | 12 |
| 13 from alerts import increment_monarch |
13 from datetime import datetime as dt | 14 from datetime import datetime as dt |
14 from google.appengine.api import memcache | 15 from google.appengine.api import memcache |
15 from google.appengine.api import users | 16 from google.appengine.api import users |
16 from google.appengine.datastore import datastore_query | 17 from google.appengine.datastore import datastore_query |
17 from google.appengine.ext import ndb | 18 from google.appengine.ext import ndb |
18 | 19 |
19 | 20 |
20 ALLOWED_APP_IDS = ('google.com:monarch-email-alerts-parser') | 21 ALLOWED_APP_IDS = ('google.com:monarch-email-alerts-parser') |
21 INBOUND_APP_ID = 'X-Appengine-Inbound-Appid' | 22 INBOUND_APP_ID = 'X-Appengine-Inbound-Appid' |
22 | 23 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 | 84 |
84 | 85 |
85 class TimeSeriesAlertsHandler(webapp2.RequestHandler): | 86 class TimeSeriesAlertsHandler(webapp2.RequestHandler): |
86 ALERT_TYPE = 'ts-alerts' | 87 ALERT_TYPE = 'ts-alerts' |
87 MEMCACHE_COMPRESSION_LEVEL = 9 | 88 MEMCACHE_COMPRESSION_LEVEL = 9 |
88 # Alerts which have continued to fire are re-sent every 5 minutes, so stale | 89 # Alerts which have continued to fire are re-sent every 5 minutes, so stale |
89 # alerts older than 300 seconds are replaced by incoming alerts. | 90 # alerts older than 300 seconds are replaced by incoming alerts. |
90 STALE_ALERT_TIMEOUT = 300 | 91 STALE_ALERT_TIMEOUT = 300 |
91 | 92 |
92 def get(self, key=None): | 93 def get(self, key=None): |
| 94 increment_monarch('ts-alerts') |
93 self.remove_expired_alerts() | 95 self.remove_expired_alerts() |
94 if not users.get_current_user(): | 96 if not users.get_current_user(): |
95 results = {'date': dt.utcnow(), | 97 results = {'date': dt.utcnow(), |
96 'redirect-url': users.create_login_url(self.request.uri)} | 98 'redirect-url': users.create_login_url(self.request.uri)} |
97 self.write_json(results) | 99 self.write_json(results) |
98 return | 100 return |
99 | 101 |
100 if key: | 102 if key: |
101 logging.info('getting the key: ' + key) | 103 logging.info('getting the key: ' + key) |
102 try: | 104 try: |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 def set_memcache(self, key, data): | 219 def set_memcache(self, key, data): |
218 json_data = generate_json_dump(data, False) | 220 json_data = generate_json_dump(data, False) |
219 compression_level = self.MEMCACHE_COMPRESSION_LEVEL | 221 compression_level = self.MEMCACHE_COMPRESSION_LEVEL |
220 compressed = zlib.compress(json_data, compression_level) | 222 compressed = zlib.compress(json_data, compression_level) |
221 memcache.set(key, compressed) | 223 memcache.set(key, compressed) |
222 | 224 |
223 | 225 |
224 class TimeSeriesAlertsHistory(alerts_history.AlertsHistory): | 226 class TimeSeriesAlertsHistory(alerts_history.AlertsHistory): |
225 | 227 |
226 def get(self, timestamp=None): | 228 def get(self, timestamp=None): |
| 229 increment_monarch('ts-alerts-history') |
227 result_json = {} | 230 result_json = {} |
228 if not users.get_current_user(): | 231 if not users.get_current_user(): |
229 result_json['login-url'] = users.create_login_url(self.request.uri) | 232 result_json['login-url'] = users.create_login_url(self.request.uri) |
230 return result_json | 233 return result_json |
231 | 234 |
232 alerts = TSAlertsJSON.query_active().fetch() | 235 alerts = TSAlertsJSON.query_active().fetch() |
233 if timestamp: | 236 if timestamp: |
234 try: | 237 try: |
235 time = dt.fromtimestamp(int(timestamp)) | 238 time = dt.fromtimestamp(int(timestamp)) |
236 except ValueError: | 239 except ValueError: |
(...skipping 27 matching lines...) Expand all Loading... |
264 self.response.headers['Content-Type'] = 'application/json' | 267 self.response.headers['Content-Type'] = 'application/json' |
265 data = generate_json_dump(data) | 268 data = generate_json_dump(data) |
266 self.response.write(data) | 269 self.response.write(data) |
267 | 270 |
268 | 271 |
269 app = webapp2.WSGIApplication([ | 272 app = webapp2.WSGIApplication([ |
270 ('/ts-alerts', TimeSeriesAlertsHandler), | 273 ('/ts-alerts', TimeSeriesAlertsHandler), |
271 ('/ts-alerts/(.*)', TimeSeriesAlertsHandler), | 274 ('/ts-alerts/(.*)', TimeSeriesAlertsHandler), |
272 ('/ts-alerts-history', TimeSeriesAlertsHistory), | 275 ('/ts-alerts-history', TimeSeriesAlertsHistory), |
273 ('/ts-alerts-history/(.*)', TimeSeriesAlertsHistory)]) | 276 ('/ts-alerts-history/(.*)', TimeSeriesAlertsHistory)]) |
OLD | NEW |