fix(autoobserve): never number a service label onto another service's real name

Services still identical after every naming tier get #n. The number now skips any label another service already shows, so a service really named like a numbered label keeps it and every row stays distinct.
This commit is contained in:
Ashish Kolhe
2026-11-03 11:59:20 +05:30
parent b823bdc94b
commit ef4d17ad07
2 changed files with 21 additions and 3 deletions
@@ -329,4 +329,15 @@ describe("toCoverageSource service names", () => {
"web",
]);
});
it("never numbers a label onto the name another service really has", () => {
const labels = names([
svc("api", "prod"),
svc("api", "prod", "other"),
svc("api (prod/ns)", "elsewhere"),
svc("api (prod/ns) #1", "elsewhere"),
]);
expect(new Set(labels).size).toBe(4);
expect(labels[3]).toBe("api (prod/ns) #1");
});
});
@@ -293,9 +293,16 @@ const uniqueNames = (rows: AutoObserveCoverageRow[]) => {
}
const labels = rows.map((_, i) => labelOf(i));
const groups = groupBy(labels);
return labels.map((label, i) => {
const group = groups.get(label) ?? [i];
return group.length > 1 ? `${label} #${group.indexOf(i) + 1}` : label;
const taken = new Set(labels);
const lastNumber = new Map<string, number>();
return labels.map((label) => {
if ((groups.get(label)?.length ?? 1) < 2) return label;
let n = (lastNumber.get(label) ?? 0) + 1;
while (taken.has(`${label} #${n}`)) n += 1;
const numbered = `${label} #${n}`;
lastNumber.set(label, n);
taken.add(numbered);
return numbered;
});
};