Skip to content

Commit

Permalink
Use asExternalUri For Server Url
Browse files Browse the repository at this point in the history
This allows for automatic port forwarding of trace-server api calls when using remote.

Signed-off-by: Will Yang <[email protected]>
  • Loading branch information
williamsyang-work committed Oct 18, 2023
1 parent ba68345 commit 3c7802d
Show file tree
Hide file tree
Showing 14 changed files with 1,660 additions and 1,480 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"workbench.colorCustomizations": {
"activityBar.background": "#273111",
"titleBar.activeBackground": "#374518",
"titleBar.activeForeground": "#F8FBF2"
},
}
11 changes: 10 additions & 1 deletion vscode-trace-common/src/client/tsp-client-provider-impl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TspClient } from 'tsp-typescript-client/lib/protocol/tsp-client';
import { RestClient, ConnectionStatusListener } from 'tsp-typescript-client/lib/protocol/rest-client';
import { ServerStateManager } from 'tsp-typescript-client/lib/protocol/request-manager';
import { ExperimentManager } from 'traceviewer-base/lib/experiment-manager';
import { TraceManager } from 'traceviewer-base/lib/trace-manager';
import { ITspClientProvider } from 'traceviewer-base/lib/tsp-client-provider';
Expand Down Expand Up @@ -29,7 +30,6 @@ export class TspClientProvider implements ITspClientProvider {
this._signalHandler?.notifyConnection(status);
};
RestClient.addConnectionStatusListener(this._statusListener);
this._tspClient.checkHealth();

this._urlProvider = _urlProvider;
this._listeners = [];
Expand All @@ -53,6 +53,10 @@ export class TspClientProvider implements ITspClientProvider {
return this._experimentManager;
}

public getRequestManager(): typeof ServerStateManager {
return ServerStateManager;
}

