-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Jormun] optim geovelo #2711
[Jormun] optim geovelo #2711
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,10 @@ | |
import logging | ||
import requests as requests | ||
import pybreaker | ||
import json | ||
import ujson | ||
|
||
import itertools | ||
import sys | ||
from navitiacommon import response_pb2 | ||
from jormungandr import app | ||
from jormungandr.exceptions import TechnicalError, InvalidArguments, UnableToParse | ||
|
@@ -89,7 +92,7 @@ def status(self): | |
@classmethod | ||
def _pt_object_summary_isochrone(cls, pt_object): | ||
coord = get_pt_object_coord(pt_object) | ||
return [coord.lat, coord.lon, getattr(pt_object, 'uri', None)] | ||
return [coord.lat, coord.lon, None] | ||
|
||
@classmethod | ||
def _make_request_arguments_bike_details(cls, bike_speed_mps): | ||
|
@@ -131,7 +134,11 @@ def _call_geovelo(self, url, method=requests.post, data=None): | |
url, | ||
timeout=self.timeout, | ||
data=data, | ||
headers={'content-type': 'application/json', 'Api-Key': self.api_key}, | ||
headers={ | ||
'content-type': 'application/json', | ||
'Api-Key': self.api_key, | ||
'Accept-Encoding': 'gzip, br', | ||
}, | ||
) | ||
except pybreaker.CircuitBreakerError as e: | ||
logging.getLogger(__name__).error('Geovelo routing service dead (error: {})'.format(e)) | ||
|
@@ -157,14 +164,12 @@ def _get_matrix(cls, json_response): | |
if json_response[0] != ["start_reference", "end_reference", "duration"]: | ||
logging.getLogger(__name__).error('Geovelo parsing error. Response: {}'.format(json_response)) | ||
raise UnableToParse('Geovelo parsing error. Response: {}'.format(json_response)) | ||
for e in json_response[1:]: | ||
routing = row.routing_response.add() | ||
if e[2]: | ||
routing.duration = e[2] | ||
routing.routing_status = response_pb2.reached | ||
else: | ||
routing.duration = -1 | ||
routing.routing_status = response_pb2.unknown | ||
|
||
add_ = row.routing_response.add | ||
for e in itertools.islice(json_response, 1, sys.maxint): | ||
duration, routing_status = (e[2], response_pb2.reached) if e[2] else (-1, response_pb2.unknown) | ||
add_(duration=duration, routing_status=routing_status) | ||
|
||
return sn_routing_matrix | ||
|
||
@classmethod | ||
|
@@ -198,10 +203,10 @@ def get_street_network_routing_matrix( | |
|
||
data = self._make_request_arguments_isochrone(origins, destinations, request['bike_speed']) | ||
r = self._call_geovelo( | ||
'{}/{}'.format(self.service_url, 'api/v2/routes_m2m'), requests.post, json.dumps(data) | ||
'{}/{}'.format(self.service_url, 'api/v2/routes_m2m'), requests.post, ujson.dumps(data) | ||
) | ||
self._check_response(r) | ||
resp_json = r.json() | ||
resp_json = ujson.loads(r.text) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/kennethreitz/requests/issues/1595 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, i was looking forward to being able to specify the json module in .json(), like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mypy doesn't like requests.models.json = ujson |
||
|
||
if len(resp_json) - 1 != len(origins) * len(destinations): | ||
logging.getLogger(__name__).error('Geovelo nb response != nb requested') | ||
|
@@ -283,7 +288,9 @@ def _get_response(cls, json_response, pt_object_origin, pt_object_destination, f | |
|
||
speed = section.length / section.duration if section.duration != 0 else 0 | ||
|
||
for geovelo_instruction in geovelo_section['details']['instructions'][1:]: | ||
for geovelo_instruction in itertools.islice( | ||
geovelo_section['details']['instructions'], 1, sys.maxint | ||
): | ||
path_item = section.street_network.path_items.add() | ||
path_item.name = geovelo_instruction[1] | ||
path_item.length = geovelo_instruction[2] | ||
|
@@ -292,9 +299,7 @@ def _get_response(cls, json_response, pt_object_origin, pt_object_destination, f | |
|
||
shape = decode_polyline(geovelo_resp['sections'][0]['geometry']) | ||
for sh in shape: | ||
coord = section.street_network.coordinates.add() | ||
coord.lon = sh[0] | ||
coord.lat = sh[1] | ||
section.street_network.coordinates.add(lon=sh[0], lat=sh[1]) | ||
|
||
return resp | ||
|
||
|
@@ -320,10 +325,10 @@ def _direct_path( | |
'objects_as_ids=true&', | ||
), | ||
requests.post, | ||
json.dumps(data), | ||
ujson.dumps(data), | ||
) | ||
self._check_response(r) | ||
resp_json = r.json() | ||
resp_json = ujson.loads(r.text) | ||
|
||
if len(resp_json) != 1: | ||
logging.getLogger(__name__).error('Geovelo nb response != nb requested') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
None
for stop?And are we sure that geovelo keep the request order? And by sure I mean does is it documented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since in the old code, we didn't check the order... I assumed that the order was kept