1- # this python file uses the following encoding utf-8
2-
3- # Python Standard Library
1+ # Standard library modules
42from collections import namedtuple
53from datetime import timedelta
4+ from typing import Dict , List
65
76
8- AuditResult = namedtuple ('AuditResult' , ['title' , 'score' , 'display' ])
7+ AuditResult = namedtuple ('AuditResult' , ['id' , ' title' , 'score' , 'display' , 'numval' , 'numunit ' ])
98AuditCategoryResult = namedtuple ('AuditCategoryResult' , ['passed' , 'failed' ])
109
11- BASE_TIMINGS = [
12- 'first-contentful-paint' ,
13- 'speed-index' ,
14- 'interactive' ,
15- 'first-meaningful-paint' ,
16- 'first-cpu-idle' ,
17- 'estimated-input-latency' ,
18- 'time-to-first-byte' ,
19- ]
20- """list(str) default list of timings"""
21-
22-
23- class LighthouseReport (object ):
10+ class LighthouseReport :
2411 """
25- Lighthouse report abstract
12+ Represents a Lighthouse report.
2613
27- Should provide more pleasant report then the lighthouse JSON.
14+ Provides a more user-friendly interface to the Lighthouse JSON report .
2815 """
2916
30- def __init__ (self , data , timings = BASE_TIMINGS ):
17+ def __init__ (self , data : Dict , timings : List [ str ] = [] ):
3118 """
3219 Args:
33- data (dict): JSON loaded lighthouse report
34- timings (list(str), optional): list of timings
35- to gather
20+ data (dict): JSON loaded Lighthouse report
21+
22+ timings (list(str), optional): List of timings to gather
23+ from Lighthouse report. Defaults to an empty list.
3624 """
3725
3826 self .__data = data
3927 self .__timings = timings
4028
4129 @property
42- def score (self ):
43- """ Dictionary of lighthouse's category name: score (0 to 1) """
30+ def score (self ) -> Dict [str , float ]:
31+ """
32+ Dictionary of Lighthouse category names and their corresponding scores (0 to 1).
33+ """
4434
4535 return {
4636 k : v .get ('score' , 0 )
@@ -49,24 +39,29 @@ def score(self):
4939 }
5040
5141 @property
52- def timings (self ):
53- """ Dictionary of lighthouse's timings names: timedelta times """
42+ def timings (self ) -> Dict [str , timedelta ]:
43+ """
44+ Dictionary of Lighthouse timing names and their corresponding times as timedeltas.
45+ """
5446
5547 return {
56- k : timedelta (milliseconds = v .get ('rawValue ' ))
48+ k : timedelta (milliseconds = v .get ('numericValue ' ))
5749 for k , v
5850 in self .__data ['audits' ].items ()
5951 if k in self .__timings
6052 }
6153
62- @property
63- def audits (self ):
54+ def audits (self , score_thresh : float = 1 ) -> Dict [str , AuditCategoryResult ]:
6455 """
65- Dictionary of audits as category name: object with passed/failed keys
66- with lists attached.
56+ Returns a dictionary of Lighthouse audits, grouped by category, with passed/failed
57+ keys and lists of passed/failed audits attached.
58+
59+ Args:
60+ score_thresh (float, optional): The minimum score required for an audit to be considered
61+ passed. Defaults to 1 (all audits must pass).
6762 """
68- res = {}
6963
64+ res = {}
7065 for category , data in self .__data ['categories' ].items ():
7166 all_audit_refs = [
7267 x .get ('id' )
@@ -76,23 +71,30 @@ def audits(self):
7671 sdm_to_reject = ['manual' , 'notApplicable' , 'informative' ]
7772 passed_audits = [
7873 AuditResult (** {
74+ 'id' : v ['id' ],
7975 'title' : v ['title' ],
8076 'score' : v ['score' ],
8177 'display' : v .get ('displayValue' ),
78+ 'numval' : v .get ('numericValue' ),
79+ 'numunit' : v .get ('numericUnit' ),
8280 })
8381 for k , v in all_audits .items ()
84- if v .get ('score' , 0 ) == 1 and
82+ if v .get ('score' ,0 ) is not None and
83+ v .get ('score' , 0 ) >= score_thresh and
8584 v .get ('scoreDisplayMode' ) not in sdm_to_reject
8685 ]
8786
8887 failed_audits = [
8988 AuditResult (** {
89+ 'id' : v ['id' ],
9090 'title' : v ['title' ],
9191 'score' : v ['score' ],
9292 'display' : v .get ('displayValue' ),
93+ 'numval' : v .get ('numericValue' ),
94+ 'numunit' : v .get ('numericUnit' ),
9395 })
9496 for k , v in all_audits .items ()
95- if v .get ('score' , 0 ) < 1 and
97+ if ( v .get ('score' ,0 ) is None or v . get ( 'score' , 0 ) < score_thresh ) and
9698 v .get ('scoreDisplayMode' ) not in sdm_to_reject
9799 ]
98100
0 commit comments