1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import datetime |
5
|
|
|
import jinja2 |
6
|
|
|
import os |
|
|
|
|
7
|
|
|
import sys |
|
|
|
|
8
|
|
|
|
9
|
|
|
import lighthouse_garden |
10
|
|
|
from lighthouse_garden import info |
11
|
|
|
from lighthouse_garden.utility import output, system |
12
|
|
|
from lighthouse_garden.lighthouse import database, utility |
13
|
|
|
|
14
|
|
|
ASSETS_CSS = '/templates/assets/css' |
15
|
|
|
ASSETS_JS = '/templates/assets/js' |
16
|
|
|
|
17
|
|
|
TAG_CSS = 'style' |
18
|
|
|
TAG_JS = 'script' |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
def generate_dashboard(): |
|
|
|
|
22
|
|
|
_now = datetime.datetime.now() |
23
|
|
|
_file = f'{system.config["export_path"]}index.html' |
24
|
|
|
output.println(f'{output.Subject.INFO} Generating dashboard {output.CliFormat.BLACK}{_file}{output.CliFormat.ENDC}') |
|
|
|
|
25
|
|
|
|
26
|
|
|
html = open(_file, "w") |
27
|
|
|
|
28
|
|
|
_rendered_html = render_template('index.html.j2', |
29
|
|
|
logo=render_logo(), |
30
|
|
|
date=_now.strftime("%Y-%m-%dT%H:%M:%SZ"), |
31
|
|
|
assets_css=render_assets(ASSETS_CSS, TAG_CSS), |
32
|
|
|
assets_js=render_assets(ASSETS_JS, TAG_JS), |
33
|
|
|
items=render_items(), |
34
|
|
|
title=system.config['title'], |
35
|
|
|
description=system.config['description'], |
36
|
|
|
version=info.__version__, |
37
|
|
|
homepage=info.__homepage__, |
38
|
|
|
lighthouse_version=system.config['lighthouse']['version'] |
39
|
|
|
) |
40
|
|
|
html.write(_rendered_html) |
41
|
|
|
html.close() |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def render_items(): |
|
|
|
|
45
|
|
|
_items = '' |
46
|
|
|
results = database.sort_by_average( |
47
|
|
|
database.get_last_results() |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
for result in results: |
51
|
|
|
_target = database.get_target_by_attribute(result['title'], 'title') |
52
|
|
|
_item = render_template('partials/item.html.j2', |
53
|
|
|
title=result['title'], |
54
|
|
|
url=result['url'], |
55
|
|
|
last_report=result['report'], |
56
|
|
|
identifier=_target['identifier'], |
57
|
|
|
graph_values_y=','.join(map(str, database.get_history_by_attribute( |
58
|
|
|
_target, 'performance'))), |
59
|
|
|
graph_values_x=','.join(map(str, database.get_history_by_attribute( |
60
|
|
|
_target, 'date'))), |
61
|
|
|
graph_values_text=','.join(map(str, database.get_history_by_attribute( |
|
|
|
|
62
|
|
|
_target, 'report'))), |
63
|
|
|
circle_average=render_percentage_circle( |
64
|
|
|
description=f'<strong>Average</strong><br/>' |
65
|
|
|
f'Calculates all available performance values to an average value.<br/><br/>' |
|
|
|
|
66
|
|
|
f'Maximum value: {int(result["average"]["max"])}<br/>' |
|
|
|
|
67
|
|
|
f'Minimum value: {int(result["average"]["min"])}<br/>', |
|
|
|
|
68
|
|
|
value=result['average']['value'] |
69
|
|
|
), |
70
|
|
|
circle_performance=render_percentage_circle( |
71
|
|
|
trend=render_trend(result), |
72
|
|
|
description=f'<strong>Performance</strong><br/>' |
|
|
|
|
73
|
|
|
f'The performance score is calculated directly from various metrics.<br/><br/>' |
|
|
|
|
74
|
|
|
f'Click here to get more information from the last report.', |
|
|
|
|
75
|
|
|
url=f'{result["report"]}#performance', |
76
|
|
|
value=result['performance'] |
77
|
|
|
), |
78
|
|
|
circle_accessibility=render_percentage_circle( |
79
|
|
|
description=f'<strong>Accessibility</strong><br/>' |
|
|
|
|
80
|
|
|
f'These checks highlight opportunities to improve the accessibility of ' |
|
|
|
|
81
|
|
|
f'your web app.<br/><br/>' |
82
|
|
|
f'Click here to get more information from the last report.', |
|
|
|
|
83
|
|
|
value=result['accessibility'], |
84
|
|
|
url=f'{result["report"]}#accessibility', |
85
|
|
|
additional_class='small' |
86
|
|
|
), |
87
|
|
|
circle_best_practices=render_percentage_circle( |
88
|
|
|
description=f'<strong>Best practices</strong><br/>' |
|
|
|
|
89
|
|
|
f'Further information about best practices.' |
90
|
|
|
f'See the lighthouse report for further information.<br/><br/>' |
|
|
|
|
91
|
|
|
f'Click here to get more information from the last report.', |
|
|
|
|
92
|
|
|
value=result['best-practices'], |
93
|
|
|
url=f'{result["report"]}#best-practices', |
94
|
|
|
additional_class='small' |
95
|
|
|
), |
96
|
|
|
circle_seo=render_percentage_circle( |
97
|
|
|
description=f'<strong>SEO</strong><br/>' |
|
|
|
|
98
|
|
|
f'These checks ensure that your page is optimized for search engine ' |
|
|
|
|
99
|
|
|
f'results ranking.<br/><br/>' |
100
|
|
|
f'Click here to get more information from the last report.', |
|
|
|
|
101
|
|
|
value=result['seo'], |
102
|
|
|
url=f'{result["report"]}#seo', |
103
|
|
|
additional_class='small' |
104
|
|
|
), |
105
|
|
|
api_json=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.json', |
|
|
|
|
106
|
|
|
badge_average=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.average.svg', |
|
|
|
|
107
|
|
|
badge_performance=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.performance.svg', |
|
|
|
|
108
|
|
|
badge_accessibility=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.accessibility.svg', |
|
|
|
|
109
|
|
|
badge_best_practices=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.best-practices.svg', |
|
|
|
|
110
|
|
|
badge_seo=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.seo.svg', |
|
|
|
|
111
|
|
|
) |
112
|
|
|
_items += _item |
113
|
|
|
return _items |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
def render_percentage_circle(description, value, trend='', url='', additional_class=None): |
|
|
|
|
117
|
|
|
return render_template('partials/circle.html.j2', |
118
|
|
|
url=url, |
119
|
|
|
value=str(int(round(value))), |
120
|
|
|
description=description, |
121
|
|
|
color=get_percentage_classification(int(round(value))), |
122
|
|
|
small=additional_class, |
123
|
|
|
trend=trend |
124
|
|
|
) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def render_trend(result): |
128
|
|
|
""" |
129
|
|
|
|
130
|
|
|
:param result: |
131
|
|
|
:return: |
132
|
|
|
""" |
133
|
|
|
_description = '' |
134
|
|
|
_class = '' |
135
|
|
|
if result['average']['trend'] == 1: |
136
|
|
|
_description = f'<strong>Performance trend</strong><br/>' \ |
|
|
|
|
137
|
|
|
f'Ascending trend in dependence of the last performance measurement to the average value.' |
|
|
|
|
138
|
|
|
_class = 'asc' |
139
|
|
|
elif result['average']['trend'] == -1: |
140
|
|
|
_description = f'<strong>Performance trend</strong><br/>' \ |
|
|
|
|
141
|
|
|
f'Descending trend in dependence of the last performance measurement to the average value.' |
|
|
|
|
142
|
|
|
_class = 'desc' |
143
|
|
|
|
144
|
|
|
if result['average']['trend'] == 0: |
|
|
|
|
145
|
|
|
return '' |
146
|
|
|
else: |
147
|
|
|
return render_template('partials/trend.html.j2', |
148
|
|
|
description=_description, |
|
|
|
|
149
|
|
|
trend=_class |
|
|
|
|
150
|
|
|
) |
|
|
|
|
151
|
|
|
|
152
|
|
|
|
153
|
|
|
def get_percentage_classification(value): |
|
|
|
|
154
|
|
|
if value >= 90: |
|
|
|
|
155
|
|
|
return 'green' |
156
|
|
|
elif value >= 50: |
157
|
|
|
return 'orange' |
158
|
|
|
else: |
159
|
|
|
return 'red' |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
def render_assets(path, tag): |
163
|
|
|
""" |
164
|
|
|
|
165
|
|
|
:param path: String |
166
|
|
|
:param tag: String |
167
|
|
|
:return: String |
168
|
|
|
""" |
169
|
|
|
_html = '' |
170
|
|
|
for file in os.listdir(os.path.dirname(lighthouse_garden.__file__) + path): |
171
|
|
|
with open(os.path.dirname(lighthouse_garden.__file__) + path + '/' + file, 'r') as read_file: |
|
|
|
|
172
|
|
|
_html += f'<!-- {file} -->\n<{tag}>\n{read_file.read()}\n</{tag}>\n' |
173
|
|
|
return _html |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
def render_logo(): |
177
|
|
|
""" |
178
|
|
|
|
179
|
|
|
:return: |
180
|
|
|
""" |
181
|
|
|
with open(os.path.dirname(lighthouse_garden.__file__) + '/templates/assets/tower.svg', 'r') as read_file: |
|
|
|
|
182
|
|
|
return read_file.read() |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
def render_template(template, **args): |
186
|
|
|
""" |
187
|
|
|
Render a template with jinja2 |
188
|
|
|
:param template: String Template file |
189
|
|
|
:param args: Dictionary Template arguments |
190
|
|
|
:return: String Rendered Template |
191
|
|
|
""" |
192
|
|
|
_template_loader = jinja2.FileSystemLoader(searchpath="./lighthouse_garden/templates/") |
193
|
|
|
_template_environment = jinja2.Environment(loader=_template_loader) |
194
|
|
|
_template = _template_environment.get_template(template) |
195
|
|
|
return _template.render(args) |
196
|
|
|
|