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

get_trend()   A

Complexity

Conditions 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
rs 9.95
c 0
b 0
f 0
cc 3
nop 2
1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
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, system
0 ignored issues
show
Unused Code introduced by
Unused system imported from lighthouse_garden.utility
Loading history...
11
from lighthouse_garden.lighthouse import utility, database
12
13
14
def get_result_by_report_file(target, file_name):
15
    """
16
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
        return None
38
39
    _result = defaultdict(lambda: defaultdict(dict))
40
    _result = {
41
        'title': target['title'],
42
        'url': target['url'],
43
        'performance': _performance,
44
        'report': f'{utility.get_data_dir(absolute_path=False)}{file_name}.report.html',
45
        'link': f'#{target["identifier"]}',
46
        'date': '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
47
    }
48
49
    # additional metrics
50
    if 'accessibility' in _report['categories'] and _report['categories']['accessibility']['score']:
51
        _result['accessibility'] = int(round(_report['categories']['accessibility']['score'] * 100))
52
53
    if 'best-practices' in _report['categories'] and _report['categories']['best-practices']['score']:
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...
54
        _result['best-practices'] = int(round(_report['categories']['best-practices']['score'] * 100))
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...
55
56
    if 'seo' in _report['categories'] and _report['categories']['seo']['score']:
57
        _result['seo'] = int(round(_report['categories']['seo']['score'] * 100))
58
59
    database.add_value_to_history(target, _result)
60
61
    # audits
62
    _result['audits'] = {}
63
    if _report['audits']['first-contentful-paint']:
64
        _result['audits']['first-contentful-paint'] = _report['audits']['first-contentful-paint']['displayValue']
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...
65
66
    if _report['audits']['largest-contentful-paint']:
67
        _result['audits']['largest-contentful-paint'] = _report['audits']['largest-contentful-paint'][
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...
68
            'displayValue']
69
70
    if _report['audits']['speed-index']:
71
        _result['audits']['speed-index'] = _report['audits']['speed-index']['displayValue']
72
73
    if _report['audits']['total-blocking-time']:
74
        _result['audits']['total-blocking-time'] = _report['audits']['total-blocking-time']['displayValue']
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (107/100).

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

Loading history...
75
76
    if _report['audits']['interactive']:
77
        _result['audits']['interactive'] = _report['audits']['interactive']['displayValue']
78
79
    if _report['audits']['cumulative-layout-shift']:
80
        _result['audits']['cumulative-layout-shift'] = _report['audits']['cumulative-layout-shift']['displayValue']
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (115/100).

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

Loading history...
81
82
    # average
83
    _result['average'] = {
84
        'value': database.get_average_by_attribute(target, 'performance'),
85
        'min': database.get_average_peak(target, 'performance', True),
86
        'max': database.get_average_peak(target, 'performance', False),
87
        'trend': get_trend(target, 'performance')
88
    }
89
90
    return _result
91
92
93
def get_trend(target, attribute):
94
    """
95
96
    :param target:
97
    :param attribute:
98
    :return:
99
    """
100
    _average = database.get_average_by_attribute(target, attribute)
101
    _value = database.get_last_value(target, attribute)
102
103
    _indicator = (10.0 * _average) / 100.0
104
105
    if (_average - _indicator) > _value:
106
        return -1
107
108
    if (_average + _indicator) < _value:
109
        return 1
110
111
    return 0
112
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
113