Skip to content

Commit e231f28

Browse files
committed
Limit response size in simple query db
1 parent 65ecd41 commit e231f28

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

budgetkey_api/modules/simpledb.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def fetch_table(self, table):
148148

149149
class SimpleDBBlueprint(Blueprint):
150150

151-
MAX_PAYLOAD_SIZE = 250 * 1024 # 250 KB
151+
MAX_PAYLOAD_SIZE = 120 * 1024 # 120 KB
152152

153153
def __init__(self, connection_string, search_blueprint, db_blueprint):
154154
super().__init__('simpledb', 'simpledb')
@@ -196,28 +196,22 @@ def get_table(self, table):
196196
def get_tables(self):
197197
return jsonpify(self.tables.TABLES)
198198

199-
def trim_json(self, data, max_size):
200-
ret = dict(
201-
download_url=data.get('download_url'),
202-
warnings=data.get('warnings'),
203-
num_rows=0,
204-
total_rows=data.get('total', 0),
205-
rows=[]
206-
)
207-
rows = data.get('rows', [])
208-
ret_json = json.dumps(ret, ensure_ascii=False)
199+
def trim_json(self, data, rows_field, count_field, max_size):
200+
rows = data.get(rows_field, [])
201+
data[rows_field] = []
202+
ret_json = json.dumps(data, ensure_ascii=False)
209203
current_len = len(ret_json)
210204
trimmed_rows = []
211205
for row in rows:
212206
row_json = json.dumps(row, ensure_ascii=False)
213-
current_len += len(row_json) + 1
207+
current_len += len(row_json) + 2
214208
if current_len > max_size:
215209
break
216210
else:
217211
trimmed_rows.append(row)
218-
ret['num_rows'] = len(trimmed_rows)
219-
ret['rows'] = trimmed_rows
220-
ret_json = json.dumps(ret, ensure_ascii=False)
212+
data[count_field] = len(trimmed_rows)
213+
data[rows_field] = trimmed_rows
214+
ret_json = json.dumps(data, ensure_ascii=False)
221215
return ret_json
222216

223217
def query_table(self, table):
@@ -236,7 +230,14 @@ def query_table(self, table):
236230
warnings = check_for_common_errors(table, sql)
237231
if warnings:
238232
ret['warnings'] = warnings
239-
ret = self.trim_json(ret, self.MAX_PAYLOAD_SIZE)
233+
ret = dict(
234+
download_url=ret.get('download_url'),
235+
warnings=ret.get('warnings'),
236+
num_rows=ret.get('page_size', 0),
237+
total_rows=ret.get('total', 0),
238+
rows=ret.get('rows', []),
239+
)
240+
ret = self.trim_json(ret, 'rows', 'num_rows', self.MAX_PAYLOAD_SIZE)
240241
return Response(ret, mimetype='application/json')
241242

242243
def simple_search(self, table):
@@ -277,9 +278,14 @@ def simple_search(self, table):
277278
res[k1] = src[k]
278279
break
279280
results.append(res)
280-
ret['search_results'] = results
281-
return jsonpify(ret)
282-
281+
ret = dict(
282+
search_results=results,
283+
num_results=len(results),
284+
total_results=ret.get('search_counts', {}).get('total_overall', 0),
285+
)
286+
ret = self.trim_json(ret, 'search_results', 'num_results', self.MAX_PAYLOAD_SIZE)
287+
return Response(ret, mimetype='application/json')
288+
283289

284290
def setup_simpledb(app, es_blueprint, db_blueprint):
285291
sdb = SimpleDBBlueprint(os.environ['DATABASE_READONLY_URL'], es_blueprint, db_blueprint)

0 commit comments

Comments
 (0)