|
1 | | -import { ExtensionContext, ViewColumn } from "vscode"; |
| 1 | +import { CancellationTokenSource, ExtensionContext, ViewColumn } from "vscode"; |
2 | 2 | import { AbstractWebview, WebviewPanelConfig } from "../abstract-webview"; |
3 | 3 | import { |
4 | 4 | FromDataExtensionsEditorMessage, |
5 | 5 | ToDataExtensionsEditorMessage, |
6 | 6 | } from "../pure/interface-types"; |
| 7 | +import { ProgressUpdate } from "../progress"; |
| 8 | +import { extLogger, TeeLogger } from "../common"; |
| 9 | +import { CoreCompletedQuery, QueryRunner } from "../queryRunner"; |
| 10 | +import { qlpackOfDatabase } from "../contextual/queryResolver"; |
| 11 | +import { file } from "tmp-promise"; |
| 12 | +import { writeFile } from "fs-extra"; |
| 13 | +import { dump } from "js-yaml"; |
| 14 | +import { |
| 15 | + getOnDiskWorkspaceFolders, |
| 16 | + showAndLogExceptionWithTelemetry, |
| 17 | +} from "../helpers"; |
| 18 | +import { DatabaseItem } from "../local-databases"; |
| 19 | +import { CodeQLCliServer } from "../cli"; |
| 20 | +import { decodeBqrsToExternalApiUsages } from "./bqrs"; |
| 21 | +import { redactableError } from "../pure/errors"; |
| 22 | +import { asError, getErrorMessage } from "../pure/helpers-pure"; |
7 | 23 |
|
8 | 24 | export class DataExtensionsEditorView extends AbstractWebview< |
9 | 25 | ToDataExtensionsEditorMessage, |
10 | 26 | FromDataExtensionsEditorMessage |
11 | 27 | > { |
12 | | - public constructor(ctx: ExtensionContext) { |
| 28 | + public constructor( |
| 29 | + ctx: ExtensionContext, |
| 30 | + private readonly cliServer: CodeQLCliServer, |
| 31 | + private readonly queryRunner: QueryRunner, |
| 32 | + private readonly queryStorageDir: string, |
| 33 | + private readonly databaseItem: DatabaseItem, |
| 34 | + ) { |
13 | 35 | super(ctx); |
14 | 36 | } |
15 | 37 |
|
@@ -49,5 +71,154 @@ export class DataExtensionsEditorView extends AbstractWebview< |
49 | 71 |
|
50 | 72 | protected async onWebViewLoaded() { |
51 | 73 | super.onWebViewLoaded(); |
| 74 | + |
| 75 | + await this.loadExternalApiUsages(); |
| 76 | + } |
| 77 | + |
| 78 | + protected async loadExternalApiUsages(): Promise<void> { |
| 79 | + try { |
| 80 | + const queryResult = await this.runQuery(); |
| 81 | + if (!queryResult) { |
| 82 | + await this.clearProgress(); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + await this.showProgress({ |
| 87 | + message: "Loading results", |
| 88 | + step: 1100, |
| 89 | + maxStep: 1500, |
| 90 | + }); |
| 91 | + |
| 92 | + const bqrsPath = queryResult.outputDir.bqrsPath; |
| 93 | + |
| 94 | + const bqrsChunk = await this.getResults(bqrsPath); |
| 95 | + if (!bqrsChunk) { |
| 96 | + await this.clearProgress(); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + await this.showProgress({ |
| 101 | + message: "Finalizing results", |
| 102 | + step: 1450, |
| 103 | + maxStep: 1500, |
| 104 | + }); |
| 105 | + |
| 106 | + const externalApiUsages = decodeBqrsToExternalApiUsages(bqrsChunk); |
| 107 | + |
| 108 | + await this.postMessage({ |
| 109 | + t: "setExternalApiUsages", |
| 110 | + externalApiUsages, |
| 111 | + }); |
| 112 | + |
| 113 | + await this.clearProgress(); |
| 114 | + } catch (err) { |
| 115 | + void showAndLogExceptionWithTelemetry( |
| 116 | + redactableError( |
| 117 | + asError(err), |
| 118 | + )`Failed to load external APi usages: ${getErrorMessage(err)}`, |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private async runQuery(): Promise<CoreCompletedQuery | undefined> { |
| 124 | + const qlpacks = await qlpackOfDatabase(this.cliServer, this.databaseItem); |
| 125 | + |
| 126 | + const packsToSearch = [qlpacks.dbschemePack]; |
| 127 | + if (qlpacks.queryPack) { |
| 128 | + packsToSearch.push(qlpacks.queryPack); |
| 129 | + } |
| 130 | + |
| 131 | + const suiteFile = ( |
| 132 | + await file({ |
| 133 | + postfix: ".qls", |
| 134 | + }) |
| 135 | + ).path; |
| 136 | + const suiteYaml = []; |
| 137 | + for (const qlpack of packsToSearch) { |
| 138 | + suiteYaml.push({ |
| 139 | + from: qlpack, |
| 140 | + queries: ".", |
| 141 | + include: { |
| 142 | + id: `${this.databaseItem.language}/telemetry/fetch-external-apis`, |
| 143 | + }, |
| 144 | + }); |
| 145 | + } |
| 146 | + await writeFile(suiteFile, dump(suiteYaml), "utf8"); |
| 147 | + |
| 148 | + const queries = await this.cliServer.resolveQueriesInSuite( |
| 149 | + suiteFile, |
| 150 | + getOnDiskWorkspaceFolders(), |
| 151 | + ); |
| 152 | + |
| 153 | + if (queries.length !== 1) { |
| 154 | + void extLogger.log(`Expected exactly one query, got ${queries.length}`); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + const query = queries[0]; |
| 159 | + |
| 160 | + const tokenSource = new CancellationTokenSource(); |
| 161 | + |
| 162 | + const queryRun = this.queryRunner.createQueryRun( |
| 163 | + this.databaseItem.databaseUri.fsPath, |
| 164 | + { queryPath: query, quickEvalPosition: undefined }, |
| 165 | + false, |
| 166 | + getOnDiskWorkspaceFolders(), |
| 167 | + undefined, |
| 168 | + this.queryStorageDir, |
| 169 | + undefined, |
| 170 | + undefined, |
| 171 | + ); |
| 172 | + |
| 173 | + return queryRun.evaluate( |
| 174 | + (update) => this.showProgress(update, 1500), |
| 175 | + tokenSource.token, |
| 176 | + new TeeLogger(this.queryRunner.logger, queryRun.outputDir.logPath), |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + private async getResults(bqrsPath: string) { |
| 181 | + const bqrsInfo = await this.cliServer.bqrsInfo(bqrsPath); |
| 182 | + if (bqrsInfo["result-sets"].length !== 1) { |
| 183 | + void extLogger.log( |
| 184 | + `Expected exactly one result set, got ${bqrsInfo["result-sets"].length}`, |
| 185 | + ); |
| 186 | + return undefined; |
| 187 | + } |
| 188 | + |
| 189 | + const resultSet = bqrsInfo["result-sets"][0]; |
| 190 | + |
| 191 | + await this.showProgress({ |
| 192 | + message: "Decoding results", |
| 193 | + step: 1200, |
| 194 | + maxStep: 1500, |
| 195 | + }); |
| 196 | + |
| 197 | + return this.cliServer.bqrsDecode(bqrsPath, resultSet.name); |
| 198 | + } |
| 199 | + |
| 200 | + /* |
| 201 | + * Progress in this class is a bit weird. Most of the progress is based on running the query. |
| 202 | + * Query progress is always between 0 and 1000. However, we still have some steps that need |
| 203 | + * to be done after the query has finished. Therefore, the maximum step is 1500. This captures |
| 204 | + * that there's 1000 steps of the query progress since that takes the most time, and then |
| 205 | + * an additional 500 steps for the rest of the work. The progress doesn't need to be 100% |
| 206 | + * accurate, so this is just a rough estimate. |
| 207 | + */ |
| 208 | + private async showProgress(update: ProgressUpdate, maxStep?: number) { |
| 209 | + await this.postMessage({ |
| 210 | + t: "showProgress", |
| 211 | + step: update.step, |
| 212 | + maxStep: maxStep ?? update.maxStep, |
| 213 | + message: update.message, |
| 214 | + }); |
| 215 | + } |
| 216 | + |
| 217 | + private async clearProgress() { |
| 218 | + await this.showProgress({ |
| 219 | + step: 0, |
| 220 | + maxStep: 0, |
| 221 | + message: "", |
| 222 | + }); |
52 | 223 | } |
53 | 224 | } |
0 commit comments