Skip to content

Commit 65adcd9

Browse files
improve code quality
1 parent 835d940 commit 65adcd9

File tree

3 files changed

+47
-89
lines changed

3 files changed

+47
-89
lines changed

src/classes/insightsConnection.ts

Lines changed: 33 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,45 @@ export class InsightsConnection {
316316
}
317317
}
318318

319-
public async getDataInsights(
320-
targetUrl: string,
321-
body: string,
319+
public generateDatasourceEndpoints(
320+
type: DataSourceTypes,
321+
udaName: string,
322+
): string {
323+
let endpoint: string = "";
324+
switch (type) {
325+
case DataSourceTypes.UDA:
326+
endpoint =
327+
this.retrieveEndpoints("serviceGateway", "udaBase") +
328+
udaName.split(".").slice(1).join("/");
329+
break;
330+
case DataSourceTypes.API:
331+
endpoint = this.retrieveEndpoints("serviceGateway", "data") || "";
332+
break;
333+
case DataSourceTypes.SQL:
334+
endpoint = this.retrieveEndpoints("serviceGateway", "sql") || "";
335+
break;
336+
case DataSourceTypes.QSQL:
337+
default:
338+
endpoint = this.retrieveEndpoints("serviceGateway", "qsql") || "";
339+
break;
340+
}
341+
return new url.URL(endpoint, this.node.details.server).toString();
342+
}
343+
344+
public async getDatasourceQuery(
345+
type: DataSourceTypes,
346+
body: any,
322347
): Promise<GetDataObjectPayload | undefined> {
323348
if (this.connected) {
324-
const requestUrl = new url.URL(
325-
targetUrl,
326-
this.node.details.server,
327-
).toString();
349+
const udaName = (body as UDARequestBody).name
350+
? (body as UDARequestBody).name
351+
: "";
352+
const requestUrl = this.generateDatasourceEndpoints(type, udaName);
328353
const options = await this.getOptions(
329354
false,
330355
customHeadersOctet,
331356
"POST",
332-
requestUrl.toString(),
357+
requestUrl,
333358
body,
334359
);
335360

@@ -493,72 +518,6 @@ export class InsightsConnection {
493518
return token;
494519
}
495520

496-
public async getUDAQuery(
497-
udaReqBody: UDARequestBody,
498-
): Promise<any | undefined> {
499-
if (this.connected && this.connEndpoints) {
500-
const udaEndpoint =
501-
this.connEndpoints.serviceGateway.udaBase +
502-
udaReqBody.name.split(".").slice(1).join("/");
503-
const udaURL = new url.URL(udaEndpoint, this.node.details.server);
504-
const options = await this.getOptions(
505-
false,
506-
customHeadersOctet,
507-
"POST",
508-
udaURL.toString(),
509-
udaReqBody.params,
510-
);
511-
512-
if (!options) {
513-
return;
514-
}
515-
516-
options.responseType = "arraybuffer";
517-
518-
const results = await window.withProgress(
519-
{
520-
location: ProgressLocation.Notification,
521-
cancellable: false,
522-
},
523-
async (progress, token) => {
524-
token.onCancellationRequested(() => {
525-
kdbOutputLog(`User cancelled the Datasource Run.`, "WARNING");
526-
});
527-
528-
progress.report({ message: "Query executing..." });
529-
530-
return await axios(options)
531-
.then((response: any) => {
532-
kdbOutputLog(
533-
`[Datasource RUN] Status: ${response.status}.`,
534-
"INFO",
535-
);
536-
if (isCompressed(response.data)) {
537-
response.data = uncompress(response.data);
538-
}
539-
return {
540-
error: "",
541-
arrayBuffer: response.data.buffer
542-
? response.data.buffer
543-
: response.data,
544-
};
545-
})
546-
.catch((error: any) => {
547-
kdbOutputLog(
548-
`[Datasource RUN] Status: ${error.response.status}.`,
549-
"INFO",
550-
);
551-
return {
552-
error: { buffer: error.response.data },
553-
arrayBuffer: undefined,
554-
};
555-
});
556-
},
557-
);
558-
return results;
559-
}
560-
}
561-
562521
public async getUDAScratchpadQuery(
563522
udaReqBody: UDARequestBody,
564523
): Promise<any | undefined> {

src/commands/dataSourceCommand.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ export async function runApiDataSource(
304304
return;
305305
}
306306
const apiBody = getApiBody(fileContent);
307-
const apiCall = await selectedConn.getDataInsights(
308-
ext.insightsServiceGatewayUrls.data,
307+
const apiCall = await selectedConn.getDatasourceQuery(
308+
DataSourceTypes.API,
309309
JSON.stringify(apiBody),
310310
);
311311

@@ -415,8 +415,8 @@ export async function runQsqlDataSource(
415415
target: target,
416416
query: fileContent.dataSource.qsql.query,
417417
};
418-
const qsqlCall = await selectedConn.getDataInsights(
419-
ext.insightsServiceGatewayUrls.qsql,
418+
const qsqlCall = await selectedConn.getDatasourceQuery(
419+
DataSourceTypes.QSQL,
420420
JSON.stringify(qsqlBody),
421421
);
422422

@@ -437,8 +437,8 @@ export async function runSqlDataSource(
437437
const sqlBody = {
438438
query: fileContent.dataSource.sql.query,
439439
};
440-
const sqlCall = await selectedConn.getDataInsights(
441-
ext.insightsServiceGatewayUrls.sql,
440+
const sqlCall = await selectedConn.getDatasourceQuery(
441+
DataSourceTypes.SQL,
442442
JSON.stringify(sqlBody),
443443
);
444444

@@ -478,10 +478,6 @@ export async function runUDADataSource(
478478
}
479479
});
480480
}
481-
// if (Object.keys(params).length === 0) {
482-
// params["table"] = "nsansUdaTable";
483-
// parameterTypes["table"] = -10;
484-
// }
485481
const udaReqBody: UDARequestBody = {
486482
language: "q",
487483
name: UDA.name,
@@ -492,7 +488,10 @@ export async function runUDADataSource(
492488
sampleSize: 10000,
493489
};
494490

495-
const udaCall = await selectedConn.getUDAQuery(udaReqBody);
491+
const udaCall = await selectedConn.getDatasourceQuery(
492+
DataSourceTypes.UDA,
493+
udaReqBody,
494+
);
496495

497496
if (udaCall?.error) {
498497
return parseError(udaCall.error);

test/suite/commands.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ describe("dataSourceCommand2", () => {
362362
dataSourceUtils,
363363
"checkIfTimeParamIsCorrect",
364364
);
365-
getDataInsightsStub = sinon.stub(insightsConn, "getDataInsights");
365+
getDataInsightsStub = sinon.stub(insightsConn, "getDatasourceQuery");
366366
handleWSResultsStub = sinon.stub(queryUtils, "handleWSResults");
367367
handleScratchpadTableRes = sinon.stub(
368368
queryUtils,
@@ -432,7 +432,7 @@ describe("dataSourceCommand2", () => {
432432

433433
beforeEach(() => {
434434
windowMock = sinon.mock(vscode.window);
435-
getDataInsightsStub = sinon.stub(insightsConn, "getDataInsights");
435+
getDataInsightsStub = sinon.stub(insightsConn, "getDatasourceQuery");
436436
handleWSResultsStub = sinon.stub(queryUtils, "handleWSResults");
437437
handleScratchpadTableRes = sinon.stub(
438438
queryUtils,
@@ -480,7 +480,7 @@ describe("dataSourceCommand2", () => {
480480

481481
beforeEach(() => {
482482
windowMock = sinon.mock(vscode.window);
483-
getDataInsightsStub = sinon.stub(insightsConn, "getDataInsights");
483+
getDataInsightsStub = sinon.stub(insightsConn, "getDatasourceQuery");
484484
handleWSResultsStub = sinon.stub(queryUtils, "handleWSResults");
485485
handleScratchpadTableRes = sinon.stub(
486486
queryUtils,
@@ -637,7 +637,7 @@ describe("dataSourceCommand2", () => {
637637
handleScratchpadTableRes = sinon
638638
.stub(queryUtils, "handleScratchpadTableRes")
639639
.returns("dummy results");
640-
getDataInsightsStub = sinon.stub(insightsConn, "getDataInsights");
640+
getDataInsightsStub = sinon.stub(insightsConn, "getDatasourceQuery");
641641
writeQueryResultsToViewStub = sinon.stub(
642642
serverCommand,
643643
"writeQueryResultsToView",

0 commit comments

Comments
 (0)