-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
214 lines (169 loc) · 6.48 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
import StringIO
import datetime
import falcon
import json
from json import JSONEncoder
import os
import psycopg2cffi.pool
import dj_database_url
from wsgi_basic_auth import BasicAuth
from psycopg2cffi.extras import Json
database_config = dj_database_url.config()
pool = psycopg2cffi.pool.ThreadedConnectionPool(1, 10,
host=database_config['HOST'],
user=database_config['USER'],
port=5432,
password=database_config['PASSWORD'],
database=database_config['NAME'])
class DateAndDateTimeSupportingJSONEncoder(JSONEncoder):
def default(self, o):
if isinstance(o, (datetime.date, datetime.datetime)):
return o.isoformat()
return super(DateAndDateTimeSupportingJSONEncoder, self).default(o)
_default_encoder_class = DateAndDateTimeSupportingJSONEncoder
_default_encoder = _default_encoder_class()
def json_stream(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None,
encoding='utf-8', default=None, sort_keys=False, **kw):
"""Serialize ``obj`` and yield chunks."""
# cached encoder
if (not skipkeys and ensure_ascii and
check_circular and allow_nan and
cls is None and indent is None and separators is None and
encoding == 'utf-8' and default is None and not sort_keys and not kw):
iterable = _default_encoder.iterencode(obj)
else:
if cls is None:
cls = _default_encoder_class
iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
check_circular=check_circular, allow_nan=allow_nan, indent=indent,
separators=separators, encoding=encoding,
default=default, sort_keys=sort_keys, **kw).iterencode(obj)
for chunk in iterable:
yield chunk
def buffered(iterable, buflen=4096):
buffer = None
seen = 0
for chunk in iterable:
if buffer is None:
buffer = StringIO.StringIO()
buffer.write(chunk)
seen += len(chunk)
if seen > buflen:
buffer.seek(0)
yield buffer.read()
buffer = None
seen = 0
if buffer is not None:
buffer.seek(0)
yield buffer.read()
class JSONTranslator(object):
def process_request(self, req, resp):
# req.stream corresponds to the WSGI wsgi.input environ variable,
# and allows you to read bytes from the request body.
#
# See also: PEP 3333
if req.content_length in (None, 0):
# Nothing to do
return
try:
req.context['doc'] = json.load(req.stream)
except (ValueError, UnicodeDecodeError):
raise falcon.HTTPError(falcon.HTTP_753,
'Malformed JSON',
'Could not decode the request body. The '
'JSON was incorrect or not encoded as '
'UTF-8.')
def process_response(self, req, resp, resource):
if 'result' not in req.context:
return
resp.stream = buffered(json_stream(req.context['result']))
class PostgresConnectionPool(object):
def process_request(self, req, resp):
req.context['connection'] = pool.getconn()
def process_response(self, req, resp, resource):
if 'connection' not in req.context:
return
pool.putconn(req.context['connection'])
class MatchResource:
"""
create table matches (
date DATE PRIMARY KEY,
success BIGINT DEFAULT 0,
failure BIGINT DEFAULT 0
);
"""
def on_post(self, req, resp):
doc = req.context['doc']
today = datetime.date.today()
with req.context['connection'] as conn:
cur = conn.cursor()
cur.execute("INSERT INTO matches (date, success, failure) "
"VALUES (%s, (%s = TRUE)::INTEGER, (%s = FALSE)::INTEGER) "
"ON CONFLICT (date) DO UPDATE SET "
"success = matches.success + (%s = TRUE)::INTEGER, "
"failure = matches.failure + (%s = FALSE)::INTEGER "
"WHERE matches.date = %s",
[today, doc, doc, doc, doc, today])
class MismatchDataResource:
"""
create table failures (
id BIGSERIAL PRIMARY KEY,
data json
);
"""
def on_post(self, req, resp):
name = req.params.get('name', 'unnamed')
doc = req.context['doc']
with req.context['connection'] as conn:
cur = conn.cursor()
cur.execute("INSERT INTO failures (name, data) VALUES (%s, %s)", [name, Json(doc)])
class StreamedDict(dict):
def __init__(self, source, f=lambda x: x):
self.source = source
self.f = f
def iteritems(self):
for x in self.source:
yield self.f(x)
def __len__(self):
return 1
class StreamedList(list):
def __init__(self, source, f=lambda x: x):
self.source = source
self.f = f
def __iter__(self):
for x in self.source:
yield self.f(x)
def __len__(self):
return 1
class DataDownloadResource:
"""
Big JSON document, with all your datas.
"""
def on_get(self, req, resp):
with req.context['connection'] as conn:
match_cur = conn.cursor()
match_cur.execute("SELECT * FROM matches")
def date_match_record(record):
return record[0].isoformat(), {
'success': record[1],
'failure': record[2],
}
failure_cur = conn.cursor()
failure_cur.execute("SELECT * FROM failures")
resp.content_type = 'application/json'
req.context['result'] = {
'stats': StreamedDict(match_cur, date_match_record),
'failures': list(failure_cur)
}
app = falcon.API(middleware=[
JSONTranslator(),
PostgresConnectionPool(),
])
match = MatchResource()
mismatch_data = MismatchDataResource()
app.add_route('/match/', match)
app.add_route('/mismatch-data/', mismatch_data)
app.add_route('/get-match-data/', DataDownloadResource())
if os.getenv('WSGI_AUTH_CREDENTIALS'):
app = BasicAuth(app)