.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.metrics.models.Histogram Class Reference
Inheritance diagram for searx.metrics.models.Histogram:

Public Member Functions

 __init__ (self, width=10, size=200)
 observe (self, value)
 quartiles (self)
 count (self)
 sum (self)
 average (self)
 quartile_percentage (self)
 quartile_percentage_map (self)
 percentage (self, percentage)
 __repr__ (self)

Public Attributes

 average

Protected Attributes

 _lock = threading.Lock()
 _width = width
 _size = size
list _quartiles = [0] * size
int _count = 0
int _sum = 0

Static Protected Attributes

str _slots__ = '_lock', '_size', '_sum', '_quartiles', '_count', '_width'

Detailed Description

Definition at line 17 of file models.py.

Constructor & Destructor Documentation

◆ __init__()

searx.metrics.models.Histogram.__init__ ( self,
width = 10,
size = 200 )

Definition at line 21 of file models.py.

21 def __init__(self, width=10, size=200):
22 self._lock = threading.Lock()
23 self._width = width
24 self._size = size
25 self._quartiles = [0] * size
26 self._count = 0
27 self._sum = 0
28

Member Function Documentation

◆ __repr__()

searx.metrics.models.Histogram.__repr__ ( self)

Definition at line 100 of file models.py.

100 def __repr__(self):
101 return "Histogram<avg: " + str(self.average) + ", count: " + str(self._count) + ">"
102
103

◆ average()

searx.metrics.models.Histogram.average ( self)

Definition at line 55 of file models.py.

55 def average(self):
56 with self._lock:
57 if self._count != 0:
58 return self._sum / self._count
59 return 0
60

References _count, _lock, and _sum.

◆ count()

searx.metrics.models.Histogram.count ( self)

Definition at line 47 of file models.py.

47 def count(self):
48 return self._count
49

References _count.

◆ observe()

searx.metrics.models.Histogram.observe ( self,
value )

Reimplemented in searx.metrics.models.VoidHistogram.

Definition at line 29 of file models.py.

29 def observe(self, value):
30 q = int(value / self._width)
31 if q < 0: # pylint: disable=consider-using-max-builtin
32 # Value below zero is ignored
33 q = 0
34 if q >= self._size:
35 # Value above the maximum is replaced by the maximum
36 q = self._size - 1
37 with self._lock:
38 self._quartiles[q] += 1
39 self._count += 1
40 self._sum += value
41

References _count, _lock, _quartiles, _size, _sum, and _width.

◆ percentage()

searx.metrics.models.Histogram.percentage ( self,
percentage )

Definition at line 85 of file models.py.

85 def percentage(self, percentage):
86 # use Decimal to avoid rounding errors
87 x = decimal.Decimal(0)
88 width = decimal.Decimal(self._width)
89 stop_at_value = decimal.Decimal(self._count) / 100 * percentage
90 sum_value = 0
91 with self._lock:
92 if self._count > 0:
93 for y in self._quartiles:
94 sum_value += y
95 if sum_value >= stop_at_value:
96 return x
97 x += width
98 return None
99

References _count, _lock, _quartiles, and _width.

◆ quartile_percentage()

searx.metrics.models.Histogram.quartile_percentage ( self)
Quartile in percentage

Definition at line 62 of file models.py.

62 def quartile_percentage(self):
63 '''Quartile in percentage'''
64 with self._lock:
65 if self._count > 0:
66 return [int(q * 100 / self._count) for q in self._quartiles]
67 return self._quartiles
68

References _count, _lock, and _quartiles.

◆ quartile_percentage_map()

searx.metrics.models.Histogram.quartile_percentage_map ( self)

Definition at line 70 of file models.py.

70 def quartile_percentage_map(self):
71 result = {}
72 # use Decimal to avoid rounding errors
73 x = decimal.Decimal(0)
74 width = decimal.Decimal(self._width)
75 width_exponent = -width.as_tuple().exponent
76 with self._lock:
77 if self._count > 0:
78 for y in self._quartiles:
79 yp = int(y * 100 / self._count) # pylint: disable=invalid-name
80 if yp != 0:
81 result[round(float(x), width_exponent)] = yp
82 x += width
83 return result
84

References _count, _lock, _quartiles, and _width.

◆ quartiles()

searx.metrics.models.Histogram.quartiles ( self)

Definition at line 43 of file models.py.

43 def quartiles(self):
44 return list(self._quartiles)
45

References _quartiles.

◆ sum()

searx.metrics.models.Histogram.sum ( self)

Definition at line 51 of file models.py.

51 def sum(self):
52 return self._sum
53

References _sum.

Member Data Documentation

◆ _count

searx.metrics.models.Histogram._count = 0
protected

◆ _lock

◆ _quartiles

searx.metrics.models.Histogram._quartiles = [0] * size
protected

◆ _size

searx.metrics.models.Histogram._size = size
protected

Definition at line 24 of file models.py.

Referenced by observe().

◆ _slots__

str searx.metrics.models.Histogram._slots__ = '_lock', '_size', '_sum', '_quartiles', '_count', '_width'
staticprotected

Definition at line 19 of file models.py.

◆ _sum

int searx.metrics.models.Histogram._sum = 0
protected

Definition at line 27 of file models.py.

Referenced by average(), observe(), and sum().

◆ _width

searx.metrics.models.Histogram._width = width
protected

Definition at line 23 of file models.py.

Referenced by observe(), percentage(), and quartile_percentage_map().

◆ average

searx.metrics.models.Histogram.average

Definition at line 101 of file models.py.


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/metrics/models.py