Add things to get logger config from storage

This commit is contained in:
Rafa de la Torre 2016-09-28 18:22:51 +02:00
parent fd2cc21942
commit efdc151282
4 changed files with 84 additions and 3 deletions

View File

@ -10,6 +10,12 @@ RETURNS boolean AS $$
return True return True
$$ LANGUAGE plpythonu SECURITY DEFINER; $$ 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) CREATE OR REPLACE FUNCTION cdb_dataservices_server._get_geocoder_config(username text, orgname text, provider text DEFAULT NULL)
RETURNS boolean AS $$ RETURNS boolean AS $$
cache_key = "user_geocoder_config_{0}".format(username) cache_key = "user_geocoder_config_{0}".format(username)

View File

@ -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) 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 $$ RETURNS Geometry AS $$
import cartodb_services
cartodb_services.init(plpy, GD)
from cartodb_services.mapzen import MapzenGeocoder from cartodb_services.mapzen import MapzenGeocoder
from cartodb_services.mapzen.types import country_to_iso3 from cartodb_services.mapzen.types import country_to_iso3
from cartodb_services.metrics import QuotaService 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'] redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
user_geocoder_config = GD["user_geocoder_config_{0}".format(username)] user_geocoder_config = GD["user_geocoder_config_{0}".format(username)]
plpy.execute("SELECT cdb_dataservices_server._get_logger_config()") server_config_storage = InDbServerConfigStorage()
logger_config = GD["logger_config"]
logger_config = LoggerConfigBuilder(server_config_storage).get()
logger = Logger(logger_config) logger = Logger(logger_config)
quota_service = QuotaService(user_geocoder_config, redis_conn) quota_service = QuotaService(user_geocoder_config, redis_conn)
if not quota_service.check_user_quota(): if not quota_service.check_user_quota():
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')

View File

@ -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