From 3a6cc4c364a310c63931bf990c395e3b0f7077b9 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Thu, 29 Sep 2016 18:41:16 +0200 Subject: [PATCH] Add mapzen config and integrate into legacy code --- server/extension/sql/20_geocode_street.sql | 5 +- .../refactor/service/__init__.py | 0 .../refactor/service/mapzen_geocoder.py | 113 ++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 server/lib/python/cartodb_services/cartodb_services/refactor/service/__init__.py create mode 100644 server/lib/python/cartodb_services/cartodb_services/refactor/service/mapzen_geocoder.py diff --git a/server/extension/sql/20_geocode_street.sql b/server/extension/sql/20_geocode_street.sql index f0dfeec..4546a78 100644 --- a/server/extension/sql/20_geocode_street.sql +++ b/server/extension/sql/20_geocode_street.sql @@ -147,6 +147,7 @@ RETURNS Geometry AS $$ from cartodb_services.refactor.tools.logger import LoggerConfigBuilder from cartodb_services.refactor.storage.redis_config import RedisMetadataConnectionConfigBuilder from cartodb_services.refactor.storage.redis_connection import RedisConnectionBuilder + from cartodb_services.refactor.service.mapzen_geocoder import MapzenGeocoderConfigBuilder server_config_storage = InDbServerConfigStorage() @@ -158,7 +159,9 @@ RETURNS Geometry AS $$ user_config_storage = UserConfigStorageFactory(redis_metadata_connection, username).get() org_config_storage = OrgConfigStorageFactory(redis_metadata_connection, orgname).get() - user_geocoder_config = GD["user_geocoder_config_{0}".format(username)] + # TODO rename this variable + user_geocoder_config = MapzenGeocoderConfigBuilder(server_config_storage, user_config_storage, org_config_storage, username, orgname).get() + redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection'] diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/service/__init__.py b/server/lib/python/cartodb_services/cartodb_services/refactor/service/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/service/mapzen_geocoder.py b/server/lib/python/cartodb_services/cartodb_services/refactor/service/mapzen_geocoder.py new file mode 100644 index 0000000..2db5784 --- /dev/null +++ b/server/lib/python/cartodb_services/cartodb_services/refactor/service/mapzen_geocoder.py @@ -0,0 +1,113 @@ +from dateutil.parser import parse as date_parse + +# TODO: rename this file +class MapzenGeocoderConfig(object): + """ + Value object that represents the configuration needed to operate the mapzen service. + """ + + def __init__(self, + geocoding_quota, + soft_geocoding_limit, + period_end_date, + cost_per_hit, + log_path, + mapzen_api_key, + username, + organization): + self._geocoding_quota = geocoding_quota + self._soft_geocoding_limit = soft_geocoding_limit + self._period_end_date = period_end_date + self._cost_per_hit = cost_per_hit + self._log_path = log_path + self._mapzen_api_key = mapzen_api_key + self._username = username + self._organization = organization + + # Kind of generic properties. Note which ones are for actually running the + # service and which ones are needed for quota stuff. + @property + def service_type(self): + return 'geocoder_mapzen' + + @property + def provider(self): + return 'mapzen' + + @property + def is_high_resolution(self): + return True + + @property + def geocoding_quota(self): + return self._geocoding_quota + + @property + def soft_geocoding_limit(self): + return self._soft_geocoding_limit + + @property + def period_end_date(self): + return self._period_end_date + + @property + def cost_per_hit(self): + return self._cost_per_hit + + # Server config, TODO: locate where this is actually used + @property + def log_path(self): + return self._log_path + + # This is actually the specific one to run requests against the remote endpoitn + @property + def mapzen_api_key(self): + return self._mapzen_api_key + + # These two identify the user + @property + def username(self): + return self._username + @property + def organization(self): + return self._organization + + # TODO: for BW compat, remove + @property + def google_geocoder(self): + return False + + +class MapzenGeocoderConfigBuilder(object): + + def __init__(self, server_conf, user_conf, org_conf, username, orgname): + self._server_conf = server_conf + self._user_conf = user_conf + self._org_conf = org_conf + self._username = username + self._orgname = orgname + + + def get(self): + mapzen_server_conf = self._server_conf.get('mapzen_conf') + geocoding_quota = mapzen_server_conf['geocoder']['monthly_quota'] + mapzen_api_key = mapzen_server_conf['geocoder']['api_key'] + + soft_geocoding_limit = self._user_conf.get('soft_geocoding_limit') + + cost_per_hit=0 + + period_end_date_str = self._org_conf.get('period_end_date') or self._user_conf.get('period_end_date') + period_end_date = date_parse(period_end_date_str) + + logger_conf = self._server_conf.get('logger_conf') + log_path = logger_conf['geocoder_log_path'] + + return MapzenGeocoderConfig(geocoding_quota, + soft_geocoding_limit, + period_end_date, + cost_per_hit, + log_path, + mapzen_api_key, + self._username, + self._orgname)