.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

int _lock = 1
 
 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 15 of file models.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 19 of file models.py.

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

Member Function Documentation

◆ __repr__()

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

Definition at line 98 of file models.py.

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

◆ average()

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

Definition at line 53 of file models.py.

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

References searx.metrics.models.Histogram._count, searx.metrics.models.Histogram._lock, searx.results.ResultContainer._lock, and searx.metrics.models.Histogram._sum.

◆ count()

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

Definition at line 45 of file models.py.

45 def count(self):
46 return self._count
47

References searx.metrics.models.Histogram._count.

◆ observe()

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

Reimplemented in searx.metrics.models.VoidHistogram.

Definition at line 27 of file models.py.

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

References searx.metrics.models.Histogram._size, and searx.metrics.models.Histogram._width.

◆ percentage()

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

Definition at line 83 of file models.py.

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

References searx.metrics.models.Histogram._count, searx.metrics.models.Histogram._lock, searx.results.ResultContainer._lock, searx.metrics.models.Histogram._quartiles, and searx.metrics.models.Histogram._width.

◆ quartile_percentage()

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

Definition at line 60 of file models.py.

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

References searx.metrics.models.Histogram._count, searx.metrics.models.Histogram._lock, searx.results.ResultContainer._lock, and searx.metrics.models.Histogram._quartiles.

◆ quartile_percentage_map()

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

Definition at line 68 of file models.py.

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

References searx.metrics.models.Histogram._count, searx.metrics.models.Histogram._lock, searx.results.ResultContainer._lock, searx.metrics.models.Histogram._quartiles, and searx.metrics.models.Histogram._width.

◆ quartiles()

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

Definition at line 41 of file models.py.

41 def quartiles(self):
42 return list(self._quartiles)
43

References searx.metrics.models.Histogram._quartiles.

◆ sum()

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

Definition at line 49 of file models.py.

49 def sum(self):
50 return self._sum
51

References searx.metrics.models.Histogram._sum.

Member Data Documentation

◆ _count

◆ _lock [1/2]

◆ _lock [2/2]

◆ _quartiles

◆ _size

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

Definition at line 22 of file models.py.

Referenced by searx.metrics.models.Histogram.observe().

◆ _slots__

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

Definition at line 17 of file models.py.

◆ _sum

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

◆ _width

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

◆ average

searx.metrics.models.Histogram.average

Definition at line 99 of file models.py.


The documentation for this class was generated from the following file: