Skip to content

Commit e6582d1

Browse files
authored
feat: simplify modelizer (#238)
1 parent 5878845 commit e6582d1

File tree

2 files changed

+55
-129
lines changed

2 files changed

+55
-129
lines changed

backend/audits/tasks.py

+5-88
Original file line numberDiff line numberDiff line change
@@ -122,102 +122,19 @@ def poll_audit_results(audit_uuid, json_url, previous_api_response="", repeat_in
122122
formatted_results_array = format_wpt_json_results_for_script(
123123
response["data"]
124124
)
125+
125126
for formatted_results in formatted_results_array:
127+
screenshot_url = formatted_results.pop("screenshot_url")
128+
126129
audit_results = AuditResults(
127130
audit=audit,
128131
wpt_results_json_url=json_url,
129132
wpt_results_user_url=wpt_results_user_url,
130-
wpt_metric_first_view_tti=formatted_results[
131-
"wpt_metric_first_view_tti"
132-
],
133-
wpt_metric_repeat_view_tti=formatted_results[
134-
"wpt_metric_repeat_view_tti"
135-
],
136-
wpt_metric_first_view_speed_index=formatted_results[
137-
"wpt_metric_first_view_speed_index"
138-
],
139-
wpt_metric_repeat_view_speed_index=formatted_results[
140-
"wpt_metric_repeat_view_speed_index"
141-
],
142-
wpt_metric_first_view_first_paint=formatted_results[
143-
"wpt_metric_first_view_first_paint"
144-
],
145-
wpt_metric_repeat_view_first_paint=formatted_results[
146-
"wpt_metric_repeat_view_first_paint"
147-
],
148-
wpt_metric_first_view_first_meaningful_paint=formatted_results[
149-
"wpt_metric_first_view_first_meaningful_paint"
150-
],
151-
wpt_metric_repeat_view_first_meaningful_paint=formatted_results[
152-
"wpt_metric_repeat_view_first_meaningful_paint"
153-
],
154-
wpt_metric_first_view_load_time=formatted_results[
155-
"wpt_metric_first_view_load_time"
156-
],
157-
wpt_metric_repeat_view_load_time=formatted_results[
158-
"wpt_metric_repeat_view_load_time"
159-
],
160-
wpt_metric_first_view_first_contentful_paint=formatted_results[
161-
"wpt_metric_first_view_first_contentful_paint"
162-
],
163-
wpt_metric_repeat_view_first_contentful_paint=formatted_results[
164-
"wpt_metric_repeat_view_first_contentful_paint"
165-
],
166-
wpt_metric_first_view_time_to_first_byte=formatted_results[
167-
"wpt_metric_first_view_time_to_first_byte"
168-
],
169-
wpt_metric_repeat_view_time_to_first_byte=formatted_results[
170-
"wpt_metric_repeat_view_time_to_first_byte"
171-
],
172-
wpt_metric_first_view_visually_complete=formatted_results[
173-
"wpt_metric_first_view_visually_complete"
174-
],
175-
wpt_metric_repeat_view_visually_complete=formatted_results[
176-
"wpt_metric_repeat_view_visually_complete"
177-
],
178-
wpt_metric_lighthouse_performance=formatted_results[
179-
"wpt_metric_lighthouse_performance"
180-
],
181-
script_step_name=formatted_results.get("step_name"),
182-
script_step_number=formatted_results.get("step_number"),
183-
lh_metric_tti_displayed_value=formatted_results.get(
184-
"lh_metric_tti_displayed_value"
185-
),
186-
lh_metric_tti_score=formatted_results.get("lh_metric_tti_score"),
187-
lh_metric_first_contentful_paint_displayed_value=formatted_results.get(
188-
"lh_metric_first_contentful_paint_displayed_value"
189-
),
190-
lh_metric_first_contentful_paint_score=formatted_results.get(
191-
"lh_metric_first_contentful_paint_score"
192-
),
193-
lh_metric_speed_index_displayed_value=formatted_results.get(
194-
"lh_metric_speed_index_displayed_value"
195-
),
196-
lh_metric_speed_index_score=formatted_results.get(
197-
"lh_metric_speed_index_score"
198-
),
199-
lh_metric_first_meaningful_paint_displayed_value=formatted_results.get(
200-
"lh_metric_first_meaningful_paint_displayed_value"
201-
),
202-
lh_metric_first_meaningful_paint_score=formatted_results.get(
203-
"lh_metric_first_meaningful_paint_score"
204-
),
205-
lh_metric_first_cpu_idle_displayed_value=formatted_results.get(
206-
"lh_metric_first_cpu_idle_displayed_value"
207-
),
208-
lh_metric_first_cpu_idle_score=formatted_results.get(
209-
"lh_metric_first_cpu_idle_score"
210-
),
211-
lh_metric_max_potential_first_input_delay_displayed_value=formatted_results.get(
212-
"lh_metric_max_potential_first_input_delay_displayed_value"
213-
),
214-
lh_metric_max_potential_first_input_delay_score=formatted_results.get(
215-
"lh_metric_max_potential_first_input_delay_score"
216-
),
133+
**formatted_results,
217134
)
218135
audit_results.save()
219136

220-
project.screenshot_url = formatted_results["screenshot_url"]
137+
project.screenshot_url = screenshot_url
221138
project.save()
222139

223140
AuditStatusHistory.objects.create(

backend/audits/wpt_utils/normalizer.py

+50-41
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,47 @@ def format_wpt_json_results_for_page(data):
55
lighthouse_data = data.get("lighthouse")
66
lighthouse_metrics = dict()
77
# lighthouse_data["audits"] only exists on Chrome browser
8-
if lighthouse_data.get("audits"):
8+
if lighthouse_data is not None and lighthouse_data.get("audits"):
9+
10+
lighthouse_audits = lighthouse_data.get("audits")
11+
912
lighthouse_metrics = {
10-
"lh_metric_tti_displayed_value": lighthouse_data["audits"]["interactive"][
13+
"lh_metric_tti_displayed_value": lighthouse_audits["interactive"][
1114
"displayValue"
1215
],
13-
"lh_metric_tti_score": lighthouse_data["audits"]["interactive"]["score"],
14-
"lh_metric_first_contentful_paint_displayed_value": lighthouse_data[
15-
"audits"
16-
]["first-contentful-paint"]["displayValue"],
17-
"lh_metric_first_contentful_paint_score": lighthouse_data["audits"][
16+
"lh_metric_tti_score": lighthouse_audits["interactive"]["score"],
17+
"lh_metric_first_contentful_paint_displayed_value": lighthouse_audits[
1818
"first-contentful-paint"
19-
]["score"],
20-
"lh_metric_speed_index_displayed_value": lighthouse_data["audits"][
21-
"speed-index"
2219
]["displayValue"],
23-
"lh_metric_speed_index_score": lighthouse_data["audits"]["speed-index"][
24-
"score"
20+
"lh_metric_first_contentful_paint_score": lighthouse_audits[
21+
"first-contentful-paint"
22+
]["score"],
23+
"lh_metric_speed_index_displayed_value": lighthouse_audits["speed-index"][
24+
"displayValue"
2525
],
26-
"lh_metric_first_meaningful_paint_displayed_value": lighthouse_data[
27-
"audits"
28-
]["first-meaningful-paint"]["displayValue"],
29-
"lh_metric_first_meaningful_paint_score": lighthouse_data["audits"][
26+
"lh_metric_speed_index_score": lighthouse_audits["speed-index"]["score"],
27+
"lh_metric_first_meaningful_paint_displayed_value": lighthouse_audits[
28+
"first-meaningful-paint"
29+
]["displayValue"],
30+
"lh_metric_first_meaningful_paint_score": lighthouse_audits[
3031
"first-meaningful-paint"
3132
]["score"],
32-
"lh_metric_first_cpu_idle_displayed_value": lighthouse_data["audits"][
33+
"lh_metric_first_cpu_idle_displayed_value": lighthouse_audits[
3334
"first-cpu-idle"
3435
]["displayValue"],
35-
"lh_metric_first_cpu_idle_score": lighthouse_data["audits"][
36-
"first-cpu-idle"
37-
]["score"],
38-
"lh_metric_max_potential_first_input_delay_displayed_value": lighthouse_data[
39-
"audits"
40-
][
36+
"lh_metric_first_cpu_idle_score": lighthouse_audits["first-cpu-idle"][
37+
"score"
38+
],
39+
"lh_metric_max_potential_first_input_delay_displayed_value": lighthouse_audits[
4140
"max-potential-fid"
4241
][
4342
"displayValue"
4443
],
45-
"lh_metric_max_potential_first_input_delay_score": lighthouse_data[
46-
"audits"
47-
]["max-potential-fid"]["score"],
44+
"lh_metric_max_potential_first_input_delay_score": lighthouse_audits[
45+
"max-potential-fid"
46+
]["score"],
4847
}
48+
4949
wpt_metrics = {
5050
"wpt_metric_first_view_tti": data["median"]["firstView"].get(
5151
"TimeToInteractive"
@@ -57,14 +57,18 @@ def format_wpt_json_results_for_page(data):
5757
)
5858
or data["median"]["repeatView"].get("FirstInteractive")
5959
or data["median"]["repeatView"].get("LastInteractive"),
60-
"wpt_metric_first_view_speed_index": data["median"]["firstView"]["SpeedIndex"],
61-
"wpt_metric_repeat_view_speed_index": data["median"]["repeatView"][
60+
"wpt_metric_first_view_speed_index": data["median"]["firstView"].get(
6261
"SpeedIndex"
63-
],
64-
"wpt_metric_first_view_first_paint": data["median"]["firstView"]["firstPaint"],
65-
"wpt_metric_repeat_view_first_paint": data["median"]["repeatView"][
62+
),
63+
"wpt_metric_repeat_view_speed_index": data["median"]["repeatView"].get(
64+
"SpeedIndex"
65+
),
66+
"wpt_metric_first_view_first_paint": data["median"]["firstView"].get(
6667
"firstPaint"
67-
],
68+
),
69+
"wpt_metric_repeat_view_first_paint": data["median"]["repeatView"].get(
70+
"firstPaint"
71+
),
6872
"wpt_metric_first_view_first_meaningful_paint": data["median"]["firstView"].get(
6973
"firstMeaningfulPaint"
7074
),
@@ -77,24 +81,29 @@ def format_wpt_json_results_for_page(data):
7781
"wpt_metric_repeat_view_first_contentful_paint": data["median"][
7882
"repeatView"
7983
].get("firstContentfulPaint"),
80-
"wpt_metric_first_view_load_time": data["median"]["firstView"]["loadTime"],
81-
"wpt_metric_repeat_view_load_time": data["median"]["repeatView"]["loadTime"],
82-
"wpt_metric_first_view_time_to_first_byte": data["median"]["firstView"]["TTFB"],
84+
"wpt_metric_first_view_load_time": data["median"]["firstView"].get("loadTime"),
85+
"wpt_metric_repeat_view_load_time": data["median"]["repeatView"].get(
86+
"loadTime"
87+
),
88+
"wpt_metric_first_view_time_to_first_byte": data["median"]["firstView"].get(
89+
"TTFB"
90+
),
8391
"wpt_metric_repeat_view_time_to_first_byte": data["median"]["repeatView"][
8492
"TTFB"
8593
],
86-
"wpt_metric_first_view_visually_complete": data["median"]["firstView"][
94+
"wpt_metric_first_view_visually_complete": data["median"]["firstView"].get(
8795
"visualComplete"
88-
],
89-
"wpt_metric_repeat_view_visually_complete": data["median"]["repeatView"][
96+
),
97+
"wpt_metric_repeat_view_visually_complete": data["median"]["repeatView"].get(
9098
"visualComplete"
91-
],
99+
),
92100
"wpt_metric_lighthouse_performance": data["median"]["firstView"].get(
93101
"lighthouse.Performance"
94102
),
95103
"screenshot_url": data["median"]["firstView"]["images"]["screenShot"],
96104
}
97105
wpt_metrics.update(lighthouse_metrics)
106+
98107
return [wpt_metrics]
99108

100109

@@ -182,8 +191,8 @@ def format_wpt_json_results_for_script(data):
182191
"lighthouse.Performance"
183192
),
184193
"screenshot_url": first_view_step_data["images"]["screenShot"],
185-
"step_name": first_view_step_data["eventName"],
186-
"step_number": first_view_step_data["step"],
194+
"script_step_name": first_view_step_data["eventName"],
195+
"script_step_number": first_view_step_data["step"],
187196
}
188197
)
189198

0 commit comments

Comments
 (0)