dashboard.py 4.6 KB

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