@@ -13,17 +13,14 @@ import {
1313 ToDataExtensionsEditorMessage ,
1414} from "../pure/interface-types" ;
1515import { ProgressUpdate } from "../progress" ;
16- import { extLogger , TeeLogger } from "../common" ;
17- import { CoreCompletedQuery , QueryRunner } from "../queryRunner" ;
18- import { qlpackOfDatabase } from "../contextual/queryResolver" ;
19- import { file } from "tmp-promise" ;
20- import { readFile , writeFile } from "fs-extra" ;
21- import { dump as dumpYaml , load as loadYaml } from "js-yaml" ;
16+ import { QueryRunner } from "../queryRunner" ;
2217import {
23- getOnDiskWorkspaceFolders ,
2418 showAndLogExceptionWithTelemetry ,
2519 showAndLogWarningMessage ,
2620} from "../helpers" ;
21+ import { extLogger } from "../common" ;
22+ import { readFile , writeFile } from "fs-extra" ;
23+ import { load as loadYaml } from "js-yaml" ;
2724import { DatabaseItem , DatabaseManager } from "../local-databases" ;
2825import { CodeQLCliServer } from "../cli" ;
2926import { asError , assertNever , getErrorMessage } from "../pure/helpers-pure" ;
@@ -34,6 +31,7 @@ import { ResolvableLocationValue } from "../pure/bqrs-cli-types";
3431import { showResolvableLocation } from "../interface-utils" ;
3532import { decodeBqrsToExternalApiUsages } from "./bqrs" ;
3633import { redactableError } from "../pure/errors" ;
34+ import { readQueryResults , runQuery } from "./external-api-usage-query" ;
3735import { createDataExtensionYaml , loadDataExtensionYaml } from "./yaml" ;
3836import { ExternalApiUsage } from "./external-api-usage" ;
3937import { ModeledMethod } from "./modeled-method" ;
@@ -192,22 +190,36 @@ export class DataExtensionsEditorView extends AbstractWebview<
192190 }
193191
194192 protected async loadExternalApiUsages ( ) : Promise < void > {
193+ const cancellationTokenSource = new CancellationTokenSource ( ) ;
194+
195195 try {
196- const queryResult = await this . runQuery ( ) ;
196+ const queryResult = await runQuery ( {
197+ cliServer : this . cliServer ,
198+ queryRunner : this . queryRunner ,
199+ databaseItem : this . databaseItem ,
200+ queryStorageDir : this . queryStorageDir ,
201+ logger : extLogger ,
202+ progress : ( progressUpdate : ProgressUpdate ) => {
203+ void this . showProgress ( progressUpdate , 1500 ) ;
204+ } ,
205+ token : cancellationTokenSource . token ,
206+ } ) ;
197207 if ( ! queryResult ) {
198208 await this . clearProgress ( ) ;
199209 return ;
200210 }
201211
202212 await this . showProgress ( {
203- message : "Loading results" ,
213+ message : "Decoding results" ,
204214 step : 1100 ,
205215 maxStep : 1500 ,
206216 } ) ;
207217
208- const bqrsPath = queryResult . outputDir . bqrsPath ;
209-
210- const bqrsChunk = await this . getResults ( bqrsPath ) ;
218+ const bqrsChunk = await readQueryResults ( {
219+ cliServer : this . cliServer ,
220+ bqrsPath : queryResult . outputDir . bqrsPath ,
221+ logger : extLogger ,
222+ } ) ;
211223 if ( ! bqrsChunk ) {
212224 await this . clearProgress ( ) ;
213225 return ;
@@ -322,83 +334,6 @@ export class DataExtensionsEditorView extends AbstractWebview<
322334 await this . clearProgress ( ) ;
323335 }
324336
325- private async runQuery ( ) : Promise < CoreCompletedQuery | undefined > {
326- const qlpacks = await qlpackOfDatabase ( this . cliServer , this . databaseItem ) ;
327-
328- const packsToSearch = [ qlpacks . dbschemePack ] ;
329- if ( qlpacks . queryPack ) {
330- packsToSearch . push ( qlpacks . queryPack ) ;
331- }
332-
333- const suiteFile = (
334- await file ( {
335- postfix : ".qls" ,
336- } )
337- ) . path ;
338- const suiteYaml = [ ] ;
339- for ( const qlpack of packsToSearch ) {
340- suiteYaml . push ( {
341- from : qlpack ,
342- queries : "." ,
343- include : {
344- id : `${ this . databaseItem . language } /telemetry/fetch-external-apis` ,
345- } ,
346- } ) ;
347- }
348- await writeFile ( suiteFile , dumpYaml ( suiteYaml ) , "utf8" ) ;
349-
350- const queries = await this . cliServer . resolveQueriesInSuite (
351- suiteFile ,
352- getOnDiskWorkspaceFolders ( ) ,
353- ) ;
354-
355- if ( queries . length !== 1 ) {
356- void extLogger . log ( `Expected exactly one query, got ${ queries . length } ` ) ;
357- return ;
358- }
359-
360- const query = queries [ 0 ] ;
361-
362- const tokenSource = new CancellationTokenSource ( ) ;
363-
364- const queryRun = this . queryRunner . createQueryRun (
365- this . databaseItem . databaseUri . fsPath ,
366- { queryPath : query , quickEvalPosition : undefined } ,
367- false ,
368- getOnDiskWorkspaceFolders ( ) ,
369- undefined ,
370- this . queryStorageDir ,
371- undefined ,
372- undefined ,
373- ) ;
374-
375- return queryRun . evaluate (
376- ( update ) => this . showProgress ( update , 1500 ) ,
377- tokenSource . token ,
378- new TeeLogger ( this . queryRunner . logger , queryRun . outputDir . logPath ) ,
379- ) ;
380- }
381-
382- private async getResults ( bqrsPath : string ) {
383- const bqrsInfo = await this . cliServer . bqrsInfo ( bqrsPath ) ;
384- if ( bqrsInfo [ "result-sets" ] . length !== 1 ) {
385- void extLogger . log (
386- `Expected exactly one result set, got ${ bqrsInfo [ "result-sets" ] . length } ` ,
387- ) ;
388- return undefined ;
389- }
390-
391- const resultSet = bqrsInfo [ "result-sets" ] [ 0 ] ;
392-
393- await this . showProgress ( {
394- message : "Decoding results" ,
395- step : 1200 ,
396- maxStep : 1500 ,
397- } ) ;
398-
399- return this . cliServer . bqrsDecode ( bqrsPath , resultSet . name ) ;
400- }
401-
402337 /*
403338 * Progress in this class is a bit weird. Most of the progress is based on running the query.
404339 * Query progress is always between 0 and 1000. However, we still have some steps that need
0 commit comments