dashboard.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. from flask import Blueprint, render_template, url_for, request
  5. import requests
  6. __author__ = 'James Iter'
  7. __date__ = '2017/8/17'
  8. __contact__ = 'james.iter.cn@gmail.com'
  9. __copyright__ = '(c) 2017 by James Iter.'
  10. blueprint = Blueprint(
  11. 'v_dashboard',
  12. __name__,
  13. url_prefix='/'
  14. )
  15. def show():
  16. resource_path = request.path
  17. host_url = request.host_url.rstrip('/')
  18. hosts_url = host_url + url_for('api_hosts.r_get_by_filter')
  19. guests_distribute_count_url = host_url + url_for('api_guests.r_distribute_count')
  20. disks_distribute_count_url = host_url + url_for('api_disks.r_distribute_count')
  21. guests_current_top_10_url = host_url + url_for('api_performance.r_current_top_10')
  22. hosts_current_top_10_url = host_url + url_for('api_host_performance.r_current_top_10')
  23. hosts_ret = requests.get(url=hosts_url)
  24. hosts_ret = json.loads(hosts_ret.content)
  25. # Host node_id 与 Host 的映射
  26. hosts_mapping_by_node_id = dict()
  27. for host in hosts_ret['data']:
  28. hosts_mapping_by_node_id[int(host['node_id'])] = host
  29. guests_distribute_count_ret = requests.get(url=guests_distribute_count_url)
  30. guests_distribute_count_ret = json.loads(guests_distribute_count_ret.content)
  31. disks_distribute_count_ret = requests.get(url=disks_distribute_count_url)
  32. disks_distribute_count_ret = json.loads(disks_distribute_count_ret.content)
  33. guests_current_top_10_ret = requests.get(url=guests_current_top_10_url)
  34. guests_current_top_10_ret = json.loads(guests_current_top_10_ret.content)
  35. guests_url = host_url + url_for('api_guests.r_get_by_filter')
  36. guests_uuid = list()
  37. for k, v in guests_current_top_10_ret['data'].items():
  38. for item in v:
  39. if 'guest_uuid' not in item:
  40. break
  41. if item['guest_uuid'] not in guests_uuid:
  42. guests_uuid.append(item['guest_uuid'])
  43. guests_url = guests_url + '?filter=uuid:in:' + ','.join(guests_uuid)
  44. guests_ret = requests.get(url=guests_url)
  45. guests_ret = json.loads(guests_ret.content)
  46. # Guest uuid 与 Guest 的映射
  47. guests_mapping_by_uuid = dict()
  48. for guest in guests_ret['data']:
  49. guests_mapping_by_uuid[guest['uuid']] = guest
  50. disks_url = host_url + url_for('api_disks.r_get_by_filter')
  51. disks_uuid = list()
  52. for k, v in guests_current_top_10_ret['data'].items():
  53. for item in v:
  54. if 'disk_uuid' not in item:
  55. break
  56. if item['disk_uuid'] not in disks_uuid:
  57. disks_uuid.append(item['disk_uuid'])
  58. disks_url = disks_url + '?filter=uuid:in:' + ','.join(disks_uuid)
  59. disks_ret = requests.get(url=disks_url)
  60. disks_ret = json.loads(disks_ret.content)
  61. # Disk uuid 与 Disk 的映射
  62. disks_mapping_by_uuid = dict()
  63. for disk in disks_ret['data']:
  64. disks_mapping_by_uuid[disk['uuid']] = disk
  65. hosts_current_top_10_ret = requests.get(url=hosts_current_top_10_url)
  66. hosts_current_top_10_ret = json.loads(hosts_current_top_10_ret.content)
  67. hosts_current_top_10_ret['data']['memory_rate'] = hosts_current_top_10_ret['data']['cpu_load']
  68. for i in range(hosts_current_top_10_ret['data']['memory_rate'].__len__()):
  69. memory_total = hosts_mapping_by_node_id[hosts_current_top_10_ret['data']['memory_rate'][i]['node_id']]['memory']
  70. memory_available = hosts_current_top_10_ret['data']['memory_rate'][i]['memory_available']
  71. memory_used = memory_total - memory_available
  72. hosts_current_top_10_ret['data']['memory_rate'][i]['memory_rate'] = int(memory_used / float(memory_total) * 100)
  73. hosts_current_top_10_ret['data']['memory_rate'].sort(key=lambda _k: _k['memory_rate'], reverse=True)
  74. hosts_sum = {'cpu': 0, 'memory': 0}
  75. for host in hosts_ret['data']:
  76. hosts_sum['cpu'] += host['cpu']
  77. hosts_sum['memory'] += host['memory']
  78. return render_template('dashboard.html', hosts_sum=hosts_sum, resource_path=resource_path,
  79. guests_distribute_count_ret=guests_distribute_count_ret,
  80. disks_distribute_count_ret=disks_distribute_count_ret,
  81. guests_current_top_10_ret=guests_current_top_10_ret,
  82. guests_mapping_by_uuid=guests_mapping_by_uuid, disks_mapping_by_uuid=disks_mapping_by_uuid,
  83. hosts_current_top_10_ret=hosts_current_top_10_ret,
  84. hosts_mapping_by_node_id=hosts_mapping_by_node_id)