Usage Entity labels

This commit is contained in:
yuneng-jiang
2025-12-12 12:13:51 -08:00
parent 1531b58493
commit 5b0bf86ee5
4 changed files with 34 additions and 7 deletions
@@ -265,7 +265,7 @@ describe("EntityUsage", () => {
});
expect(await screen.findByText("Tag Spend Overview")).toBeInTheDocument();
expect(await screen.findByText("$-")).toBeInTheDocument();
expect(await screen.findByText("$0.00")).toBeInTheDocument();
expect(screen.getByText("Total Spend")).toBeInTheDocument();
expect(screen.getAllByText("0")[0]).toBeInTheDocument();
});
@@ -305,6 +305,20 @@ const EntityUsage: React.FC<EntityUsageProps> = ({
}
};
const getEntityLabel = (entity: string, metadata?: Record<string, any>): string => {
if (entityList) {
const entityItem = entityList.find((item) => item.value === entity);
if (entityItem) {
return entityItem.label;
}
}
// Fallback to team_alias for backward compatibility
if (metadata?.team_alias) {
return metadata.team_alias;
}
return entity;
};
const filterDataByTags = (data: EntityMetricWithMetadata[]) => {
if (selectedTags.length === 0) return data;
return data.filter((item) => selectedTags.includes(item.metadata.id));
@@ -328,7 +342,7 @@ const EntityUsage: React.FC<EntityUsageProps> = ({
cache_creation_input_tokens: 0,
},
metadata: {
alias: (data.metadata as any).team_alias || entity,
alias: getEntityLabel(entity, data.metadata as any),
id: entity,
},
};
@@ -472,7 +486,7 @@ const EntityUsage: React.FC<EntityUsageProps> = ({
const metrics = entityData as EntityMetrics;
return (
<p key={entity} className="text-sm text-gray-600">
{metrics.metadata.team_alias || entity}: $
{getEntityLabel(entity, metrics.metadata)}: $
{formatNumberWithCommas(metrics.metrics.spend, 2)}
</p>
);
@@ -72,8 +72,8 @@ describe("dataUtils", () => {
});
it("should handle zero and non-finite values", () => {
expect(formatNumberWithCommas(0)).toBe("-");
expect(formatNumberWithCommas(0, 2)).toBe("-");
expect(formatNumberWithCommas(0)).toBe("0");
expect(formatNumberWithCommas(0, 2)).toBe("0.00");
expect(formatNumberWithCommas(Infinity)).toBe("-");
expect(formatNumberWithCommas(Number.NaN)).toBe("-");
});
@@ -83,6 +83,18 @@ describe("dataUtils", () => {
expect(formatNumberWithCommas(12_345, 2, true)).toBe("12.35K");
expect(formatNumberWithCommas(-1_200, 2, true)).toBe("-1.20K");
});
it("should show zero when showZero is true", () => {
expect(formatNumberWithCommas(0, 0, false, true)).toBe("0");
expect(formatNumberWithCommas(0, 2, false, true)).toBe("0.00");
expect(formatNumberWithCommas(0, 0, true, true)).toBe("0");
});
it("should return '-' for zero when showZero is false", () => {
expect(formatNumberWithCommas(0, 0, false, false)).toBe("-");
expect(formatNumberWithCommas(0, 2, false, false)).toBe("-");
expect(formatNumberWithCommas(0, 0, true, false)).toBe("-");
});
});
describe("getSpendString", () => {
+3 -2
View File
@@ -16,8 +16,9 @@ export const formatNumberWithCommas = (
value: number | null | undefined,
decimals: number = 0,
abbreviate: boolean = false,
showZero: boolean = true,
): string => {
if (value === null || value === undefined || !Number.isFinite(value) || value === 0) {
if (value === null || value === undefined || !Number.isFinite(value) || (value === 0 && !showZero)) {
return "-";
}
@@ -51,7 +52,7 @@ export const getSpendString = (value: number | null | undefined, decimals: numbe
return "-";
}
const formatted = formatNumberWithCommas(value, decimals);
const formatted = formatNumberWithCommas(value, decimals, false, false);
const numericFormatted = Number(formatted.replace(/,/g, ""));
if (numericFormatted === 0) {