Passed
Branch master (5f3343)
by Konrad
01:49
created

lighthouse_garden.export.render   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 121
dl 0
loc 196
rs 10
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A get_percentage_classification() 0 7 3
A render_trend() 0 23 4
A render_template() 0 11 1
B render_items() 0 70 2
A render_percentage_circle() 0 8 1
A render_assets() 0 12 3
A generate_dashboard() 0 21 1
A render_logo() 0 7 2
1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# -*- coding: utf-8 -*-
3
4
import datetime
5
import jinja2
6
import os
0 ignored issues
show
introduced by
standard import "import os" should be placed before "import jinja2"
Loading history...
7
import sys
0 ignored issues
show
Unused Code introduced by
The import sys seems to be unused.
Loading history...
introduced by
standard import "import sys" should be placed before "import jinja2"
Loading history...
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():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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}')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (120/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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(
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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/>'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (125/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
66
                                                f'Maximum value: {int(result["average"]["max"])}<br/>'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
67
                                                f'Minimum value: {int(result["average"]["min"])}<br/>',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
68
                                    value=result['average']['value']
69
                                ),
70
                                circle_performance=render_percentage_circle(
71
                                    trend=render_trend(result),
72
                                    description=f'<strong>Performance</strong><br/>'
0 ignored issues
show
introduced by
Using an f-string that does not have any interpolated variables
Loading history...
73
                                                f'The performance score is calculated directly from various metrics.<br/><br/>'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (127/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
74
                                                f'Click here to get more information from the last report.',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
75
                                    url=f'{result["report"]}#performance',
76
                                    value=result['performance']
77
                                ),
78
                                circle_accessibility=render_percentage_circle(
79
                                    description=f'<strong>Accessibility</strong><br/>'
0 ignored issues
show
introduced by
Using an f-string that does not have any interpolated variables
Loading history...
80
                                                f'These checks highlight opportunities to improve the accessibility of '
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (120/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
81
                                                f'your web app.<br/><br/>'
82
                                                f'Click here to get more information from the last report.',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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/>'
0 ignored issues
show
introduced by
Using an f-string that does not have any interpolated variables
Loading history...
89
                                                f'Further information about best practices.'
90
                                                f'See the lighthouse report for further information.<br/><br/>'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (111/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
91
                                                f'Click here to get more information from the last report.',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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/>'
0 ignored issues
show
introduced by
Using an f-string that does not have any interpolated variables
Loading history...
98
                                                f'These checks ensure that your page is optimized for search engine '
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
99
                                                f'results ranking.<br/><br/>'
100
                                                f'Click here to get more information from the last report.',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
106
                                badge_average=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.average.svg',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (129/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
107
                                badge_performance=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.performance.svg',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (137/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
108
                                badge_accessibility=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.accessibility.svg',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (141/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
109
                                badge_best_practices=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.best-practices.svg',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (143/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
110
                                badge_seo=f'{utility.get_data_dir(absolute_path=False)}_{_target["identifier"]}.seo.svg',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (121/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
111
                                )
112
        _items += _item
113
    return _items
114
115
116
def render_percentage_circle(description, value, trend='', url='', additional_class=None):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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/>' \
0 ignored issues
show
introduced by
Using an f-string that does not have any interpolated variables
Loading history...
137
                       f'Ascending trend in dependence of the last performance measurement to the average value.'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (113/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
138
        _class = 'asc'
139
    elif result['average']['trend'] == -1:
140
        _description = f'<strong>Performance trend</strong><br/>' \
0 ignored issues
show
introduced by
Using an f-string that does not have any interpolated variables
Loading history...
141
                       f'Descending trend in dependence of the last performance measurement to the average value.'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (114/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
142
        _class = 'desc'
143
144
    if result['average']['trend'] == 0:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
145
        return ''
146
    else:
147
        return render_template('partials/trend.html.j2',
148
                           description=_description,
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
149
                           trend=_class
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
150
                           )
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation.
Loading history...
151
152
153
def get_percentage_classification(value):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
154
    if value >= 90:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
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:
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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:
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (109/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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