Skip to content
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
88 changes: 59 additions & 29 deletions scripts/dev-portal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -448,21 +448,49 @@ <h2>No stacks running</h2>
`;
}

// Group stacks by worktree name so all envs for the same
// worktree appear in a single card.
// All envs for the same worktree appear in a single card.
const groups = [...groupByWorktree(stacks).values()];
const dupNames = duplicateNames(
groups.map(group => cardHeader(group).worktree),
);

return `<div class="stacks">${groups.map(group => renderWorktreeCard(group, dupNames)).join('')}</div>`;
}

// Group by full path so checkouts that share a folder name (a/hyperdx,
// b/hyperdx) get separate cards. Items with no known path fall back to
// their folder name.
function groupByWorktree(items) {
const groups = new Map();
for (const stack of stacks) {
const key = stack.worktree || 'unknown';
for (const item of items) {
const key = item.worktreePath || item.worktree || 'unknown';
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(stack);
groups.get(key).push(item);
}
return groups;
}

function duplicateNames(names) {
const seen = new Set();
const dups = new Set();
for (const name of names) (seen.has(name) ? dups : seen).add(name);
return dups;
}

return `<div class="stacks">${[...groups.values()].map(renderWorktreeCard).join('')}</div>`;
// The folder name, or the full path when another card has the same name.
function worktreeLabel(name, worktreePath, dupNames) {
return dupNames.has(name) && worktreePath ? worktreePath : name;
}

function renderWorktreeCard(stacks) {
// Use the dev stack for the header since it has the richest metadata.
const header = stacks.find(s => s.envType === 'dev') || stacks[0];
// Use the dev stack for the header since it has the richest metadata.
function cardHeader(stacks) {
return stacks.find(s => s.envType === 'dev') || stacks[0];
}

function renderWorktreeCard(stacks, dupNames) {
const header = cardHeader(stacks);
const worktreePath =
(stacks.find(s => s.worktreePath) || {}).worktreePath || '';
const devStack = stacks.find(s => s.envType === 'dev');
const appService =
devStack &&
Expand Down Expand Up @@ -506,7 +534,7 @@ <h2>No stacks running</h2>
<div style="display:flex;align-items:center">
<span class="branch">${escapeHtml(header.branch)}</span>
</div>
<div class="worktree">${escapeHtml(header.worktree)}</div>
<div class="worktree" title="${escapeHtml(worktreePath)}">${escapeHtml(worktreeLabel(header.worktree, worktreePath, dupNames))}</div>
</div>
<div class="stack-actions">
${chUrl ? `<a href="${chUrl}" target="_blank" class="open-btn open-btn-secondary">Open ClickHouse</a>` : ''}
Expand Down Expand Up @@ -652,7 +680,11 @@ <h2>No stacks running</h2>
function filterHistoryEntries(entries, query) {
if (!query) return entries;
return entries.filter(entry =>
fuzzyMatch(query, [entry.worktree || '', entry.branch || '']),
fuzzyMatch(query, [
entry.worktree || '',
entry.worktreePath || '',
entry.branch || '',
]),
);
}

Expand All @@ -678,13 +710,10 @@ <h2>No past runs</h2>

const filtered = filterHistoryEntries(entries, historySearchQuery);

// Group filtered entries by worktree
const groups = new Map();
for (const entry of filtered) {
const key = entry.worktree || 'unknown';
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(entry);
}
const groups = groupByWorktree(filtered);
const dupNames = duplicateNames(
[...groups.values()].map(group => group[0].worktree),
);

const noResults = filtered.length === 0;

Expand All @@ -698,29 +727,30 @@ <h2>Past Runs</h2>
noResults
? `<div class="empty-state"><p>No runs matching "${escapeHtml(historySearchQuery)}"</p></div>`
: `<div class="stacks">
${[...groups.entries()].map(([worktree, group]) => renderHistoryWorktreeCard(worktree, group)).join('')}
${[...groups.entries()].map(([key, group]) => renderHistoryWorktreeCard(key, group, dupNames)).join('')}
</div>`
}
`;
}

