File size: 2,894 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
	BasicMetrics,
	BasicMetricsList,
	BasicMetricsScored,
	Metrics,
	PerformanceCategories,
	PerformanceReport,
	Scores,
} from './types';

export const BASIC_METRICS_SCORES: Record< string, [ number, number ] > = {
	cls: [ 0.1, 0.25 ], // https://web.dev/articles/cls
	fid: [ 100, 300 ], // https://web.dev/articles/fid
	lcp: [ 2500, 4000 ], // https://web.dev/articles/lcp
	fcp: [ 1800, 3000 ], // https://web.dev/articles/fcp
	ttfb: [ 800, 1800 ], // https://web.dev/articles/ttfb
	inp: [ 200, 500 ], // https://web.dev/articles/inp
};

/**
 * Get the score of a metric based on its value according to the Google
 * documentation that has been represented in the BASIC_METRICS_SCORES constant.
 * @param metric The metric in the Metrics type
 * @param value The value of the metric
 * @returns A score based on the value of the metric
 */
export function getScore( metric: Metrics, value: number ): Scores {
	const [ good, poor ] = BASIC_METRICS_SCORES[ metric ];
	if ( value <= good ) {
		return 'good';
	}
	if ( value > poor ) {
		return 'poor';
	}
	return 'needs-improvement';
}

/**
 * Get the overall score of the site based on the advanced performance
 * report. If the performance is greater than 0.9, the score is good,
 * @param metrics A record of metrics with their scores
 * @returns The overall score of the site
 */
function getOveralScore( metrics?: PerformanceReport ): Scores {
	if ( ! metrics ) {
		return 'good';
	}

	return metrics.performance >= 0.9 ? 'good' : 'poor';
}

/**
 * Helper method that returns if the Score is good or false otherwise
 * @param score The score to check
 * @returns True if the score is good, false otherwise
 */
function isScoreGood( score: Scores ): boolean {
	return score === 'good';
}

export function getPerformanceCategory( metrics?: PerformanceReport ): PerformanceCategories {
	const overallScore = getOveralScore( metrics );
	if ( isScoreGood( overallScore ) && metrics?.is_wpcom ) {
		return 'wpcom-high-performer';
	}
	if ( isScoreGood( overallScore ) && ! metrics?.is_wpcom ) {
		return 'non-wpcom-high-performer';
	}
	if ( ! isScoreGood( overallScore ) && metrics?.is_wpcom ) {
		return 'wpcom-low-performer';
	}
	return 'non-wpcom-low-performer';
}

export function getBasicMetricsScored( metrics: BasicMetrics ) {
	return ( Object.entries( metrics ) as BasicMetricsList ).reduce( ( acc, [ key, value ] ) => {
		acc[ key ] = { value: value, score: getScore( key as Metrics, value ) };
		return acc;
	}, {} as BasicMetricsScored );
}

export function getBasicMetricsFromPerfReport( metrics?: any ): BasicMetricsScored | undefined {
	if ( ! metrics ) {
		return undefined;
	}

	const basicMetrics = {
		cls: metrics.cls,
		fid: metrics.fid,
		lcp: metrics.lcp,
		fcp: metrics.fcp,
		ttfb: metrics.ttfb,
		inp: metrics.inp,
		tbt: metrics.tbt,
		overall: metrics.overall,
	};
	return getBasicMetricsScored( basicMetrics );
}