Skip to content

Commit

Permalink
Update problems database when refreshing prolems view
Browse files Browse the repository at this point in the history
Ref #296
  • Loading branch information
algorys committed Mar 30, 2018
1 parent a64764b commit 3775578
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
29 changes: 28 additions & 1 deletion alignak_app/backend/datamanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,31 @@ def get_items_and_problems(self):

return items_and_problems

def update_problems(self):
"""
Update "problems" database
"""

# Cleaning problems database
for item in self.database['problems']:
updated_item = self.get_item(item.item_type, item.item_id)
if updated_item:
if not self.is_problem(item.item_type, updated_item.data):
self.database['problems'].remove(item)
else:
item.data = updated_item.data

# Update if new host are in problem.
for item in self.database['host']:
item_in_problem = self.get_item('problems', item.item_id)
if not item_in_problem and self.is_problem(item.item_type, item.data):
self.database['problems'].append(item)
elif item_in_problem and not self.is_problem(item.item_type, item.data):
self.database['problems'].remove(item_in_problem)
elif item_in_problem and self.is_problem(item.item_type, item.data):
item_in_problem.data = item.data

def get_problems(self):
"""
Return items who are in problem: hosts and services
Expand All @@ -446,6 +471,8 @@ def get_problems(self):
:rtype: dict
"""

self.update_problems()

hosts_nb = 0
services_nb = 0
problems = self.database['problems']
Expand All @@ -457,7 +484,7 @@ def get_problems(self):
else:
hosts_nb += 1

# problems = sorted(problems, key=lambda x: x.data['ls_state'], reverse=True)
problems = sorted(problems, key=lambda x: x.data['ls_state'], reverse=True)
problems = sorted(problems, key=lambda x: x.item_type)

problems_data = {
Expand Down
15 changes: 11 additions & 4 deletions test/test_panel_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class TestPanelQWidget(unittest2.TestCase):
'name': 'host%d' % i,
'alias': 'Host %d' % i,
'_id': '_id%d' % i,
'ls_downtimed': True,
'ls_acknowledged': True,
'ls_state': 'UNKNOWN',
'ls_downtimed': False,
'ls_acknowledged': False,
'ls_state': 'UNREACHABLE',
'ls_output': 'output host %d' % i,
'ls_last_check': '',
'_realm': '59c4e38535d17b8dcb0bed42',
Expand Down Expand Up @@ -164,10 +164,17 @@ def test_spy_host(self):
def test_update_panels(self):
"""Update QTabPanel Problems"""

data_manager.database['problems'] = []
data_manager.update_database('host', self.host_list)
for item in self.host_list:
data_manager.database['problems'].append(item)
for item in self.service_list:
data_manager.database['problems'].append(item)

under_test = PanelQWidget()
under_test.initialize()

# 10 problems for CRITICAL services
# 20 problems for CRITICAL services and UNREACHABLE hosts
problems_index = under_test.get_tab_order().index('p')
self.assertEqual('Problems (20)', under_test.tab_widget.tabText(problems_index))

Expand Down

0 comments on commit 3775578

Please sign in to comment.