Example #1
0
class QuotaChecker:
    def __init__(self, user_service_config, redis_connection):
        self._user_service_config = user_service_config
        self._user_service = UserMetricsService(self._user_service_config,
                                                redis_connection)

    def check(self):
        """ Check if the current user quota surpasses the current quota """
        if re.match('^geocoder_',
                    self._user_service_config.service_type) is not None:
            return self.__check_geocoder_quota()
        elif re.match('.*_isolines$',
                      self._user_service_config.service_type) is not None:
            return self.__check_isolines_quota()
        elif re.match('^routing_',
                      self._user_service_config.service_type) is not None:
            return self.__check_routing_quota()
        elif re.match('^obs_',
                      self._user_service_config.service_type) is not None:
            return self.__check_data_observatory_quota()
        else:
            return False

    def __check_geocoder_quota(self):
        # We don't have quota check for google geocoder
        if self._user_service_config.google_geocoder:
            return True

        user_quota = self._user_service_config.geocoding_quota
        today = date.today()
        service_type = self._user_service_config.service_type
        current_used = self._user_service.used_quota(service_type, today)
        soft_geocoding_limit = self._user_service_config.soft_geocoding_limit

        if soft_geocoding_limit or (user_quota > 0
                                    and current_used < user_quota):
            return True
        else:
            return False

    def __check_isolines_quota(self):
        user_quota = self._user_service_config.isolines_quota
        today = date.today()
        service_type = self._user_service_config.service_type
        current_used = self._user_service.used_quota(service_type, today)
        soft_isolines_limit = self._user_service_config.soft_isolines_limit

        if soft_isolines_limit or (user_quota > 0
                                   and current_used < user_quota):
            return True
        else:
            return False

    def __check_routing_quota(self):
        user_quota = self._user_service_config.routing_quota
        today = date.today()
        service_type = self._user_service_config.service_type
        current_used = self._user_service.used_quota(service_type, today)
        soft_limit = self._user_service_config.soft_limit

        if soft_limit or (user_quota > 0 and current_used < user_quota):
            return True
        else:
            return False

    def __check_data_observatory_quota(self):
        user_quota = self._user_service_config.monthly_quota
        soft_limit = self._user_service_config.soft_limit
        today = date.today()
        service_type = self._user_service_config.service_type
        current_used = self._user_service.used_quota(service_type, today)

        # Quick workaround so we don't take into account numer of credits
        # spent for users that have defined the quota.
        # See https://github.com/CartoDB/bigmetadata/issues/215
        if soft_limit or (user_quota > 0):
            return True
        else:
            return False
Example #2
0
class QuotaChecker:

    def __init__(self, user_service_config, redis_connection):
        self._user_service_config = user_service_config
        self._user_service = UserMetricsService(
            self._user_service_config, redis_connection)

    def check(self):
        """ Check if the current user quota surpasses the current quota """
        if re.match('geocoder_*',
                    self._user_service_config.service_type) is not None:
            return self.__check_geocoder_quota()
        elif re.match('here_isolines',
                      self._user_service_config.service_type) is not None:
            return self.__check_isolines_quota()
        elif re.match('mapzen_isolines',
                      self._user_service_config.service_type) is not None:
            return self.__check_isolines_quota()
        elif re.match('routing_mapzen',
                      self._user_service_config.service_type) is not None:
            return self.__check_routing_quota()
        elif re.match('obs_*',
                      self._user_service_config.service_type) is not None:
            return self.__check_data_observatory_quota()
        else:
            return False

    def __check_geocoder_quota(self):
        # We don't have quota check for google geocoder
        if self._user_service_config.google_geocoder:
            return True

        user_quota = self._user_service_config.geocoding_quota
        today = date.today()
        service_type = self._user_service_config.service_type
        current_used = self._user_service.used_quota(service_type, today)
        soft_geocoding_limit = self._user_service_config.soft_geocoding_limit

        if soft_geocoding_limit or (user_quota > 0 and current_used < user_quota):
            return True
        else:
            return False

    def __check_isolines_quota(self):
        user_quota = self._user_service_config.isolines_quota
        today = date.today()
        service_type = self._user_service_config.service_type
        current_used = self._user_service.used_quota(service_type, today)
        soft_isolines_limit = self._user_service_config.soft_isolines_limit

        if soft_isolines_limit or (user_quota > 0 and current_used < user_quota):
            return True
        else:
            return False

    def __check_routing_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)
        soft_limit = self._user_service_config.soft_limit

        if soft_limit or (user_quota > 0 and current_used < user_quota):
            return True
        else:
            return False

    def __check_data_observatory_quota(self):
        user_quota = self._user_service_config.monthly_quota
        soft_limit = self._user_service_config.soft_limit
        today = date.today()
        service_type = self._user_service_config.service_type
        current_used = self._user_service.used_quota(service_type, today)

        if soft_limit or (user_quota > 0 and current_used < user_quota):
            return True
        else:
            return False