Skip to content

Commit

Permalink
Add TimePeriod in requests, thread and datamanager
Browse files Browse the repository at this point in the history
Ref #246
  • Loading branch information
algorys committed Feb 28, 2018
1 parent 5761f56 commit c933220
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 54 deletions.
62 changes: 30 additions & 32 deletions alignak_app/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from alignak_app.items.service import Service
from alignak_app.items.user import User
from alignak_app.items.realm import Realm
from alignak_app.items.period import Period

from alignak_app.utils.config import settings

Expand Down Expand Up @@ -227,38 +228,6 @@ def patch(self, endpoint, data, headers):

return request

def get_period_name(self, endpoint_id):
"""
Get the period name or alias
:param endpoint_id: id of endpoint
:type endpoint_id: str
:return: name or alias of timeperiod
:rtype: str
"""

projection = [
'name',
'alias'
]

endpoint = '/'.join(['timeperiod', endpoint_id])

period = self.get(endpoint, projection=projection)

if period:
if 'host' in endpoint or 'service' in endpoint:
period_items = period['_items'][0]
else:
period_items = period

if 'alias' in period_items:
return period_items['alias']
if 'name' in period_items:
return period_items['name']

return 'n/a'

def query_realms_data(self):
"""
Launch a request on ``realm`` endpoint
Expand Down Expand Up @@ -288,6 +257,35 @@ def query_realms_data(self):
if realms_list:
data_manager.update_database('realm', realms_list)

def query_period_data(self):
"""
Launch a request on ``timeperiod`` endpoint
"""

request_data = Period.get_request_model()

request = self.get(
request_data['endpoint'],
request_data['params'],
request_data['projection']
)

if request:
periods_list = []
for item in request['_items']:
period = Period()

period.create(
item['_id'],
item,
item['name'],
)
periods_list.append(period)

if periods_list:
data_manager.update_database('timeperiod', periods_list)

def query_user_data(self):
"""
Launch request for "user" endpoint
Expand Down
23 changes: 22 additions & 1 deletion alignak_app/backend/datamanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self):
'service': [],
'user': [],
'realm': [],
'timeperiod': [],
}
self.old_notifications = []

Expand All @@ -61,7 +62,9 @@ def is_ready(self):
"""

cur_collected = ''
db_names = ['livesynthesis', 'host', 'alignakdaemon', 'user', 'service', 'realm']
db_names = [
'livesynthesis', 'host', 'alignakdaemon', 'user', 'realm', 'timeperiod', 'service'
]
for db_name in db_names:
try:
assert bool(self.database[db_name])
Expand Down Expand Up @@ -153,6 +156,24 @@ def get_realm_name(self, realm):

return 'n/a'

def get_period_name(self, period):
"""
Return the period name or alias
:param period: wanted period ``_id``
:type period: str
:return: the wanted realm alias or name if available
:rtype: str
"""

if self.database['timeperiod']:
wanted_period = self.get_item('timeperiod', period)

if wanted_period:
return wanted_period.get_display_name()

return 'n/a'

def get_synthesis_count(self):
"""
Get on "synthesis" endpoint and return the states of hosts and services
Expand Down
4 changes: 2 additions & 2 deletions alignak_app/items/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def __init__(self):
@staticmethod
def get_request_model():
"""
Return the request model for alignakdaemon requests
Return the request model for timeperiod requests
:return: request model for alignakdaemon endpoint
:return: request model for timeperiod endpoint
:rtype: dict
"""

Expand Down
4 changes: 2 additions & 2 deletions alignak_app/items/realm.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def __init__(self):
@staticmethod
def get_request_model():
"""
Return the request model for alignakdaemon requests
Return the request model for realm requests
:return: request model for alignakdaemon endpoint
:return: request model for realm endpoint
:rtype: dict
"""

