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
30# FIXME: This probably belongs in the buildbot module.
31class FailureMap(object):
32    def __init__(self):
33        self._failures = []
34
35    def add_regression_window(self, builder, regression_window):
36        self._failures.append({
37            'builder': builder,
38            'regression_window': regression_window,
39        })
40
41    def is_empty(self):
42        return not self._failures
43
44    def failing_revisions(self):
45        failing_revisions = [failure_info['regression_window'].revisions()
46                             for failure_info in self._failures]
47        return sorted(set(sum(failing_revisions, [])))
48
49    def builders_failing_for(self, revision):
50        return self._builders_failing_because_of([revision])
51
52    def tests_failing_for(self, revision):
53        tests = [failure_info['regression_window'].failing_tests()
54                 for failure_info in self._failures
55                 if revision in failure_info['regression_window'].revisions()
56                    and failure_info['regression_window'].failing_tests()]
57        result = set()
58        for test in tests:
59            result = result.union(test)
60        return sorted(result)
61
62    def _old_failures(self, is_old_failure):
63        return filter(lambda revision: is_old_failure(revision),
64                      self.failing_revisions())
65
66    def _builders_failing_because_of(self, revisions):
67        revision_set = set(revisions)
68        return [failure_info['builder'] for failure_info in self._failures
69                if revision_set.intersection(
70                    failure_info['regression_window'].revisions())]
71
72    # FIXME: We should re-process old failures after some time delay.
73    # https://bugs.webkit.org/show_bug.cgi?id=36581
74    def filter_out_old_failures(self, is_old_failure):
75        old_failures = self._old_failures(is_old_failure)
76        old_failing_builder_names = set([builder.name()
77            for builder in self._builders_failing_because_of(old_failures)])
78
79        # We filter out all the failing builders that could have been caused
80        # by old_failures.  We could miss some new failures this way, but
81        # emperically, this reduces the amount of spam we generate.
82        failures = self._failures
83        self._failures = [failure_info for failure_info in failures
84            if failure_info['builder'].name() not in old_failing_builder_names]
85        self._cache = {}
86