1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
3
|
|
|
|
4
|
|
|
import datetime |
5
|
|
|
import anybadge |
6
|
|
|
from lighthouse_garden.utility import info, output, system |
7
|
|
|
from lighthouse_garden.lighthouse import database, utility, interpreter |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def fetch_data(): |
11
|
|
|
""" |
12
|
|
|
Fetch the lighthouse data of all given targets |
13
|
|
|
:return: |
14
|
|
|
""" |
15
|
|
|
output.println(f'{output.Subject.INFO} Checking export path', verbose_only=True) |
16
|
|
|
system.check_path(f'{system.config["export_path"]}/{system.config["data_dir"]}') |
17
|
|
|
|
18
|
|
|
output.println(f'{output.Subject.INFO} Starting to process targets') |
19
|
|
|
for target in system.config['targets']: |
20
|
|
|
_output_name = lighthouse(target) |
21
|
|
|
_result = interpreter.get_result_by_report_file(target, _output_name) |
22
|
|
|
if _result: |
23
|
|
|
database.set_data(target, _result) |
24
|
|
|
output.println(f'{output.Subject.OK} > {info.get_target_name(target)} ... {_result["performance"]}') |
|
|
|
|
25
|
|
|
utility.extend_html_report_with_info(_result, f'{utility.get_data_dir()}{_output_name}.report.html') |
|
|
|
|
26
|
|
|
generate_badges(target) |
27
|
|
|
else: |
28
|
|
|
utility.remove_file(f'{utility.get_data_dir()}{_output_name}.report.html') |
29
|
|
|
utility.remove_file(f'{utility.get_data_dir()}{_output_name}.report.json') |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def lighthouse(target): |
33
|
|
|
""" |
34
|
|
|
Fetch the lighthouse data of a specific target |
35
|
|
|
:param target: Dict |
36
|
|
|
:return: |
37
|
|
|
""" |
38
|
|
|
output.println(f'{output.Subject.INFO} Fetching performance data for {info.get_target_name(target)}', verbose_only=True) |
|
|
|
|
39
|
|
|
|
40
|
|
|
_output_name = generate_output_name(target) |
41
|
|
|
_result = system.run_command( |
42
|
|
|
f'lighthouse {target["url"]} {build_options(_output_name)}', allow_fail=True |
43
|
|
|
) |
44
|
|
|
|
45
|
|
|
if not _result: |
46
|
|
|
output.println(f'{output.Subject.ERROR} > {info.get_target_name(target)} ...') |
47
|
|
|
system.config['errors'] = True |
48
|
|
|
|
49
|
|
|
return _output_name |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def build_options(output_name): |
53
|
|
|
""" |
54
|
|
|
Build the lighthouse options |
55
|
|
|
@ToDo: Use a separate config file? |
56
|
|
|
:param output_name: String |
57
|
|
|
:return: |
58
|
|
|
""" |
59
|
|
|
_options = system.config['lighthouse']['options'] |
60
|
|
|
_options += f' --chrome-flags="{system.config["lighthouse"]["chrome_flags"]}"' |
61
|
|
|
_options += f' --output json --output html --output-path {system.config["export_path"]}/{system.config["data_dir"]}' \ |
|
|
|
|
62
|
|
|
f'{output_name}' |
63
|
|
|
return _options |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def generate_output_name(target): |
67
|
|
|
""" |
68
|
|
|
Generate a temporary output name, e.g. _google_28-12-2020_15-59 |
69
|
|
|
:return: |
70
|
|
|
""" |
71
|
|
|
return f'_{target["identifier"]}_{datetime.datetime.now().strftime("%d-%m-%Y_%H-%M")}' |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def generate_badges(target): |
75
|
|
|
""" |
76
|
|
|
Generate the badges for the various metrics of a specific target |
77
|
|
|
:param target: Dict |
78
|
|
|
:return: |
79
|
|
|
""" |
80
|
|
|
output.println(f'{output.Subject.INFO} Generating badges', verbose_only=True) |
81
|
|
|
data = database.get_data(target) |
82
|
|
|
badges = { |
83
|
|
|
'performance': { |
84
|
|
|
'label': 'performance', |
85
|
|
|
'value': data['performance'] if 'performance' in data else '', |
86
|
|
|
}, |
|
|
|
|
87
|
|
|
'accessibility': { |
88
|
|
|
'label': 'accessibility', |
89
|
|
|
'value': data['accessibility'] if 'accessibility' in data else '', |
90
|
|
|
}, |
91
|
|
|
'best-practices': { |
92
|
|
|
'label': 'best-practices', |
93
|
|
|
'value': data['best-practices'] if 'best-practices' in data else '', |
94
|
|
|
}, |
95
|
|
|
'seo': { |
96
|
|
|
'label': 'seo', |
97
|
|
|
'value': data['seo'] if 'seo' in data else '', |
98
|
|
|
}, |
99
|
|
|
'average': { |
100
|
|
|
'label': 'performance', |
101
|
|
|
'value': data['average']['value'] if 'average' in data else '', |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
for key, value in badges.items(): |
106
|
|
|
generate_badge( |
107
|
|
|
target=target, |
108
|
|
|
layout=value, |
109
|
|
|
attribute=key |
110
|
|
|
) |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def generate_badge(target, layout, attribute): |
114
|
|
|
""" |
115
|
|
|
Generate a badge |
116
|
|
|
:param target: Dict |
117
|
|
|
:param layout: Dict |
118
|
|
|
:param attribute: String |
119
|
|
|
:return: |
120
|
|
|
""" |
121
|
|
|
thresholds = {50: 'red', |
122
|
|
|
90: 'yellow', |
123
|
|
|
100: 'green'} |
124
|
|
|
badge = anybadge.Badge(layout['label'], round(layout['value']), thresholds=thresholds) |
125
|
|
|
|
126
|
|
|
badge.write_badge(f'{utility.get_data_dir()}_{target["identifier"]}.{attribute}.svg', overwrite=True) |
|
|
|
|
127
|
|
|
|