lighthouse_garden.export.render.render_trend()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 23
rs 9.7
c 0
b 0
f 0
cc 4
nop 1
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
8
import lighthouse_garden
9
from lighthouse_garden import info
10
from lighthouse_garden.utility import output, system
11
from lighthouse_garden.lighthouse import database, utility
12
13
"""
14
CONSTANTS
15
"""
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
16
ASSETS_CSS = '/templates/assets/css'
17
ASSETS_JS = '/templates/assets/js'
18
19
TAG_CSS = 'style'
20
TAG_JS = 'script'
21
22
23
def generate_dashboard():
24
    """
25
    Generate the static html dashboard
26
    :return:
27
    """
28
    _now = datetime.datetime.now()
29
    _file = f'{system.config["export_path"]}index.html'
30
    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...
31
32
    html = open(_file, "w")
33
34
    _rendered_html = render_template('index.html.j2',
35
                                     logo=render_logo(),
36
                                     date=_now.strftime("%Y-%m-%dT%H:%M:%SZ"),
37
                                     assets_css=render_assets(ASSETS_CSS, TAG_CSS),
38
                                     assets_js=render_assets(ASSETS_JS, TAG_JS),
39
                                     items=render_items(),
40
                                     title=system.config['title'],
41
                                     description=system.config['description'],
42
                                     version=info.__version__,
43
                                     homepage=info.__homepage__,
44
                                     lighthouse_version=system.config['lighthouse']['version']
45
                                     )
46
    html.write(_rendered_html)
47
    html.close()
48
49
50
def render_items():
51
    """
52
    Render the target items for the dashboard
53
    @ToDo: Outsource description text?
54
    :return:
55
    """
56
    _items = ''
57
    results = database.sort_by_average(
58
        database.get_last_results()
59
    )
60
61
    for result in results:
62
        _target = database.get_target_by_attribute(result['title'], 'title')
63
        _item = render_template('partials/item.html.j2',
64
                                title=result['title'],
65
                                url=result['url'],
66
                                last_report=result['report'],
67
                                identifier=_target['identifier'],
68
                                graph_values_y=','.join(map(str, database.get_history_by_attribute(
69
                                    _target, 'performance'))),
70
                                graph_values_x=','.join(map(str, database.get_history_by_attribute(
71
                                    _target, 'date'))),
72
                                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...
73
                                    _target, 'report'))),
74
                                circle_average=render_percentage_circle(
75
                                    description=f'<strong>Average</strong><br/>'
76
                                                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...
77
                                                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...
78
                                                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...
79
                                    value=result['average']['value']
80
                                ),
81
                                circle_performance=render_percentage_circle(
82
                                    trend=render_trend(result),
83
                                    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...
84
                                                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...
85
                                                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...
86
                                    url=f'{result["report"]}#performance',
87
                                    title=f'Report {result["date"]}',
88
                                    value=result['performance']
89
                                ),
90
                                circle_accessibility=render_percentage_circle(
91
                                    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...
92
                                                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...
93
                                                f'your web app.<br/><br/>'
94
                                                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...
95
                                    value=result['accessibility'],
96
                                    url=f'{result["report"]}#accessibility',
97
                                    title=f'Report {result["date"]}',
98
                                    additional_class='small'
99
                                ),
100
                                circle_best_practices=render_percentage_circle(
101
                                    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...
102
                                                f'Further information about best practices.'
103
                                                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...
104
                                                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...
105
                                    value=result['best-practices'],
106
                                    url=f'{result["report"]}#best-practices',
107
                                    title=f'Report {result["date"]}',
108
                                    additional_class='small'
109
                                ),
110
                                circle_seo=render_percentage_circle(
111
                                    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...
112
                                                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...
113
                                                f'results ranking.<br/><br/>'
114
                                                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...
115
                                    value=result['seo'],
116
                                    url=f'{result["report"]}#seo',
117
                                    title=f'Report {result["date"]}',
118
                                    additional_class='small'
119
                                ),
120
                                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...
121
                                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...
122
                                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...
123
                                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...
124
                                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...
125
                                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...
126
                                )
127
        _items += _item
128
    return _items
129
130
131
def render_percentage_circle(description, value, trend='', url='', title='', additional_class=None):
0 ignored issues
show
best-practice introduced by
Too many arguments (6/5)
Loading history...
132
    """
133
    Render the percentage circle for a specific attribute
134
    :param description: String
135
    :param value: Integer
136
    :param trend: String
137
    :param url: String
138
    :param additional_class:
139
    :return:
140
    """
141
    return render_template('partials/circle.html.j2',
142
                           url=url,
143
                           title=title,
144
                           value=str(int(round(value))),
145
                           description=description,
146
                           color=get_percentage_classification(int(round(value))),
147
                           small=additional_class,
148
                           trend=trend
149
                           )
150
151
152
def render_trend(result):
153
    """
154
    Render the performance trend
155
    :param result: Dict
156
    :return:
157
    """
158
    _description = ''
159
    _class = ''
160
    if result['average']['trend'] == 1:
161
        _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...
162
                       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...
163
        _class = 'asc'
164
    elif result['average']['trend'] == -1:
165
        _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...
166
                       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...
167
        _class = 'desc'
168
169
    if result['average']['trend'] == 0:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
170
        return ''
171
    else:
172
        return render_template('partials/trend.html.j2',
173
                           description=_description,
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
174
                           trend=_class
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
175
                           )
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation.
Loading history...
176
177
178
def get_percentage_classification(value):
179
    """
180
    Get the percentage classification by a value
181
    @ToDo: Make this configurable
182
    :param value:
183
    :return:
184
    """
185
    if value >= 90:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
186
        return 'green'
187
    elif value >= 50:
188
        return 'orange'
189
    else:
190
        return 'red'
191
192
193
def render_assets(path, tag):
194
    """
195
    Render the css/js assets
196
    :param path: String
197
    :param tag: String
198
    :return: String
199
    """
200
    _html = ''
201
    for file in os.listdir(os.path.dirname(lighthouse_garden.__file__) + path):
202
        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...
203
            _html += f'<!-- {file} -->\n<{tag}>\n{read_file.read()}\n</{tag}>\n'
204
    return _html
205
206
207
def render_logo():
208
    """
209
    Render the SVG logo file
210
    :return:
211
    """
212
    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...
213
        return read_file.read()
214
215
216
def render_template(template, **args):
217
    """
218
    Render a template with jinja2
219
    :param template: String Template file
220
    :param args: Dictionary Template arguments
221
    :return: String Rendered Template
222
    """
223
    _template_loader = jinja2.FileSystemLoader(searchpath=os.path.dirname(lighthouse_garden.__file__) + "/templates/")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

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

Loading history...
224
    _template_environment = jinja2.Environment(loader=_template_loader)
225
    _template = _template_environment.get_template(template)
226
    return _template.render(args)
227