Server data observatory functions
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
from config import GeocoderConfig, IsolinesRoutingConfig, InternalGeocoderConfig, RoutingConfig, ConfigException
|
||||
from config import GeocoderConfig, IsolinesRoutingConfig, InternalGeocoderConfig, RoutingConfig, ConfigException, DataObservatoryConfig
|
||||
from quota import QuotaService
|
||||
from user import UserMetricsService
|
||||
|
||||
@@ -32,6 +32,28 @@ class ServiceConfig(object):
|
||||
def organization(self):
|
||||
return self._orgname
|
||||
|
||||
class DataObservatoryConfig(ServiceConfig):
|
||||
|
||||
PERIOD_END_DATE = 'period_end_date'
|
||||
|
||||
def __init__(self, redis_connection, db_conn, username, orgname=None):
|
||||
super(DataObservatoryConfig, self).__init__(redis_connection, db_conn,
|
||||
username, orgname)
|
||||
self._monthly_quota = self._db_config.data_observatory_monthly_quota
|
||||
self._period_end_date = date_parse(self._redis_config[self.PERIOD_END_DATE])
|
||||
|
||||
@property
|
||||
def service_type(self):
|
||||
return 'data_observatory'
|
||||
|
||||
@property
|
||||
def monthly_quota(self):
|
||||
return self._monthly_quota
|
||||
|
||||
@property
|
||||
def period_end_date(self):
|
||||
return self._period_end_date
|
||||
|
||||
|
||||
class RoutingConfig(ServiceConfig):
|
||||
|
||||
@@ -315,6 +337,7 @@ class ServicesDBConfig:
|
||||
self._get_here_config()
|
||||
self._get_mapzen_config()
|
||||
self._get_logger_config()
|
||||
self._get_data_observatory_config()
|
||||
|
||||
def _get_here_config(self):
|
||||
heremaps_conf_json = self._get_conf('heremaps_conf')
|
||||
@@ -340,6 +363,14 @@ class ServicesDBConfig:
|
||||
self._mapzen_geocoder_api_key = mapzen_conf['geocoder']['api_key']
|
||||
self._mapzen_geocoder_quota = mapzen_conf['geocoder']['monthly_quota']
|
||||
|
||||
def _get_data_observatory_config(self):
|
||||
do_conf_json = self._get_conf('data_observatory_conf')
|
||||
if not do_conf_json:
|
||||
raise ConfigException('Data Observatory configuration missing')
|
||||
else:
|
||||
do_conf = json.loads(do_conf_json)
|
||||
self._data_observatory_monthly_quota = do_conf['monthly_quota']
|
||||
|
||||
def _get_logger_config(self):
|
||||
logger_conf_json = self._get_conf('logger_conf')
|
||||
if not logger_conf_json:
|
||||
@@ -396,6 +427,10 @@ class ServicesDBConfig:
|
||||
def geocoder_log_path(self):
|
||||
return self._geocoder_log_path
|
||||
|
||||
@property
|
||||
def data_observatory_monthly_quota(self):
|
||||
return self._data_observatory_monthly_quota
|
||||
|
||||
|
||||
class ServicesRedisConfig:
|
||||
|
||||
|
||||
@@ -73,6 +73,9 @@ class QuotaChecker:
|
||||
elif re.match('routing_mapzen',
|
||||
self._user_service_config.service_type) is not None:
|
||||
return self.__check_routing_quota()
|
||||
elif re.match('data_observatory',
|
||||
self._user_service_config.service_type) is not None:
|
||||
return self.__check_data_observatory_quota()
|
||||
else:
|
||||
return False
|
||||
|
||||
@@ -114,3 +117,14 @@ class QuotaChecker:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def __check_data_observatory_quota(self):
|
||||
user_quota = self._user_service_config.monthly_quota
|
||||
today = date.today()
|
||||
service_type = self._user_service_config.service_type
|
||||
current_used = self._user_service.used_quota(service_type, today)
|
||||
|
||||
if (user_quota > 0 and current_used <= user_quota):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user