Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 'noExperiments' context after trace open #297

Merged
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
157 changes: 83 additions & 74 deletions vscode-trace-extension/src/trace-explorer/trace-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,93 +63,101 @@ export const fileHandler =
cancellable: true
},
async (progress, token) => {
if (token.isCancellationRequested) {
progress.report({ message: ProgressMessages.COMPLETE, increment: 100 });
return;
}
try {
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 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);

// 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);
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);
}
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<TspTrace>();
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 (tracesArray.length === 0) {
progress.report({ message: ProgressMessages.COMPLETE, increment: 100 });
traceLogger.showError('No valid traces found in the selected directory: ' + resolvedTraceURI);
return;
}
}

if (token.isCancellationRequested) {
rollbackTraces(traces, 20, progress);
progress.report({ message: ProgressMessages.COMPLETE, increment: 50 });
return;
}
progress.report({ message: ProgressMessages.OPENING_TRACES, increment: 20 });
const traces = new Array<TspTrace>();
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.'
);
}
}

progress.report({ message: ProgressMessages.MERGING_TRACES, increment: 40 });
if (traces === undefined || traces.length === 0) {
progress.report({ message: ProgressMessages.COMPLETE, increment: 30 });
return;
}
if (token.isCancellationRequested) {
rollbackTraces(traces, 20, progress);
progress.report({ message: ProgressMessages.COMPLETE, increment: 50 });
return;
}

const experiment = await experimentManager.openExperiment(name, traces);
const panel = TraceViewerPanel.createOrShow(context.extensionUri, experiment?.name ?? name, undefined);
if (experiment) {
panel.setExperiment(experiment);
}
progress.report({ message: ProgressMessages.MERGING_TRACES, increment: 40 });
if (traces === undefined || traces.length === 0) {
progress.report({ message: ProgressMessages.COMPLETE, increment: 30 });
return;
}

if (token.isCancellationRequested) {
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);
}

if (token.isCancellationRequested) {
if (experiment) {
experimentManager.deleteExperiment(experiment.UUID);
}
rollbackTraces(traces, 20, progress);
progress.report({ message: ProgressMessages.COMPLETE, increment: 10 });
panel.dispose();
return;
}
rollbackTraces(traces, 20, progress);
progress.report({ message: ProgressMessages.COMPLETE, increment: 10 });
panel.dispose();
return;
progress.report({ message: ProgressMessages.COMPLETE, increment: 30 });
} finally {
updateNoExperimentsContext();
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a creative way of ensuring to update that context after that "leaky" block executes, without adding it in a bunch of places - I like

}
progress.report({ message: ProgressMessages.COMPLETE, increment: 30 });
}
);
};
Expand Down
Loading