From a45f664905a6bb0d6e6e1510507ef62402f4d21e Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Mon, 29 Jan 2024 10:38:52 +0100 Subject: dashboard: Show all servers, online or offline Show in dashboard the list of all servers, regardless if they are online or offline. If they are offline, uptime appears as 'offline' Modify dashboard template to make it work with offline servers --- ogcp/templates/dashboard.html | 12 ++++++++++-- ogcp/views.py | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/ogcp/templates/dashboard.html b/ogcp/templates/dashboard.html index 257cfe3..bc89128 100644 --- a/ogcp/templates/dashboard.html +++ b/ogcp/templates/dashboard.html @@ -30,14 +30,22 @@ {{ server.images | length }} {% set disk = server.disk %} + {% if disk['total'] != 0 %} {% set used = (((disk['total'] - disk['free']) / disk['total']) * 100)|int %} + {% else %} + {% set used = 0 %} + {% endif %}
{% set memory = server.stats.memory %} + {% if memory['size'] != 0 %} {% set used = (((memory['size'] - memory['free']) / memory['size']) * 100)|int %} + {% else %} + {% set used = 0 %} + {% endif %}
@@ -64,7 +72,7 @@
-{% for id, server in servers.items() %} +{% for id, server in servers.items() if server.online %} {% set stats = server.stats %} {% set time_dict = server.time_dict %} {% set images = server.images %} diff --git a/ogcp/views.py b/ogcp/views.py index f0049fc..24df2e3 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -391,17 +391,35 @@ def index(): dashboard_servers[server_id]['oglive_list'] = i['json'] all_stats = multi_request('get', '/stats') - for i in all_stats: - server_id = i['server'].id - stats = i['json'] - dashboard_servers[server_id]['stats'] = stats - - timestamp = datetime.datetime.fromtimestamp(stats.get('time').get('now')) - now = timestamp.strftime('%Y-%m-%d %H:%M:%S') - boot = display_time(stats.get('time').get('boot')) - start = display_time(stats.get('time').get('start')) - time_dict = {'now': now, 'boot': boot, 'start': start} - dashboard_servers[server_id]['time_dict'] = time_dict + for server in servers: + active = False + stat = None + for i in all_stats: + if i['server'].id == server.id: + active = True + stat = i + break + if active: + server_id = stat['server'].id + dashboard_servers[server_id]['online'] = True + stats = stat['json'] + dashboard_servers[server_id]['stats'] = stats + timestamp = datetime.datetime.fromtimestamp(stats.get('time').get('now')) + now = timestamp.strftime('%Y-%m-%d %H:%M:%S') + boot = display_time(stats.get('time').get('boot')) + start = display_time(stats.get('time').get('start')) + time_dict = {'now': now, 'boot': boot, 'start': start} + dashboard_servers[server_id]['time_dict'] = time_dict + else: + dashboard_servers[server.id] = {} + dashboard_servers[server.id]['online'] = False + dashboard_servers[server.id]['name'] = server.name + dashboard_servers[server.id]['time_dict'] = {'now': None, 'boot': 'Offline', 'start': None} + dashboard_servers[server.id]['clients'] = [] + dashboard_servers[server.id]['images'] = [] + dashboard_servers[server.id]['disk'] = {'total': 0, 'free':0} + dashboard_servers[server.id]['stats'] = {'memory': {'free': 0, 'size':0}, 'swap': ''} + dashboard_servers[server.id]['oglive_list'] = [] clients_response = multi_request('get', '/clients') for i in clients_response: -- cgit v1.2.3-18-g5258