Skip to content

Commit

Permalink
Merge pull request #215 from KxSystems/KXI-33841
Browse files Browse the repository at this point in the history
[KXI-33841] DS doesn't show console output
  • Loading branch information
Philip-Carneiro-KX authored Dec 18, 2023
2 parents 9a05420 + 31fdafd commit 10c40dd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
getConnectedInsightsNode,
} from "../utils/dataSource";
import {
arrayToTable,
handleScratchpadTableRes,
handleWSError,
handleWSResults,
Expand Down Expand Up @@ -226,11 +227,10 @@ export async function runDataSource(

dataSourceForm.insightsNode = getConnectedInsightsNode();
const fileContent = dataSourceForm;
commands.executeCommand("kdb-results.focus");

let res: any;
const selectedType = getSelectedType(fileContent);

ext.isDatasourceExecution = true;
switch (selectedType) {
case "API":
res = await runApiDataSource(fileContent);
Expand All @@ -244,6 +244,7 @@ export async function runDataSource(
break;
}

ext.isDatasourceExecution = false;
if (res.error) {
window.showErrorMessage(res.error);
} else if (ext.resultsViewProvider.isVisible()) {
Expand All @@ -253,8 +254,9 @@ export async function runDataSource(
selectedType
);
} else {
const resString = arrayToTable(res);
writeQueryResultsToConsole(
res,
resString,
getQuery(fileContent, selectedType),
selectedType
);
Expand Down
1 change: 1 addition & 0 deletions src/extensionVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export namespace ext {
export let serverObjects: ServerObject;
export let openSslVersion: string | null;
export let resultPanelCSV: string;
export let isDatasourceExecution: boolean;
export const rowLimit = 150000000;

export let connection: Connection | undefined;
Expand Down
34 changes: 33 additions & 1 deletion src/utils/queryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function handleWSResults(ab: ArrayBuffer): any {
if (res.rows.length === 0) {
return "No results found.";
}
if (ext.resultsViewProvider.isVisible()) {
if (ext.resultsViewProvider.isVisible() || ext.isDatasourceExecution) {
return getValueFromArray(res.rows);
}
return convertRows(res.rows);
Expand Down Expand Up @@ -191,6 +191,38 @@ export function convertRowsToConsole(rows: string[]): string[] {
return result;
}

export function arrayToTable(data: any[]): any {
// Obter as chaves do primeiro objeto para usar como cabeçalho da tabela
if (!Array.isArray(data) || data.length === 0) {
return data;
}

const header = Object.keys(data[0]);

// Calcular o tamanho máximo de cada coluna
const columnLengths = header.map((key) => {
return Math.max(key.length, ...data.map((obj) => String(obj[key]).length));
});

// Converter o cabeçalho em uma string, alinhando os valores
const headerString = header
.map((key, i) => key.padEnd(columnLengths[i]))
.join(" ");

const separator = header
.map((key, i) => "-".repeat(columnLengths[i]))
.join("---");
// Converter cada objeto em uma string, alinhando os valores
const rows = data.map((obj) => {
return header
.map((key, i) => String(obj[key]).padEnd(columnLengths[i]))
.join(" ");
});

// Juntar o cabeçalho e as linhas para formar a tabela
return [headerString, separator, ...rows].join("\n");
}

export function getConnectionType(type: ServerType): string {
switch (type) {
case ServerType.KDB:
Expand Down
24 changes: 24 additions & 0 deletions test/suite/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,30 @@ describe("Utils", () => {
assert.deepStrictEqual(result, { error: "Query error" });
});
});

describe("arrayToTable", () => {
it("should format an array of objects as a table", () => {
const data = [
{ a: "0", b: "1.4198733294891718e+38" },
{ a: "1", b: "-1.2894694634258276e+29" },
];

const expected =
"a b \n" +
"---------------------------\n" +
"0 1.4198733294891718e+38 \n" +
"1 -1.2894694634258276e+29";

const result = queryUtils.arrayToTable(data);
assert.strictEqual(result, expected);
});

it("should return the input if it is not a non-empty array", () => {
const data = [];
const result = queryUtils.arrayToTable(data);
assert.strictEqual(result, data);
});
});
});

describe("Registration", () => {
Expand Down

0 comments on commit 10c40dd

Please sign in to comment.