Skip to content

Commit

Permalink
Merge pull request #4481 from relative-ci/fix-metrics-treemap-single-run
Browse files Browse the repository at this point in the history
fix metrics treemap single run
  • Loading branch information
vio authored May 24, 2024
2 parents 867413a + f6554ba commit 4619a91
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
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

0 comments on commit 4619a91

Please sign in to comment.