Skip to content
356 changes: 141 additions & 215 deletions src/classes/insightsConnection.ts

Large diffs are not rendered by default.

63 changes: 34 additions & 29 deletions src/classes/localConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
*/

import * as nodeq from "node-q";
import { commands, window } from "vscode";
import { commands } from "vscode";

import { ext } from "../extensionVariables";
import { QueryResult, QueryResultType } from "../models/queryResult";
import { delay, kdbOutputLog } from "../utils/core";
import { delay } from "../utils/core";
import { convertStringToArray, handleQueryResults } from "../utils/execution";
import { MessageKind, notify } from "../utils/notifications";
import { queryWrapper } from "../utils/queryUtils";

const logger = "localConnection";

export class LocalConnection {
public connected: boolean;
public connLabel: string;
Expand Down Expand Up @@ -84,38 +87,37 @@ export class LocalConnection {
ext.serverProvider.reload();

if (this.connLabel.endsWith("[local]")) {
window
.showErrorMessage(
`Connection to server ${this.options.host}:${this.options.port} failed.`,
"Start q process",
)
.then((res) => {
if (res) {
commands.executeCommand(
"kdb.connections.localProcess.start",
ext.connectionsList.find(
(conn) => conn.label === this.connLabel,
),
);
}
});
notify(
`Connection to server ${this.options.host}:${this.options.port} failed.`,
MessageKind.ERROR,
{ logger, params: err },
"Start q process",
).then((res) => {
if (res) {
commands.executeCommand(
"kdb.connections.localProcess.start",
ext.connectionsList.find(
(conn) => conn.label === this.connLabel,
),
);
}
});
} else {
window.showErrorMessage(
notify(
`Connection to server ${this.options.host}:${this.options.port} failed.`,
MessageKind.ERROR,
{ logger, params: err },
);
}

kdbOutputLog(
`Connection to server ${this.options.host}:${this.options.port} failed! Details: ${err?.message}`,
"CONNECTION",
);
return;
}
conn.addListener("close", () => {
commands.executeCommand("kdb.connections.disconnect", this.connLabel);
kdbOutputLog(
notify(
`Connection closed: ${this.options.host}:${this.options.port}`,
"INFO",
MessageKind.DEBUG,
{ logger },
);
ext.outputChannel.show();
});
Expand Down Expand Up @@ -306,9 +308,10 @@ export class LocalConnection {
'{[q] t:system"T";tm:@[{$[x>0;[system"T ",string x;1b];0b]};0;{0b}];r:$[tm;@[0;(q;::);{[tm; t; msgs] if[tm;system"T ",string t];\'msgs}[tm;t]];@[q;::;{\'x}]];if[tm;system"T ",string t];r}{do[1000;2+2];{@[{.z.ide.ns.r1:x;:.z.ide.ns.r1};x;{r:y;:r}[;x]]}({:x!{![sv[`;] each x cross `Tables`Functions`Variables; system each "afv" cross enlist[" "] cross enlist string x]} each x} [{raze x,.z.s\'[{x where{@[{1#get x};x;`]~1#.q}\'[x]}` sv\'x,\'key x]}`]),(enlist `.z)!flip (`.z.Tables`.z.Functions`.z.Variables)!(enlist 0#`;enlist `ac`bm`exit`pc`pd`pg`ph`pi`pm`po`pp`ps`pw`vs`ts`s`wc`wo`ws;enlist `a`b`e`f`h`i`k`K`l`o`q`u`w`W`x`X`n`N`p`P`z`Z`t`T`d`D`c`zd)}';
this.connection?.k(globalQuery, (err, result) => {
if (err) {
window.showErrorMessage(
`Failed to retrieve kdb+ global variables: '${err.message}`,
);
notify("Failed to retrieve kdb+ global variables.", MessageKind.ERROR, {
logger,
params: err,
});
return;
}

Expand Down Expand Up @@ -350,8 +353,10 @@ export class LocalConnection {
const reservedQuery = ".Q.res";
this.connection?.k(reservedQuery, (err, result) => {
if (err) {
window.showErrorMessage(
`Failed to retrieve kdb+ reserved keywords: '${err.message}`,
notify(
"Failed to retrieve kdb+ reserved keywords.",
MessageKind.ERROR,
{ logger, params: err },
);
return;
}
Expand Down
63 changes: 29 additions & 34 deletions src/commands/buildToolsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import Path from "path";
import {
Diagnostic,
DiagnosticSeverity,
ProgressLocation,
Range,
TextDocument,
Uri,
window,
workspace,
} from "vscode";

import { ext } from "../extensionVariables";
import { getBasename } from "../utils/core";
import { Runner } from "../utils/notifications";

const cache = new Map<string, Thenable<Diagnostic[]>>();

Expand Down Expand Up @@ -182,39 +182,34 @@ const severity: { [key: string]: DiagnosticSeverity } = {
};

function lint(document: TextDocument) {
return window.withProgress(
{
title: "Linting",
location: ProgressLocation.Window,
cancellable: true,
},
async (_progress, token) => {
try {
const results = await getLinterResults(document.uri);
if (token.isCancellationRequested) {
cache.delete(document.uri.path);
return [];
}
return results.map((result) => {
const diagnostic = new Diagnostic(
new Range(
result.startLine - 1,
result.startCol - 1,
result.endLine - 1,
result.endCol,
),
result.description,
severity[result.errorClass],
);
diagnostic.source = "qlint";
diagnostic.code = result.label;
return diagnostic;
});
} catch (error) {
throw new Error(`Linting Failed ${error}`);
const runner = Runner.create(async (_, token) => {
try {
const results = await getLinterResults(document.uri);
if (token.isCancellationRequested) {
cache.delete(document.uri.path);
return [];
}
},
);
return results.map((result) => {
const diagnostic = new Diagnostic(
new Range(
result.startLine - 1,
result.startCol - 1,
result.endLine - 1,
result.endCol,
),
result.description,
severity[result.errorClass],
);
diagnostic.source = "qlint";
diagnostic.code = result.label;
return diagnostic;
});
} catch (error) {
throw new Error(`Linting Failed ${error}`);
}
});
runner.title = `Linting ${getBasename(document.uri)}.`;
return runner.execute();
}

async function setDiagnostics(document: TextDocument) {
Expand Down
79 changes: 45 additions & 34 deletions src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,26 @@ import { scratchpadVariableInput } from "../models/items/server";
import { UDARequestBody } from "../models/uda";
import { DataSourcesPanel } from "../panels/datasource";
import { ConnectionManagementService } from "../services/connectionManagerService";
import {
kdbOutputLog,
noSelectedConnectionAction,
offerConnectAction,
} from "../utils/core";
import { noSelectedConnectionAction } from "../utils/core";
import {
checkIfTimeParamIsCorrect,
convertTimeToTimestamp,
createKdbDataSourcesFolder,
getConnectedInsightsNode,
} from "../utils/dataSource";
import { MessageKind, notify } from "../utils/notifications";
import {
addQueryHistory,
generateQSqlBody,
handleScratchpadTableRes,
handleWSError,
handleWSResults,
} from "../utils/queryUtils";
import { Telemetry } from "../utils/telemetryClient";
import { retrieveUDAtoCreateReqBody } from "../utils/uda";
import { validateScratchpadOutputVariableName } from "../validators/interfaceValidator";

const logger = "dataSourceCommand";

export async function addDataSource(): Promise<void> {
const kdbDataSourcesFolderPath = createKdbDataSourcesFolder();

Expand All @@ -74,10 +72,11 @@ export async function addDataSource(): Promise<void> {
defaultDataSourceContent.insightsNode = insightsNode;

fs.writeFileSync(filePath, JSON.stringify(defaultDataSourceContent));
window.showInformationMessage(
notify(
`Created ${fileName} in ${kdbDataSourcesFolderPath}.`,
MessageKind.INFO,
{ logger, telemetry: "Datasource.Created" },
);
Telemetry.sendEvent("Datasource.Created");
}

export async function populateScratchpad(
Expand All @@ -101,7 +100,6 @@ export async function populateScratchpad(
selectedConnection instanceof LocalConnection ||
!selectedConnection
) {
offerConnectAction(connLabel);
DataSourcesPanel.running = false;
return;
}
Expand All @@ -115,9 +113,10 @@ export async function populateScratchpad(
qenvEnabled === "Enabled",
);
} else {
kdbOutputLog(
`[DATASOURCE] Invalid scratchpad output variable name: ${outputVariable}`,
"ERROR",
notify(
`Invalid scratchpad output variable name: ${outputVariable}`,
MessageKind.ERROR,
{ logger },
);
}
});
Expand All @@ -144,7 +143,6 @@ export async function runDataSource(

try {
if (selectedConnection instanceof LocalConnection || !selectedConnection) {
offerConnectAction(connLabel);
return;
}
selectedConnection.getMeta();
Expand All @@ -155,14 +153,15 @@ export async function runDataSource(
dataSourceForm.insightsNode = getConnectedInsightsNode();
const fileContent = dataSourceForm;

kdbOutputLog(
`[DATASOURCE] Running ${fileContent.name} datasource...`,
"INFO",
);
let res: any;
const selectedType = getSelectedType(fileContent);
ext.isDatasourceExecution = true;
Telemetry.sendEvent("Datasource." + selectedType + ".Run");

notify(`Running ${fileContent.name} datasource...`, MessageKind.DEBUG, {
logger,
telemetry: "Datasource." + selectedType + ".Run",
});

switch (selectedType) {
case "API":
res = await runApiDataSource(fileContent, selectedConnection);
Expand All @@ -189,13 +188,18 @@ export async function runDataSource(
const query = getQuery(fileContent, selectedType);

if (!success) {
Telemetry.sendEvent("Datasource." + selectedType + ".Run.Error");
window.showErrorMessage(res.error);
notify("Query execution failed.", MessageKind.ERROR, {
logger,
params: res.error,
telemetry: "Datasource." + selectedType + ".Run.Error",
});
}
if (ext.isResultsTabVisible) {
if (success) {
const resultCount = typeof res === "string" ? "0" : res.rows.length;
kdbOutputLog(`[DATASOURCE] Results: ${resultCount} rows`, "INFO");
notify(`Results: ${resultCount} rows`, MessageKind.DEBUG, {
logger,
});
} else if (!success) {
res = res.errorMsg ? res.errorMsg : res.error;
}
Expand All @@ -209,9 +213,10 @@ export async function runDataSource(
);
} else {
if (success) {
kdbOutputLog(
`[DATASOURCE] Results is a string with length: ${res.length}`,
"INFO",
notify(
`Results is a string with length: ${res.length}`,
MessageKind.DEBUG,
{ logger },
);
} else if (res.error) {
res = res.errorMsg ? res.errorMsg : res.error;
Expand All @@ -229,8 +234,10 @@ export async function runDataSource(
addDStoQueryHistory(dataSourceForm, success, connLabel, executorName);
}
} catch (error) {
window.showErrorMessage((error as Error).message);
kdbOutputLog(`[DATASOURCE] ${(error as Error).message}`, "ERROR", true);
notify(`Datasource error: ${error}.`, MessageKind.ERROR, {
logger,
params: error,
});
DataSourcesPanel.running = false;
} finally {
DataSourcesPanel.running = false;
Expand Down Expand Up @@ -281,8 +288,10 @@ export async function runApiDataSource(
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",
notify(
"The time parameters (startTS and endTS) are not correct, please check the format or if the startTS is before the endTS",
MessageKind.ERROR,
{ logger },
);
return;
}
Expand Down Expand Up @@ -445,7 +454,10 @@ export async function runUDADataSource(
const udaReqBody = await retrieveUDAtoCreateReqBody(uda, selectedConn);

if (udaReqBody.error) {
kdbOutputLog(`[DATASOURCE] Error: ${udaReqBody.error}`, "ERROR", true);
notify(`Datasource error.`, MessageKind.ERROR, {
logger,
params: udaReqBody.error,
});
return udaReqBody;
}

Expand Down Expand Up @@ -492,11 +504,10 @@ export function parseError(error: GetDataError) {
if (error instanceof Object && error.buffer) {
return handleWSError(error.buffer);
} else {
kdbOutputLog(
`[DATASOURCE] Error: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
"ERROR",
true,
);
notify(`Datasource error.`, MessageKind.ERROR, {
logger,
params: error,
});
return {
error,
};
Expand Down
Loading