Skip to content

Commit

Permalink
core: create separate product savings type (#15726)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine authored Jan 4, 2024
1 parent 30e617e commit 3b22bf6
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 19 deletions.
11 changes: 6 additions & 5 deletions core/audits/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Util} from '../../shared/util.js';

const DEFAULT_PASS = 'defaultPass';

/** @type {LH.Audit.MetricSavings} */
/** @type {Record<keyof LH.Audit.ProductMetricSavings, number>} */
const METRIC_SAVINGS_PRECISION = {
FCP: 50,
LCP: 50,
Expand Down Expand Up @@ -349,16 +349,17 @@ class Audit {
}

/**
* @param {LH.Audit.MetricSavings|undefined} metricSavings
* @return {LH.Audit.MetricSavings|undefined}
* @param {LH.Audit.ProductMetricSavings|undefined} metricSavings
* @return {LH.Audit.ProductMetricSavings|undefined}
*/
static _quantizeMetricSavings(metricSavings) {
if (!metricSavings) return;

/** @type {LH.Audit.MetricSavings} */
/** @type {LH.Audit.ProductMetricSavings} */
const normalizedMetricSavings = {...metricSavings};

for (const key of /** @type {Array<LH.Result.MetricAcronym>} */ (Object.keys(metricSavings))) {
// eslint-disable-next-line max-len
for (const key of /** @type {Array<keyof LH.Audit.ProductMetricSavings>} */ (Object.keys(metricSavings))) {
let value = metricSavings[key];
if (value === undefined) continue;

Expand Down
2 changes: 1 addition & 1 deletion core/audits/byte-efficiency/byte-efficiency-audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class ByteEfficiencyAudit extends Audit {

const wastedBytes = results.reduce((sum, item) => sum + item.wastedBytes, 0);

/** @type {LH.Audit.MetricSavings} */
/** @type {LH.Audit.ProductMetricSavings} */
const metricSavings = {
FCP: 0,
LCP: 0,
Expand Down
7 changes: 3 additions & 4 deletions report/renderer/performance-category-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

/** @typedef {import('./dom.js').DOM} DOM */
/** @typedef {LH.Result.MetricAcronym | 'All'} FilterType */

import {CategoryRenderer} from './category-renderer.js';
import {ReportUtils} from './report-utils.js';
Expand Down Expand Up @@ -233,7 +232,7 @@ export class PerformanceCategoryRenderer extends CategoryRenderer {
groupEl.classList.add('lh-audit-group--diagnostics');

/**
* @param {FilterType} acronym
* @param {string} acronym
*/
function refreshFilteredAudits(acronym) {
for (const audit of allInsights) {
Expand Down Expand Up @@ -352,15 +351,15 @@ export class PerformanceCategoryRenderer extends CategoryRenderer {
* Render the control to filter the audits by metric. The filtering is done at runtime by CSS only
* @param {LH.ReportResult.AuditRef[]} filterableMetrics
* @param {HTMLDivElement} categoryEl
* @param {(acronym: FilterType) => void} onFilterChange
* @param {(acronym: string) => void} onFilterChange
*/
renderMetricAuditFilter(filterableMetrics, categoryEl, onFilterChange) {
const metricFilterEl = this.dom.createElement('div', 'lh-metricfilter');
const textEl = this.dom.createChildOf(metricFilterEl, 'span', 'lh-metricfilter__text');
textEl.textContent = Globals.strings.showRelevantAudits;

const filterChoices = [
/** @type {const} */ ({acronym: 'All'}),
/** @type {const} */ ({acronym: 'All', id: 'All'}),
...filterableMetrics,
];

Expand Down
10 changes: 9 additions & 1 deletion types/audit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ declare module Audit {
export type ScoreDisplayModes = AuditResult.ScoreDisplayModes;
export type MetricSavings = AuditResult.MetricSavings;

export type ProductMetricSavings = {
FCP?: number;
LCP?: number;
TBT?: number;
CLS?: number;
INP?: number;
};

type Context = Util.Immutable<{
/** audit options */
options: Record<string, any>;
Expand Down Expand Up @@ -87,7 +95,7 @@ declare module Audit {
/** If an audit encounters unusual execution circumstances, strings can be put in this optional array to add top-level warnings to the LHR. */
runWarnings?: Array<IcuMessage>;
/** Estimates of how much this audit affects various performance metrics. Values will be in the unit of the respective metrics. */
metricSavings?: MetricSavings;
metricSavings?: ProductMetricSavings;
/** Score details including p10 and median for calculating an audit's log-normal score. */
scoringOptions?: ScoreOptions;
/** A string identifying how the score should be interpreted for display. Overrides audit meta `scoreDisplayMode` if defined. */
Expand Down
2 changes: 1 addition & 1 deletion types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ declare module Config {
id: string;
weight: number;
group?: string;
acronym?: Result.MetricAcronym;
acronym?: string;
relevantAudits?: string[];
}

Expand Down
5 changes: 1 addition & 4 deletions types/lhr/audit-result.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import {FormattedIcu} from './i18n.js';
import AuditDetails from './audit-details.js';
import LHResult from './lhr.js';

interface ScoreDisplayModes {
/** Scores of 0-1 (map to displayed scores of 0-100). */
Expand All @@ -32,9 +31,7 @@ interface ScoreDisplayModes {

type ScoreDisplayMode = ScoreDisplayModes[keyof ScoreDisplayModes];

export type MetricSavings = {
[M in LHResult.MetricAcronym]?: number;
};
export type MetricSavings = Partial<Record<string, number>>;

/** Audit result returned in Lighthouse report. All audits offer a description and score of 0-1. */
export interface Result {
Expand Down
4 changes: 1 addition & 3 deletions types/lhr/lhr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ declare module Result {
/** Optional grouping within the category. Matches the key of a Result.Group. */
group?: string;
/** The conventional acronym for the audit/metric. */
acronym?: MetricAcronym;
acronym?: string;
/** Any audit IDs closely relevant to this one. */
relevantAudits?: string[];
}
Expand Down Expand Up @@ -197,8 +197,6 @@ declare module Result {

/** Gather mode used to collect artifacts. */
type GatherMode = 'navigation'|'timespan'|'snapshot';

type MetricAcronym = 'FCP' | 'LCP' | 'TBT' | 'CLS' | 'INP' | 'SI' | 'TTI' | 'FMP';
}

export default Result;

0 comments on commit 3b22bf6

Please sign in to comment.