-
Notifications
You must be signed in to change notification settings - Fork 20
feat: Add the ability to export the Entity System trend data to a csv #397
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,20 @@ | ||
| // Copyright (C) Microsoft Corporation. All rights reserved. | ||
|
|
||
| import { Disposable, Webview, WebviewPanel, window, Uri, ViewColumn } from 'vscode'; | ||
| import { Disposable, Webview, WebviewPanel, window, workspace, Uri, ViewColumn } from 'vscode'; | ||
| import { EventEmitter } from 'stream'; | ||
| import { getUri } from '../utilities/getUri'; | ||
| import { getNonce } from '../utilities/getNonce'; | ||
| import { DebuggerRequestHandler } from '../requests/debugger-request-handler'; | ||
| import { DiagnosticsTabDescriptor, StatData, StatsListener, StatsProvider } from '../stats/stats-provider'; | ||
|
|
||
| type ExportDataMessage = { | ||
| type: 'export-data'; | ||
| format: 'csv'; | ||
| mimeType?: string; | ||
| suggestedFileName?: string; | ||
| content?: string; | ||
| }; | ||
|
|
||
| export class MinecraftDiagnosticsPanel { | ||
| private static activeDiagnosticsPanels: MinecraftDiagnosticsPanel[] = []; | ||
|
|
||
|
|
@@ -37,7 +45,7 @@ | |
| this._panel.webview.html = this._getWebviewContent( | ||
| this._panel.webview, | ||
| extensionUri, | ||
| statsTracker.manualControl() | ||
| statsTracker.manualControl(), | ||
| ); | ||
|
|
||
| // Handle events from the webview panel | ||
|
|
@@ -48,7 +56,7 @@ | |
| this._panel.webview.html = this._getWebviewContent( | ||
| this._panel.webview, | ||
| extensionUri, | ||
| statsTracker.manualControl() | ||
| statsTracker.manualControl(), | ||
| ); | ||
| break; | ||
| case 'pause': | ||
|
|
@@ -74,6 +82,9 @@ | |
| case 'debugger-request': | ||
| this._debuggerRequestHandler.handleDebuggerRequest(message.request, message.args); | ||
| break; | ||
| case 'export-data': | ||
| void this.handleExportDataMessage(message as ExportDataMessage); | ||
| break; | ||
| default: | ||
| console.error('Unknown message type:', message.type); | ||
| break; | ||
|
|
@@ -128,10 +139,39 @@ | |
| this._statsTracker.addStatListener(this._statsCallback); | ||
| } | ||
|
|
||
| private async handleExportDataMessage(message: ExportDataMessage): Promise<void> { | ||
| if (typeof message.content !== 'string') { | ||
| console.error('Received export-data message without a valid content string.'); | ||
| return; | ||
| } | ||
|
|
||
| const suggestedFileName = | ||
| typeof message.suggestedFileName === 'string' && message.suggestedFileName.trim() !== '' | ||
| ? message.suggestedFileName | ||
| : 'diagnostics-export.csv'; | ||
| const workspaceFolderUri = workspace.workspaceFolders?.[0]?.uri; | ||
|
|
||
| const outputUri = await window.showSaveDialog({ | ||
| title: 'Export Diagnostics Data', | ||
| saveLabel: 'Export', | ||
| defaultUri: workspaceFolderUri ? Uri.joinPath(workspaceFolderUri, suggestedFileName) : undefined, | ||
| filters: { | ||
| 'CSV Files': ['csv'], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there's a warning here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the VS Code API fighting with our linter, these need to be human readable: https://code.visualstudio.com/api/references/vscode-api#SaveDialogOptions I'm thinking I might make a follow up story to do a pass on our linter warnings and see if we can adjust some configurations to make everything happy, as this has come up numerous times now. |
||
| }, | ||
| }); | ||
|
|
||
| if (!outputUri) { | ||
| return; | ||
| } | ||
|
|
||
| await workspace.fs.writeFile(outputUri, Buffer.from(message.content, 'utf8')); | ||
| window.showInformationMessage(`Exported diagnostics data to ${outputUri.fsPath}.`); | ||
| } | ||
|
|
||
| public static render(extensionUri: Uri, statsTracker: StatsProvider, eventEmitter: EventEmitter): void { | ||
| const statsTrackerId = statsTracker.uniqueId; | ||
| const existingPanel = MinecraftDiagnosticsPanel.activeDiagnosticsPanels.find( | ||
| panel => panel._statsTracker.uniqueId === statsTrackerId | ||
| panel => panel._statsTracker.uniqueId === statsTrackerId, | ||
| ); | ||
| if (existingPanel) { | ||
| existingPanel._panel.reveal(ViewColumn.One); | ||
|
|
@@ -147,10 +187,16 @@ | |
| Uri.joinPath(extensionUri, 'out'), | ||
| Uri.joinPath(extensionUri, 'webview-ui/build'), | ||
| ], | ||
| } | ||
| }, | ||
| ); | ||
| MinecraftDiagnosticsPanel.activeDiagnosticsPanels.push( | ||
| new MinecraftDiagnosticsPanel(panel, extensionUri, statsTracker, eventEmitter, new DebuggerRequestHandler(panel.webview)), | ||
| new MinecraftDiagnosticsPanel( | ||
| panel, | ||
| extensionUri, | ||
| statsTracker, | ||
| eventEmitter, | ||
| new DebuggerRequestHandler(panel.webview), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
@@ -163,7 +209,7 @@ | |
|
|
||
| // Remove the current panel from the active panel list | ||
| MinecraftDiagnosticsPanel.activeDiagnosticsPanels = MinecraftDiagnosticsPanel.activeDiagnosticsPanels.filter( | ||
| panel => panel !== this | ||
| panel => panel !== this, | ||
| ); | ||
|
|
||
| // Dispose of the current webview panel | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright (C) Microsoft Corporation. All rights reserved. | ||
|
|
||
| export type CsvExportRow = Record<string, string | number>; | ||
|
|
||
| export type DiagnosticsExportFormat = 'csv'; | ||
|
|
||
| export interface CsvExporter { | ||
| readonly format: 'csv'; | ||
| readonly fileExtension: string; | ||
| readonly mimeType: string; | ||
| exportRows(headers: string[], rows: CsvExportRow[]): string; | ||
| } | ||
|
|
||
| function escapeCsvValue(value: string | number): string { | ||
| const serialized = String(value); | ||
|
|
||
| if (!/[",\n\r]/.test(serialized)) { | ||
| return serialized; | ||
| } | ||
|
|
||
| return `"${serialized.replace(/"/g, '""')}"`; | ||
| } | ||
|
|
||
| function toCsvRow(values: (string | number)[]): string { | ||
| return values.map(escapeCsvValue).join(','); | ||
| } | ||
|
|
||
| export class TableCsvExporter implements CsvExporter { | ||
| public readonly format = 'csv' as const; | ||
|
|
||
| public readonly fileExtension = 'csv'; | ||
|
|
||
| public readonly mimeType = 'text/csv'; | ||
|
|
||
| public exportRows(headers: string[], rows: CsvExportRow[]): string { | ||
| const csvLines: string[] = [toCsvRow(headers)]; | ||
|
|
||
| rows.forEach(row => { | ||
| const values = headers.map(header => row[header] ?? ''); | ||
| csvLines.push(toCsvRow(values)); | ||
| }); | ||
|
|
||
| return csvLines.join('\n'); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.