Skip to content

Commit

Permalink
Add history endpoint for Hosts, user endpoint
Browse files Browse the repository at this point in the history
Ref #201
  • Loading branch information
algorys committed Sep 29, 2017
1 parent 93fd80b commit 5f3a124
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
30 changes: 29 additions & 1 deletion alignak_app/core/data_manager.py
Expand Up @@ -36,6 +36,7 @@ class DataManager(object):

def __init__(self):
self.database = self.get_database_model()
self.history_database = self.get_history_database_model()

@staticmethod
def get_database_model():
Expand All @@ -50,7 +51,23 @@ def get_database_model():
'host': {},
'service': {},
'alignakdaemon': {},
'livesynthesis': {}
'livesynthesis': {},
'user': {},
'history': {}
}

@staticmethod
def get_history_database_model():
"""
Return history database model
:return: history database model
:rtype: dict
"""

return {
'history': [],
'notification': []
}

def update_item_type(self, item_type, data):
Expand Down Expand Up @@ -85,6 +102,17 @@ def get_item(self, item_type, item_id):
if item_id in self.database[item_type]:
return self.database[item_type][item_id]

def update_history_item(self, history_type, data):
"""
TODO
:param history_type:
:param data:
:return:
"""

if data != self.history_database[history_type]:
self.history_database[history_type] = data

def get_synthesis_count(self):
"""
Get on "synthesis" endpoint and return the states of hosts and services
Expand Down
52 changes: 45 additions & 7 deletions alignak_app/threads/backend_thread.py
Expand Up @@ -47,6 +47,7 @@ def __init__(self, parent=None):
super(BackendQThread, self).__init__(parent)
self.requests_models = None
self.request_nb = 0
self.cur_host_id = None

def set_requests_models(self):
"""
Expand All @@ -64,6 +65,13 @@ def set_requests_models(self):
'aggregation', 'ls_last_state_changed'
]
daemons_projection = ['alive', 'type', 'name']
user_projection = {
'_realm', 'is_admin', 'back_role_super_admin', 'alias', 'name', 'notes', 'email',
'can_submit_commands', 'token', 'host_notifications_enabled',
'service_notifications_enabled', 'host_notification_period',
'service_notification_period', 'host_notification_options',
'service_notification_options',
}

self.requests_models = {
'host': {
Expand All @@ -81,6 +89,17 @@ def set_requests_models(self):
'livesynthesis': {
'params': None,
'projection': None
},
'user': {
'params': {'where': json.dumps({'token': app_backend.backend.token})},
'projection': user_projection
},
'history': {
'params': {
# 'where': json.dumps({'host': self.cur_host_id}),
'sort': '-_id',
},
'projection': {'service_name': 1, 'message': 1, 'type': 1}
}
}

Expand All @@ -104,19 +123,38 @@ def run(self):

# Requests on each endpoint defined in model
for endpoint in self.requests_models:
request = app_backend.get(
endpoint,
params=self.requests_models[endpoint]['params'],
projection=self.requests_models[endpoint]['projection'],
all_items=True
)
backend_database[endpoint] = request['_items']
all_items = True
if 'user' in endpoint:
all_items = False
if 'history' not in endpoint:
request = app_backend.get(
endpoint,
params=self.requests_models[endpoint]['params'],
projection=self.requests_models[endpoint]['projection'],
all_items=all_items
)
backend_database[endpoint] = request['_items']

# Update DataManager
for item_type in backend_database:
print(item_type)
print(backend_database[item_type])
data_manager.update_item_type(
item_type,
backend_database[item_type]
)

for host in backend_database['host']:
self.requests_models['history']['params']['where'] = json.dumps({
'host': host['_id']}),
request = app_backend.get(
'history',
params=self.requests_models['history']['params'],
projection=self.requests_models['history']['projection'],
all_items=False
)
backend_database['history'][host['_id']] = request['_items']

data_manager.update_history_item('history', backend_database['history'])

self.update_data.emit(data_manager)
3 changes: 3 additions & 0 deletions alignak_app/threads/thread_manager.py
Expand Up @@ -82,6 +82,9 @@ def update_data_manager(data_manager):
print("Daemon: %s" % test_daemon)
test_synthesis = data_manager.get_synthesis_count()
print("Synthesis: %s" % test_synthesis)
test_user = data_manager.get_item('user', '59c4e3c135d17b8dff6acc5d')
print("User: %s" % test_user)
print("History: %s" % data_manager.history_database['history'])


# FOR TESTS
Expand Down

0 comments on commit 5f3a124

Please sign in to comment.