Skip to content

Commit

Permalink
Add livesynthesis endpoint in BackendQThread
Browse files Browse the repository at this point in the history
Ref #201
  • Loading branch information
algorys committed Sep 28, 2017
1 parent 36bbe32 commit 93fd80b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 14 deletions.
4 changes: 1 addition & 3 deletions alignak_app/core/backend.py
Expand Up @@ -475,7 +475,5 @@ def get_host_history(self, host_id):
return {}


"""
Creating "app_backend" variable.
"""
# Creating "app_backend" variable.
app_backend = AppBackend()
84 changes: 75 additions & 9 deletions alignak_app/core/data_manager.py
Expand Up @@ -23,18 +23,19 @@
DataManager manage and store the Alignak data provided by BackendQthread
"""

from logging import getLogger


logger = getLogger(__name__)


class DataManager(object):
"""
Class who store Alignak data
"""

def __init__(self):
self.database = {
'host': {},
'service': {},
'alignakdaemon': {}
}
self.database = self.get_database_model()

@staticmethod
def get_database_model():
Expand All @@ -48,7 +49,8 @@ def get_database_model():
return {
'host': {},
'service': {},
'alignakdaemon': {}
'alignakdaemon': {},
'livesynthesis': {}
}

def update_item_type(self, item_type, data):
Expand All @@ -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

Expand All @@ -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()
11 changes: 10 additions & 1 deletion alignak_app/threads/backend_thread.py
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +78,10 @@ def set_requests_models(self):
'params': None,
'projection': daemons_projection
},
'livesynthesis': {
'params': None,
'projection': None
}
}

def run(self):
Expand All @@ -82,6 +91,7 @@ def run(self):
"""

logger.info("Run BackendQThread...")
# FOR TESTS
self.request_nb += 1
print("--------- Request N° %d ---------------" % self.request_nb)
Expand All @@ -100,7 +110,6 @@ def run(self):
projection=self.requests_models[endpoint]['projection'],
all_items=True
)

backend_database[endpoint] = request['_items']

# Update DataManager
Expand Down
9 changes: 8 additions & 1 deletion alignak_app/threads/thread_manager.py
Expand Up @@ -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

Expand All @@ -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()


Expand All @@ -53,7 +57,8 @@ def start(self):
"""

print("Start backend Manager")
logger.info("Start backend Manager...")

timer = QTimer(self)
timer.setInterval(30000)
timer.start()
Expand All @@ -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
Expand Down

0 comments on commit 93fd80b

Please sign in to comment.