|
| 1 | +import { statSync } from "fs"; |
| 2 | +import { ViewColumn } from "vscode"; |
| 3 | + |
| 4 | +import type { App } from "../common/app"; |
| 5 | +import { redactableError } from "../common/errors"; |
| 6 | +import type { |
| 7 | + FromComparePerformanceViewMessage, |
| 8 | + ToComparePerformanceViewMessage, |
| 9 | +} from "../common/interface-types"; |
| 10 | +import type { Logger } from "../common/logging"; |
| 11 | +import { showAndLogExceptionWithTelemetry } from "../common/logging"; |
| 12 | +import { extLogger } from "../common/logging/vscode"; |
| 13 | +import type { WebviewPanelConfig } from "../common/vscode/abstract-webview"; |
| 14 | +import { AbstractWebview } from "../common/vscode/abstract-webview"; |
| 15 | +import { withProgress } from "../common/vscode/progress"; |
| 16 | +import { telemetryListener } from "../common/vscode/telemetry"; |
| 17 | +import type { HistoryItemLabelProvider } from "../query-history/history-item-label-provider"; |
| 18 | +import { PerformanceOverviewScanner } from "../log-insights/performance-comparison"; |
| 19 | +import { scanLog } from "../log-insights/log-scanner"; |
| 20 | +import type { ResultsView } from "../local-queries"; |
| 21 | + |
| 22 | +export class ComparePerformanceView extends AbstractWebview< |
| 23 | + ToComparePerformanceViewMessage, |
| 24 | + FromComparePerformanceViewMessage |
| 25 | +> { |
| 26 | + constructor( |
| 27 | + app: App, |
| 28 | + public logger: Logger, |
| 29 | + public labelProvider: HistoryItemLabelProvider, |
| 30 | + private resultsView: ResultsView, |
| 31 | + ) { |
| 32 | + super(app); |
| 33 | + } |
| 34 | + |
| 35 | + async showResults(fromJsonLog: string, toJsonLog: string) { |
| 36 | + const panel = await this.getPanel(); |
| 37 | + panel.reveal(undefined, false); |
| 38 | + |
| 39 | + // Close the results viewer as it will have opened when the user clicked the query in the history view |
| 40 | + // (which they must do as part of the UI interaction for opening the performance view). |
| 41 | + // The performance view generally needs a lot of width so it's annoying to have the result viewer open. |
| 42 | + this.resultsView.hidePanel(); |
| 43 | + |
| 44 | + await this.waitForPanelLoaded(); |
| 45 | + |
| 46 | + function scanLogWithProgress(log: string, logDescription: string) { |
| 47 | + const bytes = statSync(log).size; |
| 48 | + return withProgress( |
| 49 | + async (progress) => |
| 50 | + scanLog(log, new PerformanceOverviewScanner(), progress), |
| 51 | + |
| 52 | + { |
| 53 | + title: `Scanning evaluator log ${logDescription} (${(bytes / 1024 / 1024).toFixed(1)} MB)`, |
| 54 | + }, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + const [fromPerf, toPerf] = await Promise.all([ |
| 59 | + fromJsonLog === "" |
| 60 | + ? new PerformanceOverviewScanner() |
| 61 | + : scanLogWithProgress(fromJsonLog, "1/2"), |
| 62 | + scanLogWithProgress(toJsonLog, fromJsonLog === "" ? "1/1" : "2/2"), |
| 63 | + ]); |
| 64 | + |
| 65 | + await this.postMessage({ |
| 66 | + t: "setPerformanceComparison", |
| 67 | + from: fromPerf.getData(), |
| 68 | + to: toPerf.getData(), |
| 69 | + comparison: fromJsonLog !== "", |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + protected getPanelConfig(): WebviewPanelConfig { |
| 74 | + return { |
| 75 | + viewId: "comparePerformanceView", |
| 76 | + title: "Compare CodeQL Performance", |
| 77 | + viewColumn: ViewColumn.Active, |
| 78 | + preserveFocus: true, |
| 79 | + view: "compare-performance", |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + protected onPanelDispose(): void {} |
| 84 | + |
| 85 | + protected async onMessage( |
| 86 | + msg: FromComparePerformanceViewMessage, |
| 87 | + ): Promise<void> { |
| 88 | + switch (msg.t) { |
| 89 | + case "viewLoaded": |
| 90 | + this.onWebViewLoaded(); |
| 91 | + break; |
| 92 | + |
| 93 | + case "telemetry": |
| 94 | + telemetryListener?.sendUIInteraction(msg.action); |
| 95 | + break; |
| 96 | + |
| 97 | + case "unhandledError": |
| 98 | + void showAndLogExceptionWithTelemetry( |
| 99 | + extLogger, |
| 100 | + telemetryListener, |
| 101 | + redactableError( |
| 102 | + msg.error, |
| 103 | + )`Unhandled error in performance comparison view: ${msg.error.message}`, |
| 104 | + ); |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments