From 314a173b6cc3125190786e43158b7a60063f33bf Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 16 Jul 2024 11:41:41 +0200 Subject: ogcp: show cache contents in client details Show cache contents in client details for a more complete view of the client's state. Move the cache inspector code to its own template for reusability. --- ogcp/templates/actions/cache.html | 147 +--------------------------- ogcp/templates/actions/client_details.html | 4 + ogcp/templates/cache_inspector.html | 152 +++++++++++++++++++++++++++++ ogcp/views.py | 81 +++++++++------ 4 files changed, 211 insertions(+), 173 deletions(-) create mode 100644 ogcp/templates/cache_inspector.html diff --git a/ogcp/templates/actions/cache.html b/ogcp/templates/actions/cache.html index 46a8699..d64aebe 100644 --- a/ogcp/templates/actions/cache.html +++ b/ogcp/templates/actions/cache.html @@ -54,151 +54,8 @@ {{ _('Delete') }} - -
- -
-
- {{ _('Detailed cache usage') }} -
-
- - - -
    -
  • - -
  • -
  • -

    {{ _('Images in cache:') }}

    -
    -
  • -
-
    -
  • - {{ _('Disk size') }} -
  • -
  • - {{ _('used') }} (%) -
  • -
  • - {{ _('available') }} (%) -
  • -
-
    -
  • -
  • -
  • -
-
-
- - - - - - - -{% else %} -
- {{ _('Cache is currently empty in the selected client(s)') }} -
{% endif %} +{% include 'cache_inspector.html' %} + {% endblock %} diff --git a/ogcp/templates/actions/client_details.html b/ogcp/templates/actions/client_details.html index 0362d5b..070e4d6 100644 --- a/ogcp/templates/actions/client_details.html +++ b/ogcp/templates/actions/client_details.html @@ -9,6 +9,8 @@ {% block nav_client_add %}active{% endblock %} {% block content %} +{% set ip_list = [form.ip.data] %} +

{{_('Client details')}}

@@ -106,4 +108,6 @@ {% endif %} +{% include 'cache_inspector.html' %} + {% endblock %} diff --git a/ogcp/templates/cache_inspector.html b/ogcp/templates/cache_inspector.html new file mode 100644 index 0000000..42dd983 --- /dev/null +++ b/ogcp/templates/cache_inspector.html @@ -0,0 +1,152 @@ +{% if storage_data is defined and images_data is defined and client_images is defined and ip_list is defined %} + +
+{% if images_data|length > 0 %} +
+
+ {{ _('Detailed cache usage') }} +
+
+ {% if ip_list|length > 1 %} + + + {% endif %} + +
    +
  • + +
  • +
  • +

    {{ _('Images in cache:') }}

    +
    +
  • +
+
    +
  • + {{ _('Disk size') }} +
  • +
  • + {{ _('used') }} (%) +
  • +
  • + {{ _('available') }} (%) +
  • +
+
    +
  • +
  • +
  • +
+
+
+ + + + + + +{% else %} +
+ {{ _('No cache contents') }} +
+{% endif %} + +{% endif %} \ No newline at end of file diff --git a/ogcp/views.py b/ogcp/views.py index 2a6c244..2c8f650 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -354,6 +354,34 @@ def hash_password(pwd): return pwd_hash +def get_cache_info(clients_info, storage_data, images_data, client_images): + for client_info in clients_info: + ip = client_info['ip'] + cache_size = int(client_info['cache_size']) + used_cache = 0 + for image_info in client_info['images']: + image_name = image_info['name'] + checksum = image_info['checksum'] + used_cache += int(image_info['size']) + img_identifier = f'{image_name}.{checksum}' + + if ip in client_images: + client_images[ip].append(img_identifier) + else: + client_images[ip] = [img_identifier] + + if img_identifier in images_data: + images_data[img_identifier]['clients'].append(ip) + else: + images_data[img_identifier] = { + 'clients': [ip], + 'size': int(image_info['size']), + 'name': image_name, + 'checksum': checksum + } + + storage_data[ip] = {'used': used_cache, + 'total': cache_size} def authenticate_user(username, pwd): for user in app.config['USERS']: @@ -1171,9 +1199,9 @@ def action_client_cache(): if not validate_elements(ips): return redirect(url_for('commands')) - server = get_server_from_clients(ips_list) form.ips.data = ' '.join(ips_list) + server = get_server_from_clients(ips_list) r = server.get('/cache/list', payload={'clients': ips_list}) if not r: return ogserver_down('commands') @@ -1189,33 +1217,8 @@ def action_client_cache(): storage_data = {} images_data = {} client_images = {} - for client_info in clients_info: - ip = client_info['ip'] - cache_size = int(client_info['cache_size']) - used_cache = 0 - for image_info in client_info['images']: - image_name = image_info['name'] - checksum = image_info['checksum'] - used_cache += int(image_info['size']) - img_identifier = f'{image_name}.{checksum}' - - if ip in client_images: - client_images[ip].append(img_identifier) - else: - client_images[ip] = [img_identifier] - if img_identifier in images_data: - images_data[img_identifier]['clients'].append(ip) - else: - images_data[img_identifier] = { - 'clients': [ip], - 'size': int(image_info['size']), - 'name': image_name, - 'checksum': checksum - } - - storage_data[ip] = {'used': used_cache, - 'total': cache_size} + get_cache_info(clients_info, storage_data, images_data, client_images) for img_identifier in images_data: image_data = images_data[img_identifier] @@ -1321,10 +1324,31 @@ def action_client_info(): else: entry['image'] = "" + r = server.get('/cache/list', payload={'clients': [ip]}) + if not r: + return ogserver_down('commands') + if r.status_code != requests.codes.ok: + return ogserver_error('commands') + + clients_info = r.json()['clients'] + + if not clients_info: + flash(_('ogServer returned an empty client list'), category='error') + return redirect(url_for('commands')) + + storage_data = {} + images_data = {} + client_images = {} + + get_cache_info(clients_info, storage_data, images_data, client_images) + scopes, clients = get_scopes(set(ips)) return render_template('actions/client_details.html', form=form, - parent="commands.html", scopes=scopes, setup=setup) + parent="commands.html", scopes=scopes, setup=setup, + images_data=images_data, + storage_data=storage_data, + client_images=client_images) @app.route('/action/client/update', methods=['GET', 'POST']) @login_required @@ -1455,6 +1479,7 @@ def action_client_update(): entry['image'] = "" form.submit.render_kw = {"formaction": url_for('action_client_update')} + return render_template('actions/client_details.html', form=form, parent="scopes.html", scopes=scopes, setup=setup) -- cgit v1.2.3-18-g5258