diff --git a/server/extension/sql/15_config_helper.sql b/server/extension/sql/15_config_helper.sql index 5f6fc77..9d83ebd 100644 --- a/server/extension/sql/15_config_helper.sql +++ b/server/extension/sql/15_config_helper.sql @@ -10,6 +10,12 @@ RETURNS boolean AS $$ return True $$ LANGUAGE plpythonu SECURITY DEFINER; +-- This is done in order to avoid an undesired depedency on cartodb extension +CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_conf_getconf(input_key text) +RETURNS JSON AS $$ + SELECT VALUE FROM cartodb.cdb_conf WHERE key = input_key; +$$ LANGUAGE SQL STABLE SECURITY DEFINER; + CREATE OR REPLACE FUNCTION cdb_dataservices_server._get_geocoder_config(username text, orgname text, provider text DEFAULT NULL) RETURNS boolean AS $$ cache_key = "user_geocoder_config_{0}".format(username) diff --git a/server/extension/sql/20_geocode_street.sql b/server/extension/sql/20_geocode_street.sql index 02e2a2f..2c4df2f 100644 --- a/server/extension/sql/20_geocode_street.sql +++ b/server/extension/sql/20_geocode_street.sql @@ -137,17 +137,25 @@ $$ LANGUAGE plpythonu; CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_mapzen_geocode_street_point(username TEXT, orgname TEXT, searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL) RETURNS Geometry AS $$ + import cartodb_services + cartodb_services.init(plpy, GD) from cartodb_services.mapzen import MapzenGeocoder from cartodb_services.mapzen.types import country_to_iso3 from cartodb_services.metrics import QuotaService - from cartodb_services.tools import Logger,LoggerConfig + from cartodb_services.tools import Logger + from cartodb_services.refactor.storage.server_config import InDbServerConfigStorage + from cartodb_services.refactor.tools.logger import LoggerConfigBuilder redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection'] user_geocoder_config = GD["user_geocoder_config_{0}".format(username)] - plpy.execute("SELECT cdb_dataservices_server._get_logger_config()") - logger_config = GD["logger_config"] + server_config_storage = InDbServerConfigStorage() + + logger_config = LoggerConfigBuilder(server_config_storage).get() logger = Logger(logger_config) + + + quota_service = QuotaService(user_geocoder_config, redis_conn) if not quota_service.check_user_quota(): raise Exception('You have reached the limit of your quota') diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/tools/__init__.py b/server/lib/python/cartodb_services/cartodb_services/refactor/tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/tools/logger.py b/server/lib/python/cartodb_services/cartodb_services/refactor/tools/logger.py new file mode 100644 index 0000000..febde62 --- /dev/null +++ b/server/lib/python/cartodb_services/cartodb_services/refactor/tools/logger.py @@ -0,0 +1,67 @@ +class LoggerConfig(object): + + """This class is a value object needed to setup a Logger""" + + def __init__(self, server_environment, rollbar_api_key, log_file_path, min_log_level): + self._server_environment = server_environment + self._rollbar_api_key = rollbar_api_key + self._log_file_path = log_file_path + self._min_log_level = min_log_level + + @property + def environment(self): + return self._server_environment + + @property + def rollbar_api_key(self): + return self._rollbar_api_key + + @property + def log_file_path(self): + return self._log_file_path + + @property + def min_log_level(self): + return self._min_log_level + +class ConfigException(Exception): + pass + +# TODO this needs tests +# TODO FTM this is just config, maybe move around +class LoggerConfigBuilder(object): + + def __init__(self, server_config_storage): + self._server_config_storage = server_config_storage + + def get(self): + server_environment = self._get_server_environment() + + logger_conf = self._server_config_storage.get('logger_conf') + if not logger_conf: + raise ConfigException('Logger configuration missing') + + rollbar_api_key = self._get_value_or_none(logger_conf, 'rollbar_api_key') + log_file_path = self._get_value_or_none(logger_conf, 'log_file_path') + min_log_level = self._get_value_or_none(logger_conf, 'min_log_level') or 'warning' + + logger_config = LoggerConfig(server_environment, rollbar_api_key, log_file_path, min_log_level) + return logger_config + + def _get_server_environment(self): + server_config = self._server_config_storage.get('server_conf') + if not server_config: + environment = 'development' + else: + if 'environment' in server_config: + environment = server_config['environment'] + else: + environment = 'development' + + return environment + + def _get_value_or_none(self, logger_conf, key): + value = None + if key in logger_conf: + value = logger_conf[key] + return value