1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
3
|
|
|
|
4
|
|
|
import datetime |
5
|
|
|
import json |
6
|
|
|
import os |
7
|
|
|
import sys |
8
|
|
|
from collections import defaultdict |
9
|
|
|
|
10
|
|
|
from lighthouse_garden.utility import output |
11
|
|
|
from lighthouse_garden.lighthouse import utility, database |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def get_result_by_report_file(target, file_name): |
15
|
|
|
""" |
16
|
|
|
Generate the result by a given lighthouse report file |
17
|
|
|
:param target: Dict |
18
|
|
|
:param file_name: String |
19
|
|
|
:return: |
20
|
|
|
""" |
21
|
|
|
output.println(f'{output.Subject.INFO} Processing result of report', verbose_only=True) |
22
|
|
|
_report_path = f'{utility.get_data_dir()}{file_name}.report.json' |
23
|
|
|
_report = None |
24
|
|
|
|
25
|
|
|
if os.path.isfile(_report_path): |
26
|
|
|
with open(_report_path, 'r') as read_file: |
27
|
|
|
_report = json.load(read_file) |
28
|
|
|
else: |
29
|
|
|
sys.exit(f'{output.Subject.ERROR} Report file not found: {_report_path}') |
30
|
|
|
|
31
|
|
|
if not isinstance(_report, dict): |
32
|
|
|
sys.exit(f'{output.Subject.ERROR} Report not readable') |
33
|
|
|
|
34
|
|
|
if _report['categories']['performance']['score']: |
35
|
|
|
_performance = int(round(_report['categories']['performance']['score'] * 100)) |
36
|
|
|
else: |
37
|
|
|
output.println(f'{output.Subject.ERROR} Missing performance score', verbose_only=True) |
38
|
|
|
return None |
39
|
|
|
|
40
|
|
|
_result = defaultdict(lambda: defaultdict(dict)) |
41
|
|
|
_result = { |
42
|
|
|
'title': target['title'], |
43
|
|
|
'url': target['url'], |
44
|
|
|
'performance': _performance, |
45
|
|
|
'report': f'{utility.get_data_dir(absolute_path=False)}{file_name}.report.html', |
46
|
|
|
'link': f'#{target["identifier"]}', |
47
|
|
|
'date': '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()) |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
# additional metrics |
51
|
|
|
if 'accessibility' in _report['categories'] and _report['categories']['accessibility']['score']: |
52
|
|
|
_result['accessibility'] = int(round(_report['categories']['accessibility']['score'] * 100)) |
53
|
|
|
|
54
|
|
|
if 'best-practices' in _report['categories'] and _report['categories']['best-practices']['score']: |
|
|
|
|
55
|
|
|
_result['best-practices'] = int(round(_report['categories']['best-practices']['score'] * 100)) |
|
|
|
|
56
|
|
|
|
57
|
|
|
if 'seo' in _report['categories'] and _report['categories']['seo']['score']: |
58
|
|
|
_result['seo'] = int(round(_report['categories']['seo']['score'] * 100)) |
59
|
|
|
|
60
|
|
|
database.add_value_to_history(target, _result) |
61
|
|
|
|
62
|
|
|
# audits |
63
|
|
|
_result['audits'] = {} |
64
|
|
|
if _report['audits']['first-contentful-paint']: |
65
|
|
|
_result['audits']['first-contentful-paint'] = _report['audits']['first-contentful-paint']['displayValue'] |
|
|
|
|
66
|
|
|
|
67
|
|
|
if _report['audits']['largest-contentful-paint']: |
68
|
|
|
_result['audits']['largest-contentful-paint'] = _report['audits']['largest-contentful-paint'][ |
|
|
|
|
69
|
|
|
'displayValue'] |
70
|
|
|
|
71
|
|
|
if _report['audits']['speed-index']: |
72
|
|
|
_result['audits']['speed-index'] = _report['audits']['speed-index']['displayValue'] |
73
|
|
|
|
74
|
|
|
if _report['audits']['total-blocking-time']: |
75
|
|
|
_result['audits']['total-blocking-time'] = _report['audits']['total-blocking-time']['displayValue'] |
|
|
|
|
76
|
|
|
|
77
|
|
|
if _report['audits']['interactive']: |
78
|
|
|
_result['audits']['interactive'] = _report['audits']['interactive']['displayValue'] |
79
|
|
|
|
80
|
|
|
if _report['audits']['cumulative-layout-shift']: |
81
|
|
|
_result['audits']['cumulative-layout-shift'] = _report['audits']['cumulative-layout-shift']['displayValue'] |
|
|
|
|
82
|
|
|
|
83
|
|
|
# average |
84
|
|
|
_result['average'] = { |
85
|
|
|
'value': database.get_average_by_attribute(target, 'performance'), |
86
|
|
|
'min': database.get_average_peak(target, 'performance', True), |
87
|
|
|
'max': database.get_average_peak(target, 'performance', False), |
88
|
|
|
'trend': get_trend(target, 'performance') |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return _result |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def get_trend(target, attribute): |
95
|
|
|
""" |
96
|
|
|
Calculates a performance trend in dependence of the last performance measurement to the average value |
|
|
|
|
97
|
|
|
:param target: Dict |
98
|
|
|
:param attribute: String |
99
|
|
|
:return: |
100
|
|
|
""" |
101
|
|
|
_average = database.get_average_by_attribute(target, attribute) |
102
|
|
|
_value = database.get_last_value(target, attribute) |
103
|
|
|
|
104
|
|
|
_indicator = (10.0 * _average) / 100.0 |
105
|
|
|
|
106
|
|
|
if (_average - _indicator) > _value: |
107
|
|
|
return -1 |
108
|
|
|
|
109
|
|
|
if (_average + _indicator) < _value: |
110
|
|
|
return 1 |
111
|
|
|
|
112
|
|
|
return 0 |
113
|
|
|
|
|
|
|
|
114
|
|
|
|