OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
agable
2015/08/12 22:18:08
I'd sort the functions in this file into
a) Auth s
| |
2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Utility functions for Sheriff-o-matic.""" | |
7 | |
8 import calendar | |
9 import datetime | |
10 import hashlib | |
11 import json | |
12 import logging | |
13 import time | |
14 import urllib2 | |
15 import webapp2 | |
16 | |
17 from datetime import datetime as dt | |
18 from google.appengine.api import app_identity | |
19 from google.appengine.api import urlfetch | |
20 from google.appengine.api import users | |
21 | |
22 from components import auth | |
23 | |
24 | |
25 class DateTimeEncoder(json.JSONEncoder): | |
26 | |
27 def default(self, obj): # pylint: disable=E0202 | |
28 if isinstance(obj, datetime.datetime): | |
29 return calendar.timegm(obj.timetuple()) | |
30 # Let the base class default method raise the TypeError. | |
31 return json.JSONEncoder.default(self, obj) | |
32 | |
33 | |
34 def increment_monarch(endpoint): | |
35 base_url = app_identity.get_default_version_hostname() | |
36 url = 'http://%s-dot-%s/monitoring/%s' % ('monitoring', base_url, endpoint) | |
37 return urlfetch.fetch(url=url, method=urlfetch.GET) | |
38 | |
39 | |
40 def is_googler(): | |
41 user = users.get_current_user() | |
42 if user: | |
43 email = user.email() | |
44 return email.endswith('@google.com') and '+' not in email | |
45 return False | |
46 | |
47 | |
48 def is_trooper_or_admin(): | |
49 return (auth.is_group_member("mdb/chrome-troopers") or | |
50 users.is_current_user_admin()) | |
51 | |
52 | |
53 def convert_to_secs(duration_str): | |
54 duration_str = duration_str.strip() | |
55 if duration_str[-1] == 's': | |
56 return int(duration_str[:-1]) | |
57 elif duration_str[-1] == 'm': | |
58 return 60 * int(duration_str[:-1]) | |
59 elif duration_str[-1] == 'h': | |
60 return 3600 * int(duration_str[:-1]) | |
61 elif duration_str[-1] == 'd': | |
62 return 24 * 3600 * int(duration_str[:-1]) | |
63 elif duration_str[-1] == 'w': | |
64 return 7 * 24 * 3600 * int(duration_str[:-1]) | |
65 else: | |
66 raise Exception('Invalid duration_str ' + duration_str[-1]) | |
67 | |
68 | |
69 def secs_ago(time_string, time_now=None): | |
70 try: | |
71 time_sent = dt.strptime(time_string, '%Y-%m-%d %H:%M:%S %Z') | |
72 except ValueError: | |
73 time_sent = dt.strptime(time_string, '%Y-%m-%d %H:%M:%S') | |
74 time_now = time_now or int(dt.utcnow().strftime('%s')) | |
75 latency = int(time_now) - int(time_sent.strftime('%s')) | |
76 return latency | |
77 | |
78 | |
79 def hash_string(input_str): | |
80 return hashlib.sha1(input_str).hexdigest() | |
81 | |
82 | |
83 def generate_json_dump(alerts, human_readable=True): | |
84 if human_readable: | |
85 return json.dumps(alerts, cls=DateTimeEncoder, | |
86 indent=2, | |
87 separators=(',', ': ')) | |
88 return json.dumps(alerts, cls=DateTimeEncoder, | |
89 indent=None, | |
90 separators=(',', ':')) | |
OLD | NEW |