Expand Down
4 changes: 2 additions & 2 deletions alignak_app/qobjects/dock/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def get_hosts_notif_widget(self):
period_title.setObjectName("subtitle")
host_notif_layout.addWidget(period_title, 3, 0, 1, 1)
self.labels['host_notification_period'].setText(
app_backend.get_period_name(
data_manager.get_period_name(
data_manager.database['user'].data['host_notification_period']
)
)
Expand Down Expand Up @@ -371,7 +371,7 @@ def get_services_notif_widget(self):
period_title.setObjectName("subtitle")
service_notif_layout.addWidget(period_title, 3, 0, 1, 1)
self.labels['service_notification_period'].setText(
app_backend.get_period_name(
data_manager.get_period_name(
data_manager.database['user'].data['service_notification_period']
)
)
Expand Down
2 changes: 2 additions & 0 deletions alignak_app/qthreads/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def run(self): # pragma: no cover
app_backend.query_livesynthesis_data()
elif 'realm' in self.thread_name:
app_backend.query_realms_data()
elif 'timeperiod' in self.thread_name:
app_backend.query_period_data()
elif self.thread_name == 'history':
if self.data:
app_backend.query_history_data(self.data['hostname'], self.data['host_id'])
Expand Down
2 changes: 1 addition & 1 deletion alignak_app/qthreads/threadmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_threads_to_launch(self):
threads_to_launch = []

# Add BackendQThread only if they are not already running
for cur_thread in ['livesynthesis', 'host', 'service', 'user', 'realm',
for cur_thread in ['livesynthesis', 'host', 'service', 'user', 'realm', 'timeperiod',
'alignakdaemon', 'notifications', 'history']:
if self.current_thread:
if cur_thread != self.current_thread.thread_name:
Expand Down
25 changes: 11 additions & 14 deletions test/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ def test_query_realms_data(self):

self.assertIsNotNone(data_manager.database['realm'])

def test_query_period_data(self):
"""Query TimePeriod Data"""

under_test = BackendClient()
under_test.login()

from alignak_app.backend.datamanager import data_manager
under_test.query_period_data()

self.assertIsNotNone(data_manager.database['timeperiod'])

def test_query_user_data(self):
"""Query User Data"""

Expand Down Expand Up @@ -207,20 +218,6 @@ def test_query_history_data(self):
self.assertEqual(old_database[0].item_id, data_manager.database['history'][0].item_id)
self.assertEqual(old_database[0].item_id, self.host_id)

def test_get_period_name(self):
"""Get Period Name"""

backend_test = BackendClient()
backend_test.login()

under_test = backend_test.get_period_name('host')

self.assertEqual('n/a', under_test)

under_test = backend_test.get_period_name('service')

self.assertEqual('n/a', under_test)

def test_get_backend_status_icon(self):
"""Get Backend Status Icon Name"""

Expand Down
50 changes: 50 additions & 0 deletions test/test_data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from alignak_app.items.service import Service
from alignak_app.items.user import User
from alignak_app.items.realm import Realm
from alignak_app.items.period import Period


class TestDataManager(unittest2.TestCase):
Expand Down Expand Up @@ -152,6 +153,7 @@ class TestDataManager(unittest2.TestCase):
event.create(data['_id'], data)
event_list.append(event)

# Realm data test
realm_list = []
for i in range(0, 10):
realm = Realm()
Expand All @@ -164,6 +166,7 @@ class TestDataManager(unittest2.TestCase):
'realm%d' % i
)
realm_list.append(realm)

realm_noalias = Realm()
realm_noalias.create(
'_id',
Expand All @@ -174,6 +177,30 @@ class TestDataManager(unittest2.TestCase):
)
realm_list.append(realm_noalias)

# TimePeriod data test
period_list = []
for i in range(0, 10):
period = Realm()
period.create(
'_id%d' % i,
{
'name': 'period%d' % i,
'alias': 'My Time Period %d' % i,
},
'period%d' % i
)
period_list.append(period)

period_noalias = Period()
period_noalias.create(
'_id',
{
'name': 'period',
},
'period'
)
period_list.append(period_noalias)

def test_initialize(self):
"""Initialize DataManager"""

Expand Down Expand Up @@ -257,6 +284,29 @@ def test_get_realm_name(self):

self.assertEqual('n/a', no_realm_test)

def test_get_period_name(self):
"""Get Time Period in db"""

under_test = DataManager()

self.assertFalse(under_test.database['timeperiod'])

under_test.update_database('timeperiod', self.period_list)

self.assertTrue(under_test.database['timeperiod'])

period_test = under_test.get_period_name('_id4')

self.assertEqual('My Time Period 4', period_test)

noalias_period_test = under_test.get_period_name('_id')

self.assertEqual('Period', noalias_period_test)

no_period_test = under_test.get_period_name('no_period')

self.assertEqual('n/a', no_period_test)

def test_get_livesynthesis(self):
"""Get Livesynthesis in db"""

Expand Down

0 comments on commit c933220

Please sign in to comment.