Add monitoring of patches and queues to the QueueStatusServer
[WebKit-https.git] / Tools / QueueStatusServer / loggers / recordpatchevent.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 config.logging import queue_log_duration
30 from model.patchlog import PatchLog
31 from model.queuelog import QueueLog
32 from model.workitems import WorkItems
33 from model.activeworkitems import ActiveWorkItems
34
35
36 class RecordPatchEvent(object):
37     @classmethod
38     def added(cls, attachment_id, queue_name):
39         PatchLog.lookup(attachment_id, queue_name)
40         queue_log = QueueLog.get_current(queue_name, queue_log_duration)
41         patches_waiting = cls._get_patches_waiting(queue_name)
42         if patches_waiting and queue_log.max_patches_waiting < patches_waiting:
43             queue_log.max_patches_waiting = patches_waiting
44             queue_log.put()
45
46     @classmethod
47     def retrying(cls, attachment_id, queue_name, bot_id=None):
48         patch_log = PatchLog.lookup(attachment_id, queue_name)
49         if bot_id:
50             patch_log.bot_id = bot_id
51         patch_log.retry_count += 1
52         patch_log.put()
53
54         queue_log = QueueLog.get_current(queue_name, queue_log_duration)
55         queue_log.patch_retry_count += 1
56         queue_log.put()
57
58     @classmethod
59     def started(cls, attachment_id, queue_name, bot_id=None):
60         patch_log = PatchLog.lookup(attachment_id, queue_name)
61         if bot_id:
62             patch_log.bot_id = bot_id
63         patch_log.calculate_wait_duration()
64         patch_log.put()
65
66         queue_log = QueueLog.get_current(queue_name, queue_log_duration)
67         queue_log.patch_wait_durations.append(patch_log.wait_duration)
68         queue_log.put()
69
70     @classmethod
71     def stopped(cls, attachment_id, queue_name, bot_id=None):
72         patch_log = PatchLog.lookup(attachment_id, queue_name)
73         if bot_id:
74             patch_log.bot_id = bot_id
75         patch_log.finished = True
76         patch_log.calculate_process_duration()
77         patch_log.put()
78
79         if patch_log.process_duration:
80             queue_log = QueueLog.get_current(queue_name, queue_log_duration)
81             queue_log.patch_process_durations.append(patch_log.process_duration)
82             queue_log.put()
83
84     @classmethod
85     def updated(cls, attachment_id, queue_name, bot_id=None):
86         patch_log = PatchLog.lookup(attachment_id, queue_name)
87         if bot_id:
88             patch_log.bot_id = bot_id
89         patch_log.status_update_count += 1
90         patch_log.put()
91
92         queue_log = QueueLog.get_current(queue_name, queue_log_duration)
93         queue_log.status_update_count += 1
94         queue_log.put()
95
96     @classmethod
97     def _get_patches_waiting(cls, queue_name):
98         work_items = WorkItems.lookup_by_queue(queue_name)
99         active_work_items = ActiveWorkItems.lookup_by_queue(queue_name)
100         if work_items and active_work_items:
101             return len(set(work_items.item_ids) - set(active_work_items.item_ids))