/
jobs.py
469 lines (418 loc) · 17.6 KB
/
jobs.py
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import datetime
import logging
import django_filters
from dateutil import parser
from django.core.exceptions import ObjectDoesNotExist
from django.db import models as django_models
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND
from treeherder.model.error_summary import get_error_summary
from treeherder.model.models import (
Job,
JobLog,
OptionCollection,
Repository,
TextLogError,
TextLogStep,
)
from treeherder.webapp.api import pagination, serializers
from treeherder.webapp.api.utils import CharInFilter, NumberInFilter, to_timestamp
logger = logging.getLogger(__name__)
class JobFilter(django_filters.FilterSet):
"""
We use this gigantic class to provide the same filtering interface
as the previous jobs API
"""
id = django_filters.NumberFilter(field_name="id")
id__in = NumberInFilter(field_name="id", lookup_expr="in")
tier__in = NumberInFilter(field_name="tier", lookup_expr="in")
push_id__in = NumberInFilter(field_name="push_id", lookup_expr="in")
job_guid = django_filters.CharFilter(field_name="guid")
job_guid__in = CharInFilter(field_name="guid", lookup_expr="in")
build_architecture = django_filters.CharFilter(field_name="build_platform__architecture")
build_os = django_filters.CharFilter(field_name="build_platform__os_name")
build_platform = django_filters.CharFilter(field_name="build_platform__platform")
build_system_type = django_filters.CharFilter(field_name="signature__build_system_type")
job_group_id = django_filters.NumberFilter(field_name="job_group_id")
job_group_name = django_filters.CharFilter(field_name="job_group__name")
job_group_symbol = django_filters.CharFilter(field_name="job_group__symbol")
job_type_name = django_filters.CharFilter(field_name="job_type__name")
job_type_symbol = django_filters.CharFilter(field_name="job_type__symbol")
machine_name = django_filters.CharFilter(field_name="machine__name")
machine_platform_architecture = django_filters.CharFilter(
field_name="machine_platform__architecture"
)
machine_platform_os = django_filters.CharFilter(field_name="machine_platform__os_name")
platform = django_filters.CharFilter(field_name="machine_platform__platform")
ref_data_name = django_filters.CharFilter(field_name="signature__name")
signature = django_filters.CharFilter(field_name="signature__signature")
task_id = django_filters.CharFilter(field_name="taskcluster_metadata__task_id")
retry_id = django_filters.NumberFilter(field_name="taskcluster_metadata__retry_id")
class Meta:
model = Job
fields = {
"option_collection_hash": ["exact"],
"build_platform_id": ["exact"],
"failure_classification_id": ["exact"],
"job_type_id": ["exact"],
"job_group_id": ["exact"],
"reason": ["exact"],
"state": ["exact"],
"result": ["exact"],
"who": ["exact"],
"tier": ["lt", "lte", "exact", "gt", "gte"],
"id": ["lt", "lte", "exact", "gt", "gte"],
"push_id": ["lt", "lte", "exact", "gt", "gte"],
"last_modified": ["lt", "lte", "exact", "gt", "gte"],
"submit_time": ["lt", "lte", "exact", "gt", "gte"],
"start_time": ["lt", "lte", "exact", "gt", "gte"],
"end_time": ["lt", "lte", "exact", "gt", "gte"],
}
filter_overrides = {
django_models.DateTimeField: {"filter_class": django_filters.IsoDateTimeFilter}
}
class JobsViewSet(viewsets.ReadOnlyModelViewSet):
"""
This viewset is the jobs endpoint.
"""
_default_select_related = [
"job_type",
"job_group",
"machine_platform",
"signature",
"taskcluster_metadata",
"push",
]
_query_field_names = [
"submit_time",
"start_time",
"end_time",
"failure_classification_id",
"id",
"job_group__name",
"job_group__symbol",
"job_type__name",
"job_type__symbol",
"last_modified",
"option_collection_hash",
"machine_platform__platform",
"option_collection_hash",
"push_id",
"push__revision",
"result",
"signature__signature",
"state",
"tier",
"taskcluster_metadata__task_id",
"taskcluster_metadata__retry_id",
]
_output_field_names = [
"failure_classification_id",
"id",
"job_group_name",
"job_group_symbol",
"job_type_name",
"job_type_symbol",
"last_modified",
"platform",
"push_id",
"push_revision",
"result",
"signature",
"state",
"tier",
"task_id",
"retry_id",
"duration",
"platform_option",
]
queryset = (
Job.objects.all()
.order_by("id")
.select_related(*_default_select_related)
.values(*_query_field_names)
)
serializer_class = serializers.JobSerializer
filterset_class = JobFilter
pagination_class = pagination.JobPagination
def get_serializer_context(self):
option_collection_map = OptionCollection.objects.get_option_collection_map()
return {"option_collection_map": option_collection_map}
def list(self, request, *args, **kwargs):
resp = super().list(request, *args, **kwargs)
resp.data["job_property_names"] = self._output_field_names
return Response(resp.data)
class JobsProjectViewSet(viewsets.ViewSet):
"""
This viewset is the project bound version of the jobs endpoint.
"""
# data that we want to do select_related on when returning job objects
# (so we don't have a zillion db queries)
_default_select_related = [
"build_platform",
"job_type",
"job_group",
"machine_platform",
"machine",
"signature",
"repository",
"taskcluster_metadata",
]
_property_query_mapping = [
("build_architecture", "build_platform__architecture", None),
("build_os", "build_platform__os_name", None),
("build_platform", "build_platform__platform", None),
("build_platform_id", "build_platform_id", None),
("build_system_type", "signature__build_system_type", None),
("end_timestamp", "end_time", to_timestamp),
("failure_classification_id", "failure_classification_id", None),
("id", "id", None),
("job_group_description", "job_group__description", None),
("job_group_id", "job_group_id", None),
("job_group_name", "job_group__name", None),
("job_group_symbol", "job_group__symbol", None),
("job_guid", "guid", None),
("job_type_description", "job_type__description", None),
("job_type_id", "job_type_id", None),
("job_type_name", "job_type__name", None),
("job_type_symbol", "job_type__symbol", None),
("last_modified", "last_modified", None),
("machine_name", "machine__name", None),
("machine_platform_architecture", "machine_platform__architecture", None),
("machine_platform_os", "machine_platform__os_name", None),
("option_collection_hash", "option_collection_hash", None),
("platform", "machine_platform__platform", None),
("push_id", "push_id", None),
("reason", "reason", None),
("ref_data_name", "signature__name", None),
("result", "result", None),
("result_set_id", "push_id", None),
("signature", "signature__signature", None),
("start_timestamp", "start_time", to_timestamp),
("state", "state", None),
("submit_timestamp", "submit_time", to_timestamp),
("tier", "tier", None),
("who", "who", None),
("task_id", "taskcluster_metadata__task_id", None),
("retry_id", "taskcluster_metadata__retry_id", None),
]
_option_collection_hash_idx = [pq[0] for pq in _property_query_mapping].index(
"option_collection_hash"
)
def _get_job_list_response(self, job_qs, offset, count, return_type):
"""
custom method to serialize + format jobs information
It's worth doing this big ugly thing (as opposed to using
the django rest framework serializer or whatever) as
this function is often in the critical path
"""
option_collection_map = OptionCollection.objects.get_option_collection_map()
results = []
for values in job_qs[offset : (offset + count)].values_list(
*[pq[1] for pq in self._property_query_mapping]
):
platform_option = option_collection_map.get(
values[self._option_collection_hash_idx], ""
)
# some values need to be transformed
values = list(values)
for i, _ in enumerate(values):
func = self._property_query_mapping[i][2]
if func:
values[i] = func(values[i])
# append results differently depending on if we are returning
# a dictionary or a list
if return_type == "dict":
results.append(
dict(
zip(
[pq[0] for pq in self._property_query_mapping] + ["platform_option"],
values + [platform_option],
)
)
)
else:
results.append(values + [platform_option])
response_dict = {"results": results}
if return_type == "list":
response_dict.update(
{
"job_property_names": [pq[0] for pq in self._property_query_mapping]
+ ["platform_option"]
}
)
return response_dict
def retrieve(self, request, project, pk=None):
"""
GET method implementation for detail view
Return a single job with log_references and
artifact names and links to the artifact blobs.
"""
try:
job = Job.objects.select_related(*self._default_select_related).get(
repository__name=project, id=pk
)
except Job.DoesNotExist:
return Response(f"No job with id: {pk}", status=HTTP_404_NOT_FOUND)
resp = serializers.JobProjectSerializer(job, read_only=True).data
resp["resource_uri"] = reverse("jobs-detail", kwargs={"project": project, "pk": pk})
resp["logs"] = []
for name, url in JobLog.objects.filter(job=job).values_list("name", "url"):
resp["logs"].append({"name": name, "url": url})
platform_option = job.get_platform_option()
if platform_option:
resp["platform_option"] = platform_option
try:
resp["task_id"] = job.taskcluster_metadata.task_id
resp["retry_id"] = job.taskcluster_metadata.retry_id
# Keep for backwards compatability
resp["taskcluster_metadata"] = {
"task_id": job.taskcluster_metadata.task_id,
"retry_id": job.taskcluster_metadata.retry_id,
}
except ObjectDoesNotExist:
pass
return Response(resp)
def list(self, request, project):
"""
GET method implementation for list view
Optional parameters (default):
- offset (0)
- count (10)
- return_type (dict)
"""
max_jobs_count = 2000
filter_params = {}
# various hacks to ensure API backwards compatibility
for param_key, param_value in request.query_params.items():
# replace `result_set_id` with `push_id`
if param_key.startswith("result_set_id"):
new_param_key = param_key.replace("result_set_id", "push_id")
filter_params[new_param_key] = param_value
# convert legacy timestamp parameters to time ones
elif param_key in ["submit_timestamp", "start_timestamp", "end_timestamp"]:
new_param_key = param_key.replace("timestamp", "time")
filter_params[new_param_key] = datetime.datetime.fromtimestamp(float(param_value))
# sanity check 'last modified'
elif param_key.startswith("last_modified"):
try:
parser.parse(param_value)
except ValueError:
return Response(
f"Invalid date value for `last_modified`: {param_value}",
status=HTTP_400_BAD_REQUEST,
)
filter_params[param_key] = param_value
# default case
else:
filter_params[param_key] = param_value
try:
offset = int(filter_params.get("offset", 0))
count = int(filter_params.get("count", 10))
except ValueError:
return Response("Invalid value for offset or count", status=HTTP_400_BAD_REQUEST)
return_type = filter_params.get("return_type", "dict").lower()
if count > max_jobs_count:
msg = f"Specified count exceeds API MAX_JOBS_COUNT value: {max_jobs_count}"
return Response({"detail": msg}, status=HTTP_400_BAD_REQUEST)
try:
repository = Repository.objects.get(name=project)
except Repository.DoesNotExist:
return Response(
{"detail": f"No project with name {project}"}, status=HTTP_404_NOT_FOUND
)
jobs = JobFilter(
{k: v for (k, v) in filter_params.items()},
queryset=Job.objects.filter(repository=repository).select_related(
*self._default_select_related
),
).qs
response_body = self._get_job_list_response(jobs, offset, count, return_type)
response_body["meta"] = dict(repository=project, offset=offset, count=count)
return Response(response_body)
# TODO remove
@action(detail=True, methods=["get"])
def text_log_steps(self, request, project, pk=None):
"""
Gets a list of steps associated with this job
"""
try:
job = Job.objects.get(repository__name=project, id=pk)
except ObjectDoesNotExist:
return Response(f"No job with id: {pk}", status=HTTP_404_NOT_FOUND)
textlog_steps = (
TextLogStep.objects.filter(job=job)
.order_by("started_line_number")
.prefetch_related("errors")
)
return Response(
serializers.TextLogStepSerializer(textlog_steps, many=True, read_only=True).data
)
@action(detail=True, methods=["get"])
def text_log_errors(self, request, project, pk=None):
"""
Gets a list of error lines associated with this job
"""
try:
job = Job.objects.get(repository__name=project, id=pk)
except Job.DoesNotExist:
return Response(f"No job with id: {pk}", status=HTTP_404_NOT_FOUND)
textlog_errors = (
TextLogError.objects.filter(job=job)
.select_related("_metadata", "_metadata__failure_line")
.prefetch_related("classified_failures", "matches")
.order_by("id")
)
return Response(
serializers.TextLogErrorSerializer(textlog_errors, many=True, read_only=True).data
)
@action(detail=True, methods=["get"])
def bug_suggestions(self, request, project, pk=None):
"""
Gets a set of bug suggestions for this job
"""
try:
job = Job.objects.get(repository__name=project, id=pk)
except ObjectDoesNotExist:
return Response(f"No job with id: {pk}", status=HTTP_404_NOT_FOUND)
return Response(get_error_summary(job))
@action(detail=True, methods=["get"])
def similar_jobs(self, request, project, pk=None):
"""
Get a list of jobs similar to the one selected.
"""
try:
repository = Repository.objects.get(name=project)
except Repository.DoesNotExist:
return Response(
{"detail": f"No project with name {project}"}, status=HTTP_404_NOT_FOUND
)
try:
job = Job.objects.get(repository=repository, id=pk)
except ObjectDoesNotExist:
return Response(f"No job with id: {pk}", status=HTTP_404_NOT_FOUND)
filter_params = request.query_params.copy()
try:
offset = int(filter_params.get("offset", 0))
# we don't need a big page size on this endoint,
# let's cap it to 50 elements
count = int(filter_params.get("count", 50))
except ValueError:
return Response("Invalid value for offset or count", status=HTTP_400_BAD_REQUEST)
return_type = filter_params.get("return_type", "dict").lower()
nosuccess = filter_params.get("nosuccess", False)
jobs = JobFilter(
{k: v for (k, v) in filter_params.items()},
queryset=Job.objects.filter(job_type_id=job.job_type_id, repository=repository)
.exclude(id=job.id)
.exclude(result=("success" if nosuccess is not False else None))
.select_related(*self._default_select_related),
).qs
# similar jobs we want in descending order from most recent
jobs = jobs.order_by("-push_id", "-start_time")
response_body = self._get_job_list_response(jobs, offset, count, return_type)
response_body["meta"] = dict(offset=offset, count=count, repository=project)
return Response(response_body)