add static constructor to init proxy statistics

This commit is contained in:
Stefan Allius
2023-10-20 00:27:21 +02:00
parent 5433e18389
commit 11d7d616fa
2 changed files with 24 additions and 16 deletions

View File

@@ -4,14 +4,28 @@ import struct, json, logging, os
class Infos: class Infos:
stat = {} stat = {}
app_name = os.getenv('SERVICE_NAME', 'proxy')
version = os.getenv('VERSION', 'unknown')
@classmethod
def static_init(cls):
logging.info('Initialize proxy statistics')
# init proxy counter in the class.stat dictionary
cls.stat['proxy'] = {}
for key in cls.__info_defs:
name = cls.__info_defs[key]['name']
if name[0]=='proxy':
cls.stat['proxy'][name[1]] = 0
# add values from the environment to the device definition table
prxy = cls.__info_devs['proxy']
prxy['sw'] = cls.version
prxy['mdl'] = cls.app_name
def __init__(self): def __init__(self):
self.db = {} self.db = {}
self.app_name = os.getenv('SERVICE_NAME', 'proxy')
self.version = os.getenv('VERSION', 'unknown')
self.tracer = logging.getLogger('data') self.tracer = logging.getLogger('data')
prxy = self.__info_devs['proxy']
prxy['sw'] = self.version
prxy['mdl'] = self.app_name
__info_devs={ __info_devs={
'proxy': {'singleton': True, 'name':'Proxy', 'mf':'Stefan Allius'}, 'proxy': {'singleton': True, 'name':'Proxy', 'mf':'Stefan Allius'},
@@ -237,23 +251,14 @@ class Infos:
yield json.dumps (attr), component, node_id, attr['uniq_id'] yield json.dumps (attr), component, node_id, attr['uniq_id']
def __init_counter (self, counter:str) -> dict:
'''init proxy statistic counter, when its missing'''
if not 'proxy' in self.stat:
self.stat['proxy'] = {}
dict = self.stat['proxy']
if not counter in dict:
dict[counter] = 0
return dict
def inc_counter (self, counter:str) -> None: def inc_counter (self, counter:str) -> None:
'''inc proxy statistic counter''' '''inc proxy statistic counter'''
dict = self.__init_counter (counter) dict = self.stat['proxy']
dict[counter] += 1 dict[counter] += 1
def dec_counter (self, counter:str) -> None: def dec_counter (self, counter:str) -> None:
'''dec proxy statistic counter''' '''dec proxy statistic counter'''
dict = self.__init_counter (counter) dict = self.stat['proxy']
dict[counter] -= 1 dict[counter] -= 1

View File

@@ -4,6 +4,7 @@ from async_stream import AsyncStream
from inverter import Inverter from inverter import Inverter
from config import Config from config import Config
from mqtt import Mqtt from mqtt import Mqtt
from infos import Infos
async def handle_client(reader, writer): async def handle_client(reader, writer):
@@ -69,6 +70,8 @@ if __name__ == "__main__":
# call Mqtt singleton to establisch the connection to the mqtt broker # call Mqtt singleton to establisch the connection to the mqtt broker
mqtt = Mqtt() mqtt = Mqtt()
# initialize the proxy statistics
Infos.static_init()
# #
# Register some UNIX Signal handler for a gracefully server shutdown on Docker restart and stop # Register some UNIX Signal handler for a gracefully server shutdown on Docker restart and stop