|
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 { getOnDiskWorkspaceFolders } from "../helpers"; |
| 15 | +import { DatabaseItem } from "../local-databases"; |
| 16 | +import { CodeQLCliServer } from "../cli"; |
7 | 17 |
|
8 | 18 | export class DataExtensionsEditorView extends AbstractWebview< |
9 | 19 | ToDataExtensionsEditorMessage, |
10 | 20 | FromDataExtensionsEditorMessage |
11 | 21 | > { |
12 | | - public constructor(ctx: ExtensionContext) { |
| 22 | + public constructor( |
| 23 | + ctx: ExtensionContext, |
| 24 | + private readonly cliServer: CodeQLCliServer, |
| 25 | + private readonly queryRunner: QueryRunner, |
| 26 | + private readonly queryStorageDir: string, |
| 27 | + private readonly databaseItem: DatabaseItem, |
| 28 | + ) { |
13 | 29 | super(ctx); |
14 | 30 | } |
15 | 31 |
|
@@ -49,5 +65,136 @@ export class DataExtensionsEditorView extends AbstractWebview< |
49 | 65 |
|
50 | 66 | protected async onWebViewLoaded() { |
51 | 67 | super.onWebViewLoaded(); |
| 68 | + |
| 69 | + await this.loadExternalApiUsages(); |
| 70 | + } |
| 71 | + |
| 72 | + protected async loadExternalApiUsages(): Promise<void> { |
| 73 | + const queryResult = await this.runQuery(); |
| 74 | + if (!queryResult) { |
| 75 | + await this.clearProgress(); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + await this.showProgress({ |
| 80 | + message: "Loading results", |
| 81 | + step: 1100, |
| 82 | + maxStep: 1500, |
| 83 | + }); |
| 84 | + |
| 85 | + const bqrsPath = queryResult.outputDir.bqrsPath; |
| 86 | + |
| 87 | + const results = await this.getResults(bqrsPath); |
| 88 | + if (!results) { |
| 89 | + await this.clearProgress(); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + await this.showProgress({ |
| 94 | + message: "Finalizing results", |
| 95 | + step: 1450, |
| 96 | + maxStep: 1500, |
| 97 | + }); |
| 98 | + |
| 99 | + await this.postMessage({ |
| 100 | + t: "setExternalApiRepoResults", |
| 101 | + results, |
| 102 | + }); |
| 103 | + |
| 104 | + await this.clearProgress(); |
| 105 | + } |
| 106 | + |
| 107 | + private async runQuery(): Promise<CoreCompletedQuery | undefined> { |
| 108 | + const qlpacks = await qlpackOfDatabase(this.cliServer, this.databaseItem); |
| 109 | + |
| 110 | + const packsToSearch = [qlpacks.dbschemePack]; |
| 111 | + if (qlpacks.queryPack) { |
| 112 | + packsToSearch.push(qlpacks.queryPack); |
| 113 | + } |
| 114 | + |
| 115 | + const suiteFile = ( |
| 116 | + await file({ |
| 117 | + postfix: ".qls", |
| 118 | + }) |
| 119 | + ).path; |
| 120 | + const suiteYaml = []; |
| 121 | + for (const qlpack of packsToSearch) { |
| 122 | + suiteYaml.push({ |
| 123 | + from: qlpack, |
| 124 | + queries: ".", |
| 125 | + include: { |
| 126 | + id: `${this.databaseItem.language}/telemetry/fetch-external-apis`, |
| 127 | + }, |
| 128 | + }); |
| 129 | + } |
| 130 | + await writeFile(suiteFile, dump(suiteYaml), "utf8"); |
| 131 | + |
| 132 | + const queries = await this.cliServer.resolveQueriesInSuite( |
| 133 | + suiteFile, |
| 134 | + getOnDiskWorkspaceFolders(), |
| 135 | + ); |
| 136 | + |
| 137 | + if (queries.length !== 1) { |
| 138 | + void extLogger.log(`Expected exactly one query, got ${queries.length}`); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + const query = queries[0]; |
| 143 | + |
| 144 | + const tokenSource = new CancellationTokenSource(); |
| 145 | + |
| 146 | + const queryRun = this.queryRunner.createQueryRun( |
| 147 | + this.databaseItem.databaseUri.fsPath, |
| 148 | + { queryPath: query, quickEvalPosition: undefined }, |
| 149 | + false, |
| 150 | + getOnDiskWorkspaceFolders(), |
| 151 | + undefined, |
| 152 | + this.queryStorageDir, |
| 153 | + undefined, |
| 154 | + undefined, |
| 155 | + ); |
| 156 | + |
| 157 | + return queryRun.evaluate( |
| 158 | + (update) => this.showProgress(update, 1500), |
| 159 | + tokenSource.token, |
| 160 | + new TeeLogger(this.queryRunner.logger, queryRun.outputDir.logPath), |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + private async getResults(bqrsPath: string) { |
| 165 | + const bqrsInfo = await this.cliServer.bqrsInfo(bqrsPath); |
| 166 | + if (bqrsInfo["result-sets"].length !== 1) { |
| 167 | + void extLogger.log( |
| 168 | + `Expected exactly one result set, got ${bqrsInfo["result-sets"].length}`, |
| 169 | + ); |
| 170 | + return undefined; |
| 171 | + } |
| 172 | + |
| 173 | + const resultSet = bqrsInfo["result-sets"][0]; |
| 174 | + |
| 175 | + await this.showProgress({ |
| 176 | + message: "Decoding results", |
| 177 | + step: 1200, |
| 178 | + maxStep: 1500, |
| 179 | + }); |
| 180 | + |
| 181 | + return this.cliServer.bqrsDecode(bqrsPath, resultSet.name); |
| 182 | + } |
| 183 | + |
| 184 | + private async showProgress(update: ProgressUpdate, maxStep?: number) { |
| 185 | + await this.postMessage({ |
| 186 | + t: "showProgress", |
| 187 | + step: update.step, |
| 188 | + maxStep: maxStep ?? update.maxStep, |
| 189 | + message: update.message, |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + private async clearProgress() { |
| 194 | + await this.showProgress({ |
| 195 | + step: 0, |
| 196 | + maxStep: 0, |
| 197 | + message: "", |
| 198 | + }); |
52 | 199 | } |
53 | 200 | } |
0 commit comments