Exemplo n.º 1
0
    def test_can_get_all_limiters(self):
        self.db.query(Limiter).delete()

        limiter = LimiterFactory.create(url='http://test.com/')
        LimiterFactory.create()
        LimiterFactory.create()

        limiters = Limiter.get_all(self.db)

        expect(limiters).not_to_be_null()
        expect(limiters).to_length(3)
        expect(limiters).to_include(limiter)

        DomainFactory.create(name='test.com', url='http://test.com')
        limiters = Limiter.get_all(self.db, domain_filter='test.com')

        expect(limiters).not_to_be_null()
        expect(limiters).to_length(1)
Exemplo n.º 2
0
    def get_limiters_for_domains(cls, db, active_domains):
        from holmes.models import Limiter  # Avoid circular dependency

        all_limiters = Limiter.get_all(db)

        limiters = []
        for limiter in all_limiters:
            for domain in active_domains:
                if limiter.matches(domain.url):
                    limiters.append(limiter)

        return limiters
Exemplo n.º 3
0
    def get_limiters_for_domains(cls, db, active_domains):
        from holmes.models import Limiter  # Avoid circular dependency

        all_limiters = Limiter.get_all(db)

        limiters = []
        for limiter in all_limiters:
            for domain in active_domains:
                if limiter.matches(domain.url):
                    limiters.append(limiter)

        return limiters
Exemplo n.º 4
0
    def get_domain_limiters(self):
        domains = self.redis.get('domain-limiters')

        if domains:
            domains = loads(domains)
        else:
            limiters = Limiter.get_all(self.db)
            if limiters:
                domains = [{d.url: d.value} for d in limiters]
                self.set_domain_limiters(
                    domains, self.config.LIMITER_VALUES_CACHE_EXPIRATION)

        return domains
Exemplo n.º 5
0
    def get_domain_limiters(self):
        domains = self.redis.get('domain-limiters')

        if domains:
            domains = loads(domains)
        else:
            limiters = Limiter.get_all(self.db)
            if limiters:
                domains = [{d.url: d.value} for d in limiters]
                self.set_domain_limiters(
                    domains,
                    self.config.LIMITER_VALUES_CACHE_EXPIRATION
                )

        return domains
Exemplo n.º 6
0
    def test_can_add_or_update_limiter(self):
        self.db.query(Limiter).delete()

        limiters = Limiter.get_all(self.db)
        expect(limiters).to_equal([])

        # Add
        url = 'http://globo.com/'
        value = 2
        Limiter.add_or_update_limiter(self.db, url, value)
        limiter = Limiter.by_url(url, self.db)

        expect(limiter.value).to_equal(2)

        # Update
        url = 'http://globo.com/'
        value = 3
        Limiter.add_or_update_limiter(self.db, url, value)
        limiter = Limiter.by_url(url, self.db)

        expect(limiter.value).to_equal(3)
Exemplo n.º 7
0
    def get(self):
        limiters = Limiter.get_all(self.db)

        result = []

        for limit in limiters:
            current_value = yield self.cache.get_limit_usage(limit.url) or 0

            percentage = 0
            if limit.value > 0:
                percentage = float(current_value) / limit.value

            result.append({
                'id': limit.id,
                'url': limit.url,
                'currentValue': current_value,
                'maxValue': limit.value or 0,
                'concurrentRequestsPercentage': percentage
            })

        self.write_json(result)
Exemplo n.º 8
0
    def get(self):

        limiters = Limiter.get_all(
            self.db, domain_filter=self.get_argument('domain_filter', None)
        )

        result = []

        for limit in limiters:
            current_value = yield self.cache.get_limit_usage(limit.url) or 0

            percentage = 0
            if limit.value > 0:
                percentage = float(current_value) / limit.value

            result.append({
                'id': limit.id,
                'url': limit.url,
                'currentValue': current_value,
                'maxValue': limit.value or 0,
                'concurrentRequestsPercentage': percentage
            })

        self.write_json(result)