aboutsummaryrefslogtreecommitdiffstats
path: root/web_utils.py
blob: 073076fc2c1af002f86971994a8954522da1fa80 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright (C) 2022 The Qt Company Ltd.
# Contact: https://www.qt.io/licensing/
#
# You may use this file under the terms of the CC0 license.
# See the file LICENSE.CC0 from this package for details.

import requests
from dash import dcc, html


def get_header():
    """
    This is in charge of return the divs that form the header,
    both the left logo/title and the right side menu.
    """
    return html.Div(
        children=[
            html.Div(
                children=[
                    html.A(
                        children=[
                            html.Img(src="/assets/theqtproject.png", className="header-logo")
                        ],
                        href="https://contribute.qt-project.org",
                    ),
                ],
                className="header-title-left",
            ),
            html.Div(
                children=[
                    html.A(children="Code Review", href="https://codereview.qt-project.org/"),
                    html.A(children="Bug Tracker", href="https://bugreports.qt.io"),
                    html.A(children="Wiki", href="https://wiki.qt.io"),
                    html.A(children="Docs", href="https://doc.qt.io"),
                    html.A(children="Mailing List", href="https://lists.qt-project.org"),
                    html.A(children="Forum", href="https://forum.qt.io"),
                    html.A(children="Qt.io", href="https://qt.io", style={"color": "#41CD52"}),
                ],
                className="header-menu-right",
            ),
        ],
        className="header",
    )


def get_column(divs=[], columns_number="six", classes=""):
    """
    This returns the left divs which contain explanatory text
    about different topics, defined by different markdown files
    on this project.
    """

    def get_div(div):
        content, color = div

        _style = {}
        if color is not None:
            _style = {"background-color": color}

        _classes = ["card", "pad"] + classes.split()

        return html.Div(
            children=[
                dcc.Markdown(content),
            ],
            className=" ".join(_classes),
            style=_style,
        )

    content = []
    for div in divs:
        if isinstance(div, tuple):
            content.append(get_div(div))
        else:
            content.append(div)

    return html.Div(children=content, className=f"{columns_number} columns")


def get_services_status():
    gerrit_url = "https://codereview.qt-project.org/projects/qt%2Fqtbase/HEAD"
    coin_url = "https://testresults.qt.io/coin/api/capabilities"

    def get_st(url):
        try:
            response = requests.get(url)
            if response.status_code == 200:
                return "Online   🟩"
            elif response.status_code == 404:
                return "Offline  🟥"
            else:
                return "Undefined  🟧"
        except Exception as e:
            print("Exception caught on the request:", e)
            return "Undefined  🟧"

    return html.Div(
        children=[
            html.H3(children="Services Status"),
            html.Div(
                children=[
                    html.Div(
                        children=[
                            html.B(
                                children="Gerrit: ",
                                style={"display": "inline-block", "margin-right": "5px"},
                            ),
                            html.A(
                                children=f"{get_st(gerrit_url)}",
                                href="https://codereview.qt-project.org",
                                style={"display": "inline-block"},
                            ),
                        ],
                    ),
                ],
                className="five columns",
            ),
            html.Div(
                children=[
                    html.Div(
                        children=[
                            html.B(
                                children="COIN: ",
                                style={"display": "inline-block", "margin-right": "5px"},
                            ),
                            html.A(
                                children=f"{get_st(coin_url)}",
                                href="https://testresults.qt.io",
                                style={"display": "inline-block"},
                            ),
                        ],
                    ),
                ],
                className="five columns",
            ),
        ],
        className="row card",
        style={"padding": "10px"},
    )


def get_file_content(filename, color=None):
    print(f"Reading '{filename}'...")
    content = ""
    with open(filename, "r") as f:
        content = f.read()
    return content, color


def get_filter(modules, years):
    """
    Get div containing the combobox to filter the modules
    and years, to trigger the charts update.
    """
    return html.Div(
        children=[
            html.Div(
                children=[
                    html.Div(
                        children=[
                            html.Div(children="Module", className="menu-title"),
                            dcc.Dropdown(
                                id="module-filter",
                                options=[{"label": m, "value": m} for m in modules],
                                value="qtbase",
                                clearable=False,
                                className="dropdown",
                            ),
                        ],
                    ),
                    dcc.Checklist(
                        id="tqtc-filter",
                        options=[
                            {"label": "Include commits from The Qt Company", "value": "TQtC"},
                        ],
                        value=["TQtC"],
                        className="tqtc-filter",
                    ),
                ],
                className="five columns",
            ),
            html.Div(
                children=[
                    html.Div(
                        children=[
                            html.Div(children="Starting year", className="menu-title"),
                            dcc.Dropdown(
                                id="year-filter",
                                options=[{"label": m, "value": m} for m in years],
                                value=2018,
                                clearable=False,
                                className="dropdown",
                            ),
                        ],
                    ),
                ],
                className="three columns",
            ),
            html.Div(
                children=[
                    html.Div(
                        children=[
                            html.Div(children="Contributors", className="menu-title"),
                            dcc.Input(
                                id="contributors-filter",
                                value=10,
                                min=5,
                                type="number",
                                className="input-number",
                            ),
                        ],
                    ),
                ],
                className="three columns",
            ),
        ],
        className="row card option-select",
    )