rewire the metrics; make queue length metric available
This commit is contained in:
@ -1,3 +1,29 @@
|
||||
class Metric(object):
|
||||
def getValue(self):
|
||||
return 0
|
||||
|
||||
|
||||
class CounterMetric(Metric):
|
||||
def __init__(self):
|
||||
self.counter = 0
|
||||
|
||||
def inc(self, increment=1):
|
||||
self.counter += increment
|
||||
|
||||
def getValue(self):
|
||||
return {
|
||||
"count": self.counter,
|
||||
}
|
||||
|
||||
|
||||
class DirectMetric(Metric):
|
||||
def __init__(self, getter):
|
||||
self.getter = getter
|
||||
|
||||
def getValue(self):
|
||||
return self.getter()
|
||||
|
||||
|
||||
class Metrics(object):
|
||||
sharedInstance = None
|
||||
|
||||
@ -10,21 +36,27 @@ class Metrics(object):
|
||||
def __init__(self):
|
||||
self.metrics = {}
|
||||
|
||||
def pushDecodes(self, band, mode, count=1):
|
||||
if band is None:
|
||||
band = "unknown"
|
||||
else:
|
||||
band = band.getName()
|
||||
def addMetric(self, name, metric):
|
||||
self.metrics[name] = metric
|
||||
|
||||
if mode is None:
|
||||
mode = "unknown"
|
||||
def hasMetric(self, name):
|
||||
return name in self.metrics
|
||||
|
||||
if not band in self.metrics:
|
||||
self.metrics[band] = {}
|
||||
if not mode in self.metrics[band]:
|
||||
self.metrics[band][mode] = {"count": 0}
|
||||
|
||||
self.metrics[band][mode]["count"] += count
|
||||
def getMetric(self, name):
|
||||
if not self.hasMetric(name):
|
||||
return None
|
||||
return self.metrics[name]
|
||||
|
||||
def getMetrics(self):
|
||||
return self.metrics
|
||||
result = {}
|
||||
|
||||
for (key, metric) in self.metrics.items():
|
||||
partial = result
|
||||
keys = key.split(".")
|
||||
for keypart in keys[0:-1]:
|
||||
if not keypart in partial:
|
||||
partial[keypart] = {}
|
||||
partial = partial[keypart]
|
||||
partial[keys[-1]] = metric.getValue()
|
||||
|
||||
return result
|
||||
|
Reference in New Issue
Block a user