This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.py
343 lines (303 loc) · 11.7 KB
/
app.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
import json
import time
import aiohttp
import aioredis
from quart import Quart, render_template, request, g
from quart_cors import cors
from docopt import docopt
from wdreconcile.engine import ReconcileEngine
from wdreconcile.suggest import SuggestEngine
from wdreconcile.monitoring import Monitoring
from config import *
try:
from config import wikibase_name
from config import wikibase_main_page
except ImportError:
wikibase_name = 'Wikidata'
wikibase_main_page = 'https://www.wikidata.org/wiki/Wikidata:Main_Page'
app = Quart(__name__, static_url_path='/static/', static_folder='static/')
app = cors(app, allow_origin='*')
@app.before_serving
async def setup():
app.redis_client = aioredis.from_url(redis_uri, encoding='utf-8', decode_responses=True)
app.http_connector = aiohttp.TCPConnector(limit_per_host=10)
app.http_session_obj = aiohttp.ClientSession(connector=app.http_connector)
app.http_session = await app.http_session_obj.__aenter__()
@app.before_request
async def request_context():
g.reconcile = ReconcileEngine(app.redis_client, app.http_session)
g.suggest = SuggestEngine(app.redis_client, app.http_session)
g.monitoring = Monitoring(app.redis_client)
@app.after_serving
async def teardown():
await app.http_session.__aexit__(None, None, None)
app.redis_client.close()
await app.redis_client.wait_closed()
def jsonp(view):
async def wrapped(*posargs, **kwargs):
args = {}
# if we access the args via get(),
# we can get encoding errors...
post_data = await request.form
for k in post_data:
args[k] = post_data.get(k)
for k in request.args:
args[k] = request.args.get(k)
callback = args.get('callback')
status_code = 200
try:
result = await view(args, *posargs, **kwargs)
except (Exception) as e:#ValueError, AttributeError, KeyError) as e:
import traceback, sys
traceback.print_exc(file=sys.stdout)
result = {'status':'error',
'message':'invalid query',
'details': str(e)}
status_code = 403
if callback:
result = '%s(%s);' % (callback, json.dumps(result))
if status_code == 200:
return result
else:
result['arguments'] = args
return result, status_code
return wrapped
@app.route('/api', endpoint='api-default-lang', methods=['GET','POST'])
@jsonp
async def api_default_lang(args):
if 'lang' not in args:
args['lang'] = 'en'
return await api(args)
@app.route('/<lang>/api', endpoint='api', methods=['GET','POST'])
@jsonp
async def api_custom_lang(args, lang):
args['lang'] = lang
return await api(args)
async def api(args):
query = args.get('query')
queries = args.get('queries')
extend = args.get('extend')
lang = fix_lang(args.get('lang'))
start_time = time.time()
if query:
try:
query = json.loads(query)
except ValueError:
query = {'query':query}
result = await g.reconcile.process_single_query(query,
default_language=lang)
processing_time = time.time() - start_time
await g.monitoring.log_request(1, processing_time)
return result
elif queries:
queries = json.loads(queries)
res = await g.reconcile.process_queries(queries,
default_language=lang)
processing_time = time.time() - start_time
await g.monitoring.log_request(len(queries), processing_time)
return res
elif extend:
args['extend'] = json.loads(extend)
return await g.reconcile.fetch_properties_by_batch(args)
else:
default_types = []
if default_type_entity:
default_types = [
{
'id': default_type_entity,
'name': await g.reconcile.item_store.get_label(default_type_entity, lang)
}
]
identify = {
'name':service_name + (' (%s)' % lang),
'versions': ['0.1', '0.2'],
'view':{'url':qid_url_pattern},
'identifierSpace': identifier_space,
'schemaSpace': schema_space,
'suggest' : {
'type' : {
'service_url' : this_host,
'service_path' : '/%s/suggest/type' % lang
},
'property' : {
'service_url' : this_host,
'service_path' : '/%s/suggest/property' % lang
},
'entity' : {
'service_url' : this_host,
'service_path' : '/%s/suggest/entity' % lang
}
},
'preview' : {
'url': this_host+'/%s/preview?id={{id}}' % lang,
'width' : preview_width,
'height': preview_height,
},
'defaultTypes': default_types,
'extend' : {
'propose_properties': {
'service_url' : this_host,
'service_path' : '/%s/propose_properties' % lang,
},
'property_settings': [
{
'name': 'limit',
'label': 'Limit',
'help_text': 'Maximum number of values to return per row (0 for no limit)',
'type': 'number',
'default': 0,
},
{
'name': 'rank',
'label': 'Ranks',
'help_text': 'Filter statements by rank',
'type': 'select',
'default': 'best',
'choices': [
{
'value': 'any',
'name': 'Any rank',
},
{
'value': 'best',
'name': 'Only the best rank',
},
{
'value': 'no_deprecated',
'name': 'Preferred and normal ranks',
}
]
},
{
'name': 'references',
'label': 'References',
'help_text': 'Filter statements by their references',
'type': 'select',
'default' : 'any',
'choices': [
{
'value': 'any',
'name': 'Any statement',
},
{
'value': 'referenced',
'name': 'At least one reference',
},
{
'value': 'no_wiki',
'name': 'At least one non-wiki reference',
}
]
},
{
'name': 'count',
'label': 'Return counts instead of values',
'help_text': 'The number of values will be returned.',
'type': 'checkbox',
'default': False,
}
]
},
}
return identify
@app.route('/suggest/type', endpoint='suggest-type-default-lang', methods=['GET','POST'])
@jsonp
async def suggest_property(args):
args['lang'] = fix_lang(args.get('lang'))
return await g.suggest.find_type(args)
@app.route('/suggest/property', endpoint='suggest-property-default-lang', methods=['GET','POST'])
@jsonp
async def suggest_property(args):
args['lang'] = fix_lang(args.get('lang'))
return await g.suggest.find_property(args)
@app.route('/suggest/entity', endpoint='suggest-entity-default-lang', methods=['GET','POST'])
@jsonp
async def suggest_property(args):
args['lang'] = fix_lang(args.get('lang'))
return await g.suggest.find_entity(args)
@app.route('/preview', endpoint='preview-default-lang', methods=['GET','POST'])
@jsonp
async def preview(args):
args['lang'] = fix_lang(args.get('lang'))
return await g.suggest.preview(args)
@app.route('/<lang>/suggest/type', endpoint='suggest-type', methods=['GET','POST'])
@jsonp
async def suggest_type(args, lang):
args['lang'] = fix_lang(lang)
return await g.suggest.find_type(args)
@app.route('/<lang>/suggest/property', endpoint='suggest-property', methods=['GET','POST'])
@jsonp
async def suggest_property(args, lang):
args['lang'] = fix_lang(lang)
return await g.suggest.find_property(args)
@app.route('/<lang>/suggest/entity', endpoint='suggest-entity', methods=['GET','POST'])
@jsonp
async def suggest_entity(args, lang):
args['lang'] = fix_lang(lang)
return await g.suggest.find_entity(args)
@app.route('/<lang>/flyout/type', endpoint='flyout-type', methods=['GET','POST'])
@jsonp
async def flyout_type(args, lang):
args['lang'] = fix_lang(lang)
return await g.suggest.flyout_type(args)
@app.route('/<lang>/flyout/property', endpoint='flyout-property', methods=['GET','POST'])
@jsonp
async def flyout_property(args, lang):
args['lang'] = fix_lang(lang)
return await g.suggest.flyout_property(args)
@app.route('/<lang>/flyout/entity', endpoint='flyout-entity', methods=['GET','POST'])
@jsonp
async def flyout_entity(args, lang):
args['lang'] = fix_lang(lang)
return await g.suggest.flyout_entity(args)
@app.route('/<lang>/preview', endpoint='preview', methods=['GET','POST'])
@jsonp
async def preview(args, lang):
args['lang'] = fix_lang(lang)
return await g.suggest.preview(args)
@app.route('/fetch_values', endpoint='fetch-values-default-lang', methods=['GET','POST'])
@jsonp
async def fetch_values(args):
args['lang'] = fix_lang(args.get('lang'))
return await g.reconcile.fetch_values(args)
@app.route('/<lang>/fetch_values', endpoint='fetch-values', methods=['GET','POST'])
@jsonp
async def fetch_values(args, lang):
args['lang'] = fix_lang(lang)
return await g.reconcile.fetch_values(args)
@app.route('/<lang>/propose_properties', endpoint='propose-properties', methods=['GET','POST'])
@jsonp
async def propose_properties(args, lang):
args['lang'] = fix_lang(lang)
return await g.suggest.propose_properties(args)
@app.route('/<lang>/fetch_property_by_batch', endpoint='fetch-property-batch', methods=['GET','POST'])
@jsonp
async def fetch_property_by_batch(args, lang):
args['lang'] = fix_lang(lang)
return await reconcile.fetch_property_by_batch(args)
@app.route('/<lang>/fetch_properties_by_batch', endpoint='fetch-properties-batch', methods=['GET','POST'])
@jsonp
async def fetch_property_by_batch(args, lang):
args['lang'] = fix_lang(lang)
args['extend'] = json.loads(args.get('extend', '{}'))
return await reconcile.fetch_properties_by_batch(args)
@app.route('/', endpoint='home')
async def home():
context = {
'service_status_url': this_host+'/monitoring',
'endpoint_url': this_host+'/en/api',
'wikibase_name': wikibase_name,
'wikibase_main_page': wikibase_main_page
}
return await render_template('index.html', **context)
@app.route('/monitoring')
async def monitor():
return {'stats': await g.monitoring.get_rates()}
def fix_lang(lng):
if not lng:
return 'en'
if lng == 'jp':
return 'ja'
return lng
if __name__ == '__main__':
app.run(debug=True, port=8000, host='0.0.0.0')