dashboard.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. # alive 的布尔值 True,在 url 中传输前,会自动转换为字符串 'True'
  20. hosts_url = host_url + url_for('api_hosts.r_get_by_filter', alive=True)
  21. guests_distribute_count_url = host_url + url_for('api_guests.r_distribute_count')
  22. disks_distribute_count_url = host_url + url_for('api_disks.r_distribute_count')
  23. guests_current_top_10_url = host_url + url_for('api_guest_performance.r_current_top_10')
  24. hosts_current_top_10_url = host_url + url_for('api_host_performance.r_current_top_10')
  25. hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
  26. hosts_ret = json.loads(hosts_ret.content)
  27. # Host node_id 与 Host 的映射
  28. hosts_mapping_by_node_id = dict()
  29. for host in hosts_ret['data']:
  30. hosts_mapping_by_node_id[int(host['node_id'])] = host
  31. guests_distribute_count_ret = requests.get(url=guests_distribute_count_url, cookies=request.cookies)
  32. guests_distribute_count_ret = json.loads(guests_distribute_count_ret.content)
  33. disks_distribute_count_ret = requests.get(url=disks_distribute_count_url, cookies=request.cookies)
  34. disks_distribute_count_ret = json.loads(disks_distribute_count_ret.content)
  35. guests_current_top_10_ret = requests.get(url=guests_current_top_10_url, cookies=request.cookies)
  36. guests_current_top_10_ret = json.loads(guests_current_top_10_ret.content)
  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 = host_url + url_for('api_guests.r_get_by_filter', 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_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 = host_url + url_for('api_disks.r_get_by_filter', filter='uuid:in:' + ','.join(disks_uuid))
  59. disks_ret = requests.get(url=disks_url, cookies=request.cookies)
  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, cookies=request.cookies)
  66. hosts_current_top_10_ret = json.loads(hosts_current_top_10_ret.content)
  67. hosts_current_top_10_ret['data']['memory_rate'] = copy.copy(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)