def remove_expired_alerts(self): active_alerts = TSAlertsJSON.query_active().fetch() for alert in active_alerts: alert_age = utils.secs_ago(alert.json["alert_sent_utc"]) if alert_age > self.STALE_ALERT_TIMEOUT: logging.info("%s expired. alert age: %d.", alert.key.id(), alert_age) alert.active_until = dt.utcnow() alert.json["active_until"] = dt.strftime(alert.active_until, "%s") alert.put() memcache.set(alert.key.id(), alert.json)
def get(self, timestamp=None): result_json = {} if not users.get_current_user(): result_json["login-url"] = users.create_login_url(self.request.uri) return result_json alerts = TSAlertsJSON.query_active().fetch() if timestamp: try: time = dt.fromtimestamp(int(timestamp)) except ValueError: self.response.set_status(400, "Invalid timestamp.") return if time > dt.utcnow(): self.response.write("Sheriff-o-matic cannot predict the future... yet.") self.response.set_status(400, "Invalid timestamp.") else: time = dt.utcnow() alerts += TSAlertsJSON.query(TSAlertsJSON.active_until > time).fetch() history = [] for a in alerts: ts, private = timestamp, a.json["private"] in_range = not (ts and utils.secs_ago(a.json["active_since_utc"], ts) < 0) permission = utils.is_googler() or not private if in_range and permission: history.append(a.json) result_json.update( { "timestamp": time.strftime("%s"), "time_string": time.strftime("%Y-%m-%d %H:%M:%S %Z"), "active_alerts": history, } ) self.write_json(result_json)