Skip to content

Commit

Permalink
Merge pull request #231 from KxSystems/KXI-35687
Browse files Browse the repository at this point in the history
KXI-35687 Fix results tabs for insights
  • Loading branch information
Philip-Carneiro-KX authored Jan 23, 2024
2 parents 9693264 + bd69b5d commit 15964d1
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 242 deletions.
10 changes: 6 additions & 4 deletions src/ipc/cClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Tools from "./tools";

function stringifyTemporal(
temporal: DDateClass | DDateTimeClass | DMonthClass,
stringify: () => string
stringify: () => string,
): string {
const i = temporal.i;

Expand Down Expand Up @@ -48,7 +48,9 @@ export class DTimestampClass {
}

toString(): string {
return this.toDate().formatNano("YYYY-MM-DD HH:mm:ss.SSSSSSSSS");
return this.toDate()
.formatNano("YYYY-MM-DD HH:mm:ss.SSSSSSSSS")
.replace(" ", "D");
}
}
export class DMonthClass {
Expand Down Expand Up @@ -84,7 +86,7 @@ export class DDateClass {

toString(): string {
return stringifyTemporal(this, () =>
moment(new Date(this.i)).utcOffset(0).format("YYYY-MM-DD")
moment(new Date(this.i)).utcOffset(0).format("YYYY-MM-DD"),
);
}
}
Expand All @@ -103,7 +105,7 @@ export class DDateTimeClass {

toString(): string {
return stringifyTemporal(this, () =>
this.toDate().format("YYYY-MM-DD HH:mm:ss.SSS")
this.toDate().format("YYYY-MM-DD HH:mm:ss.SSS").replace(" ", "T"),
);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/models/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Connection {
private options: nodeq.ConnectionParameters;
private connection?: nodeq.Connection;
public connected: boolean;
private isError: boolean = false;
private result?: string;

constructor(connectionString: string, creds?: string[], tls?: boolean) {
Expand Down Expand Up @@ -117,6 +118,7 @@ export class Connection {
!!stringify,
(err: Error, res: QueryResult) => {
if (err) {
this.isError = true;
this.result = handleQueryResults(
err.toString(),
QueryResultType.Error,
Expand All @@ -131,6 +133,10 @@ export class Connection {
const result = await this.waitForResult();

if (ext.resultsViewProvider.isVisible() && stringify) {
if (this.isError) {
this.isError = false;
return result;
}
return convertStringToArray(result);
}

Expand All @@ -150,6 +156,7 @@ export class Connection {

private handleQueryResult = (res: QueryResult): void => {
if (res.errored) {
this.isError = true;
this.result = handleQueryResults(
res.error + (res.backtrace ? "\n" + res.backtrace : ""),
QueryResultType.Error,
Expand Down
98 changes: 72 additions & 26 deletions src/services/resultsPanelProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ext } from "../extensionVariables";
import * as utils from "../utils/execution";
import { getNonce } from "../utils/getNonce";
import { getUri } from "../utils/getUri";
import { InsightsNode } from "./kdbTreeProvider";

export class KdbResultsViewProvider implements WebviewViewProvider {
public static readonly viewType = "kdb-results";
Expand Down Expand Up @@ -60,7 +61,7 @@ export class KdbResultsViewProvider implements WebviewViewProvider {
this._view.webview.postMessage(queryResults);
this._view.webview.html = this._getWebviewContent(
queryResults,
dataSourceType
dataSourceType,
);
}
}
Expand Down Expand Up @@ -101,23 +102,69 @@ export class KdbResultsViewProvider implements WebviewViewProvider {
utils.exportToCsv(workspaceUri);
}

convertToGrid(queryResult: any[]): string {
const columnDefs = Object.keys(queryResult[0]).map((key: string) => {
const sanitizedKey = this.sanitizeString(key);
return { field: sanitizedKey, headerName: sanitizedKey };
});
const rowData = queryResult.map((row: any) => {
for (const key in row) {
if (Object.prototype.hasOwnProperty.call(row, key)) {
row[key] =
row[key] !== undefined && row[key] !== null
? this.sanitizeString(row[key])
: "";
}
generateCoumnDefs(results: any, isInsights: boolean): any {
if (isInsights) {
if (results.rows.length === 0) {
return Object.keys(results.meta).map((key: string) => {
const sanitizedKey = this.sanitizeString(key);
const type = results.meta[key];
const headerTooltip = type;
return {
field: sanitizedKey,
headerName: sanitizedKey,
headerTooltip,
};
});
} else {
return Object.keys(results.rows[0]).map((key: string) => {
const sanitizedKey = this.sanitizeString(key);
const type = results.meta[key];
const headerTooltip = type;
return {
field: sanitizedKey,
headerName: sanitizedKey,
headerTooltip,
};
});
}
return row;
});
ext.resultPanelCSV = this.convertToCsv(rowData).join("\n");
} else {
if (typeof results[0] === "string") {
return results.map((key: string) => {
const sanitizedKey = this.sanitizeString(key);
return { field: sanitizedKey, headerName: sanitizedKey };
});
}
return Object.keys(results[0]).map((key: string) => {
const sanitizedKey = this.sanitizeString(key);
return { field: sanitizedKey, headerName: sanitizedKey };
});
}
}

convertToGrid(results: any): string {
const isInsights = ext.connectionNode instanceof InsightsNode;
const queryResult = isInsights ? results.rows : results;

const columnDefs = this.generateCoumnDefs(results, isInsights);
let rowData = [];
if (!isInsights && typeof results[0] === "string") {
rowData = [];
} else {
rowData = queryResult.map((row: any) => {
for (const key in row) {
if (Object.prototype.hasOwnProperty.call(row, key)) {
row[key] =
row[key] !== undefined && row[key] !== null
? this.sanitizeString(row[key])
: "";
}
}
return row;
});
}
if (rowData.length > 0) {
ext.resultPanelCSV = this.convertToCsv(rowData).join("\n");
}
return JSON.stringify({
defaultColDef: {
sortable: true,
Expand All @@ -136,6 +183,7 @@ export class KdbResultsViewProvider implements WebviewViewProvider {
ensureDomOrder: true,
suppressContextMenu: true,
suppressDragLeaveHidesColumns: true,
tooltipShowDelay: 200,
});
}

Expand Down Expand Up @@ -187,13 +235,11 @@ export class KdbResultsViewProvider implements WebviewViewProvider {
if (typeof queryResult === "string" || typeof queryResult === "number") {
result =
queryResult !== ""
? `<p>${queryResult}</p>`
? `<p class="results-txt">${queryResult
.toString()
.replace(/\n/g, "<br/>")}</p>`
: "<p>No results to show</p>";
} else if (
typeof queryResult === "object" &&
queryResult !== null &&
queryResult instanceof Array
) {
} else if (queryResult) {
isGrid = true;
gridOptionsString = this.convertToGrid(queryResult);
}
Expand All @@ -215,12 +261,12 @@ export class KdbResultsViewProvider implements WebviewViewProvider {
<link rel="stylesheet" href="${this._getLibUri("resultsPanel.css")}" />
<link rel="stylesheet" href="${this._getLibUri("ag-grid.min.css")}" />
<link rel="stylesheet" href="${this._getLibUri(
"ag-theme-alpine.min.css"
"ag-theme-alpine.min.css",
)}" />
<title>Q Results</title>
<script nonce="${nonce}" src="${this._getLibUri(
"ag-grid-community.min.js"
)}"></script>
"ag-grid-community.min.js",
)}"></script>
</head>
<body>
<div id="results" class="results-view-container">
Expand Down
13 changes: 9 additions & 4 deletions src/utils/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function runQFileTerminal(filename?: string): void {

export function handleQueryResults(
results: any,
type: QueryResultType
type: QueryResultType,
): string {
let handledResult: string;
switch (type) {
Expand Down Expand Up @@ -177,7 +177,7 @@ function processLine(
line: string,
index: number,
fieldLengths: number[],
fieldNames: string[]
fieldNames: string[],
): object {
let start = 0;
const obj: { [key: string]: any } = { Index: index + 1 };
Expand Down Expand Up @@ -207,10 +207,15 @@ export function convertStringToArray(str: string): any[] {
return lines.flatMap((line, index) =>
fieldLengths.length > 0
? processLine(line, index, fieldLengths, fieldNames)
: []
: [],
);
}

if (lines.length === 2 && lines[1].startsWith("---")) {
lines.splice(1, 1);
return lines[0].split(" ").filter((part) => part !== "");
}

return lines
.flatMap((line, index) => {
const parts = line.split("|").map((part) => part.trim());
Expand All @@ -219,6 +224,6 @@ export function convertStringToArray(str: string): any[] {
: processLineWithoutSeparator(line, index);
})
.filter(
(obj) => !("Value" in obj && (obj.Value as string).startsWith("-"))
(obj) => !("Value" in obj && (obj.Value as string).startsWith("-")),
);
}
Loading

0 comments on commit 15964d1

Please sign in to comment.