-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsql_json_server.py
380 lines (313 loc) · 10.1 KB
/
sql_json_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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import sys
import itertools
from collections import OrderedDict
import urllib2
from urllib2 import urlopen
import urlparse
import os
import threading
import shelve
import itertools
import re
import psycopg2
#import MySQLdb as mysql
#from MySQLdb.cursors import SSCursor
import json
import copy
import cherrypy as cp
import pydatacube
import pydatacube.jsonstat
import pydatacube.sql
jsonp_callback_check = re.compile("^[a-zA-Z0-9_]+$")
def jsonp_tool(callback_name='callback'):
def jsonp_handler(*args, **kwargs):
request = cp.serving.request
orig_handler = request._jsonp_inner_handler
if callback_name not in request.params:
return orig_handler(*args, **kwargs)
callback = request.params.pop(callback_name)
if not jsonp_callback_check.match(callback):
raise ValueError("Invalid JSONP callback name")
value = orig_handler(*args, **kwargs)
ct = cp.serving.response.headers['Content-Type']
if not ct.startswith("application/json"):
return value
cp.serving.response.headers['Content-Type'] = "application/javascript"
if isinstance(value, basestring):
return "%s(%s)"%(callback, str(value))
# We probably have an iterator
return itertools.chain((callback, '('), value, (')'))
request = cp.serving.request
request._jsonp_inner_handler = request.handler
request.handler = jsonp_handler
cp.tools.jsonp = cp.Tool('before_handler', jsonp_tool, priority=31)
def json_expose(func):
func = cp.tools.json_out()(func)
func.exposed = True
return func
def is_exposed(obj):
if getattr(obj, 'func_name', False) == 'index':
return False
if callable(obj) and getattr(obj, 'exposed', False):
return True
if hasattr(obj, 'index'):
idx = getattr(obj, 'index')
if callable(idx) and getattr(idx, 'exposed', False):
return True
return False
HAL_BLACKLIST = {'favicon_ico': True}
def default_hal_dir(obj):
for name in dir(obj):
if name.startswith('__'):
continue
if name in HAL_BLACKLIST:
continue
yield (name, getattr(obj, name))
def object_hal_links(obj, dirrer=default_hal_dir):
links = {}
if is_exposed(obj):
links['self'] = {'href': cp.url(relative=False)}
for name, value in dirrer(obj):
if not is_exposed(value):
continue
link = {'href': cp.url(name, relative=False)}
links[name] = link
return links
def int_or_none(val):
if val is None: return None
return int(val)
def str_to_bool(val):
if val == 'true':
return True
if val == 'false':
return False
return val
class DbCubeResource(object):
MAX_ENTRIES=1000
MAX_GROUPS=100
def __init__(self, connector, resource_id, filters=None):
self._connector = connector
self._resource_id = resource_id
if filters is None: filters = {}
self._filters = filters
self.__cube = None
def _get_cube(self, bypass_cache=False):
if self.__cube:
return self.__cube
con = self._connector(bypass_cache=bypass_cache)
self.__cube = pydatacube.sql.SqlDataCube(con,
self._resource_id)
if self._filters:
self.__cube = self.__cube.filter(**self._filters)
return self.__cube
@property
def _cube(self):
return self._get_cube()
@json_expose
def index(self):
spec = OrderedDict(self._cube.specification)
spec['_links'] = object_hal_links(self)
return spec
def _get_rows(self, start, end, category_labels):
start = int(start)
end = int_or_none(end)
if end is None:
end = len(self._cube)
if end - start > self.MAX_ENTRIES:
raise ValueError("No more than %i entries allowed at a time. Use 'start' and 'end' parameters to limit the selection."%self.MAX_ENTRIES)
result = self._cube.rows(start=start, end=end,
category_labels=category_labels)
return result
@json_expose
def entries(self, start=0, end=None, category_labels=False):
names = self._cube.dimension_ids()
result = self._get_rows(start, end, str_to_bool(category_labels))
return [dict(zip(names, row)) for row in result]
@json_expose
def table(self, start=0, end=None, labels=False):
result = self._get_rows(start, end, str_to_bool(labels))
# mysql.connector's __len__ returns -1 which
# breaks using list(result). Nice.
return [r for r in result]
@json_expose
def columns(self, start=0, end=None, category_labels=False,
dimension_labels=False):
start = int(start)
end = int_or_none(end)
if end is None:
end = len(self._cube)
if end - start > self.MAX_ENTRIES:
raise ValueError("No more than %i entries allowed at a time. Use 'start' and 'end' parameters to limit the selection."%self.MAX_ENTRIES)
return self._cube.toColumns(start=start, end=end,
category_labels=str_to_bool(category_labels),
dimension_labels=str_to_bool(dimension_labels))
@json_expose
def group_for_columns(self, start=0, end=None, as_values=None,
category_labels=False,
dimension_labels=False):
# TODO: This could be done more efficiently in SQL
if as_values is not None:
as_values = as_values.split(',')
groups = self._cube.group_for(*as_values)
if len(groups) > self.MAX_GROUPS:
raise ValueError("No more than %i groups allowed at a time. Please use finer filtering."%self.MAX_GROUPS)
groupcols = []
category_labels = str_to_bool(category_labels)
start = int(start)
end = int_or_none(end)
dimension_labels = str_to_bool(dimension_labels)
for group in groups:
if len(group) > self.MAX_ENTRIES:
raise ValueError("No more than %i entries allowed at a time. Use 'start' and 'end' parameters to limit the selection."%self.MAX_ENTRIES)
col = group.toColumns(start=start, end=end,
category_labels=category_labels,
dimension_labels=dimension_labels)
groupcols.append(col)
return groupcols
@json_expose
def jsonstat(self):
# Get a "raw access" cube, as large resultsets are
# very slow with pgpool
cube = self._get_cube(bypass_cache=True)
return pydatacube.jsonstat.to_jsonstat(cube)
@cp.expose
def csv(self):
cp.response.headers['Content-Type']='text/plain; charset=utf-8'
# Return a pipe so that the result can be streamed
r, w = os.pipe()
r, w = os.fdopen(r, 'rb'), os.fdopen(w, 'wb')
# Get a "raw access" cube, as large resultsets are
# very slow with pgpool
cube = self._get_cube(bypass_cache=True)
hdr = ",".join(cube.dimension_ids())
w.write(hdr+"\n")
# Launch the query in a new thread, so that it will
# asynchronously write to the pipe while Cherrypy
# reads it.
thread = threading.Thread(target=lambda: cube.dump_csv(w))
thread.daemon = True
thread.start()
return r
csv._cp_config = {'response.stream': True}
def __filter(self, **kwargs):
filters = {}
for dim, catstr in kwargs.iteritems():
filters[dim] = catstr.split(',')
new_filters = copy.deepcopy(self._filters)
new_filters.update(filters)
return self.__class__(self._connector,
self._resource_id, new_filters)
def __getattr__(self, attr):
parts = attr.split('&')
if parts[0] != 'filter':
return object.__getattr__(self, attr)
args = []
kwargs = {}
for part in parts[1:]:
split = part.split('=', 1)
if len(split) == 1:
args.append(split[0])
else:
kwargs[split[0]] = split[1]
return self.__filter(*args, **kwargs)
class DatabaseExposer(object):
def __init__(self, connector):
self._connector = connector
@json_expose
def index(self):
query = "SELECT id, specification FROM _datasets"
con = self._connector()
c = con.cursor()
c.execute(query)
entries = dict()
for key, spec in c:
spec = json.loads(spec)
entry = OrderedDict()
entry['metadata'] = spec['metadata']
entry['_links'] = OrderedDict()
entry['_links']['self'] = {
'href': cp.url(key, relative=False)
}
entries[key] = entry
ret = OrderedDict()
ret['_embedded'] = entries
ret['_links'] = object_hal_links(self)
return ret
def __getattr__(self, resource_id):
if resource_id.startswith('_'):
return object.__getattr__(self, resource_id)
if resource_id is 'default':
return object.__getattr__(self, resource_id)
if resource_id is 'exposed':
return object.__getattr__(self, resource_id)
return DbCubeResource(self._connector, resource_id)
class ResourceServer(object):
def __init__(self, resources):
self.resources = resources
@json_expose
def index(self):
ret = {}
ret['_links'] = object_hal_links(self)
return ret
def serve_sql():
SERVER_ROOT = os.path.dirname(os.path.abspath('__file__'))
import string
dispatch = cp.dispatch.Dispatcher(translate=string.maketrans('', ''))
def CORS():
cp.response.headers["Access-Control-Allow-Origin"] = "*"
cp.tools.CORS = cp.Tool('before_finalize', CORS)
config = {
'global': {
'SERVER_ROOT_DIR': SERVER_ROOT
},
'/': {
'request.dispatch': dispatch,
'tools.CORS.on': True,
'tools.jsonp.on': True
},
'/browser': {
'tools.staticdir.on': True,
'tools.staticdir.root': SERVER_ROOT,
'tools.staticdir.dir': 'browser',
'tools.staticdir.index': 'index.html'
}
}
cp.config.update(config)
conffilepath = os.path.join(SERVER_ROOT, 'sql_json_server.conf')
if os.path.exists(conffilepath):
cp.config.update(conffilepath)
db_config = cp.config['database.connection']
db_config_raw = cp.config.get('database.connection.raw', db_config)
def connector(bypass_cache=False):
#import psycopg2.extras
#con = psycopg2.extras.LoggingConnection(db_config)
#con.initialize(sys.stdout)
# Pgpool is really slow with large queries, so
# allow bypassing the cache
if not bypass_cache:
con = psycopg2.connect(db_config)
else:
con = psycopg2.connect(db_config_raw)
# Autocommit allows query cache to work. This is a
# bit of a hack, the proper way would be to automatically
# commit the session after the request. Autocommit is
# a bit slower, but with readonly mode shouldn't really
# affect anything else.
con.set_session(readonly=True, autocommit=True)
return con
#return mysql.connect(charset='utf8', use_unicode=True,
# cursorclass=SSCursor,
# **db_config)
resources = DatabaseExposer(connector)
server = ResourceServer(resources)
app = cp.tree.mount(server, '/', config=config)
if os.path.exists(conffilepath):
app.merge(conffilepath)
if hasattr(cp.engine, 'signals'):
# Conditional for older cherrpy versions.
# Not even sure what this does.
cp.engine.signals.subscribe()
cp.engine.start()
cp.engine.block()
if __name__ == '__main__':
serve_sql()