-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Budgets #1708
Open
vio
wants to merge
6
commits into
master
Choose a base branch
from
budgets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Budgets #1708
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7aedfce
feat(utils): Add basic budgets functionality
vio 1934f64
feat(ui): Budgets component
vio be50147
feat(utils): Job - evaluate budgets
vio ddfd176
refactor(utils): Job - skip missing data
vio 8078f12
WIP
vio d84e24c
refactor(utils): Job - replace deep merge
vio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.checkMetric { | ||
display: inline; | ||
} | ||
|
||
.checkMetric::before { | ||
content: ' ('; | ||
} | ||
|
||
.checkMetric::after { | ||
content: ') '; | ||
} | ||
|
||
.checkMetricDelta { | ||
padding: 2px; | ||
font-size: 8px; | ||
vertical-align: super; | ||
} | ||
|
||
.checkThreshold { | ||
display: inline; | ||
font-weight: bold; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React from 'react'; | ||
import { BudgetResult, BudgetStatus, MetricRunInfoDeltaType } from '@bundle-stats/utils'; | ||
|
||
import { Budgets } from '.'; | ||
|
||
export default { | ||
title: 'Components/Budgets', | ||
comonent: Budgets, | ||
}; | ||
|
||
const BUDGETS: Array<BudgetResult> = [ | ||
{ | ||
config: { | ||
condition: { | ||
fact: 'webpack.duplicatePackagesCount.delta', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.FAILURE, | ||
}, | ||
value: 2, | ||
data: { | ||
value: 4, | ||
displayValue: '4', | ||
delta: 2, | ||
displayDelta: '+2', | ||
deltaPercentage: 50, | ||
deltaType: MetricRunInfoDeltaType.HIGH_NEGATIVE, | ||
displayDeltaPercentage: '+50%', | ||
}, | ||
matched: true, | ||
}, | ||
{ | ||
config: { | ||
condition: { | ||
fact: 'webpack.duplicatePackagesCount.value', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.WARNING, | ||
}, | ||
value: 2, | ||
data: { | ||
value: 2, | ||
displayValue: '2', | ||
delta: 0, | ||
displayDelta: '0', | ||
deltaType: MetricRunInfoDeltaType.NO_CHANGE, | ||
deltaPercentage: 0, | ||
displayDeltaPercentage: '0%', | ||
}, | ||
matched: true, | ||
}, | ||
{ | ||
config: { | ||
condition: { | ||
fact: 'webpack.packageCount.delta', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.WARNING, | ||
}, | ||
value: 10, | ||
data: { | ||
value: 10, | ||
displayValue: '1', | ||
delta: 1, | ||
displayDelta: '+1', | ||
deltaType: MetricRunInfoDeltaType.NEGATIVE, | ||
deltaPercentage: 10, | ||
displayDeltaPercentage: '+10%', | ||
}, | ||
matched: true, | ||
}, | ||
{ | ||
config: { | ||
condition: { | ||
fact: 'webpack.totalSizeByTypeALL.delta', | ||
operator: 'greaterThan', | ||
value: 5 * 1024, | ||
}, | ||
status: BudgetStatus.WARNING, | ||
}, | ||
value: 0, | ||
data: { | ||
value: 1048576, | ||
displayValue: '1MiB', | ||
delta: 10 * 1024, | ||
displayDelta: '+10KiB', | ||
deltaType: MetricRunInfoDeltaType.NEGATIVE, | ||
deltaPercentage: 1, | ||
displayDeltaPercentage: '+1%', | ||
}, | ||
matched: true, | ||
}, | ||
]; | ||
|
||
export const Default = () => <Budgets budgets={BUDGETS} />; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import React, { useMemo } from 'react'; | ||
import { | ||
BudgetEvaluated, | ||
BudgetResult, | ||
BudgetStatus, | ||
ConditionOperator, | ||
MetricRunInfoDeltaType, | ||
getGlobalMetricType, | ||
} from '@bundle-stats/utils'; | ||
|
||
import { Stack } from '../../layout/stack'; | ||
import { Alert } from '../../ui/alert'; | ||
import { Delta } from '../delta'; | ||
import { Metric } from '../metric'; | ||
// @ts-ignore | ||
import css from './budgets.module.css'; | ||
|
||
const OPERATOR_MAP: Record<ConditionOperator, string> = { | ||
smallerThanInclusive: 'equal', | ||
smallerThan: 'bellow', | ||
equal: 'equal', | ||
notEqual: 'not equal', | ||
greaterThan: 'above', | ||
greaterThanInclusive: 'equal', | ||
}; | ||
|
||
const ALERT_MAP: Record<string, string> = { | ||
FAILURE: 'danger', | ||
WARNING: 'warning', | ||
SUCCESS: 'success', | ||
}; | ||
|
||
interface BudgetProps extends React.HTMLAttributes<HTMLElement> { | ||
budget: BudgetEvaluated; | ||
} | ||
|
||
const Budget = (props: BudgetProps) => { | ||
const { budget, ...restProps } = props; | ||
|
||
const metricSegments = budget.config.condition.fact.split('.'); | ||
const metricKey = metricSegments.slice(0, -1).join('.'); | ||
|
||
const field = metricSegments[metricSegments.length - 1]; | ||
|
||
let displayField = ''; | ||
let deltaDisplayField: string; | ||
|
||
switch (field) { | ||
case 'deltaPercentage': | ||
displayField = 'relative difference'; | ||
deltaDisplayField = budget.data.displayDeltaPercentage as string; | ||
break; | ||
case 'delta': | ||
displayField = 'absolute difference'; | ||
deltaDisplayField = budget.data.displayDelta as string; | ||
break; | ||
default: | ||
displayField = 'value'; | ||
deltaDisplayField = budget.data.displayDeltaPercentage as string; | ||
} | ||
|
||
const metric = getGlobalMetricType(metricKey); | ||
|
||
return ( | ||
<Alert kind={ALERT_MAP[budget.config.status]} {...restProps}> | ||
<p> | ||
<strong>{metric.label}</strong> | ||
{` ${displayField} `} | ||
<Metric inline value={budget.data.value} formatter={metric.formatter} className={css.checkMetric}> | ||
<Delta | ||
displayValue={deltaDisplayField} | ||
deltaType={budget.data.deltaType as MetricRunInfoDeltaType} | ||
inverted | ||
className={css.checkMetricDelta} | ||
/> | ||
</Metric> | ||
{` is ${OPERATOR_MAP[budget.config.condition.operator]} `} | ||
<Metric | ||
value={budget.config.condition.value} | ||
formatter={metric.formatter} | ||
inline | ||
className={css.checkThreshold} | ||
/> | ||
</p> | ||
</Alert> | ||
); | ||
}; | ||
|
||
export interface BudgetsProps extends React.HTMLAttributes<HTMLElement> { | ||
budgets: Array<BudgetResult>; | ||
} | ||
|
||
export const Budgets = (props: BudgetsProps) => { | ||
const { budgets, ...restProps } = props; | ||
|
||
const [matchedBudgets] = useMemo(() => { | ||
const matched: Array<BudgetEvaluated> = []; | ||
const unmatched: Array<BudgetEvaluated> = []; | ||
|
||
budgets.forEach((budget) => { | ||
// @ts-expect-error | ||
if (typeof budget.data === 'undefined') { | ||
return; | ||
} | ||
|
||
// @ts-expect-error | ||
if (typeof budget.matched === 'undefined') { | ||
return; | ||
} | ||
|
||
// @ts-expect-error | ||
if (budget.matched === true) { | ||
matched.push(budget as BudgetEvaluated); | ||
// @ts-expect-error | ||
} else if (budget.matched === false) { | ||
unmatched.push(budget as BudgetEvaluated); | ||
} | ||
}); | ||
|
||
return [matched, unmatched]; | ||
}, [budgets]); | ||
|
||
return ( | ||
<Stack space="xxsmall" {...restProps}> | ||
{matchedBudgets.map((budget) => ( | ||
<Budget budget={budget} /> | ||
))} | ||
</Stack> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './budgets'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { BudgetStatus } from '../constants'; | ||
|
||
import * as budgets from '../budgets'; | ||
|
||
describe('Budgets', () => { | ||
test('should return skipped budget when data is missing', () => { | ||
expect( | ||
budgets.evaluate( | ||
{ | ||
condition: { | ||
fact: 'webpack.totalSizeByTypeALL.delta', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.FAILURE, | ||
}, | ||
{}, | ||
), | ||
).toEqual({ | ||
config: { | ||
condition: { | ||
fact: 'webpack.totalSizeByTypeALL.delta', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.FAILURE, | ||
}, | ||
}); | ||
}); | ||
|
||
test('should return skipped budget when the condition does not match', () => { | ||
expect( | ||
budgets.evaluate( | ||
{ | ||
condition: { | ||
fact: 'webpack.totalSizeByTypeALL.delta', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.FAILURE, | ||
}, | ||
{ | ||
webpack: { | ||
totalSizeByTypeALL: { | ||
delta: 0, | ||
}, | ||
}, | ||
}, | ||
), | ||
).toEqual({ | ||
config: { | ||
condition: { | ||
fact: 'webpack.totalSizeByTypeALL.delta', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.FAILURE, | ||
}, | ||
value: 0, | ||
data: { | ||
delta: 0, | ||
}, | ||
matched: false, | ||
}); | ||
}); | ||
|
||
test('should return matched budget', () => { | ||
expect( | ||
budgets.evaluate( | ||
{ | ||
condition: { | ||
fact: 'webpack.totalSizeByTypeALL.delta', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.FAILURE, | ||
}, | ||
{ | ||
webpack: { | ||
totalSizeByTypeALL: { | ||
delta: 1, | ||
}, | ||
}, | ||
}, | ||
), | ||
).toEqual({ | ||
config: { | ||
condition: { | ||
fact: 'webpack.totalSizeByTypeALL.delta', | ||
operator: 'greaterThan', | ||
value: 0, | ||
}, | ||
status: BudgetStatus.FAILURE, | ||
}, | ||
value: 1, | ||
data: { | ||
delta: 1, | ||
}, | ||
matched: true, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused variable, import, function or class