Changes needed to read quota from redis #281

Use per-user/org quota from redis, if present. Otherwise default to the
quota stored in server database (as it did to date).
This commit is contained in:
Rafa de la Torre 2016-10-18 17:26:43 +02:00
parent 7d137f3efc
commit 27be704bd6

View File

@ -116,6 +116,7 @@ class RoutingConfig(ServiceConfig):
ROUTING_PROVIDER_KEY = 'routing_provider' ROUTING_PROVIDER_KEY = 'routing_provider'
MAPZEN_PROVIDER = 'mapzen' MAPZEN_PROVIDER = 'mapzen'
DEFAULT_PROVIDER = 'mapzen' DEFAULT_PROVIDER = 'mapzen'
QUOTA_KEY = 'mapzen_routing_quota'
def __init__(self, redis_connection, db_conn, username, orgname=None): def __init__(self, redis_connection, db_conn, username, orgname=None):
super(RoutingConfig, self).__init__(redis_connection, db_conn, super(RoutingConfig, self).__init__(redis_connection, db_conn,
@ -124,7 +125,7 @@ class RoutingConfig(ServiceConfig):
if not self._routing_provider: if not self._routing_provider:
self._routing_provider = self.DEFAULT_PROVIDER self._routing_provider = self.DEFAULT_PROVIDER
self._mapzen_api_key = self._db_config.mapzen_routing_api_key self._mapzen_api_key = self._db_config.mapzen_routing_api_key
self._monthly_quota = self._db_config.mapzen_routing_monthly_quota self._set_monthly_quota()
self._period_end_date = date_parse(self._redis_config[self.PERIOD_END_DATE]) self._period_end_date = date_parse(self._redis_config[self.PERIOD_END_DATE])
@property @property
@ -144,6 +145,19 @@ class RoutingConfig(ServiceConfig):
def period_end_date(self): def period_end_date(self):
return self._period_end_date return self._period_end_date
def _set_monthly_quota(self):
self._monthly_quota = self._get_effective_monthly_quota()
def _get_effective_monthly_quota(self):
quota_from_redis = self._redis_config[self.QUOTA_KEY]
if quota_from_redis and quota_from_redis <> '':
return int(quota_from_redis)
else:
return self._db_config.mapzen_routing_monthly_quota
class IsolinesRoutingConfig(ServiceConfig): class IsolinesRoutingConfig(ServiceConfig):
@ -547,6 +561,7 @@ class ServicesRedisConfig:
GOOGLE_GEOCODER_CLIENT_ID = 'google_maps_client_id' GOOGLE_GEOCODER_CLIENT_ID = 'google_maps_client_id'
QUOTA_KEY = 'geocoding_quota' QUOTA_KEY = 'geocoding_quota'
ISOLINES_QUOTA_KEY = 'here_isolines_quota' ISOLINES_QUOTA_KEY = 'here_isolines_quota'
ROUTING_QUOTA_KEY = 'mapzen_routing_quota'
OBS_SNAPSHOT_QUOTA_KEY = 'obs_snapshot_quota' OBS_SNAPSHOT_QUOTA_KEY = 'obs_snapshot_quota'
OBS_GENERAL_QUOTA_KEY = 'obs_general_quota' OBS_GENERAL_QUOTA_KEY = 'obs_general_quota'
PERIOD_END_DATE = 'period_end_date' PERIOD_END_DATE = 'period_end_date'
@ -574,6 +589,9 @@ class ServicesRedisConfig:
if self.ROUTING_PROVIDER_KEY not in user_config: if self.ROUTING_PROVIDER_KEY not in user_config:
user_config[self.ROUTING_PROVIDER_KEY] = '' user_config[self.ROUTING_PROVIDER_KEY] = ''
# Mapzen routing quota might be not present
user_config[self.ROUTING_QUOTA_KEY] = user_config.get(self.ROUTING_QUOTA_KEY)
if orgname: if orgname:
self.__get_organization_config(orgname, user_config) self.__get_organization_config(orgname, user_config)
@ -587,6 +605,7 @@ class ServicesRedisConfig:
else: else:
user_config[self.QUOTA_KEY] = org_config[self.QUOTA_KEY] user_config[self.QUOTA_KEY] = org_config[self.QUOTA_KEY]
user_config[self.ISOLINES_QUOTA_KEY] = org_config[self.ISOLINES_QUOTA_KEY] user_config[self.ISOLINES_QUOTA_KEY] = org_config[self.ISOLINES_QUOTA_KEY]
user_config[self.ROUTING_QUOTA_KEY] = org_config.get(self.ROUTING_QUOTA_KEY)
if self.OBS_SNAPSHOT_QUOTA_KEY in org_config: if self.OBS_SNAPSHOT_QUOTA_KEY in org_config:
user_config[self.OBS_SNAPSHOT_QUOTA_KEY] = org_config[self.OBS_SNAPSHOT_QUOTA_KEY] user_config[self.OBS_SNAPSHOT_QUOTA_KEY] = org_config[self.OBS_SNAPSHOT_QUOTA_KEY]
if self.OBS_GENERAL_QUOTA_KEY in org_config: if self.OBS_GENERAL_QUOTA_KEY in org_config: