2019-09-18 13:40:23 +00:00
|
|
|
import threading
|
2020-06-22 22:08:59 +00:00
|
|
|
from owrx.client import ClientRegistry
|
2019-09-18 13:40:23 +00:00
|
|
|
|
2019-09-23 01:15:24 +00:00
|
|
|
|
2019-09-12 20:50:29 +00:00
|
|
|
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):
|
2019-09-13 21:03:05 +00:00
|
|
|
return {"count": self.counter}
|
2019-09-12 20:50:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DirectMetric(Metric):
|
|
|
|
def __init__(self, getter):
|
|
|
|
self.getter = getter
|
|
|
|
|
|
|
|
def getValue(self):
|
|
|
|
return self.getter()
|
|
|
|
|
|
|
|
|
2019-08-04 16:36:03 +00:00
|
|
|
class Metrics(object):
|
|
|
|
sharedInstance = None
|
2019-09-18 13:40:23 +00:00
|
|
|
creationLock = threading.Lock()
|
2019-08-04 16:36:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getSharedInstance():
|
2019-09-18 13:40:23 +00:00
|
|
|
with Metrics.creationLock:
|
|
|
|
if Metrics.sharedInstance is None:
|
|
|
|
Metrics.sharedInstance = Metrics()
|
2019-08-04 16:36:03 +00:00
|
|
|
return Metrics.sharedInstance
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.metrics = {}
|
2020-06-22 22:08:59 +00:00
|
|
|
self.addMetric("openwebrx.users", DirectMetric(ClientRegistry.getSharedInstance().clientCount))
|
2019-08-04 16:36:03 +00:00
|
|
|
|
2019-09-12 20:50:29 +00:00
|
|
|
def addMetric(self, name, metric):
|
|
|
|
self.metrics[name] = metric
|
2019-08-04 16:36:03 +00:00
|
|
|
|
2019-09-12 20:50:29 +00:00
|
|
|
def hasMetric(self, name):
|
|
|
|
return name in self.metrics
|
2019-08-04 16:36:03 +00:00
|
|
|
|
2019-09-12 20:50:29 +00:00
|
|
|
def getMetric(self, name):
|
|
|
|
if not self.hasMetric(name):
|
|
|
|
return None
|
|
|
|
return self.metrics[name]
|
2019-08-04 16:36:03 +00:00
|
|
|
|
2021-02-01 17:43:14 +00:00
|
|
|
def getFlatMetrics(self):
|
|
|
|
return self.metrics
|
|
|
|
|
|
|
|
def getHierarchicalMetrics(self):
|
2019-09-12 20:50:29 +00:00
|
|
|
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
|