diff --git a/vscode-trace-extension/src/trace-explorer/trace-utils.ts b/vscode-trace-extension/src/trace-explorer/trace-utils.ts index c8973a2..41bdab0 100644 --- a/vscode-trace-extension/src/trace-explorer/trace-utils.ts +++ b/vscode-trace-extension/src/trace-explorer/trace-utils.ts @@ -4,6 +4,7 @@ import { Trace as TspTrace } from 'tsp-typescript-client/lib/models/trace'; import { TraceViewerPanel } from '../trace-viewer-panel/trace-viewer-webview-panel'; import { getExperimentManager, getTraceManager } from '../utils/backend-tsp-client-provider'; import { traceLogger } from '../extension'; +import { updateNoExperimentsContext } from '../utils/backend-tsp-client-provider'; import { KeyboardShortcutsPanel } from '../trace-viewer-panel/keyboard-shortcuts-panel'; // eslint-disable-next-line no-shadow @@ -62,93 +63,98 @@ export const fileHandler = cancellable: true }, async (progress, token) => { - if (token.isCancellationRequested) { - progress.report({ message: ProgressMessages.COMPLETE, increment: 100 }); - return; - } - - const filePath: string = resolvedTraceURI.fsPath; - if (!filePath) { - traceLogger.showError( - 'Cannot open trace: could not retrieve path from URI for trace ' + resolvedTraceURI - ); - return; - } - - const name = path.basename(filePath); - progress.report({ message: ProgressMessages.FINDING_TRACES, increment: 10 }); - /* - * TODO: use backend service to find traces - */ - const tracesArray: string[] = []; - const fileStat = await vscode.workspace.fs.stat(resolvedTraceURI); - if (fileStat) { - if (fileStat.type === vscode.FileType.Directory) { - // Find recursively CTF traces - const foundTraces = await findTraces(filePath); + try { - // No CTF traces found. Add root directory as trace directory. - // Back-end will reject if it is not a trace - if (foundTraces.length === 0) { - foundTraces.push(filePath); - } - foundTraces.forEach(trace => tracesArray.push(trace)); - } else { - // Open single trace file - tracesArray.push(filePath); + if (token.isCancellationRequested) { + progress.report({ message: ProgressMessages.COMPLETE, increment: 100 }); + return; } - } - - if (tracesArray.length === 0) { - progress.report({ message: ProgressMessages.COMPLETE, increment: 100 }); - traceLogger.showError('No valid traces found in the selected directory: ' + resolvedTraceURI); - return; - } - - progress.report({ message: ProgressMessages.OPENING_TRACES, increment: 20 }); - const traces = new Array(); - for (let i = 0; i < tracesArray.length; i++) { - const traceName = path.basename(tracesArray[i]); - const trace = await traceManager.openTrace(tracesArray[i], traceName); - if (trace) { - traces.push(trace); - } else { + + const filePath: string = resolvedTraceURI.fsPath; + if (!filePath) { traceLogger.showError( - 'Failed to open trace: ' + - traceName + - '. There may be an issue with the server or the trace is invalid.' + 'Cannot open trace: could not retrieve path from URI for trace ' + resolvedTraceURI ); + return; } - } - - if (token.isCancellationRequested) { - rollbackTraces(traces, 20, progress); - progress.report({ message: ProgressMessages.COMPLETE, increment: 50 }); - return; - } - - progress.report({ message: ProgressMessages.MERGING_TRACES, increment: 40 }); - if (traces === undefined || traces.length === 0) { - progress.report({ message: ProgressMessages.COMPLETE, increment: 30 }); - return; - } - - const experiment = await experimentManager.openExperiment(name, traces); - const panel = TraceViewerPanel.createOrShow(context.extensionUri, experiment?.name ?? name, undefined); - if (experiment) { - panel.setExperiment(experiment); - } - - if (token.isCancellationRequested) { + + const name = path.basename(filePath); + progress.report({ message: ProgressMessages.FINDING_TRACES, increment: 10 }); + /* + * TODO: use backend service to find traces + */ + const tracesArray: string[] = []; + const fileStat = await vscode.workspace.fs.stat(resolvedTraceURI); + if (fileStat) { + if (fileStat.type === vscode.FileType.Directory) { + // Find recursively CTF traces + const foundTraces = await findTraces(filePath); + + // No CTF traces found. Add root directory as trace directory. + // Back-end will reject if it is not a trace + if (foundTraces.length === 0) { + foundTraces.push(filePath); + } + foundTraces.forEach(trace => tracesArray.push(trace)); + } else { + // Open single trace file + tracesArray.push(filePath); + } + } + + if (tracesArray.length === 0) { + progress.report({ message: ProgressMessages.COMPLETE, increment: 100 }); + traceLogger.showError('No valid traces found in the selected directory: ' + resolvedTraceURI); + return; + } + + progress.report({ message: ProgressMessages.OPENING_TRACES, increment: 20 }); + const traces = new Array(); + for (let i = 0; i < tracesArray.length; i++) { + const traceName = path.basename(tracesArray[i]); + const trace = await traceManager.openTrace(tracesArray[i], traceName); + if (trace) { + traces.push(trace); + } else { + traceLogger.showError( + 'Failed to open trace: ' + + traceName + + '. There may be an issue with the server or the trace is invalid.' + ); + } + } + + if (token.isCancellationRequested) { + rollbackTraces(traces, 20, progress); + progress.report({ message: ProgressMessages.COMPLETE, increment: 50 }); + return; + } + + progress.report({ message: ProgressMessages.MERGING_TRACES, increment: 40 }); + if (traces === undefined || traces.length === 0) { + progress.report({ message: ProgressMessages.COMPLETE, increment: 30 }); + return; + } + + const experiment = await experimentManager.openExperiment(name, traces); + const panel = TraceViewerPanel.createOrShow(context.extensionUri, experiment?.name ?? name, undefined); if (experiment) { - experimentManager.deleteExperiment(experiment.UUID); + panel.setExperiment(experiment); } - rollbackTraces(traces, 20, progress); - progress.report({ message: ProgressMessages.COMPLETE, increment: 10 }); - panel.dispose(); - return; + + if (token.isCancellationRequested) { + if (experiment) { + experimentManager.deleteExperiment(experiment.UUID); + } + rollbackTraces(traces, 20, progress); + progress.report({ message: ProgressMessages.COMPLETE, increment: 10 }); + panel.dispose(); + return; + } + progress.report({ message: ProgressMessages.COMPLETE, increment: 30 }); + } finally { + updateNoExperimentsContext(); } - progress.report({ message: ProgressMessages.COMPLETE, increment: 30 }); } ); };