performance.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. filter_str = uuids_str
  42. ret = dict()
  43. ret['state'] = ji.Common.exchange_state(20000)
  44. ret['data'] = list()
  45. limit = 60
  46. if granularity == 'hour':
  47. limit = 60
  48. elif granularity == 'six_hours':
  49. limit = 60 * 6
  50. elif granularity == 'day':
  51. limit = 60 * 24
  52. elif granularity == 'seven_days':
  53. limit = 60 * 24 * 7
  54. else:
  55. pass
  56. rows, rows_count = the_class.get_by_filter(
  57. offset=0, limit=limit, order_by='id', order='desc', filter_str=filter_str)
  58. if granularity in ['day', 'seven_days']:
  59. for row in rows:
  60. if row['timestamp'] % 600 != 0:
  61. continue
  62. ret['data'].append(row)
  63. else:
  64. ret['data'] = rows
  65. return ret
  66. except ji.PreviewingError, e:
  67. return json.loads(e.message)
  68. @Utils.dumps2response
  69. def r_cpu_memory_last_hour(uuid):
  70. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='hour')
  71. @Utils.dumps2response
  72. def r_cpu_memory_last_six_hours(uuid):
  73. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='six_hours')
  74. @Utils.dumps2response
  75. def r_cpu_memory_last_day(uuid):
  76. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='day')
  77. @Utils.dumps2response
  78. def r_cpu_memory_last_seven_days(uuid):
  79. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=CPUMemory, granularity='seven_days')
  80. @Utils.dumps2response
  81. def r_traffic_last_hour(uuid):
  82. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='hour')
  83. @Utils.dumps2response
  84. def r_traffic_last_six_hours(uuid):
  85. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='six_hours')
  86. @Utils.dumps2response
  87. def r_traffic_last_day(uuid):
  88. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='day')
  89. @Utils.dumps2response
  90. def r_traffic_last_seven_days(uuid):
  91. return get_performance_data(uuid=uuid, uuid_field='guest_uuid', the_class=Traffic, granularity='seven_days')
  92. @Utils.dumps2response
  93. def r_disk_io_last_hour(uuid):
  94. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='hour')
  95. @Utils.dumps2response
  96. def r_disk_io_last_six_hours(uuid):
  97. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='six_hours')
  98. @Utils.dumps2response
  99. def r_disk_io_last_day(uuid):
  100. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='day')
  101. @Utils.dumps2response
  102. def r_disk_io_last_seven_days(uuid):
  103. return get_performance_data(uuid=uuid, uuid_field='disk_uuid', the_class=DiskIO, granularity='seven_days')