webkit-queues PatchLog is incorrect for patches that took less than a second
[WebKit-https.git] / Tools / QueueStatusServer / model / queuelog.py
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
29 from time import time
30 from datetime import datetime
31
32 from google.appengine.ext import db
33
34 from model.workitems import WorkItems
35 from model.activeworkitems import ActiveWorkItems
36
37
38 class QueueLog(db.Model):
39     date = db.DateTimeProperty()
40     # duration specifies in seconds the time period these log values apply to.
41     duration = db.IntegerProperty()
42     queue_name = db.StringProperty()
43     bot_ids_seen = db.StringListProperty()
44     max_patches_waiting = db.IntegerProperty(default=0)
45     patch_wait_durations = db.ListProperty(int)
46     patch_process_durations = db.ListProperty(int)
47     patch_retry_count = db.IntegerProperty(default=0)
48     status_update_count = db.IntegerProperty(default=0)
49
50     @staticmethod
51     def create_key(queue_name, duration, timestamp):
52         return "%s-%s-%s" % (queue_name, duration, timestamp)
53
54     @classmethod
55     def get_at(cls, queue_name, duration, timestamp):
56         timestamp = int(timestamp / duration) * duration
57         date = datetime.utcfromtimestamp(timestamp)
58         key = cls.create_key(queue_name, duration, timestamp)
59         return cls.get_or_create(key, date=date, duration=duration, queue_name=queue_name)
60
61     @classmethod
62     def get_current(cls, queue_name, duration):
63         return cls.get_at(queue_name, duration, time())
64
65     # This is to prevent page requests from generating lots of rows in the database.
66     @classmethod
67     def get_or_create(cls, key_name, **kwargs):
68         return db.run_in_transaction(cls._get_or_create_txn, key_name, **kwargs)
69
70     def update_max_patches_waiting(self):
71         patches_waiting = self._get_patches_waiting(self.queue_name)
72         if patches_waiting > self.max_patches_waiting:
73             self.max_patches_waiting = patches_waiting
74             return True
75         return False
76
77     @classmethod
78     def _get_or_create_txn(cls, key_name, **kwargs):
79         entity = cls.get_by_key_name(key_name, parent=kwargs.get('parent'))
80         if entity is None:
81             entity = cls(key_name=key_name, **kwargs)
82         return entity
83
84     @classmethod
85     def _get_patches_waiting(cls, queue_name):
86         work_items = WorkItems.lookup_by_queue(queue_name)
87         active_work_items = ActiveWorkItems.lookup_by_queue(queue_name)
88         return len(set(work_items.item_ids) - set(active_work_items.item_ids))