/
numeric-diff.jsx
93 lines (82 loc) · 3.7 KB
/
numeric-diff.jsx
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
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
import {h, Fragment} from 'preact';
import {getDeltaLabel} from '@lhci/utils/src/audit-diff-finder';
import clsx from 'clsx';
import './numeric-diff.css';
/** @param {number} x @param {'up'|'down'} direction */
const toNearestRoundNumber = (x, direction) => {
const fn = direction === 'up' ? Math.ceil : Math.floor;
if (x < 10) return fn(x);
if (x < 100) return fn(x / 10) * 10;
if (x < 1000) return fn(x / 100) * 100;
if (x < 10000) return fn(x / 1000) * 1000;
return fn(x / 2500) * 2500;
};
/** @param {number} x @param {{asDelta?: boolean, withSuffix?: boolean}} options*/
const toDisplay = (x, options = {}) => {
let value = Math.round(x);
let fractionDigits = 0;
let suffix = ' ms';
if (Math.abs(value) >= 50) {
value /= 1000;
fractionDigits = 1;
suffix = ' s';
}
const string = value.toLocaleString(undefined, {
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits,
});
return `${options.asDelta && value >= 0 ? '+' : ''}${string}${options.withSuffix ? suffix : ''}`;
};
/** @param {{diff: LHCI.NumericAuditDiff}} props */
export const NumericDiff = props => {
const {diff} = props;
const currentNumericValue = diff.compareValue;
const baseNumericValue = diff.baseValue;
if (typeof baseNumericValue !== 'number' || typeof currentNumericValue !== 'number') {
return <span>No diff available</span>;
}
const delta = currentNumericValue - baseNumericValue;
const minValue = Math.min(currentNumericValue, baseNumericValue);
const maxValue = Math.max(currentNumericValue, baseNumericValue);
const lowerLimit = toNearestRoundNumber(minValue * 0.8, 'down');
const upperLimit = toNearestRoundNumber(maxValue * 1.2, 'up');
const range = upperLimit - lowerLimit;
const boxLeft = (100 * (minValue - lowerLimit)) / range;
const boxRight = 100 - (100 * (maxValue - lowerLimit)) / range;
const deltaType = getDeltaLabel(delta, 'audit');
const minValueIsCurrentValue = minValue === currentNumericValue;
return (
<Fragment>
<div className="audit-numeric-diff">
<div className="audit-numeric-diff__left-label">{toDisplay(lowerLimit)}</div>
<div className="audit-numeric-diff__bar">
<div
className={clsx('audit-numeric-diff__box', {
'audit-numeric-diff__box--improvement': deltaType === 'improvement',
'audit-numeric-diff__box--regression': deltaType === 'regression',
})}
style={{left: `${boxLeft}%`, right: `${boxRight}%`}}
title={`${toDisplay(baseNumericValue)} -> ${toDisplay(currentNumericValue)}`}
>
<div
className="audit-numeric-diff__now"
style={{left: minValueIsCurrentValue ? '0%' : '100%'}}
/>
<div
className="audit-numeric-diff__delta-label"
style={{[minValueIsCurrentValue ? 'right' : 'left']: '100%'}}
>
{toDisplay(delta, {asDelta: true, withSuffix: true})}
</div>
</div>
</div>
<div className="audit-numeric-diff__right-label">{toDisplay(upperLimit)}</div>
</div>
</Fragment>
);
};