Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KXI-36400 #238

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 34 additions & 36 deletions src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
getConnectedInsightsNode,
} from "../utils/dataSource";
import {
arrayToTable,
handleScratchpadTableRes,
handleWSError,
handleWSResults,
Expand Down Expand Up @@ -65,13 +64,13 @@ export async function addDataSource(): Promise<void> {

fs.writeFileSync(filePath, JSON.stringify(defaultDataSourceContent));
window.showInformationMessage(
`Created ${fileName} in ${kdbDataSourcesFolderPath}.`
`Created ${fileName} in ${kdbDataSourcesFolderPath}.`,
);
}

export async function renameDataSource(
oldName: string,
newName: string
newName: string,
): Promise<void> {
const kdbDataSourcesFolderPath = createKdbDataSourcesFolder();
if (!kdbDataSourcesFolderPath) {
Expand All @@ -80,11 +79,11 @@ export async function renameDataSource(

const oldFilePath = path.join(
kdbDataSourcesFolderPath,
`${oldName}${ext.kdbDataSourceFileExtension}`
`${oldName}${ext.kdbDataSourceFileExtension}`,
);
const newFilePath = path.join(
kdbDataSourcesFolderPath,
`${newName}${ext.kdbDataSourceFileExtension}`
`${newName}${ext.kdbDataSourceFileExtension}`,
);

const dataSourceContent = fs.readFileSync(oldFilePath, "utf8");
Expand All @@ -102,7 +101,7 @@ export async function renameDataSource(
}

export async function deleteDataSource(
dataSource: KdbDataSourceTreeItem
dataSource: KdbDataSourceTreeItem,
): Promise<void> {
const kdbDataSourcesFolderPath = createKdbDataSourcesFolder();
if (!kdbDataSourcesFolderPath) {
Expand All @@ -111,28 +110,28 @@ export async function deleteDataSource(

const dataSourceFilePath = path.join(
kdbDataSourcesFolderPath,
`${dataSource.label}${ext.kdbDataSourceFileExtension}`
`${dataSource.label}${ext.kdbDataSourceFileExtension}`,
);
if (fs.existsSync(dataSourceFilePath)) {
fs.unlinkSync(dataSourceFilePath);
window.showInformationMessage(
`Deleted ${dataSource.label} from ${kdbDataSourcesFolderPath}.`
`Deleted ${dataSource.label} from ${kdbDataSourcesFolderPath}.`,
);
}
}

export async function openDataSource(
dataSource: KdbDataSourceTreeItem,
uri: Uri
uri: Uri,
): Promise<void> {
const kdbDataSourcesFolderPath = createKdbDataSourcesFolder();
Object.assign(ext.insightsMeta, await getMeta());
if (!ext.insightsMeta.assembly) {
ext.outputChannel.appendLine(
`To edit or run a datasource you need to be connected to an Insights server`
`To edit or run a datasource you need to be connected to an Insights server`,
);
window.showErrorMessage(
"To edit or run a datasource you need to be connected to an Insights server"
"To edit or run a datasource you need to be connected to an Insights server",
);
}
if (!kdbDataSourcesFolderPath) {
Expand All @@ -141,12 +140,12 @@ export async function openDataSource(
fs.readFile(
path.join(
kdbDataSourcesFolderPath,
`${dataSource.label}${ext.kdbDataSourceFileExtension}`
`${dataSource.label}${ext.kdbDataSourceFileExtension}`,
),
(err, data) => {
if (err) {
ext.outputChannel.appendLine(
`Error reading the file ${dataSource.label}${ext.kdbDataSourceFileExtension}, this file maybe doesn't exist`
`Error reading the file ${dataSource.label}${ext.kdbDataSourceFileExtension}, this file maybe doesn't exist`,
);
window.showErrorMessage("Error reading file");
return;
Expand All @@ -155,12 +154,12 @@ export async function openDataSource(
const datasourceContent: DataSourceFiles = JSON.parse(data.toString());
DataSourcesPanel.render(uri, datasourceContent);
}
}
},
);
}

export async function saveDataSource(
dataSourceForm: DataSourceFiles
dataSourceForm: DataSourceFiles,
): Promise<void> {
const kdbDataSourcesFolderPath = createKdbDataSourcesFolder();
if (!kdbDataSourcesFolderPath) {
Expand All @@ -181,7 +180,7 @@ export async function saveDataSource(

const dataSourceFilePath = path.join(
kdbDataSourcesFolderPath,
`${dataSourceForm.name}${ext.kdbDataSourceFileExtension}`
`${dataSourceForm.name}${ext.kdbDataSourceFileExtension}`,
);

if (fs.existsSync(dataSourceFilePath)) {
Expand All @@ -191,7 +190,7 @@ export async function saveDataSource(
}

export async function populateScratchpad(
dataSourceForm: DataSourceFiles
dataSourceForm: DataSourceFiles,
): Promise<void> {
const scratchpadVariable: InputBoxOptions = {
prompt: scratchpadVariableInput.prompt,
Expand All @@ -205,14 +204,14 @@ export async function populateScratchpad(
await importScratchpad(outputVariable!, dataSourceForm!);
} else {
ext.outputChannel.appendLine(
`Invalid scratchpad output variable name: ${outputVariable}`
`Invalid scratchpad output variable name: ${outputVariable}`,
);
}
});
}

export async function runDataSource(
dataSourceForm: DataSourceFiles
dataSourceForm: DataSourceFiles,
): Promise<void> {
if (DataSourcesPanel.running) {
return;
Expand All @@ -223,10 +222,10 @@ export async function runDataSource(
Object.assign(ext.insightsMeta, await getMeta());
if (!ext.insightsMeta.assembly) {
ext.outputChannel.appendLine(
`To run a datasource you need to be connected to an Insights server`
`To run a datasource you need to be connected to an Insights server`,
);
window.showErrorMessage(
"To run a datasource you need to be connected to an Insights server"
"To run a datasource you need to be connected to an Insights server",
);
return;
}
Expand Down Expand Up @@ -257,14 +256,13 @@ export async function runDataSource(
writeQueryResultsToView(
res,
getQuery(fileContent, selectedType),
selectedType
selectedType,
);
} else {
const resString = arrayToTable(res);
writeQueryResultsToConsole(
resString,
res,
getQuery(fileContent, selectedType),
selectedType
selectedType,
);
}
} finally {
Expand All @@ -287,22 +285,22 @@ export function getSelectedType(fileContent: DataSourceFiles): string {
}

export async function runApiDataSource(
fileContent: DataSourceFiles
fileContent: DataSourceFiles,
): Promise<any> {
const isTimeCorrect = checkIfTimeParamIsCorrect(
fileContent.dataSource.api.startTS,
fileContent.dataSource.api.endTS
fileContent.dataSource.api.endTS,
);
if (!isTimeCorrect) {
window.showErrorMessage(
"The time parameters(startTS and endTS) are not correct, please check the format or if the startTS is before the endTS"
"The time parameters(startTS and endTS) are not correct, please check the format or if the startTS is before the endTS",
);
return;
}
const apiBody = getApiBody(fileContent);
const apiCall = await getDataInsights(
ext.insightsServiceGatewayUrls.data,
JSON.stringify(apiBody)
JSON.stringify(apiBody),
);

if (apiCall?.error) {
Expand All @@ -316,7 +314,7 @@ export async function runApiDataSource(
}

export function getApiBody(
fileContent: DataSourceFiles
fileContent: DataSourceFiles,
): Partial<getDataBodyPayload> {
const api = fileContent.dataSource.api;

Expand Down Expand Up @@ -353,7 +351,7 @@ export function getApiBody(
if (labels.length > 0) {
apiBody.labels = Object.assign(
{},
...labels.map((label) => ({ [label.key]: label.value }))
...labels.map((label) => ({ [label.key]: label.value })),
);
}

Expand Down Expand Up @@ -404,7 +402,7 @@ export function getApiBody(
}

export async function runQsqlDataSource(
fileContent: DataSourceFiles
fileContent: DataSourceFiles,
): Promise<any> {
const assembly = fileContent.dataSource.qsql.selectedTarget.slice(0, -4);
const target = fileContent.dataSource.qsql.selectedTarget.slice(-3);
Expand All @@ -415,7 +413,7 @@ export async function runQsqlDataSource(
};
const qsqlCall = await getDataInsights(
ext.insightsServiceGatewayUrls.qsql,
JSON.stringify(qsqlBody)
JSON.stringify(qsqlBody),
);

if (qsqlCall?.error) {
Expand All @@ -429,14 +427,14 @@ export async function runQsqlDataSource(
}

export async function runSqlDataSource(
fileContent: DataSourceFiles
fileContent: DataSourceFiles,
): Promise<any> {
const sqlBody = {
query: fileContent.dataSource.sql.query,
};
const sqlCall = await getDataInsights(
ext.insightsServiceGatewayUrls.sql,
JSON.stringify(sqlBody)
JSON.stringify(sqlBody),
);

if (sqlCall?.error) {
Expand All @@ -451,7 +449,7 @@ export async function runSqlDataSource(

export function getQuery(
fileContent: DataSourceFiles,
selectedType: string
selectedType: string,
): string {
switch (selectedType) {
case "API":
Expand Down
68 changes: 30 additions & 38 deletions src/utils/queryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export function handleWSResults(ab: ArrayBuffer): any {
if (res.rows.length === 0 && res.columns.length === 0) {
return "No results found.";
}
if (ext.resultsViewProvider.isVisible() || ext.isDatasourceExecution) {
if (ext.resultsViewProvider.isVisible()) {
return getValueFromArray(res);
}
return convertRows(res);
return convertRows(res.rows);
} catch (error) {
console.log(error);
throw error;
Expand Down Expand Up @@ -201,12 +201,14 @@ export function checkIfIsQDateTypes(obj: any): any {
return obj;
}

export function convertRows(rows: any[]): any[] {
export function convertRows(rows: any[]): any {
if (rows.length === 0) {
return [];
}
const keys = Object.keys(rows[0]);
const result = [keys.join("#$#;#$#")];
const isObj = typeof rows[0] === "object";
const isPropVal = isObj ? checkIfIsPropVal(keys) : false;
const result = isPropVal ? [] : [keys.join("#$#;header;#$#")];
for (const row of rows) {
const values = keys.map((key) => {
if (Array.isArray(row[key])) {
Expand All @@ -216,15 +218,23 @@ export function convertRows(rows: any[]): any[] {
});
result.push(values.join("#$#;#$#"));
}
return result;
return convertRowsToConsole(result).join("\n") + "\n\n";
}

export function convertRowsToConsole(rows: string[]): string[] {
if (rows.length === 0) {
return [];
}

const haveHeader = rows[0].includes("#$#;header;#$#");
let header;
if (haveHeader) {
header = rows[0].split("#$#;header;#$#");
rows.shift();
}
const vector = rows.map((row) => row.split("#$#;#$#"));
if (header) {
vector.unshift(header);
}

const columnCounters = vector[0].reduce((counters: number[], _, j) => {
const maxLength = vector.reduce(
Expand All @@ -240,7 +250,11 @@ export function convertRowsToConsole(rows: string[]): string[] {
const counter = columnCounters[j];
const diff = counter - value.length;
if (diff > 0) {
row[j] = value + " ".repeat(diff);
if (!haveHeader && j !== columnCounters.length - 1) {
row[j] = value + "|" + " ".repeat(diff > 1 ? diff - 1 : diff);
} else {
row[j] = value + " ".repeat(diff);
}
}
});
});
Expand All @@ -249,41 +263,19 @@ export function convertRowsToConsole(rows: string[]): string[] {

const totalCount = columnCounters.reduce((sum, count) => sum + count, 0);
const totalCounter = "-".repeat(totalCount);
result.splice(1, 0, totalCounter);
if (haveHeader) {
result.splice(1, 0, totalCounter);
}

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((_, 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 checkIfIsPropVal(columns: string[]): boolean {
return (
columns.length === 2 &&
columns.includes("Property") &&
columns.includes("Value")
);
}

export function getConnectionType(type: ServerType): string {
Expand Down
Loading
Loading