This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webservice.py
322 lines (308 loc) · 12.8 KB
/
webservice.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
import ast
import config
from flask import jsonify, request, Blueprint
import logging.config
import os
from responseobjects.elastic_request_builder import \
ElasticTransformDump as EsTd
from responseobjects.utilities import json_pp
# Setting up logging
base_path = os.path.dirname(os.path.abspath(__file__))
logging.config.fileConfig('{}/config/logging.conf'.format(base_path))
bp_logger = logging.getLogger("dashboardService.webservice")
# Setting up the blueprint
webservicebp = Blueprint('webservicebp', 'webservicebp')
# TODO: Write the docstrings so they can support swagger.
# Please see https://github.com/rochacbruno/flasgger
# stackoverflow.com/questions/43911510/ \
# how-to-write-docstring-for-url-parameters
@webservicebp.route('/repository/files', methods=['GET'])
@webservicebp.route('/repository/files/', methods=['GET'])
@webservicebp.route('/repository/files/<file_id>', methods=['GET'])
def get_data(file_id=None):
"""
Returns a dictionary with entries that can be used by the browser
to display the data and facets
parameters:
- name: filters
in: query
type: string
description: Filters to be applied when calling ElasticSearch
- name: from
in: query
type: integer
description: From where should we start returning the results
- name: size
in: integer
type: string
description: Size of the page being returned
- name: order
in: query
type: string
description: Whether it should be in ascending or descending order
- name: sort
in: query
type: string
description: Which field to sort by
:return: Returns a dictionary with the entries to be used when generating
the facets and/or table data
"""
# Setup logging
logger = logging.getLogger("dashboardService.webservice.get_data")
# Get all the parameters from the URL
logger.debug('Parameter file_id: {}'.format(file_id))
filters = request.args.get('filters', '{"file": {}}')
logger.debug("Filters string is: {}".format(filters))
try:
logger.info("Extracting the filter parameter from the request")
filters = ast.literal_eval(filters)
filters = {"file": {}} if filters == {} else filters
except Exception, e:
logger.error("Malformed filters parameter: {}".format(e.message))
return "Malformed filters parameter"
# Make the default pagination
logger.info("Creating pagination")
pagination = {
"from": request.args.get('from', 1, type=int),
"order": request.args.get('order', 'desc'),
"size": request.args.get('size', 5, type=int),
"sort": request.args.get('sort', 'center_name'),
}
logger.debug("Pagination: \n".format(json_pp(pagination)))
# Handle <file_id> request form
if file_id is not None:
logger.info("Handling single file id search")
filters['file']['fileId'] = {"is": [file_id]}
# Create and instance of the ElasticTransformDump
logger.info("Creating ElasticTransformDump object")
es_td = EsTd(es_domain=os.getenv("ES_DOMAIN", "elasticsearch1"),
es_port=os.getenv("ES_PORT", 9200),
es_protocol=os.getenv("ES_PROTOCOL", "http"))
# Get the response back
logger.info("Creating the API response")
response = es_td.transform_request(filters=filters,
pagination=pagination,
post_filter=True)
# Returning a single response if <file_id> request form is used
if file_id is not None:
response = response['hits'][0]
return jsonify(response)
@webservicebp.route('/repository/files/piecharts', methods=['GET'])
def get_data_pie():
"""
Returns a dictionary with entries that can be used by the
browser to generate piecharts
parameters:
- name: filters
in: query
type: string
description: Filters to be applied when calling ElasticSearch
- name: from
in: query
type: integer
description: From where should we start returning the results
- name: size
in: integer
type: string
description: Size of the page being returned
- name: order
in: query
type: string
description: Whether it should be in ascending or descending order
- name: sort
in: integer
type: string
description: Which field to sort by
:return: Returns a dictionary with the entries to be used when generating
a pie chart
"""
# Setup logging
logger = logging.getLogger("dashboardService.webservice.get_data_pie")
# Get all the parameters from the URL
filters = request.args.get('filters', '{"file": {}}')
logger.debug("Filters string is: {}".format(filters))
try:
logger.info("Extracting the filter parameter from the request")
filters = ast.literal_eval(filters)
filters = {"file": {}} if filters == {} else filters
except Exception, e:
logger.error("Malformed filters parameter: {}".format(e.message))
return "Malformed filters parameter"
# Make the default pagination
logger.info("Creating pagination")
pagination = {
"from": request.args.get('from', 1, type=int),
"order": request.args.get('order', 'desc'),
"size": request.args.get('size', 5, type=int),
"sort": request.args.get('sort', 'center_name'),
}
logger.debug("Pagination: \n".format(json_pp(pagination)))
# Create and instance of the ElasticTransformDump
logger.info("Creating ElasticTransformDump object")
es_td = EsTd(es_domain=os.getenv("ES_DOMAIN", "elasticsearch1"),
es_port=os.getenv("ES_PORT", 9200),
es_protocol=os.getenv("ES_PROTOCOL", "http"))
# Get the response back
logger.info("Creating the API response")
response = es_td.transform_request(filters=filters,
pagination=pagination,
post_filter=False)
# Returning a single response if <file_id> request form is used
return jsonify(response)
@webservicebp.route('/repository/files/summary', methods=['GET'])
def get_summary():
"""
Returns a summary based on the filters passed on to the call. Based on the
ICGC endpoint.
parameters:
- name: filters
in: query
type: string
description: Filters to be applied when calling ElasticSearch
:return: Returns a jsonified Summary API response
"""
# Setup logging
logger = logging.getLogger("dashboardService.webservice.get_summary")
# Get the filters from the URL
filters = request.args.get('filters', '{"file": {}}')
logger.debug("Filters string is: {}".format(filters))
try:
logger.info("Extracting the filter parameter from the request")
filters = ast.literal_eval(filters)
filters = {"file": {}} if filters == {} else filters
except Exception, e:
logger.error("Malformed filters parameter: {}".format(e.message))
return "Malformed filters parameter"
# Create and instance of the ElasticTransformDump
logger.info("Creating ElasticTransformDump object")
es_td = EsTd(es_domain=os.getenv("ES_DOMAIN", "elasticsearch1"),
es_port=os.getenv("ES_PORT", 9200),
es_protocol=os.getenv("ES_PROTOCOL", "http"))
# Get the response back
logger.info("Creating the API response")
response = es_td.transform_summary(filters=filters)
# Returning a single response if <file_id> request form is used
return jsonify(response)
@webservicebp.route('/keywords', methods=['GET'])
def get_search():
"""
Creates and returns a dictionary with entries that best match the query
passed in to the endpoint
parameters:
- name: filters
in: query
type: string
description: Filters to be applied when calling ElasticSearch
- name: q
in: query
type: string
description: String query to use when calling ElasticSearch
- name: type
in: query
type: string
description: Which type of response format should be returned
- name: field
in: query
type: string
description: Which field to search on. Defaults to file id
- name: from
in: query
type: integer
description: From where should we start returning the results
- name: size
in: integer
type: string
description: Size of the page being returned
:return: A dictionary with entries that best match the query passed in
to the endpoint
"""
# Setup logging
logger = logging.getLogger("dashboardService.webservice.get_search")
# Get all the parameters from the URL
# Get the query to use for searching. Forcing it to be str for now
_query = request.args.get('q', '', type=str)
logger.debug("String query is: {}".format(_query))
# Get the filters
filters = request.args.get('filters', '{"file": {}}')
try:
# Set up the default filter if it is returned as an empty dictionary
logger.info("Extracting the filter parameter from the request")
filters = ast.literal_eval(filters)
filters = {"file": {}} if filters == {} else filters
except Exception, e:
logger.error("Malformed filters parameter: {}".format(e.message))
return "Malformed filters parameter"
# Generate the pagination dictionary out of the endpoint parameters
logger.info("Creating pagination")
pagination = {
"from": request.args.get('from', 1, type=int),
"size": request.args.get('size', 5, type=int)
}
logger.debug("Pagination: \n".format(json_pp(pagination)))
# Get the entry format and search field
_type = request.args.get('type', 'file')
# Get the field to search
field = request.args.get('field', 'fileId')
# HACK: Adding this small check to make sure the search bar works with
if _type in {'donor', 'file-donor'}:
field = 'donor'
# Create and instance of the ElasticTransformDump
logger.info("Creating ElasticTransformDump object")
es_td = EsTd(es_domain=os.getenv("ES_DOMAIN", "elasticsearch1"),
es_port=os.getenv("ES_PORT", 9200),
es_protocol=os.getenv("ES_PROTOCOL", "http"))
# Get the response back
logger.info("Creating the API response")
response = es_td.transform_autocomplete_request(pagination,
filters=filters,
_query=_query,
search_field=field,
entry_format=_type)
return jsonify(response)
@webservicebp.route('/repository/files/order', methods=['GET'])
def get_order():
"""
Get the order of the facets from the order_config file
:return: A dictionary with a list containing the order of the facets
"""
# Setup logging
logger = logging.getLogger("dashboardService.webservice.get_order")
# Open the order_config file and get the order list
logger.info("Getting t")
with open('{}/order_config'.format(
os.path.dirname(config.__file__))) as order:
order_list = [line.rstrip('\n') for line in order]
return jsonify({'order': order_list})
@webservicebp.route('/repository/files/export', methods=['GET'])
def get_manifest():
"""
Creates and returns a manifest based on the filters pased on
to this endpoint
parameters:
- name: filters
in: query
type: string
description: Filters to be applied when generating the manifest
:return: A manifest that the user can use to download the files in there
"""
# Setup logging
logger = logging.getLogger("dashboardService.webservice.get_manifest")
filters = request.args.get('filters', '{"file": {}}')
logger.debug("Filters string is: {}".format(filters))
try:
logger.info("Extracting the filter parameter from the request")
filters = ast.literal_eval(filters)
filters = {"file": {}} if filters == {} else filters
except Exception, e:
logger.error("Malformed filters parameter: {}".format(e.message))
return "Malformed filters parameter"
# Create and instance of the ElasticTransformDump
logger.info("Creating ElasticTransformDump object")
es_td = EsTd(es_domain=os.getenv("ES_DOMAIN", "elasticsearch1"),
es_port=os.getenv("ES_PORT", 9200),
es_protocol=os.getenv("ES_PROTOCOL", "http"))
# Get the response back
logger.info("Creating the API response")
response = es_td.transform_manifest(filters=filters)
# Return the excel file
return response