summaryrefslogtreecommitdiffstats
path: root/tools/tryselect/selectors/chooser/app.py
blob: d09e620517d17a45bd97c2f92e15859ed8943bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function

from abc import ABCMeta, abstractproperty
from collections import defaultdict

from flask import (
    Flask,
    render_template,
    request,
)

SECTIONS = []
SUPPORTED_KINDS = set()


def register_section(cls):
    assert issubclass(cls, Section)
    instance = cls()
    SECTIONS.append(instance)
    SUPPORTED_KINDS.update(instance.kind.split(","))


class Section(object):
    __metaclass__ = ABCMeta

    @abstractproperty
    def name(self):
        pass

    @abstractproperty
    def kind(self):
        pass

    @abstractproperty
    def title(self):
        pass

    @abstractproperty
    def attrs(self):
        pass

    def contains(self, task):
        return task.kind in self.kind.split(",")

    def get_context(self, tasks):
        labels = defaultdict(lambda: {"max_chunk": 0, "attrs": defaultdict(list)})

        for task in tasks.values():
            if not self.contains(task):
                continue

            task = task.attributes
            label = labels[self.labelfn(task)]
            for attr in self.attrs:
                if attr in task and task[attr] not in label["attrs"][attr]:
                    label["attrs"][attr].append(task[attr])

                if "test_chunk" in task:
                    label["max_chunk"] = max(
                        label["max_chunk"], int(task["test_chunk"])
                    )

        return {
            "name": self.name,
            "kind": self.kind,
            "title": self.title,
            "labels": labels,
        }


@register_section
class Platform(Section):
    name = "platform"
    kind = "build"
    title = "Platforms"
    attrs = ["build_platform"]

    def labelfn(self, task):
        return task["build_platform"]

    def contains(self, task):
        if not Section.contains(self, task):
            return False

        # android-stuff tasks aren't actual platforms
        return task.task["tags"].get("android-stuff", False) != "true"


@register_section
class Test(Section):
    name = "test"
    kind = "test"
    title = "Test Suites"
    attrs = ["unittest_suite"]

    def labelfn(self, task):
        suite = task["unittest_suite"].replace(" ", "-")

        if suite.endswith("-chunked"):
            suite = suite[: -len("-chunked")]

        return suite

    def contains(self, task):
        if not Section.contains(self, task):
            return False
        return task.attributes["unittest_suite"] not in ("raptor", "talos")


@register_section
class Perf(Section):
    name = "perf"
    kind = "test"
    title = "Performance"
    attrs = ["unittest_suite", "raptor_try_name", "talos_try_name"]

    def labelfn(self, task):
        suite = task["unittest_suite"]
        label = task["{}_try_name".format(suite)]

        if not label.startswith(suite):
            label = "{}-{}".format(suite, label)

        if label.endswith("-e10s"):
            label = label[: -len("-e10s")]

        return label

    def contains(self, task):
        if not Section.contains(self, task):
            return False
        return task.attributes["unittest_suite"] in ("raptor", "talos")


@register_section
class Analysis(Section):
    name = "analysis"
    kind = "build,static-analysis-autotest"
    title = "Analysis"
    attrs = ["build_platform"]

    def labelfn(self, task):
        return task["build_platform"]

    def contains(self, task):
        if not Section.contains(self, task):
            return False
        if task.kind == "build":
            return task.task["tags"].get("android-stuff", False) == "true"
        return True


def create_application(tg):
    tasks = {l: t for l, t in tg.tasks.items() if t.kind in SUPPORTED_KINDS}
    sections = [s.get_context(tasks) for s in SECTIONS]
    context = {
        "tasks": {l: t.attributes for l, t in tasks.items()},
        "sections": sections,
    }

    app = Flask(__name__)
    app.env = "development"
    app.tasks = []

    @app.route("/", methods=["GET", "POST"])
    def chooser():
        if request.method == "GET":
            return render_template("chooser.html", **context)

        if request.form["action"] == "Push":
            labels = request.form["selected-tasks"].splitlines()
            app.tasks.extend(labels)

        shutdown = request.environ.get("werkzeug.server.shutdown")
        if shutdown:
            shutdown()
        return render_template("close.html")

    return app