performance.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. try:
  39. ji.Check.previewing(args_rules, {'uuid': uuid})
  40. uuids_str = ':'.join([uuid_field, 'in', uuid])
  41. ret = dict()
  42. ret['state'] = ji.Common.exchange_state(20000)
  43. ret['data'] = list()
  44. max_limit = 10080
  45. ts = ji.Common.ts()
  46. _boundary = ts - 60 * 60
  47. if granularity == 'hour':
  48. _boundary = ts - 60 * 60
  49. elif granularity == 'six_hours':
  50. _boundary = ts - 60 * 60 * 6
  51. elif granularity == 'day':
  52. _boundary = ts - 60 * 60 * 24
  53. elif granularity == 'seven_days':
  54. _boundary = ts - 60 * 60 * 24 * 7
  55. else:
  56. pass
  57. filter_str = ';'.join([uuids_str, 'timestamp:gt:' + _boundary.__str__()])
  58. _rows, _rows_count = the_class.get_by_filter(
  59. offset=0, limit=max_limit, order_by='id', order='asc', filter_str=filter_str)
  60. def smooth_data(boundary=0, interval=60, now_ts=ji.Common.ts(), rows=None):
  61. needs = list()
  62. data = list()
  63. for t in range(boundary, now_ts, interval):
  64. needs.append(t - t % interval)
  65. for row in rows:
  66. if row['timestamp'] % interval != 0:
  67. continue
  68. if needs.__len__() > 0:
  69. t = needs.pop(0)
  70. else:
  71. t = now_ts
  72. while t < row['timestamp']:
  73. data.append({
  74. 'timestamp': t,
  75. 'cpu_load': None
  76. })
  77. if needs.__len__() > 0:
  78. t = needs.pop(0)
  79. else:
  80. t = now_ts
  81. data.append(row)
  82. return data
  83. if granularity == 'day':
  84. ret['data'] = smooth_data(boundary=_boundary, interval=600, now_ts=ts, rows=_rows)
  85. if granularity == 'seven_days':
  86. ret['data'] = smooth_data(boundary=_boundary, interval=600, now_ts=ts, rows=_rows)
  87. else:
  88. ret['data'] = smooth_data(boundary=_boundary, interval=60, now_ts=ts, rows=_rows)
  89. return ret
  90. except ji.PreviewingError, e:
  91. return json.loads(e.message)
  92. @Utils.dumps2response
  93. def r_cpu_memory_last_hour(uuid):
  94. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='hour')
  95. @Utils.dumps2response
  96. def r_cpu_memory_last_six_hours(uuid):
  97. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='six_hours')
  98. @Utils.dumps2response
  99. def r_cpu_memory_last_day(uuid):
  100. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='day')
  101. @Utils.dumps2response
  102. def r_cpu_memory_last_seven_days(uuid):
  103. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='seven_days')
  104. @Utils.dumps2response
  105. def r_traffic_last_hour(uuid):
  106. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='hour')
  107. @Utils.dumps2response
  108. def r_traffic_last_six_hours(uuid):
  109. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='six_hours')
  110. @Utils.dumps2response
  111. def r_traffic_last_day(uuid):
  112. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='day')
  113. @Utils.dumps2response
  114. def r_traffic_last_seven_days(uuid):
  115. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='seven_days')
  116. @Utils.dumps2response
  117. def r_disk_io_last_hour(uuid):
  118. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='hour')
  119. @Utils.dumps2response
  120. def r_disk_io_last_six_hours(uuid):
  121. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='six_hours')
  122. @Utils.dumps2response
  123. def r_disk_io_last_day(uuid):
  124. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='day')
  125. @Utils.dumps2response
  126. def r_disk_io_last_seven_days(uuid):
  127. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='seven_days')