-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.py
executable file
·313 lines (268 loc) · 10.7 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
import tornado.ioloop
import tornado.web
import tornado.gen
import tornado.websocket
import tornado.auth
import tornado.escape
import hmac
import hashlib
import functools
import os
import momoko
import urlparse
import time
import datetime
import parsedatetime
import prettytable
import ascii_graph
import logging
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
define("host", default='localhost:8888', help="server host", type=str)
define("db_host", default="localhost", help="database hostname", type=str)
define("db_port", default=5432, help="database port", type=int)
define("db_name", default="callum", help="database name", type=str)
define("db_user", default="callum", help="database username", type=str)
define("db_pass", default="", help="database password", type=str)
class HumanDateParser(object):
def __init__(self):
self.calendar = parsedatetime.Calendar()
def parse(self, str):
return datetime.datetime.fromtimestamp(
time.mktime(self.calendar.parse(str)[0]))
def get_psql_credentials():
try:
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.getenv("DATABASE_URL"))
credentials = {"host": url.hostname, "port": url.port,
"dbname": url.path[1:], "user": url.username,
"password": url.password}
except:
credentials = {"host": options.db_host, "port": options.db_port,
"dbname": options.db_name, "user": options.db_user,
"password": options.db_pass}
return credentials
def _get_secret(filename, envvar):
try:
with open(os.path.join(os.path.dirname(__file__), filename)) as f:
return f.read().strip()
except IOError:
return os.getenv(envvar)
get_hmac_secret = \
functools.partial(_get_secret, ".hmac_secret", "ITTF_HMAC_SECRET")
get_cookie_secret = \
functools.partial(_get_secret, ".cookie_secret", "ITTF_COOKIE_SECRET")
get_google_secret = \
functools.partial(_get_secret, ".google_secret", "ITTF_GOOGLE_SECRET")
def hmac_authenticated(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
hash = hmac.new(
self.settings["hmac_secret"],
self.get_argument("data"),
hashlib.sha256
)
if self.get_argument("token") != hash.hexdigest():
raise tornado.web.HTTPError(401, "Invalid token")
return method(self, *args, **kwargs)
return wrapper
def bool2str(boolean):
return "yes" if boolean else "no"
class HasFreeWebSocketHandler(tornado.websocket.WebSocketHandler):
connections = set()
def open(self):
HasFreeWebSocketHandler.connections.add(self)
def on_message(self, message):
pass
def on_close(self):
HasFreeWebSocketHandler.connections.remove(self)
class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
return self.application.db
def get_current_user(self):
return self.get_secure_cookie("ittf_user")
@tornado.gen.coroutine
def has_free_toilet(self):
cursor = yield self.db.callproc("any_are_free")
raise tornado.gen.Return(cursor.fetchone()[0])
@tornado.gen.coroutine
def has_free_shower(self):
cursor = yield self.db.execute(
"SELECT is_free FROM latest_events() WHERE toilet_id = 2")
raise tornado.gen.Return(cursor.fetchone()[0])
class GoogleLoginHandler(BaseHandler, tornado.auth.GoogleOAuth2Mixin):
@tornado.gen.coroutine
def get(self):
if self.get_argument("code", False):
access = yield self.get_authenticated_user(
redirect_uri=self.settings["login_url"],
code=self.get_argument("code"))
user = yield self.oauth2_request(
"https://www.googleapis.com/oauth2/v1/userinfo",
access_token=access["access_token"])
if user["email"].endswith("@madebymany.co.uk") or \
user["email"].endswith("@madebymany.com"):
self.set_secure_cookie("ittf_user", user["email"])
self.redirect("/stats")
else:
self.redirect("/")
else:
yield self.authorize_redirect(
redirect_uri=self.settings["login_url"],
client_id=self.settings["google_oauth"]["key"],
scope=["profile", "email"],
response_type="code",
extra_params={"approval_prompt": "auto"})
class MainHandler(BaseHandler):
@tornado.gen.coroutine
def get(self):
has_free = bool2str((yield self.has_free_toilet()))
self.render("index.html", has_free_toilet=has_free)
@hmac_authenticated
@tornado.gen.coroutine
def post(self):
values = yield [self.db.mogrify(
"(%(toilet_id)s, %(is_free)s, %(timestamp)s)", t)
for t in tornado.escape.json_decode(self.get_argument("data"))
]
yield self.db.execute(
"INSERT INTO events (toilet_id, is_free, recorded_at) "
"VALUES %s;" % ", ".join(values))
self.notify_has_free()
self.finish()
@tornado.gen.coroutine
def notify_has_free(self):
has_free = bool2str((yield self.has_free_toilet()))
for connected in HasFreeWebSocketHandler.connections:
try:
connected.write_message({
"hasFree": has_free
})
except:
logging.error("Error sending message", exc_info=True)
class ShowerHandler(BaseHandler):
@tornado.gen.coroutine
def get(self):
has_free = bool2str((yield self.has_free_shower()))
self.render("shower.html", has_free_shower=has_free)
class StatsHandler(BaseHandler):
@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
parser = HumanDateParser()
text = None
op = None
where = ""
and_where = ""
start = self.get_argument("from", None)
end = self.get_argument("to", None)
if start and end:
parsed_start = parser.parse(start)
parsed_end = parser.parse(end)
text = "Showing from %s to %s" % (parsed_start, parsed_end)
op = ("WHERE recorded_at BETWEEN %s AND %s",
(parsed_start, parsed_end))
elif start:
parsed_start = parser.parse(start)
text = "Showing from %s onward" % parsed_start
op = ("WHERE recorded_at >= %s", (parsed_start,))
elif end:
parsed_end = parser.parse(end)
text = "Showing from %s backward" % parsed_end
op = ("WHERE recorded_at <= %s", (parsed_end,))
if op:
where = yield self.db.mogrify(*op)
and_where = where.replace("WHERE", "AND", 1)
queries = [
("Number of visits",
"SELECT toilet_id, count(*) "
"AS num_visits FROM visits %(where)s "
"GROUP BY toilet_id ORDER BY toilet_id;"),
("Average visit duration",
"SELECT toilet_id, avg(duration) "
"AS duration_avg FROM visits %(where)s "
"GROUP BY toilet_id ORDER BY toilet_id;"),
("Minimum visit duration",
"SELECT toilet_id, min(duration) "
"AS duration_min FROM visits %(where)s "
"GROUP BY toilet_id ORDER BY toilet_id;"),
("Maximum visit duration",
"SELECT toilet_id, max(duration) "
"AS duration_max FROM visits %(where)s "
"GROUP BY toilet_id ORDER BY toilet_id;"),
("Visits by hour",
"SELECT s.hour AS hour_of_day, count(v.hour) "
"FROM generate_series(0, 23) s(hour) "
"LEFT OUTER JOIN (SELECT recorded_at, "
"EXTRACT('hour' from recorded_at) "
"AS hour FROM visits %(where)s) v on s.hour = v.hour "
"GROUP BY s.hour ORDER BY s.hour;"),
("Visits by day",
"SELECT s.dow AS day_of_week, count(v.dow) "
"FROM generate_series(0, 6) s(dow) "
"LEFT OUTER JOIN (SELECT recorded_at, "
"EXTRACT('dow' from recorded_at) "
"AS dow FROM visits %(where)s) v on s.dow = v.dow "
"GROUP BY s.dow ORDER BY s.dow;")
]
results = yield [self.db.execute(q % {"where": where,
"and_where": and_where})
for _, q in queries]
cursor = yield self.db.execute((
"SELECT (s.period * 10) AS seconds, count(v.duration) "
"FROM generate_series(0, 500) s(period) "
"LEFT OUTER JOIN (SELECT EXTRACT(EPOCH from duration) "
"AS duration FROM visits) v on s.period = FLOOR(v.duration / 10) "
"GROUP BY s.period HAVING s.period <= 36 ORDER BY s.period;"
))
graph = "\n".join(ascii_graph.Pyasciigraph()
.graph("Frequency graph", cursor.fetchall()))
self.render("stats.html", text=text, start=start, end=end,
tables=[(queries[i][0], prettytable.from_db_cursor(r))
for i, r in enumerate(results)],
frequency_graph=graph)
class APIHandler(BaseHandler):
@tornado.gen.coroutine
def get(self):
response = tornado.escape.json_encode({
"has_free_toilet": (yield self.has_free_toilet())
})
callback = self.get_argument("callback", None)
if callback:
response = "%s(%s)" % (callback, response)
self.set_header("content-type", "application/json")
self.write(response)
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
[(r"/login", GoogleLoginHandler),
(r"/", MainHandler),
(r"/shower", ShowerHandler),
(r"/stats", StatsHandler),
(r"/api", APIHandler),
(r"/hasfreesocket", HasFreeWebSocketHandler)],
template_path=os.path.join(os.path.dirname(__file__), "templates"),
hmac_secret=get_hmac_secret(),
cookie_secret=get_cookie_secret(),
login_url="http://%s/login" % options.host,
google_oauth=dict(key=os.getenv("ITTF_GOOGLE_KEY"),
secret=get_google_secret())
)
ioloop = tornado.ioloop.IOLoop.instance()
app.db = momoko.Pool(
dsn=" ".join(["%s=%s" % c
for c in get_psql_credentials().iteritems()]),
size=6,
ioloop=ioloop
)
future = app.db.connect()
ioloop.add_future(future, lambda f: ioloop.stop())
ioloop.start()
future.result()
app.listen(options.port)
try:
ioloop.start()
except KeyboardInterrupt:
pass