-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathmodels.py
More file actions
394 lines (296 loc) · 11.5 KB
/
models.py
File metadata and controls
394 lines (296 loc) · 11.5 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# -*- coding: utf-8 -*-
# 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/.
import logging
import os
from datetime import timedelta
from functools import lru_cache
from typing import Sequence
from urllib.parse import urlparse
import orjson
import requests
import zstandard
from redis import Redis
from bugbug import bugzilla, repository, test_scheduling
from bugbug.github import Github
from bugbug.model import Model
from bugbug.models import testselect
from bugbug.utils import get_hgmo_stack
from bugbug_http.readthrough_cache import ReadthroughTTLCache
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger()
MODELS_NAMES = [
"defectenhancementtask",
"component",
"invalidcompatibilityreport",
"needsdiagnosis",
"regression",
"stepstoreproduce",
"spambug",
"testlabelselect",
"testgroupselect",
"accessibility",
"performancebug",
"worksforme",
]
DEFAULT_EXPIRATION_TTL = 7 * 24 * 3600 # A week
url = urlparse(os.environ.get("REDIS_URL", "redis://localhost/0"))
assert url.hostname is not None
redis = Redis(
host=url.hostname,
port=url.port if url.port is not None else 6379,
password=url.password,
ssl=True if url.scheme == "rediss" else False,
ssl_cert_reqs=None,
)
MODEL_CACHE: ReadthroughTTLCache[str, Model] = ReadthroughTTLCache(
timedelta(hours=1), lambda m: Model.load(f"{m}model")
)
MODEL_CACHE.start_ttl_thread()
cctx = zstandard.ZstdCompressor(level=10)
def setkey(key: str, value: bytes, compress: bool = False) -> None:
LOGGER.debug(f"Storing data at {key}: {value!r}")
if compress:
value = cctx.compress(value)
redis.set(key, value)
redis.expire(key, DEFAULT_EXPIRATION_TTL)
def classify_bug(model_name: str, bug_ids: Sequence[int], bugzilla_token: str) -> str:
from bugbug_http.app import JobInfo
# This should be called in a process worker so it should be safe to set
# the token here
bug_ids_set = set(map(int, bug_ids))
bugzilla.set_token(bugzilla_token)
bugs = bugzilla.get(bug_ids)
missing_bugs = bug_ids_set.difference(bugs.keys())
for bug_id in missing_bugs:
job = JobInfo(classify_bug, model_name, bug_id)
# TODO: Find a better error format
setkey(job.result_key, orjson.dumps({"available": False}))
if not bugs:
return "NOK"
model = MODEL_CACHE.get(model_name)
if not model:
LOGGER.info("Missing model %r, aborting" % model_name)
return "NOK"
model_extra_data = model.get_extra_data()
# TODO: Classify could choke on a single bug which could make the whole
# job to fails. What should we do here?
probs = model.classify(list(bugs.values()), True)
indexes = probs.argmax(axis=-1)
suggestions = model.le.inverse_transform(indexes)
probs_list = probs.tolist()
indexes_list = indexes.tolist()
suggestions_list = suggestions.tolist()
for i, bug_id in enumerate(bugs.keys()):
data = {
"prob": probs_list[i],
"index": indexes_list[i],
"class": suggestions_list[i],
"extra_data": model_extra_data,
}
job = JobInfo(classify_bug, model_name, bug_id)
setkey(job.result_key, orjson.dumps(data), compress=True)
# Save the bug last change
setkey(job.change_time_key, bugs[bug_id]["last_change_time"].encode())
return "OK"
def classify_issue(
model_name: str, owner: str, repo: str, issue_nums: Sequence[int]
) -> str:
from bugbug_http.app import JobInfo
github = Github(owner=owner, repo=repo)
issue_ids_set = set(map(int, issue_nums))
issues = {
issue_num: github.fetch_issue_by_number(owner, repo, issue_num, True)
for issue_num in issue_nums
}
missing_issues = issue_ids_set.difference(issues.keys())
for issue_id in missing_issues:
job = JobInfo(classify_issue, model_name, owner, repo, issue_id)
# TODO: Find a better error format
setkey(job.result_key, orjson.dumps({"available": False}))
if not issues:
return "NOK"
model = MODEL_CACHE.get(model_name)
if not model:
LOGGER.info("Missing model %r, aborting" % model_name)
return "NOK"
model_extra_data = model.get_extra_data()
# TODO: Classify could choke on a single bug which could make the whole
# job to fail. What should we do here?
probs = model.classify(list(issues.values()), True)
indexes = probs.argmax(axis=-1)
suggestions = model.le.inverse_transform(indexes)
probs_list = probs.tolist()
indexes_list = indexes.tolist()
suggestions_list = suggestions.tolist()
for i, issue_id in enumerate(issues.keys()):
data = {
"prob": probs_list[i],
"index": indexes_list[i],
"class": suggestions_list[i],
"extra_data": model_extra_data,
}
job = JobInfo(classify_issue, model_name, owner, repo, issue_id)
setkey(job.result_key, orjson.dumps(data), compress=True)
# Save the bug last change
setkey(job.change_time_key, issues[issue_id]["updated_at"].encode())
return "OK"
def classify_comment(
model_name: str, comment_ids: Sequence[int], bugzilla_token: str
) -> str:
from bugbug_http.app import JobInfo
# This should be called in a process worker so it should be safe to set
# the token here
comment_ids_set = set(map(int, comment_ids))
bugzilla.set_token(bugzilla_token)
comments = {
comment_id: bugzilla.get_comment(comment_id).values()
for comment_id in comment_ids
}
missing_comments = comment_ids_set.difference(comments.keys())
for comment_id in missing_comments:
job = JobInfo(classify_comment, model_name, comment_id)
# TODO: Find a better error format
setkey(job.result_key, orjson.dumps({"available": False}))
if not comments:
return "NOK"
model = MODEL_CACHE.get(model_name)
if not model:
LOGGER.info("Missing model %r, aborting" % model_name)
return "NOK"
model_extra_data = model.get_extra_data()
# TODO: Classify could choke on a single bug which could make the whole
# job to fails. What should we do here?
probs = model.classify(list(comments.values()), True)
indexes = probs.argmax(axis=-1)
suggestions = model.le.inverse_transform(indexes)
probs_list = probs.tolist()
indexes_list = indexes.tolist()
suggestions_list = suggestions.tolist()
for i, comment_id in enumerate(comments.keys()):
data = {
"prob": probs_list[i],
"index": indexes_list[i],
"class": suggestions_list[i],
"extra_data": model_extra_data,
}
job = JobInfo(classify_comment, model_name, comment_id)
setkey(job.result_key, orjson.dumps(data), compress=True)
# TODO: Save the comment last change
# We shall need to update one of the comment keys to show an updated comment
return "OK"
def classify_broken_site_report(model_name: str, reports_data: list[dict]) -> str:
from bugbug_http.app import JobInfo
reports = {
report["uuid"]: {"title": report["title"], "body": report["body"]}
for report in reports_data
}
if not reports:
return "NOK"
model = MODEL_CACHE.get(model_name)
if not model:
LOGGER.info("Missing model %r, aborting" % model_name)
return "NOK"
model_extra_data = model.get_extra_data()
probs = model.classify(list(reports.values()), True)
indexes = probs.argmax(axis=-1)
suggestions = model.le.inverse_transform(indexes)
probs_list = probs.tolist()
indexes_list = indexes.tolist()
suggestions_list = suggestions.tolist()
for i, report_uuid in enumerate(reports.keys()):
data = {
"prob": probs_list[i],
"index": indexes_list[i],
"class": suggestions_list[i],
"extra_data": model_extra_data,
}
job = JobInfo(classify_broken_site_report, model_name, report_uuid)
setkey(job.result_key, orjson.dumps(data), compress=True)
return "OK"
@lru_cache(maxsize=None)
def get_known_tasks() -> tuple[str, ...]:
with open("known_tasks", "r") as f:
return tuple(line.strip() for line in f)
def schedule_tests(branch: str, rev: str) -> str:
from bugbug_http import REPO_DIR
from bugbug_http.app import JobInfo
job = JobInfo(schedule_tests, branch, rev)
LOGGER.info("Processing %s...", job)
# Pull the revision to the local repository
LOGGER.info("Pulling commits from the remote repository...")
repository.pull(REPO_DIR, branch, rev)
# Load the full stack of patches leading to that revision
LOGGER.info("Loading commits to analyze using automationrelevance...")
try:
revs = get_hgmo_stack(branch, rev)
except requests.exceptions.RequestException:
LOGGER.warning(f"Push not found for {branch} @ {rev}!")
return "NOK"
test_selection_threshold = float(
os.environ.get("TEST_SELECTION_CONFIDENCE_THRESHOLD", 0.5)
)
# On "try", consider commits from other branches too (see https://bugzilla.mozilla.org/show_bug.cgi?id=1790493).
# On other repos, only consider "tip" commits (to exclude commits such as https://hg.mozilla.org/integration/autoland/rev/961f253985a4388008700a6a6fde80f4e17c0b4b).
if branch == "try":
repo_branch = None
else:
repo_branch = "tip"
# Analyze patches.
commits = repository.download_commits(
REPO_DIR,
revs=revs,
branch=repo_branch,
save=False,
use_single_process=True,
include_no_bug=True,
)
if len(commits) > 0:
testlabelselect_model = MODEL_CACHE.get("testlabelselect")
testgroupselect_model = MODEL_CACHE.get("testgroupselect")
tasks = testlabelselect_model.select_tests(commits, test_selection_threshold)
reduced = testselect.reduce_configs(
set(t for t, c in tasks.items() if c >= 0.8), 1.0
)
reduced_higher = testselect.reduce_configs(
set(t for t, c in tasks.items() if c >= 0.9), 1.0
)
groups = testgroupselect_model.select_tests(commits, test_selection_threshold)
config_groups = testselect.select_configs(groups.keys(), 0.9)
else:
tasks = {}
reduced = set()
groups = {}
config_groups = {}
data = {
"tasks": tasks,
"groups": groups,
"config_groups": config_groups,
"reduced_tasks": {t: c for t, c in tasks.items() if t in reduced},
"reduced_tasks_higher": {t: c for t, c in tasks.items() if t in reduced_higher},
"known_tasks": get_known_tasks(),
}
setkey(job.result_key, orjson.dumps(data), compress=True)
return "OK"
def get_config_specific_groups(config: str) -> str:
from bugbug_http.app import JobInfo
job = JobInfo(get_config_specific_groups, config)
LOGGER.info("Processing %s...", job)
equivalence_sets = testselect._get_equivalence_sets(0.9)
past_failures_data = test_scheduling.PastFailures("group", True)
setkey(
job.result_key,
orjson.dumps(
[
{"name": group}
for group in past_failures_data.all_runnables
if any(
equivalence_set == {config}
for equivalence_set in equivalence_sets[group]
)
]
),
compress=True,
)
return "OK"