Skip to content

Commit c245f33

Browse files
committed
Extract external API usage query to separate file
1 parent d9b362d commit c245f33

File tree

2 files changed

+122
-92
lines changed

2 files changed

+122
-92
lines changed

extensions/ql-vscode/src/data-extensions-editor/data-extensions-editor-view.ts

Lines changed: 20 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,14 @@ import {
55
ToDataExtensionsEditorMessage,
66
} from "../pure/interface-types";
77
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";
8+
import { QueryRunner } from "../queryRunner";
9+
import { showAndLogExceptionWithTelemetry } from "../helpers";
1810
import { DatabaseItem } from "../local-databases";
1911
import { CodeQLCliServer } from "../cli";
2012
import { decodeBqrsToExternalApiUsages } from "./bqrs";
2113
import { redactableError } from "../pure/errors";
2214
import { asError, getErrorMessage } from "../pure/helpers-pure";
15+
import { getResults, runQuery } from "./external-api-usage-query";
2316

2417
export class DataExtensionsEditorView extends AbstractWebview<
2518
ToDataExtensionsEditorMessage,
@@ -76,22 +69,34 @@ export class DataExtensionsEditorView extends AbstractWebview<
7669
}
7770

7871
protected async loadExternalApiUsages(): Promise<void> {
72+
const cancellationTokenSource = new CancellationTokenSource();
73+
7974
try {
80-
const queryResult = await this.runQuery();
75+
const queryResult = await runQuery({
76+
cliServer: this.cliServer,
77+
queryRunner: this.queryRunner,
78+
databaseItem: this.databaseItem,
79+
queryStorageDir: this.queryStorageDir,
80+
progress: (progressUpdate: ProgressUpdate) => {
81+
void this.showProgress(progressUpdate, 1500);
82+
},
83+
token: cancellationTokenSource.token,
84+
});
8185
if (!queryResult) {
8286
await this.clearProgress();
8387
return;
8488
}
8589

8690
await this.showProgress({
87-
message: "Loading results",
91+
message: "Decoding results",
8892
step: 1100,
8993
maxStep: 1500,
9094
});
9195

92-
const bqrsPath = queryResult.outputDir.bqrsPath;
93-
94-
const bqrsChunk = await this.getResults(bqrsPath);
96+
const bqrsChunk = await getResults({
97+
cliServer: this.cliServer,
98+
bqrsPath: queryResult.outputDir.bqrsPath,
99+
});
95100
if (!bqrsChunk) {
96101
await this.clearProgress();
97102
return;
@@ -120,83 +125,6 @@ export class DataExtensionsEditorView extends AbstractWebview<
120125
}
121126
}
122127

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-
200128
private async showProgress(update: ProgressUpdate, maxStep?: number) {
201129
await this.postMessage({
202130
t: "showProgress",
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { CoreCompletedQuery, QueryRunner } from "../queryRunner";
2+
import { qlpackOfDatabase } from "../contextual/queryResolver";
3+
import { file } from "tmp-promise";
4+
import { writeFile } from "fs-extra";
5+
import { dump } from "js-yaml";
6+
import { getOnDiskWorkspaceFolders } from "../helpers";
7+
import { extLogger, TeeLogger } from "../common";
8+
import { CancellationToken } from "vscode";
9+
import { CodeQLCliServer } from "../cli";
10+
import { DatabaseItem } from "../local-databases";
11+
import { ProgressCallback } from "../progress";
12+
13+
export type RunQueryOptions = {
14+
cliServer: CodeQLCliServer;
15+
queryRunner: QueryRunner;
16+
databaseItem: DatabaseItem;
17+
queryStorageDir: string;
18+
19+
progress: ProgressCallback;
20+
token: CancellationToken;
21+
};
22+
23+
export async function runQuery({
24+
cliServer,
25+
queryRunner,
26+
databaseItem,
27+
queryStorageDir,
28+
progress,
29+
token,
30+
}: RunQueryOptions): Promise<CoreCompletedQuery | undefined> {
31+
const qlpacks = await qlpackOfDatabase(cliServer, databaseItem);
32+
33+
const packsToSearch = [qlpacks.dbschemePack];
34+
if (qlpacks.queryPack) {
35+
packsToSearch.push(qlpacks.queryPack);
36+
}
37+
38+
const suiteFile = (
39+
await file({
40+
postfix: ".qls",
41+
})
42+
).path;
43+
const suiteYaml = [];
44+
for (const qlpack of packsToSearch) {
45+
suiteYaml.push({
46+
from: qlpack,
47+
queries: ".",
48+
include: {
49+
id: `${databaseItem.language}/telemetry/fetch-external-apis`,
50+
},
51+
});
52+
}
53+
await writeFile(suiteFile, dump(suiteYaml), "utf8");
54+
55+
const queries = await cliServer.resolveQueriesInSuite(
56+
suiteFile,
57+
getOnDiskWorkspaceFolders(),
58+
);
59+
60+
if (queries.length !== 1) {
61+
void extLogger.log(`Expected exactly one query, got ${queries.length}`);
62+
return;
63+
}
64+
65+
const query = queries[0];
66+
67+
const queryRun = queryRunner.createQueryRun(
68+
databaseItem.databaseUri.fsPath,
69+
{ queryPath: query, quickEvalPosition: undefined },
70+
false,
71+
getOnDiskWorkspaceFolders(),
72+
undefined,
73+
queryStorageDir,
74+
undefined,
75+
undefined,
76+
);
77+
78+
return queryRun.evaluate(
79+
progress,
80+
token,
81+
new TeeLogger(queryRunner.logger, queryRun.outputDir.logPath),
82+
);
83+
}
84+
85+
export type GetResultsOptions = {
86+
cliServer: CodeQLCliServer;
87+
bqrsPath: string;
88+
};
89+
90+
export async function getResults({ cliServer, bqrsPath }: GetResultsOptions) {
91+
const bqrsInfo = await cliServer.bqrsInfo(bqrsPath);
92+
if (bqrsInfo["result-sets"].length !== 1) {
93+
void extLogger.log(
94+
`Expected exactly one result set, got ${bqrsInfo["result-sets"].length}`,
95+
);
96+
return undefined;
97+
}
98+
99+
const resultSet = bqrsInfo["result-sets"][0];
100+
101+
return cliServer.bqrsDecode(bqrsPath, resultSet.name);
102+
}

0 commit comments

Comments
 (0)