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