From 357458f44da40238632232ca81a0cb97e36567df Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Tue, 22 Mar 2016 15:19:03 +0100 Subject: [PATCH] Check for some issues like empty input, recoverable exceptions, etc --- server/extension/sql/100_routing_helper.sql | 14 ++++++++++++-- .../cartodb_services/mapzen/types.py | 8 ++++++-- .../cartodb_services/tools/coordinates.py | 2 ++ .../cartodb_services/tools/polyline.py | 3 --- server/lib/python/cartodb_services/setup.py | 2 +- .../cartodb_services/test/test_heremapsrouting.py | 1 - 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/server/extension/sql/100_routing_helper.sql b/server/extension/sql/100_routing_helper.sql index cfd8035..6e561ba 100644 --- a/server/extension/sql/100_routing_helper.sql +++ b/server/extension/sql/100_routing_helper.sql @@ -28,6 +28,11 @@ RETURNS cdb_dataservices_server.simple_route AS $$ try: client = MapzenRouting(user_routing_config.mapzen_app_key) + if not origin or not destination: + plpy.notice("Empty origin or destination") + quota_service.increment_empty_service_use() + return [None, None, None] + orig_lat = plpy.execute("SELECT ST_Y('%s') AS lat" % origin)[0]['lat'] orig_lon = plpy.execute("SELECT ST_X('%s') AS lon" % origin)[0]['lon'] origin_coordinates = Coordinate(orig_lon, orig_lat) @@ -39,10 +44,15 @@ RETURNS cdb_dataservices_server.simple_route AS $$ if resp and resp.shape: shape_linestring = polyline_to_linestring(resp.shape) - quota_service.increment_success_service_use() - return [shape_linestring, resp.length, resp.duration] + if shape_linestring: + quota_service.increment_success_service_use() + return [shape_linestring, resp.length, resp.duration] + else: + quota_service.increment_empty_service_use() + return [None, None, None] else: quota_service.increment_empty_service_use() + return [None, None, None] except BaseException as e: import sys, traceback type_, value_, traceback_ = sys.exc_info() diff --git a/server/lib/python/cartodb_services/cartodb_services/mapzen/types.py b/server/lib/python/cartodb_services/cartodb_services/mapzen/types.py index bfaef3b..fa3aeed 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapzen/types.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapzen/types.py @@ -10,7 +10,11 @@ def polyline_to_linestring(polyline): coordinates.append("%s %s" % (point[1]/10, point[0]/10)) wkt_coordinates = ','.join(coordinates) - sql = "SELECT ST_GeomFromText('LINESTRING({0})', 4326) as geom".format(wkt_coordinates) - geometry = plpy.execute(sql, 1)[0]['geom'] + try: + sql = "SELECT ST_GeomFromText('LINESTRING({0})', 4326) as geom".format(wkt_coordinates) + geometry = plpy.execute(sql, 1)[0]['geom'] + except BaseException as e: + plpy.warning("Can't generate LINESTRING from polyline: {0}".format(e)) + geometry = None return geometry diff --git a/server/lib/python/cartodb_services/cartodb_services/tools/coordinates.py b/server/lib/python/cartodb_services/cartodb_services/tools/coordinates.py index c8c0748..759b02e 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tools/coordinates.py +++ b/server/lib/python/cartodb_services/cartodb_services/tools/coordinates.py @@ -17,3 +17,5 @@ class Coordinate: def to_json(self): return "{{\"lon\": {0},\"lat\": {1}}}".format(self._longitude, self._latitude) + def __str__(self): + return "{0}, {1}".format(self._longitude, self._latitude) \ No newline at end of file diff --git a/server/lib/python/cartodb_services/cartodb_services/tools/polyline.py b/server/lib/python/cartodb_services/cartodb_services/tools/polyline.py index a903e57..05b367a 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tools/polyline.py +++ b/server/lib/python/cartodb_services/cartodb_services/tools/polyline.py @@ -11,14 +11,11 @@ class PolyLine: for chunk in chunks: coordinate = self._process_chunk(chunk) coordinate /= 1e5 - print coordinate if len(coordinates) > 1: # We have to sum the previous with the offset in this chunk coordinate += coordinates[-2] coordinates.append(round(coordinate, 5)) - print coordinates - return zip(coordinates, coordinates[1:])[::2] def _extract_chunks(self, data): diff --git a/server/lib/python/cartodb_services/setup.py b/server/lib/python/cartodb_services/setup.py index 499e833..53f3b95 100644 --- a/server/lib/python/cartodb_services/setup.py +++ b/server/lib/python/cartodb_services/setup.py @@ -10,7 +10,7 @@ from setuptools import setup, find_packages setup( name='cartodb_services', - version='0.3.2', + version='0.3.3', description='CartoDB Services API Python Library', diff --git a/server/lib/python/cartodb_services/test/test_heremapsrouting.py b/server/lib/python/cartodb_services/test/test_heremapsrouting.py index 3b30b7b..a6ce503 100644 --- a/server/lib/python/cartodb_services/test/test_heremapsrouting.py +++ b/server/lib/python/cartodb_services/test/test_heremapsrouting.py @@ -132,7 +132,6 @@ class HereMapsRoutingIsolineTestCase(unittest.TestCase): HereMapsRoutingIsoline.ISOLINE_PATH) def test_calculate_isodistance_with_valid_params(self, req_mock): - print self.isoline_url url = "{0}?start=geo%2133.0%2C1.0&mode=shortest%3Bcar".format(self.isoline_url) req_mock.register_uri('GET', url, text=self.GOOD_RESPONSE) response = self.routing.calculate_isodistance('geo!33.0,1.0', 'car',