-
Notifications
You must be signed in to change notification settings - Fork 30
/
tasks.py
318 lines (291 loc) · 13.5 KB
/
tasks.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
from urllib.parse import parse_qs, urlparse
import logging
import requests
from audits.wpt_utils.status_and_info_extracter import extract_status_and_info
from audits.models import Audit, AuditResults, AuditStatusHistory, AvailableStatuses
from celery import shared_task
from projects.models import NetworkShapeOptions, Page, Script, AvailableAuditParameters
from audits.wpt_utils.normalizer import (
format_wpt_json_results_for_page,
format_wpt_json_results_for_script,
)
@shared_task
def request_audit(audit_uuid):
audit = Audit.objects.get(uuid=audit_uuid)
parameters = audit.parameters
audit_status_requested = AuditStatusHistory(
audit=audit, status=AvailableStatuses.REQUESTED.value, details="Audit requested"
)
audit_status_requested.save()
"""
See https://sites.google.com/a/webpagetest.org/docs/advanced-features/webpagetest-restful-apis
for all available parameters in the WebPageTest API
"""
payload = {
"f": "json",
"runs": 3,
"video": 1,
"location": f"{parameters.configuration.location}:{parameters.configuration.browser}.{NetworkShapeOptions[parameters.network_shape].value}",
}
if audit.page is not None:
payload["url"] = audit.page.url
payload["lighthouse"] = 1
payload["k"] = audit.page.project.wpt_api_key
wpt_instance_url = audit.page.project.wpt_instance_url
elif audit.script is not None:
payload["script"] = audit.script.script
payload["k"] = audit.script.project.wpt_api_key
wpt_instance_url = audit.script.project.wpt_instance_url
r = requests.post(f"{wpt_instance_url}/runtest.php", params=payload)
response = r.json()
if response["statusCode"] == 200:
audit_status_queueing = AuditStatusHistory(
audit=audit,
status=AvailableStatuses.QUEUEING.value,
details=str(response["data"]),
)
audit_status_queueing.save()
poll_audit_results.apply_async(
(audit_uuid, response["data"]["jsonUrl"]), countdown=15
)
elif response["statusCode"] == 400:
# Usually 400 errors come from empty script or exceeding the daily limit
audit_status_error = AuditStatusHistory(
audit=audit,
status=AvailableStatuses.ERROR.value,
details=str(response["statusText"]),
)
audit_status_error.save()
else:
audit_status_error = AuditStatusHistory(
audit=audit,
status=AvailableStatuses.ERROR.value,
details=str(response.dumps()),
)
audit_status_error.save()
@shared_task
def poll_audit_results(audit_uuid, json_url):
audit = Audit.objects.get(uuid=audit_uuid)
r = requests.get(json_url)
response = r.json()
status_code = response.get("statusCode") or response["data"].get("statusCode")
if status_code in [100, 101]:
api_response = str(response["data"]["statusText"])
status, info = extract_status_and_info(api_response)
audit_status_requested = AuditStatusHistory(
audit=audit, status=status, details=api_response, info=info
)
audit_status_requested.save()
poll_audit_results.apply_async((audit_uuid, json_url), countdown=15)
elif status_code == 200:
if audit.page is not None:
wpt_instance_url = audit.page.project.wpt_instance_url
elif audit.script is not None:
wpt_instance_url = audit.script.project.wpt_instance_url
parsed_url = urlparse(json_url)
test_id = parse_qs(parsed_url.query)["test"][0]
wpt_results_user_url = f"{wpt_instance_url}/result/{test_id}"
try:
if audit.page is not None:
project = audit.page.project
formatted_results_array = format_wpt_json_results_for_page(
response["data"]
)
elif audit.script is not None:
project = audit.script.project
formatted_results_array = format_wpt_json_results_for_script(
response["data"]
)
for formatted_results in formatted_results_array:
audit_results = AuditResults(
audit=audit,
wpt_results_json_url=json_url,
wpt_results_user_url=wpt_results_user_url,
wpt_metric_first_view_tti=formatted_results[
"wpt_metric_first_view_tti"
],
wpt_metric_repeat_view_tti=formatted_results[
"wpt_metric_repeat_view_tti"
],
wpt_metric_first_view_speed_index=formatted_results[
"wpt_metric_first_view_speed_index"
],
wpt_metric_repeat_view_speed_index=formatted_results[
"wpt_metric_repeat_view_speed_index"
],
wpt_metric_first_view_first_paint=formatted_results[
"wpt_metric_first_view_first_paint"
],
wpt_metric_repeat_view_first_paint=formatted_results[
"wpt_metric_repeat_view_first_paint"
],
wpt_metric_first_view_first_meaningful_paint=formatted_results[
"wpt_metric_first_view_first_meaningful_paint"
],
wpt_metric_repeat_view_first_meaningful_paint=formatted_results[
"wpt_metric_repeat_view_first_meaningful_paint"
],
wpt_metric_first_view_load_time=formatted_results[
"wpt_metric_first_view_load_time"
],
wpt_metric_repeat_view_load_time=formatted_results[
"wpt_metric_repeat_view_load_time"
],
wpt_metric_first_view_first_contentful_paint=formatted_results[
"wpt_metric_first_view_first_contentful_paint"
],
wpt_metric_repeat_view_first_contentful_paint=formatted_results[
"wpt_metric_repeat_view_first_contentful_paint"
],
wpt_metric_first_view_time_to_first_byte=formatted_results[
"wpt_metric_first_view_time_to_first_byte"
],
wpt_metric_repeat_view_time_to_first_byte=formatted_results[
"wpt_metric_repeat_view_time_to_first_byte"
],
wpt_metric_first_view_visually_complete=formatted_results[
"wpt_metric_first_view_visually_complete"
],
wpt_metric_repeat_view_visually_complete=formatted_results[
"wpt_metric_repeat_view_visually_complete"
],
wpt_metric_lighthouse_performance=formatted_results[
"wpt_metric_lighthouse_performance"
],
script_step_name=formatted_results.get("step_name"),
script_step_number=formatted_results.get("step_number"),
lh_metric_tti_displayed_value=formatted_results.get(
"lh_metric_tti_displayed_value"
),
lh_metric_tti_score=formatted_results.get("lh_metric_tti_score"),
lh_metric_first_contentful_paint_displayed_value=formatted_results.get(
"lh_metric_first_contentful_paint_displayed_value"
),
lh_metric_first_contentful_paint_score=formatted_results.get(
"lh_metric_first_contentful_paint_score"
),
lh_metric_speed_index_displayed_value=formatted_results.get(
"lh_metric_speed_index_displayed_value"
),
lh_metric_speed_index_score=formatted_results.get(
"lh_metric_speed_index_score"
),
lh_metric_first_meaningful_paint_displayed_value=formatted_results.get(
"lh_metric_first_meaningful_paint_displayed_value"
),
lh_metric_first_meaningful_paint_score=formatted_results.get(
"lh_metric_first_meaningful_paint_score"
),
lh_metric_first_cpu_idle_displayed_value=formatted_results.get(
"lh_metric_first_cpu_idle_displayed_value"
),
lh_metric_first_cpu_idle_score=formatted_results.get(
"lh_metric_first_cpu_idle_score"
),
lh_metric_max_potential_first_input_delay_displayed_value=formatted_results.get(
"lh_metric_max_potential_first_input_delay_displayed_value"
),
lh_metric_max_potential_first_input_delay_score=formatted_results.get(
"lh_metric_max_potential_first_input_delay_score"
),
)
audit_results.save()
project.screenshot_url = formatted_results["screenshot_url"]
project.save()
audit_status_success = AuditStatusHistory(
audit=audit,
status=AvailableStatuses.SUCCESS.value,
details=(
"Audit Successful! AuditResults uuid: %s" % str(audit_results.uuid)
),
)
audit_status_success.save()
AuditStatusHistory.objects.filter(audit=audit, status="PENDING").exclude(
uuid=audit_status_success.uuid
).delete()
except Exception:
logging.error("Could not parse audit result", stack_info=True)
audit_results = AuditResults(
audit=audit,
wpt_results_json_url=json_url,
wpt_results_user_url=wpt_results_user_url,
)
audit_results.save()
audit_status_error = AuditStatusHistory(
audit=audit,
status=AvailableStatuses.ERROR.value,
details=(
"Error while parsing the audit results from WPT. AuditResults uuid: %s"
% str(audit_results.uuid)
),
)
audit_status_error.save()
@shared_task
def request_all_audits():
pages = Page.objects.filter(project__is_active=True).iterator()
for page in pages:
audit_parameters_list = page.project.audit_parameters_list.all()
for audit_parameters in audit_parameters_list:
audit = Audit(page=page, parameters=audit_parameters)
audit.save()
request_audit.delay(audit.uuid)
scripts = Script.objects.filter(project__is_active=True).iterator()
for script in scripts:
audit_parameters_list = script.project.audit_parameters_list.all()
for audit_parameters in audit_parameters_list:
audit = Audit(script=script, parameters=audit_parameters)
audit.save()
request_audit.delay(audit.uuid)
@shared_task
def clean_old_audit_statuses():
for audit in Audit.objects.all():
statuses = AuditStatusHistory.objects.filter(audit=audit)
if AvailableStatuses.SUCCESS.value in statuses.values_list("status", flat=True):
statuses.filter(status=AvailableStatuses.PENDING.value).delete()
statuses.filter(status=AvailableStatuses.QUEUEING.value).delete()
statuses.filter(status=AvailableStatuses.RUNNING.value).delete()
@shared_task
def get_wpt_audit_configurations(wpt_instance_url="https://webpagetest.org"):
"""gets all the available locations from WPT"""
# For some reason, the key mask to get API-available locations is different between
# public and private WPT instances
wpt_key_mask = ""
if wpt_instance_url == "https://webpagetest.org":
wpt_key_mask = "A"
response = requests.get(
f"{wpt_instance_url}/getLocations.php?f=json&k={wpt_key_mask}"
)
if response.status_code != 200:
logging.error("Invalid response from WebPageTest API: non-200 response code")
raise Exception("Invalid response from WebPageTest API: non-200 response code")
try:
data = response.json()["data"]
except KeyError:
logging.error(
"Invalid response from WebPageTest API: 'data' key is not present"
)
raise Exception(
"Invalid response from WebPageTest API: 'data' key is not present"
)
for available_audit_parameter in AvailableAuditParameters.objects.filter(
wpt_instance_url=wpt_instance_url
):
available_audit_parameter.is_active = False
available_audit_parameter.save()
for location, location_data in data.items():
browsers = location_data["Browsers"].split(",")
group = location_data.get(
"group", ""
) # Private instances locations may not be grouped
label = location_data["labelShort"]
for browser in browsers:
configuration, created = AvailableAuditParameters.objects.update_or_create(
browser=browser,
location=location,
defaults={
"location_label": label,
"location_group": group,
"is_active": True,
},
wpt_instance_url=wpt_instance_url,
)