/**
* Add a listener for trace server url changes
* @param listener The listener function to be called when the url is
Expand All @@ -62,3 +66,8 @@ export class TspClientProvider implements ITspClientProvider {
this._listeners.push(listener);
}
}

// Perhaps this should live somewhere else or in its own file.
export const cancelAllOngoingRequests = (): void => {
ServerStateManager.cancelAllRequests();
}
13 changes: 12 additions & 1 deletion vscode-trace-common/src/messages/vscode-message-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export const VSCODE_MESSAGES = {
REQUEST_SELECTION_RANGE_CHANGE: 'requestSelectionRangeChange',
RESTORE_VIEW: 'restoreView',
RESTORE_COMPLETE: 'restoreComplete',
OUTPUT_DATA_CHANGED: 'outputDataChanged'
OUTPUT_DATA_CHANGED: 'outputDataChanged',
CANCEL_REQUESTS: 'cancelRequests',
TRACE_SERVER_STATUS: 'traceServerStatus',
START_SERVER: 'startServer',
};

export class VsCodeMessageManager extends Messages.MessageManager {
Expand Down Expand Up @@ -193,4 +196,12 @@ export class VsCodeMessageManager extends Messages.MessageManager {
const status: string = JSON.stringify(context);
vscode.postMessage({ command: VSCODE_MESSAGES.MARKER_CATEGORIES_CONTEXT, data: { status } });
}

cancelRequests(): void {
vscode.postMessage({ command: VSCODE_MESSAGES.CANCEL_REQUESTS });
}

startServer(): void {
vscode.postMessage({ command: VSCODE_MESSAGES.START_SERVER });
}
}
11 changes: 8 additions & 3 deletions vscode-trace-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,28 @@
"type": "webview",
"id": "traceExplorer.availableViews",
"name": "Views",
"when": "!trace-explorer.noExperiments"
"when": "!trace-explorer.noExperiments && traceViewer.serverOn"
},
{
"type": "webview",
"id": "traceExplorer.timeRangeDataView",
"name": "Time Range Data",
"when": "!trace-explorer.noExperiments"
"when": "!trace-explorer.noExperiments && traceViewer.serverOn"
},
{
"type": "webview",
"id": "traceExplorer.itemPropertiesView",
"name": "Item Properties",
"when": "!trace-explorer.noExperiments"
"when": "!trace-explorer.noExperiments && traceViewer.serverOn"
}
]
},
"viewsWelcome": [
{
"view": "activitybar",
"contents": "The Trace Server is currently offline. You can start it by executing the command or pressing the button below. \n [Start Trace Server](command:traceViewer.startServer)",
"when": "traceViewer.serverOn == false"
},
{
"view": "traces",
"contents": "No traces found [learn more](https://www.eclipse.org/tracecompass/).\n[Add traces](command:nodeDependencies.addEntry)"
Expand Down
102 changes: 65 additions & 37 deletions vscode-trace-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const tspClientProvider = new TspClientProvider(getTspClientUrl(), undefined, ne

export function activate(context: vscode.ExtensionContext): ExternalAPI {
traceLogger = new TraceExtensionLogger('Trace Extension');

vscode.commands.executeCommand('setContext', 'traceViewer.serverOn', true);
const serverStatusBarItemPriority = 1;
const serverStatusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right,
serverStatusBarItemPriority
);
context.subscriptions.push(serverStatusBarItem);
const serverStatusService = new TraceServerConnectionStatusService(serverStatusBarItem);
const serverStatusService = new TraceServerConnectionStatusService(serverStatusBarItem, isUp);

const tracesProvider = new TraceExplorerOpenedTracesViewProvider(context.extensionUri, serverStatusService);
context.subscriptions.push(
Expand Down Expand Up @@ -66,17 +66,39 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {
})
);

const updateUris = async (): Promise<void> => {
const baseUri = vscode.Uri.parse(getTraceServerUrl());
const extUri = await vscode.env.asExternalUri(baseUri);
const extUriString = extUri.toString();

tracesProvider.updateTraceServerUrl(extUriString);
myAnalysisProvider.updateTraceServerUrl(extUriString);
TraceViewerPanel.updateTraceServerUrl(extUriString);
};

const emitServerStatus = (status: boolean) => {
console.log('Emitting the server status as: ', status);
TraceViewerPanel.setServerStatus(status);
myAnalysisProvider.setServerStatus(status);
tracesProvider.setServerStatus(status);
}

const cancelAllOngoingRequests = () => {
TraceViewerPanel.cancelHttpRequests();
myAnalysisProvider.cancelHttpRequests();
tracesProvider.cancelHttpRequests();
}

const analysisProvider = new AnalysisProvider();
// TODO: For now, a different command opens traces from file explorer. Remove when we have a proper trace finder
const fileOpenHandler = fileHandler(analysisProvider);
context.subscriptions.push(
vscode.commands.registerCommand('traces.openTraceFile', async file => {
await startTraceServerIfAvailable();
if (await isUp()) {
fileOpenHandler(context, file);
}
})
);
context.subscriptions.push(vscode.commands.registerCommand('traces.openTraceFile', async file => {
await startTraceServerIfAvailable();
await updateUris();
if (await isUp()) {
fileOpenHandler(context, file);
}
}));

// Listening to configuration change for the trace server URL
context.subscriptions.push(
Expand All @@ -88,18 +110,10 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {
updateTspClient();
}

if (e.affectsConfiguration('trace-compass.traceserver.url')) {
const newUrl = getTraceServerUrl();

// Signal the change to the `Opened traces` and `Available views` webview
tracesProvider.updateTraceServerUrl(newUrl);
myAnalysisProvider.updateTraceServerUrl(newUrl);

// Signal the change to all trace panels
TraceViewerPanel.updateTraceServerUrl(newUrl);
}
})
);
if (e.affectsConfiguration('trace-compass.traceserver.url')) {
updateUris();
}
}));

const overViewOpenHandler = openOverviewHandler();

Expand Down Expand Up @@ -152,36 +166,50 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {
})
);

context.subscriptions.push(
vscode.commands.registerCommand('openedTraces.openTraceFolder', async () => {
await startTraceServerIfAvailable();
if (await isUp()) {
fileOpenHandler(context, undefined);
}
})
);
context.subscriptions.push(vscode.commands.registerCommand('openedTraces.openTraceFolder', async () => {
await startTraceServerIfAvailable();
await updateUris();
if (await isUp()) {
fileOpenHandler(context, undefined);
}
}));

context.subscriptions.push(
vscode.commands.registerCommand('traceViewer.shortcuts', () => {
keyboardShortcutsHandler(context.extensionUri);
})
);

context.subscriptions.push(vscode.commands.registerCommand('serverStatus.started', () => {
serverStatusService.render(true);
emitServerStatus(true);
cancelAllOngoingRequests();
vscode.commands.executeCommand('setContext', 'traceViewer.serverOn', true);
updateUris();
if (tracesProvider) {
tracesProvider.postMessagetoWebview(VSCODE_MESSAGES.TRACE_SERVER_STARTED, undefined);
}
}));

context.subscriptions.push(
vscode.commands.registerCommand('serverStatus.started', () => {
serverStatusService.render(true);
if (tracesProvider) {
tracesProvider.postMessagetoWebview(VSCODE_MESSAGES.TRACE_SERVER_STARTED, undefined);
}
vscode.commands.registerCommand('serverStatus.stopped', () => {
vscode.commands.executeCommand('setContext', 'traceViewer.serverOn', false);
serverStatusService.render(false);
cancelAllOngoingRequests();
emitServerStatus(false);
})
);

context.subscriptions.push(
vscode.commands.registerCommand('serverStatus.stopped', () => {
serverStatusService.render(false);
vscode.commands.registerCommand('traceViewer.startServer', () => {
startTraceServerIfAvailable();
})
);

isUp()
.then(status => vscode.commands.executeCommand('setContext', 'traceViewer.serverOn', status))
.catch(()=> vscode.commands.executeCommand('setContext', 'traceViewer.serverOn', false));

vscode.commands.executeCommand('setContext', 'traceViewer.markerSetsPresent', false);
vscode.commands.executeCommand('setContext', 'traceViewer.markerCategoriesPresent', false);
return traceExtensionAPI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ export class TraceExplorerAvailableViewsProvider implements vscode.WebviewViewPr
this._statusService.render(status);
}
return;
case VSCODE_MESSAGES.WEBVIEW_READY:
// Post the tspTypescriptClient
webviewView.webview.postMessage({
command: VSCODE_MESSAGES.SET_TSP_CLIENT,
data: getTspClientUrl()
});
if (this._selectedExperiment !== undefined) {
signalManager().fireExperimentSelectedSignal(this._selectedExperiment);
}
return;
case VSCODE_MESSAGES.WEBVIEW_READY:
// Post the tspTypescriptClient
webviewView.webview.postMessage({
command: VSCODE_MESSAGES.SET_TSP_CLIENT,
data: getTspClientUrl()
});
if (this._selectedExperiment !== undefined) {
signalManager().fireExperimentSelectedSignal(this._selectedExperiment);
}
return;
case VSCODE_MESSAGES.OUTPUT_ADDED:
if (message.data && message.data.descriptor) {
// FIXME: JSONBig.parse() created bigint if numbers are small.
Expand Down Expand Up @@ -126,6 +126,14 @@ export class TraceExplorerAvailableViewsProvider implements vscode.WebviewViewPr
}
}

public cancelHttpRequests(): void {
this._view?.webview.postMessage({ command: VSCODE_MESSAGES.CANCEL_REQUESTS });
}

public setServerStatus(status: boolean): void {
this._view?.webview.postMessage({ command: VSCODE_MESSAGES.TRACE_SERVER_STATUS, data: status.toString() });
}

/* eslint-disable max-len */
private _getHtmlForWebview(webview: vscode.Webview) {
// Get the local path to main script run in the webview, then convert it to a uri we can use in the webview.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class TraceExplorerOpenedTracesViewProvider implements vscode.WebviewView
// experimentSelectedSignal will update available views widget
signalManager().fireExperimentSelectedSignal(this._selectedExperiment);
}
this._statusService.serverStatus().then(stat => this.setServerStatus(stat));
return;
case VSCODE_MESSAGES.RE_OPEN_TRACE:
if (message.data && message.data.wrapper) {
Expand Down Expand Up @@ -132,15 +133,19 @@ export class TraceExplorerOpenedTracesViewProvider implements vscode.WebviewView
case VSCODE_MESSAGES.OPEN_TRACE:
vscode.commands.executeCommand('openedTraces.openTraceFolder');
return;
case VSCODE_MESSAGES.EXPERIMENT_SELECTED: {
let experiment: Experiment | undefined;
if (message.data && message.data.wrapper) {
experiment = convertSignalExperiment(JSONBig.parse(message.data.wrapper));
} else {
experiment = undefined;
case VSCODE_MESSAGES.EXPERIMENT_SELECTED:
if (true) {
let experiment: Experiment | undefined;
if (message.data && message.data.wrapper) {
experiment = convertSignalExperiment(JSONBig.parse(message.data.wrapper));
} else {
experiment = undefined;
}
signalManager().fireExperimentSelectedSignal(experiment);
return;
}
signalManager().fireExperimentSelectedSignal(experiment);
}
case VSCODE_MESSAGES.START_SERVER:
vscode.commands.executeCommand('traceViewer.startServer');
}
},
undefined,
Expand Down Expand Up @@ -170,6 +175,15 @@ export class TraceExplorerOpenedTracesViewProvider implements vscode.WebviewView
}
}

