Home | History | Annotate | Download | only in handlers
      1 # Copyright (C) 2010 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 import itertools
     31 
     32 from google.appengine.ext import webapp
     33 from google.appengine.ext.webapp import template
     34 
     35 from model.queues import Queue
     36 from model import queuestatus
     37 
     38 
     39 class QueueStatus(webapp.RequestHandler):
     40     def _rows_for_work_items(self, queue):
     41         queued_items = queue.work_items()
     42         active_items = queue.active_work_items()
     43         if not queued_items:
     44             return []
     45         rows = []
     46         for item_id in queued_items.item_ids:
     47             rows.append({
     48                 "attachment_id": item_id,
     49                 "bug_id": 1,
     50                 "lock_time": active_items and active_items.time_for_item(item_id),
     51             })
     52         return rows
     53 
     54     def _grouping_key_for_status(self, status):
     55         return "%s-%s" % (status.active_patch_id, status.bot_id)
     56 
     57     def _build_status_groups(self, statuses):
     58         return [list(group) for key, group in itertools.groupby(statuses, self._grouping_key_for_status)]
     59 
     60     def _fetch_statuses(self, queue, bot_id):
     61         statuses = queuestatus.QueueStatus.all()
     62         statuses.filter("queue_name =", queue.name())
     63         if bot_id:
     64             statuses.filter("bot_id =", bot_id)
     65         return statuses.order("-date").fetch(15)
     66 
     67     def _fetch_last_message_matching(self, queue, bot_id, message):
     68         statuses = queuestatus.QueueStatus.all()
     69         statuses.filter("queue_name =", queue.name())
     70         if bot_id:
     71             statuses.filter("bot_id =", bot_id)
     72         statuses.filter("message =", message)
     73         return statuses.order("-date").get()
     74 
     75     def _fetch_trailing_days_pass_count(self, queue, bot_id, days):
     76         statuses = queuestatus.QueueStatus.all()
     77         statuses.filter("queue_name =", queue.name())
     78         days_ago = datetime.datetime.now() - datetime.timedelta(days=days)
     79         statuses.filter("date >", days_ago)
     80         if bot_id:
     81             statuses.filter("bot_id =", bot_id)
     82         statuses.filter("message =", "Pass")
     83         return statuses.count()
     84 
     85     def _page_title(self, queue, bot_id):
     86         title = "%s Messages" % queue.display_name()
     87         if bot_id:
     88             title += " from \"%s\"" % (bot_id)
     89         return title
     90 
     91     def get(self, queue_name, bot_id=None):
     92         queue_name = queue_name.lower()
     93         queue = Queue.queue_with_name(queue_name)
     94         if not queue:
     95             self.error(404)
     96             return
     97 
     98         statuses = self._fetch_statuses(queue, bot_id)
     99         template_values = {
    100             "page_title": self._page_title(queue, bot_id),
    101             "work_item_rows": self._rows_for_work_items(queue),
    102             "status_groups": self._build_status_groups(statuses),
    103             "bot_id": bot_id,
    104             "last_pass": self._fetch_last_message_matching(queue, bot_id, "Pass"),
    105             "last_boot": self._fetch_last_message_matching(queue, bot_id, "Starting Queue"),
    106             "trailing_month_pass_count": self._fetch_trailing_days_pass_count(queue, bot_id, 30),
    107             "trailing_week_pass_count": self._fetch_trailing_days_pass_count(queue, bot_id, 7),
    108         }
    109         self.response.out.write(template.render("templates/queuestatus.html", template_values))
    110