@@ -5,21 +5,14 @@ import {
55 ToDataExtensionsEditorMessage ,
66} from "../pure/interface-types" ;
77import { 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" ;
1810import { DatabaseItem } from "../local-databases" ;
1911import { CodeQLCliServer } from "../cli" ;
2012import { decodeBqrsToExternalApiUsages } from "./bqrs" ;
2113import { redactableError } from "../pure/errors" ;
2214import { asError , getErrorMessage } from "../pure/helpers-pure" ;
15+ import { getResults , runQuery } from "./external-api-usage-query" ;
2316
2417export 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" ,
0 commit comments