Skip to content

Commit 9ab13de

Browse files
committed
Fixed return debug data in JSON if debug enabled on the server.
1 parent 1aed751 commit 9ab13de

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

web/websearch.py

+25-19
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353

5454

5555
app = Flask(__name__, template_folder='templates/')
56-
app.debug = not getenv('WEBSEARCH_DEBUG') is None
57-
app.debug = True
56+
app.debug = not (getenv('WEBSEARCH_DEBUG') is None)
5857

5958

6059
# ---------------------------------------------------------
@@ -473,14 +472,16 @@ def formatResponse(data, code=200):
473472
"""Format response output."""
474473
# Format json - return empty
475474
result = data['result'] if 'result' in data else {}
476-
format = 'json'
475+
if app.debug and 'debug' in data:
476+
result['debug'] = data['debug']
477+
output_format = 'json'
477478
if request.args.get('format'):
478-
format = request.args.get('format')
479+
output_format = request.args.get('format')
479480
if 'format' in data:
480-
format = data['format']
481+
output_format = data['format']
481482

482483
tpl = data['template'] if 'template' in data else 'answer.html'
483-
if format == 'html' and tpl is not None:
484+
if output_format == 'html' and tpl is not None:
484485
if 'route' not in data:
485486
data['route'] = '/'
486487
return render_template(tpl, rc=(code == 200), **data), code
@@ -1017,15 +1018,17 @@ def ppretty(value):
10171018
# returns - result, distance tuple
10181019
def reverse_search(lon, lat, debug):
10191020
result = {
1021+
'total_found': 0,
10201022
'count': 0,
1021-
'results': []
1023+
'matches': []
10221024
}
10231025

10241026
if debug:
10251027
result['debug'] = {
10261028
'longitude': lon,
10271029
'latitude': lat,
1028-
'queries': []
1030+
'queries': [],
1031+
'results': [],
10291032
}
10301033

10311034
try:
@@ -1080,37 +1083,39 @@ def reverse_search(lon, lat, debug):
10801083
# limit the result set to the single closest match
10811084
limit = " ORDER BY distance ASC LIMIT 1"
10821085

1083-
result = {}
1086+
myresult = {}
10841087
# form the final queries and execute
10851088
for where in wherelon:
10861089
sql = select + " AND ".join([where, wherelat, whereclass]) + limit
1087-
if debug:
1088-
result['debug']['queries'].append(sql)
10891090
# Boolean, {'matches': [{'weight': 0, 'id', 'attrs': {}}], 'total_found': 0}
10901091
status, result_new = get_query_result(cursor, sql, ())
1091-
if 'matches' in result and len(result['matches']) > 0:
1092-
result = mergeResultObject(result, result_new)
1092+
if debug:
1093+
result['debug']['queries'].append(sql)
1094+
result['debug']['results'].append(result_new)
1095+
if 'matches' in myresult and len(myresult['matches']) > 0:
1096+
myresult = mergeResultObject(myresult, result_new)
10931097
else:
1094-
result = result_new.copy()
1098+
myresult = result_new.copy()
10951099

1096-
count = len(result['matches'])
1100+
count = len(myresult['matches'])
10971101
db.close()
10981102

10991103
if debug:
1100-
result['debug']['matches'] = result['matches']
1104+
result['debug']['matches'] = myresult['matches']
11011105

11021106
smallest_row = None
11031107
smallest_distance = None
11041108

11051109
# For the rows returned, find the smallest calculated distance
11061110
# (the 180 meridian case may result in 2 rows to check)
1107-
for match in result['matches']:
1111+
for match in myresult['matches']:
11081112
distance = match['attrs']['distance']
11091113

11101114
if smallest_row is None or distance < smallest_distance:
11111115
smallest_row = match
11121116
smallest_distance = distance
11131117

1118+
result = mergeResultObject(result, myresult)
11141119
result['count'] = 1
11151120
result['matches'] = [smallest_row]
11161121
result['start_index'] = 1
@@ -1152,10 +1157,11 @@ def reverse_search_url(lon, lat):
11521157
code = 200
11531158
result, distance = reverse_search(lon, lat, debug)
11541159
data['result'] = prepareResultJson(result)
1155-
11561160
if debug:
11571161
times['process'] = time() - times['start']
1158-
result['debug']['times'] = times
1162+
data['debug'] = result['debug']
1163+
data['debug']['distance'] = distance
1164+
data['debug_times'] = times
11591165

11601166
return formatResponse(data, code)
11611167

0 commit comments

Comments
 (0)