recentstatus.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2009 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 datetime
  29. from google.appengine.ext import webapp
  30. from google.appengine.ext.webapp import template
  31. from model.queues import Queue
  32. from model.queuestatus import QueueStatus
  33. from model.workitems import WorkItems
  34. class QueueBubble(object):
  35. """View support class for recentstatus.html"""
  36. def __init__(self, queue):
  37. self._queue = queue
  38. self._work_items = queue.work_items()
  39. self._last_status = QueueStatus.all().filter("queue_name =", queue.name()).order("-date").get()
  40. # FIXME: name and display_name should be replaced by a .queue() accessor.
  41. def name(self):
  42. return self._queue.name()
  43. def display_name(self):
  44. return self._queue.display_name()
  45. def _last_status_date(self):
  46. if not self._last_status:
  47. return None
  48. return self._last_status.date
  49. def last_heard_from(self):
  50. if not self._work_items:
  51. return self._last_status_date()
  52. return max(self._last_status_date(), self._work_items.date)
  53. def is_alive(self):
  54. if not self.last_heard_from():
  55. return False
  56. return self.last_heard_from() > (datetime.datetime.now() - datetime.timedelta(minutes=30))
  57. def status_class(self):
  58. if not self.is_alive():
  59. return "dead"
  60. if self.pending_items_count() > 1:
  61. return "behind"
  62. return "alive"
  63. def status_text(self):
  64. if not self._work_items:
  65. return "Offline"
  66. if not self._work_items.item_ids:
  67. return "Idle"
  68. return self._last_status.message
  69. def pending_items_count(self):
  70. if not self._work_items:
  71. return 0
  72. return len(self._work_items.item_ids)
  73. class QueuesOverview(webapp.RequestHandler):
  74. def get(self):
  75. template_values = {
  76. "queues": [QueueBubble(queue) for queue in Queue.all()],
  77. }
  78. self.response.out.write(template.render("templates/recentstatus.html", template_values))