diff --git a/alignak_app/core/backend.py b/alignak_app/core/backend.py index ea42218..1dc233d 100644 --- a/alignak_app/core/backend.py +++ b/alignak_app/core/backend.py @@ -475,7 +475,5 @@ def get_host_history(self, host_id): return {} -""" - Creating "app_backend" variable. -""" +# Creating "app_backend" variable. app_backend = AppBackend() diff --git a/alignak_app/core/data_manager.py b/alignak_app/core/data_manager.py index 387ce57..91076f6 100644 --- a/alignak_app/core/data_manager.py +++ b/alignak_app/core/data_manager.py @@ -23,6 +23,11 @@ DataManager manage and store the Alignak data provided by BackendQthread """ +from logging import getLogger + + +logger = getLogger(__name__) + class DataManager(object): """ @@ -30,11 +35,7 @@ class DataManager(object): """ def __init__(self): - self.database = { - 'host': {}, - 'service': {}, - 'alignakdaemon': {} - } + self.database = self.get_database_model() @staticmethod def get_database_model(): @@ -48,7 +49,8 @@ def get_database_model(): return { 'host': {}, 'service': {}, - 'alignakdaemon': {} + 'alignakdaemon': {}, + 'livesynthesis': {} } def update_item_type(self, item_type, data): @@ -61,6 +63,8 @@ def update_item_type(self, item_type, data): :type data: dict """ + logger.info('Update [%s] database...', item_type) + for d in data: self.database[item_type][d['_id']] = d @@ -76,11 +80,73 @@ def get_item(self, item_type, item_id): :rtype: dict """ + logger.debug("Get item %s in %s", item_id, item_type) + if item_id in self.database[item_type]: return self.database[item_type][item_id] + def get_synthesis_count(self): + """ + Get on "synthesis" endpoint and return the states of hosts and services -""" - Creating "data_manager" variable. -""" + :return: states of hosts and services. + :rtype: dict + """ + + live_synthesis = { + 'hosts': { + 'up': 0, + 'down': 0, + 'unreachable': 0, + 'acknowledge': 0, + 'downtime': 0 + }, + 'services': { + 'ok': 0, + 'critical': 0, + 'unknown': 0, + 'warning': 0, + 'unreachable': 0, + 'acknowledge': 0, + 'downtime': 0 + } + } + + if self.database['livesynthesis']: + for realm in self.database['livesynthesis']: + realm_item = self.get_item('livesynthesis', realm) + live_synthesis['hosts']['up'] += realm_item['hosts_up_soft'] + live_synthesis['hosts']['up'] += realm_item['hosts_up_hard'] + + live_synthesis['hosts']['unreachable'] += realm_item['hosts_unreachable_soft'] + live_synthesis['hosts']['unreachable'] += realm_item['hosts_unreachable_hard'] + + live_synthesis['hosts']['down'] += realm_item['hosts_down_soft'] + live_synthesis['hosts']['down'] += realm_item['hosts_down_hard'] + + live_synthesis['hosts']['acknowledge'] += realm_item['hosts_acknowledged'] + live_synthesis['hosts']['downtime'] += realm_item['hosts_in_downtime'] + + live_synthesis['services']['ok'] += realm_item['services_ok_soft'] + live_synthesis['services']['ok'] += realm_item['services_ok_hard'] + + live_synthesis['services']['warning'] += realm_item['services_warning_soft'] + live_synthesis['services']['warning'] += realm_item['services_warning_hard'] + + live_synthesis['services']['critical'] += realm_item['services_critical_soft'] + live_synthesis['services']['critical'] += realm_item['services_critical_hard'] + + live_synthesis['services']['unknown'] += realm_item['services_unknown_soft'] + live_synthesis['services']['unknown'] += realm_item['services_unknown_hard'] + + live_synthesis['services']['unreachable'] += realm_item['services_unreachable_soft'] + live_synthesis['services']['unreachable'] += realm_item['services_unreachable_hard'] + + live_synthesis['services']['acknowledge'] += realm_item['services_acknowledged'] + live_synthesis['services']['downtime'] += realm_item['services_in_downtime'] + + return live_synthesis + + +# Creating "data_manager" variable. data_manager = DataManager() diff --git a/alignak_app/threads/backend_thread.py b/alignak_app/threads/backend_thread.py index d3644a0..8635b9e 100644 --- a/alignak_app/threads/backend_thread.py +++ b/alignak_app/threads/backend_thread.py @@ -25,12 +25,17 @@ import json +from logging import getLogger + from PyQt5.Qt import QThread, pyqtSignal # pylint: disable=no-name-in-module from alignak_app.core.data_manager import data_manager, DataManager from alignak_app.core.backend import app_backend +logger = getLogger(__name__) + + class BackendQThread(QThread): """ Class who create a QThread to trigger requests @@ -73,6 +78,10 @@ def set_requests_models(self): 'params': None, 'projection': daemons_projection }, + 'livesynthesis': { + 'params': None, + 'projection': None + } } def run(self): @@ -82,6 +91,7 @@ def run(self): """ + logger.info("Run BackendQThread...") # FOR TESTS self.request_nb += 1 print("--------- Request N° %d ---------------" % self.request_nb) @@ -100,7 +110,6 @@ def run(self): projection=self.requests_models[endpoint]['projection'], all_items=True ) - backend_database[endpoint] = request['_items'] # Update DataManager diff --git a/alignak_app/threads/thread_manager.py b/alignak_app/threads/thread_manager.py index 56702a4..94d13c3 100644 --- a/alignak_app/threads/thread_manager.py +++ b/alignak_app/threads/thread_manager.py @@ -25,6 +25,8 @@ import sys +from logging import getLogger + from PyQt5.Qt import QApplication # pylint: disable=no-name-in-module from PyQt5.Qt import QTimer, QObject # pylint: disable=no-name-in-module @@ -33,7 +35,9 @@ from alignak_app.core.backend import app_backend from alignak_app.threads.backend_thread import BackendQThread + init_config() +logger = getLogger(__name__) init_localization() @@ -53,7 +57,8 @@ def start(self): """ - print("Start backend Manager") + logger.info("Start backend Manager...") + timer = QTimer(self) timer.setInterval(30000) timer.start() @@ -75,6 +80,8 @@ def update_data_manager(data_manager): print("Service: %s" % test_service) test_daemon = data_manager.get_item('alignakdaemon', '59c4e64335d17b8e0c6ace0f') print("Daemon: %s" % test_daemon) + test_synthesis = data_manager.get_synthesis_count() + print("Synthesis: %s" % test_synthesis) # FOR TESTS