queuecharts.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Copyright (C) 2013 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import calendar
  29. from datetime import datetime
  30. import itertools
  31. from time import time
  32. from google.appengine.ext import webapp
  33. from google.appengine.ext.webapp import template
  34. from config import logging, charts
  35. from model.patchlog import PatchLog
  36. from model.queues import Queue
  37. from model.queuelog import QueueLog
  38. class QueueCharts(webapp.RequestHandler):
  39. def get(self, queue_name):
  40. queue_name = queue_name.lower()
  41. if not Queue.queue_with_name(queue_name):
  42. self.error(404)
  43. return
  44. timestamp = self._get_timestamp()
  45. view_range = self._get_view_range()
  46. time_unit, time_unit_name = charts.get_time_unit(view_range)
  47. all_queue_names = map(Queue.name, Queue.all())
  48. template_values = {
  49. "all_queue_names": all_queue_names,
  50. "patch_data": self._get_patch_data(queue_name, timestamp, view_range),
  51. "queue_data": self._get_queue_data(queue_name, timestamp, view_range),
  52. "queue_name": queue_name,
  53. "seconds_ago_min": 0,
  54. "seconds_ago_max": view_range,
  55. "time_unit_name": time_unit_name,
  56. "time_unit": time_unit,
  57. "timestamp": timestamp,
  58. "view_range": view_range,
  59. "view_range_choices": charts.view_range_choices,
  60. }
  61. self.response.out.write(template.render("templates/queuecharts.html", template_values))
  62. @classmethod
  63. def _get_min_med_max(cls, values, defaults=(0, 0, 0)):
  64. if not values:
  65. return defaults
  66. length = len(values)
  67. sorted_values = sorted(values)
  68. return sorted_values[0], sorted_values[length / 2], sorted_values[length - 1]
  69. def _get_patch_data(self, queue_name, timestamp, view_range):
  70. patch_logs = self._get_patch_logs(queue_name, timestamp, view_range)
  71. patch_data = []
  72. for patch_log in patch_logs:
  73. if patch_log.process_duration and patch_log.wait_duration:
  74. patch_log_timestamp = calendar.timegm(patch_log.date.utctimetuple())
  75. patch_data.append({
  76. "attachment_id": patch_log.attachment_id,
  77. "seconds_ago": timestamp - patch_log_timestamp,
  78. "process_duration": patch_log.process_duration / charts.one_minute,
  79. "retry_count": patch_log.retry_count,
  80. "status_update_count": patch_log.status_update_count,
  81. "wait_duration": patch_log.wait_duration / charts.one_minute,
  82. })
  83. return patch_data
  84. def _get_patch_logs(self, queue_name, timestamp, view_range):
  85. patch_log_query = PatchLog.all()
  86. patch_log_query = patch_log_query.filter("queue_name =", queue_name)
  87. patch_log_query = patch_log_query.filter("date >=", datetime.utcfromtimestamp(timestamp - view_range))
  88. patch_log_query = patch_log_query.filter("date <=", datetime.utcfromtimestamp(timestamp))
  89. patch_log_query = patch_log_query.order("date")
  90. return patch_log_query.run(limit=charts.patch_log_limit)
  91. def _get_queue_data(self, queue_name, timestamp, view_range):
  92. queue_logs = self._get_queue_logs(queue_name, timestamp, view_range)
  93. queue_data = []
  94. for queue_log in queue_logs:
  95. queue_log_timestamp = calendar.timegm(queue_log.date.utctimetuple())
  96. p_min, p_med, p_max = self._get_min_med_max(queue_log.patch_process_durations)
  97. w_min, w_med, w_max = self._get_min_med_max(queue_log.patch_wait_durations)
  98. queue_data.append({
  99. "bots_seen": len(queue_log.bot_ids_seen),
  100. "seconds_ago": timestamp - queue_log_timestamp,
  101. "patch_processing_min": p_min,
  102. "patch_processing_med": p_med,
  103. "patch_processing_max": p_max,
  104. "patch_retry_count": queue_log.patch_retry_count,
  105. "patch_waiting_min": w_min,
  106. "patch_waiting_med": w_med,
  107. "patch_waiting_max": w_max,
  108. "patches_completed": len(queue_log.patch_process_durations),
  109. "patches_waiting": queue_log.max_patches_waiting,
  110. "status_update_count": queue_log.status_update_count,
  111. })
  112. return queue_data
  113. def _get_queue_logs(self, queue_name, timestamp, view_range):
  114. queue_logs = []
  115. current_timestamp = timestamp - view_range
  116. while current_timestamp <= timestamp:
  117. queue_logs.append(QueueLog.get_at(queue_name, logging.queue_log_duration, current_timestamp))
  118. current_timestamp += logging.queue_log_duration
  119. return queue_logs
  120. @classmethod
  121. def _get_time_unit(cls, view_range):
  122. if view_range > charts.one_day * 2:
  123. return
  124. def _get_timestamp(self):
  125. timestamp = self.request.get("timestamp")
  126. try:
  127. return int(timestamp)
  128. except ValueError:
  129. return int(time())
  130. def _get_view_range(self):
  131. view_range = self.request.get("view_range")
  132. try:
  133. return int(view_range)
  134. except ValueError:
  135. return charts.default_view_range