@@ -84,6 +84,81 @@ export function evaluateCLSMetricScore(value: number): number {
8484 return getLogNormalScore ( { p10 : 0.1 , median : 0.25 } , value ) ;
8585}
8686
87+ export interface CrUXFieldMetricTimingResult {
88+ value : Types . Timing . MicroSeconds ;
89+ pageScope : CrUXManager . PageScope ;
90+ }
91+ export interface CrUXFieldMetricNumberResult {
92+ value : number ;
93+ pageScope : CrUXManager . PageScope ;
94+ }
95+ export interface CrUXFieldMetricResults {
96+ fcp : CrUXFieldMetricTimingResult | null ;
97+ lcp : CrUXFieldMetricTimingResult | null ;
98+ inp : CrUXFieldMetricTimingResult | null ;
99+ cls : CrUXFieldMetricNumberResult | null ;
100+ }
101+
102+ function getPageResult ( cruxFieldData : CrUXManager . PageResult [ ] , url : string , origin : string ) : CrUXManager . PageResult |
103+ undefined {
104+ return cruxFieldData . find ( result => {
105+ const key = ( result [ 'url-ALL' ] || result [ 'origin-ALL' ] ) ?. record . key ;
106+ return ( key ?. url && key . url === url ) || ( key ?. origin && key . origin === origin ) ;
107+ } ) ;
108+ }
109+
110+ function getMetricResult (
111+ pageResult : CrUXManager . PageResult , name : CrUXManager . StandardMetricNames ) : CrUXFieldMetricNumberResult | null {
112+ let value = pageResult [ 'url-ALL' ] ?. record . metrics [ name ] ?. percentiles ?. p75 ;
113+ if ( typeof value === 'string' ) {
114+ value = Number ( value ) ;
115+ }
116+ if ( typeof value === 'number' && Number . isFinite ( value ) ) {
117+ return { value, pageScope : 'url' } ;
118+ }
119+
120+ value = pageResult [ 'origin-ALL' ] ?. record . metrics [ name ] ?. percentiles ?. p75 ;
121+ if ( typeof value === 'string' ) {
122+ value = Number ( value ) ;
123+ }
124+ if ( typeof value === 'number' && Number . isFinite ( value ) ) {
125+ return { value, pageScope : 'origin' } ;
126+ }
127+
128+ return null ;
129+ }
130+
131+ function getMetricTimingResult (
132+ pageResult : CrUXManager . PageResult , name : CrUXManager . StandardMetricNames ) : CrUXFieldMetricTimingResult | null {
133+ const result = getMetricResult ( pageResult , name ) ;
134+ if ( result ) {
135+ const valueMs = result . value as Types . Timing . MilliSeconds ;
136+ return { value : Helpers . Timing . millisecondsToMicroseconds ( valueMs ) , pageScope : result . pageScope } ;
137+ }
138+
139+ return null ;
140+ }
141+
142+ export function getFieldMetricsForInsightSet (
143+ insightSet : InsightSet , metadata : Types . File . MetaData | null ) : CrUXFieldMetricResults | null {
144+ const cruxFieldData = metadata ?. cruxFieldData ;
145+ if ( ! cruxFieldData ) {
146+ return null ;
147+ }
148+
149+ const pageResult = getPageResult ( cruxFieldData , insightSet . url . href , insightSet . url . origin ) ;
150+ if ( ! pageResult ) {
151+ return null ;
152+ }
153+
154+ return {
155+ fcp : getMetricTimingResult ( pageResult , 'first_contentful_paint' ) ,
156+ lcp : getMetricTimingResult ( pageResult , 'largest_contentful_paint' ) ,
157+ inp : getMetricTimingResult ( pageResult , 'interaction_to_next_paint' ) ,
158+ cls : getMetricResult ( pageResult , 'cumulative_layout_shift' ) ,
159+ } ;
160+ }
161+
87162export function calculateMetricWeightsForSorting (
88163 insightSet : InsightSet , metadata : Types . File . MetaData | null ) : { lcp : number , inp : number , cls : number } {
89164 const weights = {
@@ -97,32 +172,14 @@ export function calculateMetricWeightsForSorting(
97172 return weights ;
98173 }
99174
100- const getPageResult = ( url : string , origin : string ) : CrUXManager . PageResult | undefined => {
101- return cruxFieldData . find ( result => {
102- const key = ( result [ 'url-ALL' ] || result [ 'origin-ALL' ] ) ?. record . key ;
103- return ( key ?. url && key . url === url ) || ( key ?. origin && key . origin === origin ) ;
104- } ) ;
105- } ;
106- const getMetricValue = ( pageResult : CrUXManager . PageResult , name : CrUXManager . StandardMetricNames ) : number | null => {
107- const score = pageResult [ 'url-ALL' ] ?. record . metrics [ name ] ?. percentiles ?. p75 ??
108- pageResult [ 'origin-ALL' ] ?. record . metrics [ name ] ?. percentiles ?. p75 ;
109- if ( typeof score === 'number' ) {
110- return score ;
111- }
112- if ( typeof score === 'string' && Number . isFinite ( Number ( score ) ) ) {
113- return Number ( score ) ;
114- }
115- return null ;
116- } ;
117-
118- const pageResult = getPageResult ( insightSet . url . href , insightSet . url . origin ) ;
119- if ( ! pageResult ) {
175+ const fieldMetrics = getFieldMetricsForInsightSet ( insightSet , metadata ) ;
176+ if ( ! fieldMetrics ) {
120177 return weights ;
121178 }
122179
123- const fieldLcp = getMetricValue ( pageResult , 'largest_contentful_paint' ) ;
124- const fieldInp = getMetricValue ( pageResult , 'interaction_to_next_paint' ) ;
125- const fieldCls = getMetricValue ( pageResult , 'cumulative_layout_shift' ) ;
180+ const fieldLcp = fieldMetrics . lcp ?. value ?? null ;
181+ const fieldInp = fieldMetrics . inp ?. value ?? null ;
182+ const fieldCls = fieldMetrics . cls ?. value ?? null ;
126183 const fieldLcpScore = fieldLcp !== null ? evaluateLCPMetricScore ( fieldLcp ) : 0 ;
127184 const fieldInpScore = fieldInp !== null ? evaluateINPMetricScore ( fieldInp ) : 0 ;
128185 const fieldClsScore = fieldCls !== null ? evaluateCLSMetricScore ( fieldCls ) : 0 ;
0 commit comments