Jelajahi Sumber

dashboard填充如Guest cpu、内存统计信息

James Iter 9 tahun lalu
induk
melakukan
8b596589be
3 mengubah file dengan 25 tambahan dan 11 penghapusan
  1. 7 1
      api/guest.py
  2. 4 4
      templates/dashboard.html
  3. 14 6
      views/dashboard.py

+ 7 - 1
api/guest.py

@@ -621,7 +621,10 @@ def r_distribute_count():
         'os_template_id': dict(),
         'status': dict(),
         'on_host': dict(),
-        'cpu_memory': dict()
+        'cpu_memory': dict(),
+        'cpu': 0,
+        'memory': 0,
+        'guests': rows.__len__()
     }
 
     for guest in rows:
@@ -643,6 +646,9 @@ def r_distribute_count():
         ret['data']['on_host'][guest['on_host']] += 1
         ret['data']['cpu_memory'][cpu_memory] += 1
 
+        ret['data']['cpu'] += guest['cpu']
+        ret['data']['memory'] += guest['memory']
+
     return ret
 
 

+ 4 - 4
templates/dashboard.html

@@ -68,7 +68,7 @@
                             <div class="tile-content-wrapper">
                                 <i class="webhh icon-cpu-processor"></i>
                                 <div style="font-size: 20px; margin-top: 18px; right: 10px; position: absolute;">
-                                    64
+                                    {{ hosts_sum.cpu }}
                                 </div>
                                 <div class="tile-header" style="opacity: .70;">
                                     宿主机 CPU 总计
@@ -81,7 +81,7 @@
                             <div class="tile-content-wrapper">
                                 <i class="webhh icon-ram"></i>
                                 <div style="font-size: 20px; margin-top: 18px; right: 10px; position: absolute;">
-                                    733 GB
+                                    {{ (hosts_sum.memory / 1024 / 1024 / 1024)|int }} GB
                                 </div>
                                 <div class="tile-header" style="opacity: .70;">
                                     宿主机内存总计
@@ -94,7 +94,7 @@
                             <div class="tile-content-wrapper">
                                 <i class="webhh icon-stampalt"></i>
                                 <div style="font-size: 20px; margin-top: 18px; right: 10px; position: absolute;">
-                                    64
+                                    {{ guests_distribute_count_ret.data.cpu }}
                                 </div>
                                 <div class="tile-header" style="opacity: .70;">
                                     虚拟机 CPU 总计
@@ -107,7 +107,7 @@
                             <div class="tile-content-wrapper">
                                 <i class="webhh icon-ruler"></i>
                                 <div style="font-size: 20px; margin-top: 18px; right: 10px; position: absolute;">
-                                    1233 GB
+                                    {{ guests_distribute_count_ret.data.memory }} GB
                                 </div>
                                 <div class="tile-header" style="opacity: .70;">
                                     虚拟机内存总计

+ 14 - 6
views/dashboard.py

@@ -26,13 +26,21 @@ def show():
 
     host_url = request.host_url.rstrip('/')
 
-    guests_url = host_url + url_for('api_guests.r_get_by_filter')
+    hosts_url = host_url + url_for('api_hosts.r_get_by_filter')
+    guests_distribute_count_url = host_url + url_for('api_guests.r_distribute_count')
 
-    if args.__len__() > 0:
-        guests_url = guests_url + '?' + '&'.join(args)
+    hosts_ret = requests.get(url=hosts_url)
+    hosts_ret = json.loads(hosts_ret.content)
 
-    guests_ret = requests.get(url=guests_url)
-    guests_ret = json.loads(guests_ret.content)
+    guests_distribute_count_ret = requests.get(url=guests_distribute_count_url)
+    guests_distribute_count_ret = json.loads(guests_distribute_count_ret.content)
 
-    return render_template('dashboard.html', guests_ret=guests_ret, resource_path=resource_path)
+    hosts_sum = {'cpu': 0, 'memory': 0}
+
+    for host in hosts_ret['data']:
+        hosts_sum['cpu'] += host['cpu']
+        hosts_sum['memory'] += host['memory']
+
+    return render_template('dashboard.html', hosts_sum=hosts_sum, resource_path=resource_path,
+                           guests_distribute_count_ret=guests_distribute_count_ret)