summaryrefslogtreecommitdiff
path: root/kafka/metrics/quota.py
blob: 4d1b0d6cb065f20499e0036a22d85872d5d50831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from __future__ import absolute_import


class Quota(object):
    """An upper or lower bound for metrics"""
    def __init__(self, bound, is_upper):
        self._bound = bound
        self._upper = is_upper

    @staticmethod
    def upper_bound(upper_bound):
        return Quota(upper_bound, True)

    @staticmethod
    def lower_bound(lower_bound):
        return Quota(lower_bound, False)

    def is_upper_bound(self):
        return self._upper

    @property
    def bound(self):
        return self._bound

    def is_acceptable(self, value):
        return ((self.is_upper_bound() and value <= self.bound) or
                (not self.is_upper_bound() and value >= self.bound))

    def __hash__(self):
        prime = 31
        result = prime + self.bound
        return prime * result + self.is_upper_bound()

    def __eq__(self, other):
        if self is other:
            return True
        return (type(self) == type(other) and
                self.bound == other.bound and
                self.is_upper_bound() == other.is_upper_bound())

    def __ne__(self, other):
        return not self.__eq__(other)