// Track which history cards are expanded (by worktree name).
// Track which history cards are expanded (by groupByWorktree key).
// All cards are collapsed by default.
const historyExpanded = new Set();

function toggleHistoryCard(worktree) {
if (historyExpanded.has(worktree)) {
historyExpanded.delete(worktree);
function toggleHistoryCard(key) {
if (historyExpanded.has(key)) {
historyExpanded.delete(key);
} else {
historyExpanded.add(worktree);
historyExpanded.add(key);
}
refreshHistory();
}

function renderHistoryWorktreeCard(worktree, entries) {
// Use the first entry for branch info
function renderHistoryWorktreeCard(key, entries, dupNames) {
// Use the first entry for branch and worktree info
const { worktree, worktreePath = '' } = entries[0];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — Card label comes from the per-run worktree name, not the canonical path, so a merged card's name changes with whichever run is newest

Cards are now grouped by the canonical (realpath) path, but the label still uses entries[0].worktree here and cardHeader(stacks).worktree on the Live tab (line 537). scripts/dev-env.sh:26 sets that name to basename "$PWD", which is the symlink's name. scripts/test-e2e.sh:101 and Makefile:38 use the basename of the git toplevel instead. Take a checkout opened through a symlink ~/hdx -> ~/code/hyperdx: its dev and e2e runs now land in one card, and that card reads "hdx" or "hyperdx" depending on which run sorts first. duplicateNames then compares these inconsistent names too. Fix: when worktreePath is set, use basename(worktreePath) as the display and duplicate-check name, and fall back to worktree only when there is no path. Doing this in the server, right where canonicalWorktreePath is applied, means both tabs get it.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

const branch = entries[0].branch || 'unknown';
const collapsed = !historyExpanded.has(worktree);
const collapsed = !historyExpanded.has(key);
const chevron = collapsed ? '\u25b6' : '\u25bc';
const count = entries.length;

Expand All @@ -732,10 +762,10 @@ <h2>Past Runs</h2>
<span class="branch">${escapeHtml(branch)}</span>
<span style="color:var(--text-muted);font-size:12px;margin-left:8px">${count} run${count !== 1 ? 's' : ''}</span>
</div>
<div class="worktree">${escapeHtml(worktree)}</div>
<div class="worktree" title="${escapeHtml(worktreePath)}">${escapeHtml(worktreeLabel(worktree, worktreePath, dupNames))}</div>
</div>
<div class="stack-actions">
<button class="history-toggle-btn" onclick="toggleHistoryCard('${escapeHtml(worktree)}')">${chevron}</button>
<button class="history-toggle-btn" onclick="toggleHistoryCard(${escapeHtml(JSON.stringify(key))})">${chevron}</button>
</div>
</div>
<div class="history-card-body${collapsed ? ' collapsed' : ''}" style="max-height:${collapsed ? 0 : entries.length * 200}px">
Expand Down
39 changes: 33 additions & 6 deletions scripts/dev-portal/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ function resolveGitRoot(dir) {
return root;
}

// The UI groups cards by this path, so it must come out the same for every
// stack of one checkout: slot files record $PWD (which may go through a
// symlink), and compose working_dir can be a subdirectory.
function canonicalWorktreePath(dir) {
if (!dir) return '';
const root = resolveGitRoot(dir) || dir;
try {
return fs.realpathSync(root);
} catch {
return root;
}
}

// Resolve git branch for a working directory. Cached per request cycle via
// the branchCache map passed in from the caller.
function resolveGitBranch(workingDir, branchCache) {
Expand Down Expand Up @@ -281,13 +294,12 @@ async function buildDashboardData() {
}

// For all envTypes: fall back to compose working_dir when missing.
// The working_dir may be a subdirectory (e.g. packages/app/tests/e2e
// for E2E containers), so resolve up to the git repo root.
const workingDir = labels['com.docker.compose.project.working_dir'] || '';
if (workingDir && !stack.worktreePath) {
stack.worktreePath = canonicalWorktreePath(workingDir);
}
if (workingDir && stack.worktree === 'unknown') {
const repoRoot = resolveGitRoot(workingDir) || workingDir;
stack.worktree = path.basename(repoRoot);
stack.worktreePath = repoRoot;
stack.worktree = path.basename(stack.worktreePath);
}
if (workingDir && stack.branch === 'unknown') {
stack.branch = resolveGitBranch(workingDir, branchCache);
Expand Down Expand Up @@ -325,7 +337,9 @@ async function buildDashboardData() {
if (stack.worktree === 'unknown' && data.worktree) {
stack.worktree = data.worktree;
}
stack.worktreePath = data.worktreePath || '';
if (data.worktreePath) {
stack.worktreePath = canonicalWorktreePath(data.worktreePath);
}

// Add API and App as services (probe their ports)
const apiUp = await probePort(data.apiPort);
Expand Down Expand Up @@ -723,12 +737,14 @@ function discoverHistory() {

// Resolve slot-level worktree/branch from the JSON file (if still alive)
let slotWorktree = null;
let slotWorktreePath = null;
let slotBranch = null;
const slotFile = path.join(SLOTS_DIR, `${slot}.json`);
try {
if (fs.existsSync(slotFile)) {
const data = JSON.parse(fs.readFileSync(slotFile, 'utf-8'));
slotWorktree = data.worktree || null;
slotWorktreePath = data.worktreePath || null;
slotBranch = data.branch || null;
if (!slotWorktree && data.worktreePath) {
slotWorktree = path.basename(data.worktreePath);
Expand All @@ -741,6 +757,7 @@ function discoverHistory() {
// Collect entries for this slot, reading meta.json where available
const slotEntries = [];
let metaWorktree = null;
let metaWorktreePath = null;
let metaBranch = null;

for (const runDir of fs.readdirSync(histDir)) {
Expand All @@ -764,16 +781,19 @@ function discoverHistory() {

// Read per-run meta.json
let runWorktree = null;
let runWorktreePath = null;
let runBranch = null;
const metaPath = path.join(runPath, 'meta.json');
try {
if (fs.existsSync(metaPath)) {
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
runWorktree = meta.worktree || null;
runWorktreePath = meta.worktreePath || null;
runBranch = meta.branch || null;
// Remember the first valid meta as a fallback for siblings
if (runWorktree && !metaWorktree) {
metaWorktree = runWorktree;
metaWorktreePath = runWorktreePath;
metaBranch = runBranch;
}
}
Expand All @@ -789,6 +809,7 @@ function discoverHistory() {
files,
totalSize,
worktree: runWorktree,
worktreePath: runWorktreePath,
branch: runBranch,
});
}
Expand All @@ -797,20 +818,26 @@ function discoverHistory() {
// priority: 1) sibling meta.json, 2) slot JSON file, 3) process.cwd()
// (only if this is the local slot).
let fallbackWorktree = metaWorktree || slotWorktree || null;
let fallbackWorktreePath = metaWorktree
? metaWorktreePath
: slotWorktreePath;
let fallbackBranch = metaBranch || slotBranch || null;
if (!fallbackWorktree && slot === localSlot) {
const cwd = process.cwd();
const repoRoot = resolveGitRoot(cwd);
if (repoRoot) {
fallbackWorktree = path.basename(repoRoot);
fallbackWorktreePath = repoRoot;
fallbackBranch = resolveGitBranch(cwd, new Map());
}
}

for (const entry of slotEntries) {
if (!entry.worktree) {
entry.worktree = fallbackWorktree || `slot-${slot}`;
entry.worktreePath = fallbackWorktreePath;
}
entry.worktreePath = canonicalWorktreePath(entry.worktreePath);
Comment thread
karl-power marked this conversation as resolved.
Resolved
if (!entry.branch) {
entry.branch = fallbackBranch || 'unknown';
}
Expand Down
Loading