performance.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from flask import Blueprint
  4. import json
  5. import jimit as ji
  6. from api.base import Base
  7. from models import CPUMemory, Traffic, DiskIO, Utils, Rules
  8. __author__ = 'James Iter'
  9. __date__ = '2017/7/2'
  10. __contact__ = 'james.iter.cn@gmail.com'
  11. __copyright__ = '(c) 2017 by James Iter.'
  12. blueprint = Blueprint(
  13. 'api_performance',
  14. __name__,
  15. url_prefix='/api/performance'
  16. )
  17. blueprints = Blueprint(
  18. 'api_performances',
  19. __name__,
  20. url_prefix='/api/performances'
  21. )
  22. cpu_memory = Base(the_class=CPUMemory, the_blueprint=blueprint, the_blueprints=blueprints)
  23. traffic = Base(the_class=Traffic, the_blueprint=blueprint, the_blueprints=blueprints)
  24. disk_io = Base(the_class=DiskIO, the_blueprint=blueprint, the_blueprints=blueprints)
  25. @Utils.dumps2response
  26. def r_cpu_memory_get_by_filter():
  27. return cpu_memory.get_by_filter()
  28. @Utils.dumps2response
  29. def r_traffic_get_by_filter():
  30. return traffic.get_by_filter()
  31. @Utils.dumps2response
  32. def r_disk_io_get_by_filter():
  33. return disk_io.get_by_filter()
  34. def get_performance_data(uuid, uuid_field, the_class=None, granularity='hour'):
  35. args_rules = [
  36. Rules.UUID.value,
  37. ]
  38. t = {'t1': 0, 't2': 1}
  39. try:
  40. ji.Check.previewing(args_rules, {'uuid': uuid})
  41. uuids_str = ':'.join([uuid_field, 'in', uuid])
  42. ret = dict()
  43. ret['state'] = ji.Common.exchange_state(20000)
  44. ret['data'] = list()
  45. max_limit = 10080
  46. ts = ji.Common.ts()
  47. _boundary = ts - 60 * 60
  48. if granularity == 'hour':
  49. _boundary = ts - 60 * 60
  50. elif granularity == 'six_hours':
  51. _boundary = ts - 60 * 60 * 6
  52. elif granularity == 'day':
  53. _boundary = ts - 60 * 60 * 24
  54. elif granularity == 'seven_days':
  55. _boundary = ts - 60 * 60 * 24 * 7
  56. else:
  57. pass
  58. filter_str = ';'.join([uuids_str, 'timestamp:gt:' + _boundary.__str__()])
  59. _rows, _rows_count = the_class.get_by_filter(
  60. offset=0, limit=max_limit, order_by='id', order='asc', filter_str=filter_str)
  61. def smooth_data(boundary=0, interval=60, now_ts=ji.Common.ts(), rows=None):
  62. needs = list()
  63. data = list()
  64. for t in range(boundary, now_ts, interval):
  65. needs.append(t - t % interval)
  66. for row in rows:
  67. if row['timestamp'] % interval != 0:
  68. continue
  69. if needs.__len__() > 0:
  70. t = needs.pop(0)
  71. else:
  72. t = now_ts
  73. while t < row['timestamp']:
  74. data.append({
  75. 'timestamp': t,
  76. 'cpu_load': None,
  77. 'memory_available': None,
  78. 'memory_unused': None,
  79. 'rx_packets': None,
  80. 'rx_bytes': None,
  81. 'tx_packets': None,
  82. 'tx_bytes': None,
  83. 'rd_req': None,
  84. 'rd_bytes': None,
  85. 'wr_req': None,
  86. 'wr_bytes': None
  87. })
  88. if needs.__len__() > 0:
  89. t = needs.pop(0)
  90. else:
  91. t = now_ts
  92. data.append(row)
  93. return data
  94. if granularity == 'day':
  95. ret['data'] = smooth_data(boundary=_boundary, interval=600, now_ts=ts, rows=_rows)
  96. if granularity == 'seven_days':
  97. ret['data'] = smooth_data(boundary=_boundary, interval=600, now_ts=ts, rows=_rows)
  98. else:
  99. ret['data'] = smooth_data(boundary=_boundary, interval=60, now_ts=ts, rows=_rows)
  100. return ret
  101. except ji.PreviewingError, e:
  102. return json.loads(e.message)
  103. @Utils.dumps2response
  104. def r_cpu_memory_last_hour(uuid):
  105. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='hour')
  106. @Utils.dumps2response
  107. def r_cpu_memory_last_six_hours(uuid):
  108. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='six_hours')
  109. @Utils.dumps2response
  110. def r_cpu_memory_last_day(uuid):
  111. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='day')
  112. @Utils.dumps2response
  113. def r_cpu_memory_last_seven_days(uuid):
  114. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='seven_days')
  115. @Utils.dumps2response
  116. def r_traffic_last_hour(uuid):
  117. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='hour')
  118. @Utils.dumps2response
  119. def r_traffic_last_six_hours(uuid):
  120. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='six_hours')
  121. @Utils.dumps2response
  122. def r_traffic_last_day(uuid):
  123. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='day')
  124. @Utils.dumps2response
  125. def r_traffic_last_seven_days(uuid):
  126. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='seven_days')
  127. @Utils.dumps2response
  128. def r_disk_io_last_hour(uuid):
  129. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='hour')
  130. @Utils.dumps2response
  131. def r_disk_io_last_six_hours(uuid):
  132. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='six_hours')
  133. @Utils.dumps2response
  134. def r_disk_io_last_day(uuid):
  135. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='day')
  136. @Utils.dumps2response
  137. def r_disk_io_last_seven_days(uuid):
  138. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='seven_days')