08c57cdb6d6f09898303017b925b8743f3396d9f
[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.warninglog import WarningLog
33
34
35 class RecordPatchEvent(object):
36     @classmethod
37     def added(cls, attachment_id, queue_name):
38         PatchLog.lookup(attachment_id, queue_name)
39         queue_log = QueueLog.get_current(queue_name, queue_log_duration)
40         if queue_log.update_max_patches_waiting():
41             queue_log.put()
42
43     @classmethod
44     def retrying(cls, attachment_id, queue_name, bot_id=None):
45         patch_log = PatchLog.lookup_if_exists(attachment_id, queue_name)
46         if not patch_log:
47             WarningLog.record("patchlog missing", "In retrying event.", attachment_id, queue_name, bot_id)
48             return
49
50         if bot_id:
51             patch_log.bot_id = bot_id
52         patch_log.retry_count += 1
53         patch_log.put()
54
55         queue_log = QueueLog.get_current(queue_name, queue_log_duration)
56         queue_log.patch_retry_count += 1
57         queue_log.put()
58
59     @classmethod
60     def started(cls, attachment_id, queue_name, bot_id=None):
61         patch_log = PatchLog.lookup_if_exists(attachment_id, queue_name)
62         if not patch_log:
63             WarningLog.record("patchlog missing", "In started event.", attachment_id, queue_name, bot_id)
64             return
65
66         # An existing wait_duration implies the patch had been started previously and is being picked up again because it had expired.
67         if not patch_log.wait_duration:
68             if bot_id:
69                 patch_log.bot_id = bot_id
70             patch_log.calculate_wait_duration()
71             patch_log.put()
72
73             queue_log = QueueLog.get_current(queue_name, queue_log_duration)
74             queue_log.patch_wait_durations.append(patch_log.wait_duration)
75             queue_log.put()
76
77     @classmethod
78     def stopped(cls, attachment_id, queue_name, bot_id=None):
79         patch_log = PatchLog.lookup_if_exists(attachment_id, queue_name)
80         if not patch_log:
81             WarningLog.record("patchlog missing", "In stopped event.", attachment_id, queue_name, bot_id)
82             return
83
84         if not patch_log.wait_duration:
85             WarningLog.record("patchlog wait duration missing", "In stopped event.", attachment_id, queue_name, bot_id)
86             return
87
88         if not patch_log.finished:
89             if bot_id:
90                 patch_log.bot_id = bot_id
91             patch_log.finished = True
92             patch_log.calculate_process_duration()
93             patch_log.put()
94
95             queue_log = QueueLog.get_current(queue_name, queue_log_duration)
96             queue_log.patch_process_durations.append(patch_log.process_duration)
97             queue_log.put()
98
99     @classmethod
100     def updated(cls, attachment_id, queue_name, bot_id=None):
101         patch_log = PatchLog.lookup_if_exists(attachment_id, queue_name)
102         if not patch_log:
103             WarningLog.record("patchlog missing", "In updated event.", attachment_id, queue_name, bot_id)
104             return
105
106         if bot_id:
107             patch_log.bot_id = bot_id
108         patch_log.status_update_count += 1
109         patch_log.put()
110
111         queue_log = QueueLog.get_current(queue_name, queue_log_duration)
112         queue_log.status_update_count += 1
113         queue_log.put()
114