Skip to content

Commit 68d112a

Browse files
committed
Fix geo search
1 parent f6ab223 commit 68d112a

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

app.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ def by_location():
9696
response.status_code = 400
9797
return response
9898

99-
return jsonify({
100-
'data': mta.get_by_point(location, 5),
101-
'updated': mta.last_update()
102-
})
99+
data = mta.get_by_point(location, 5)
100+
return _make_envelope(data)
103101

104102
@app.route('/by-route/<route>', methods=['GET'])
105103
@cross_origin
@@ -128,9 +126,18 @@ def routes():
128126
'updated': mta.last_update()
129127
})
130128

129+
def _envelope_reduce(a, b):
130+
if a['last_update'] and b['last_update']:
131+
return a if a['last_update'] < b['last_update'] else b
132+
elif a['last_update']:
133+
return a
134+
else:
135+
return b
136+
131137
def _make_envelope(data):
132-
reduce_func = lambda a,b: a if a['last_update'] < b['last_update'] else b
133-
time = reduce(reduce_func, data)['last_update']
138+
time = None
139+
if data:
140+
time = reduce(_envelope_reduce, data)['last_update']
134141

135142
return jsonify({
136143
'data': data,

mtapi/mtapi.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def distance(p1, p2):
1616
class Mtapi(object):
1717

1818
class _Station(object):
19+
last_update = None
20+
1921
def __init__(self, json):
2022
self.json = json
2123
self.trains = {}
@@ -31,13 +33,12 @@ def add_train(self, route_id, direction, train_time, feed_time):
3133
'time': train_time
3234
})
3335
self.last_update = feed_time
34-
self.has_data = True
3536

3637
def clear_train_data(self):
3738
self.trains['N'] = []
3839
self.trains['S'] = []
3940
self.routes = set()
40-
self.has_data = False
41+
self.last_update = None
4142

4243
def sort_trains(self, max_trains):
4344
self.trains['S'] = sorted(self.trains['S'], key=itemgetter('time'))[:max_trains]
@@ -48,8 +49,7 @@ def serialize(self):
4849
'N': self.trains['N'],
4950
'S': self.trains['S'],
5051
'routes': self.routes,
51-
'last_update': self.last_update,
52-
'hasData': self.has_data
52+
'last_update': self.last_update
5353
}
5454
out.update(self.json)
5555
return out
@@ -183,9 +183,10 @@ def get_by_point(self, point, limit=5):
183183
self._update()
184184

185185
with self._read_lock:
186-
sortable_stations = copy.deepcopy(self._stations)
186+
sortable_stations = copy.deepcopy(self._stations).values()
187187

188188
sortable_stations.sort(key=lambda x: distance(x['location'], point))
189+
sortable_stations = map(lambda x: x.serialize(), sortable_stations)
189190
return sortable_stations[:limit]
190191

191192
def get_routes(self):

0 commit comments

Comments
 (0)