public cancelHttpRequests(): void {
this._view?.webview.postMessage({ command: VSCODE_MESSAGES.CANCEL_REQUESTS });
}

public setServerStatus(status: boolean): void {
console.log(`Opened Traces - The server is ${status}`);
this?._view?.webview.postMessage({ command: VSCODE_MESSAGES.TRACE_SERVER_STATUS, data: status.toString() });
}

/* eslint-disable max-len */
private _getHtmlForWebview(webview: vscode.Webview) {
// Get the local path to main script run in the webview, then convert it to a uri we can use in the webview.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class TraceViewerPanel {

private static readonly viewType = 'react';
private static currentPanel: TraceViewerPanel | undefined;

private readonly _panel: vscode.WebviewPanel;
private readonly _extensionUri: vscode.Uri;
private readonly _statusService: TraceServerConnectionStatusService | undefined;
Expand Down Expand Up @@ -144,6 +144,20 @@ export class TraceViewerPanel {
);
}

public static cancelHttpRequests(): void {
Object.values(TraceViewerPanel.activePanels).forEach(
trace =>
trace?._panel.webview.postMessage({ command: VSCODE_MESSAGES.CANCEL_REQUESTS })
);
}

public static setServerStatus(status: boolean): void {
Object.values(TraceViewerPanel.activePanels).forEach(
trace =>
trace?._panel.webview.postMessage({ command: VSCODE_MESSAGES.TRACE_SERVER_STATUS, data: status.toString() })
);
}

private constructor(
extensionUri: vscode.Uri,
column: vscode.ViewColumn,
Expand Down Expand Up @@ -233,6 +247,7 @@ export class TraceViewerPanel {
data: getTspClientUrl()
});
}
this._statusService?.serverStatus().then(TraceViewerPanel.setServerStatus);
this.loadTheme();
return;
case VSCODE_MESSAGES.UPDATE_PROPERTIES:
Expand Down
Loading

0 comments on commit 3c7802d

Please sign in to comment.