rename src dir
This commit is contained in:
66
src/Statistics.py
Normal file
66
src/Statistics.py
Normal file
@ -0,0 +1,66 @@
|
||||
from loguru import logger
|
||||
import threading
|
||||
from AbstractDataObject import AbstractDataObject
|
||||
import json
|
||||
import datetime
|
||||
|
||||
class StatisticsDataObject(AbstractDataObject):
|
||||
def __init__(self, topic, payload):
|
||||
super().__init__(topic)
|
||||
self.payload = payload
|
||||
|
||||
def getPayload(self):
|
||||
return json.dumps(self.payload)
|
||||
|
||||
|
||||
class StatisticsCollector(threading.Thread):
|
||||
def __init__(self, config, queue):
|
||||
super().__init__()
|
||||
|
||||
self.config = config['stats']
|
||||
self.period = self.config['period']
|
||||
self.topic = self.config['topic']
|
||||
self.queue = queue
|
||||
|
||||
self.killBill = False
|
||||
self.killEvent = threading.Event()
|
||||
|
||||
self.stats = {
|
||||
'opcUaRequests': 0,
|
||||
'opcUaErrors' : 0,
|
||||
'opcUaTimeouts': 0,
|
||||
'mqttRequests': 0,
|
||||
'mqttErrors': 0,
|
||||
'uptime': 0
|
||||
}
|
||||
|
||||
def incOpcUaRequests(self):
|
||||
self.stats['opcUaRequests'] += 1
|
||||
|
||||
def incOpcUaErrors(self):
|
||||
self.stats['opcUaErrors'] += 1
|
||||
|
||||
def incOpcUaTimeouts(self):
|
||||
self.stats['opcUaTimeouts'] += 1
|
||||
|
||||
def incMqttRequests(self):
|
||||
self.stats['mqttRequests'] += 1
|
||||
|
||||
def incMqttErrors(self):
|
||||
self.stats['mqttErrors'] += 1
|
||||
|
||||
def stop(self):
|
||||
self.killBill = True
|
||||
logger.info("kill flag set")
|
||||
|
||||
self.killEvent.set()
|
||||
logger.info("kill events triggered")
|
||||
|
||||
def run(self):
|
||||
startTime = datetime.datetime.now()
|
||||
while not self.killBill:
|
||||
currentTime = datetime.datetime.now()
|
||||
self.stats['uptime'] = int((currentTime - startTime).total_seconds())
|
||||
self.queue.put(StatisticsDataObject(self.topic, self.stats))
|
||||
self.killEvent.wait(timeout=float(self.period))
|
||||
|
Reference in New Issue
Block a user