Skip to content
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

fix metrics treemap single run #4481

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ReportMetricRow } from '@bundle-stats/utils';

export interface TreeTotal {
current: number;
baseline: number;
baseline?: number;
}

export interface TreeLeaf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ const TileGroup = (props: TileGroupProps) => {
return [];
}

console.info({ total });

const resolvedRunInfo = getMetricRunInfo(
METRIC_TYPE_CONFIGS.METRIC_TYPE_FILE_SIZE,
total.current,
Expand Down
29 changes: 18 additions & 11 deletions packages/ui/src/components/metrics-treemap/metrics-treemap.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ function getItemValues(runs: ReportMetricRow['runs']): TreeTotal {
const runCount = runs?.length;
const current = runs[0]?.value || 0;

if (runCount < 2) {
return { current, baseline: 0 };
if (runs.length < 2) {
return { current, baseline: undefined };
}

return {
current,
baseline: runs[runCount - 1]?.value || 0,
baseline: runs?.[runCount - 1]?.value,
};
}

Expand All @@ -85,6 +85,10 @@ function setTreeNode(
// Check for existing parent in current nodes
let parentNode = nodes.find((treeNode) => treeNode.id === currentNodePath) as Tree;

// accumulate children values
const nodeValues = getItemValues(newNode?.item?.runs);
const hasBaseline = typeof nodeValues.baseline !== 'undefined';

// Create the new parentNode if missing
if (!parentNode) {
parentNode = {
Expand All @@ -94,19 +98,18 @@ function setTreeNode(
children: [],
total: {
current: 0,
baseline: 0,
baseline: hasBaseline ? 0 : undefined,
},
};

nodes.push(parentNode);
}

// accumulate children values
const nodeValues = getItemValues(newNode?.item?.runs);

parentNode.total = {
current: (parentNode.total?.current || 0) + nodeValues.current,
baseline: (parentNode.total?.baseline || 0) + (nodeValues.baseline || 0),
baseline: hasBaseline
? (parentNode.total?.baseline || 0) + (nodeValues.baseline || 0)
: undefined,
};

// If there are no other slugs, the new node is as leaf and we add it to the parent
Expand All @@ -123,7 +126,7 @@ function setTreeNode(
*/
export function getTreemapNodesGroupedByPath(items: Array<ReportMetricRow>): Tree {
const treeNodes: TreeNodeChildren = [];
const total = { current: 0, baseline: 0 };
const total: TreeTotal = { current: 0, baseline: undefined };

items.forEach((item) => {
const slugs = item.key.split('/');
Expand All @@ -138,9 +141,13 @@ export function getTreemapNodesGroupedByPath(items: Array<ReportMetricRow>): Tre
};

const childrenTotal = setTreeNode(treeNodes, baseSlugs, 0, treeNode);
console.log(childrenTotal);

total.current += childrenTotal.current || 0;
total.baseline += childrenTotal.baseline || 0;
total.current += childrenTotal.current;
total.baseline =
typeof childrenTotal.baseline !== 'undefined'
? (total.baseline || 0) + childrenTotal.baseline
: undefined;
});

return {
Expand Down
Loading