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-46936] Add console log #337

Merged
merged 9 commits into from
Jun 6, 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
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ sonar.qualitygate.wait=true
sonar.sources=src,server
sonar.tests=test
sonar.javascript.lcov.reportPaths=lcov.info
sonar.coverage.exclusions=server/src/utils/parserUtils.ts,src/ipc/**,src/models/**,src/extension.ts,src/classes/**
sonar.coverage.exclusions=server/src/utils/parserUtils.ts,src/ipc/**,src/models/**,src/extension.ts,src/classes/**,src/commands/installTools.ts,src/utils/cpUtils.ts
151 changes: 42 additions & 109 deletions src/classes/insightsConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import { jwtDecode } from "jwt-decode";
import { JwtUser } from "../models/jwt_user";
import { Telemetry } from "../utils/telemetryClient";
import { handleScratchpadTableRes, handleWSResults } from "../utils/queryUtils";
import {
invalidUsernameJWT,
kdbOutputLog,
tokenUndefinedError,
} from "../utils/core";

export class InsightsConnection {
public connected: boolean;
Expand Down Expand Up @@ -75,10 +80,7 @@ export class InsightsConnection {
);

if (token === undefined) {
ext.outputChannel.appendLine(
"Error retrieving access token for insights.",
);
window.showErrorMessage("Failed to retrieve access token for insights");
tokenUndefinedError(this.connLabel);
return undefined;
}

Expand Down Expand Up @@ -108,14 +110,7 @@ export class InsightsConnection {
this.node.details.alias,
);
if (token === undefined) {
ext.outputChannel.appendLine(
"Error retrieving access token for insights connection named: " +
this.connLabel,
);
window.showErrorMessage(
"Failed to retrieve access token for insights connection named: " +
this.connLabel,
);
tokenUndefinedError(this.connLabel);
return undefined;
}
const headers = {
Expand All @@ -138,15 +133,16 @@ export class InsightsConnection {
},
async (progress, token) => {
token.onCancellationRequested(() => {
ext.outputChannel.appendLine("User cancelled the execution.");
kdbOutputLog(`User cancelled the Datasource Run.`, "WARNING");
});

progress.report({ message: "Query executing..." });

return await axios(options)
.then((response: any) => {
ext.outputChannel.appendLine(
`request status: ${response.status}`,
kdbOutputLog(
`[Datasource RUN] Status: ${response.status}.`,
"INFO",
);
if (isCompressed(response.data)) {
response.data = uncompress(response.data);
Expand All @@ -159,8 +155,9 @@ export class InsightsConnection {
};
})
.catch((error: any) => {
ext.outputChannel.appendLine(
`request status: ${error.response.status}`,
kdbOutputLog(
`[Datasource RUN] Status: ${error.response.status}.`,
"INFO",
);
return {
error: { buffer: error.response.data },
Expand Down Expand Up @@ -219,18 +216,13 @@ export class InsightsConnection {
);

if (token === undefined) {
ext.outputChannel.appendLine(
"Error retrieving access token for insights.",
);
window.showErrorMessage("Failed to retrieve access token for insights");
tokenUndefinedError(this.connLabel);
return undefined;
}

const username = jwtDecode<JwtUser>(token.accessToken);
if (username === undefined || username.preferred_username === "") {
ext.outputChannel.appendLine(
"JWT did not contain a valid preferred username",
);
invalidUsernameJWT(this.connLabel);
}
const headers = {
headers: {
Expand All @@ -251,9 +243,7 @@ export class InsightsConnection {
},
async (progress, token) => {
token.onCancellationRequested(() => {
ext.outputChannel.appendLine(
"User cancelled the scratchpad import.",
);
kdbOutputLog(`User cancelled the scratchpad import.`, "WARNING");
});

progress.report({ message: "Populating scratchpad..." });
Expand All @@ -264,9 +254,15 @@ export class InsightsConnection {
headers,
);

ext.outputChannel.append(JSON.stringify(scratchpadResponse.data));
kdbOutputLog(
`Executed successfully, stored in ${variableName}.`,
"INFO",
);

kdbOutputLog("[SCRATCHPAD] Data:", "INFO");
kdbOutputLog(JSON.stringify(scratchpadResponse.data), "INFO");
window.showInformationMessage(
`Executed successfully, stored in ${variableName}`,
`Executed successfully, stored in ${variableName}.`,
);
Telemetry.sendEvent(
"Datasource." + dsTypeString + ".Scratchpad.Populated",
Expand Down Expand Up @@ -295,17 +291,12 @@ export class InsightsConnection {
this.node.details.alias,
);
if (token === undefined) {
ext.outputChannel.appendLine(
"Error retrieving access token for insights.",
);
window.showErrorMessage("Failed to retrieve access token for insights");
tokenUndefinedError(this.connLabel);
return undefined;
}
const username = jwtDecode<JwtUser>(token.accessToken);
if (username === undefined || username.preferred_username === "") {
ext.outputChannel.appendLine(
"JWT did not contain a valid preferred username",
);
invalidUsernameJWT(this.connLabel);
}
const body = {
expression: query,
Expand All @@ -328,15 +319,14 @@ export class InsightsConnection {
},
async (progress, token) => {
token.onCancellationRequested(() => {
ext.outputChannel.appendLine(
"User cancelled the scratchpad execution.",
);
kdbOutputLog(`User cancelled the Scrathpad execution.`, "WARNING");
});

progress.report({ message: "Query is executing..." });
const spRes = await axios
.post(scratchpadURL.toString(), body, { headers })
.then((response: any) => {
kdbOutputLog(`[SCRATCHPAD] Status: ${response.status}`, "INFO");
if (isTableView && !response.data.error) {
const buffer = new Uint8Array(
response.data.data.map((x: string) => parseInt(x, 16)),
Expand Down Expand Up @@ -370,18 +360,13 @@ export class InsightsConnection {
);

if (token === undefined) {
ext.outputChannel.appendLine(
"Error retrieving access token for insights.",
);
window.showErrorMessage("Failed to retrieve access token for insights");
tokenUndefinedError(this.connLabel);
return false;
}

const username = jwtDecode<JwtUser>(token.accessToken);
if (username === undefined || username.preferred_username === "") {
ext.outputChannel.appendLine(
"JWT did not contain a valid preferred username",
);
invalidUsernameJWT(this.connLabel);
return false;
}
const headers = {
Expand All @@ -398,27 +383,28 @@ export class InsightsConnection {
},
async (progress, token) => {
token.onCancellationRequested(() => {
ext.outputChannel.appendLine(
"User cancelled the scratchpad reset.",
);
kdbOutputLog(`User cancelled the scratchpad reset.`, "WARNING");
return false;
});

progress.report({ message: "Reseting scratchpad..." });

const res = await axios
.post(scratchpadURL.toString(), null, headers)
.then((response: any) => {
console.log(response);
ext.outputChannel.append("Scratchpad.Reseted");
.then((_response: any) => {
kdbOutputLog(
`[SCRATCHPAD] Executed successfully, scratchpad reseted at ${this.connLabel} connection.`,
"INFO",
);
window.showInformationMessage(
`Executed successfully, scratchpad reseted at ${this.connLabel} connection`,
);
Telemetry.sendEvent("Scratchpad.Reseted");
return true;
})
.catch((error: any) => {
console.log(error);
.catch((_error: any) => {
kdbOutputLog(
`[SCRATCHPAD] Error ocurried while reseting scratchpad in connection ${this.connLabel}, try again.`,
"ERROR",
);
window.showErrorMessage(
"Error ocurried while reseting scratchpad, try again.",
);
Expand All @@ -432,57 +418,4 @@ export class InsightsConnection {
return false;
}
}

public async pingInsights(): Promise<boolean | undefined> {
if (this.connected) {
const pingURL = new url.URL(
ext.insightsServiceGatewayUrls.ping,
this.node.details.server,
);

const userToken = await getCurrentToken(
this.node.details.server,
this.node.details.alias,
);

if (userToken === undefined) {
ext.outputChannel.appendLine(
"Error retrieving access token for insights.",
);
window.showErrorMessage("Failed to retrieve access token for insights");
return false;
}

const body = {
labels: {},
};
const startTime = Date.now();

return await axios
.request({
method: "post",
url: pingURL.toString(),
data: body,
headers: { Authorization: `Bearer ${userToken.accessToken}` },
timeout: 2000,
})
.then((_response: any) => {
Telemetry.sendEvent("Insights.Pinged");
return true;
})
.catch((_error: any) => {
const endTime = Date.now();
const timeString = new Date().toLocaleTimeString();
const timeDiff = endTime - startTime;
ext.outputChannel.appendLine(
`[${timeString}] Connection keep alive error: ${this.connLabel}. Ping failed. ${_error.code}: status code ${_error.response.status}. Time Elapsed ${timeDiff}ms`,
);

window.showErrorMessage(
`Error in connection: ${this.connLabel}, check kdb OUTPUT for more info.`,
);
return false;
});
}
}
}
10 changes: 6 additions & 4 deletions src/classes/localConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import * as nodeq from "node-q";
import { window } from "vscode";
import { ext } from "../extensionVariables";
import { delay } from "../utils/core";
import { delay, kdbOutputLog } from "../utils/core";
import { convertStringToArray, handleQueryResults } from "../utils/execution";
import { queryWrapper } from "../utils/queryUtils";
import { QueryResult, QueryResultType } from "../models/queryResult";
Expand Down Expand Up @@ -79,14 +79,16 @@ export class LocalConnection {
window.showErrorMessage(
`Connection to server ${this.options.host}:${this.options.port} failed! Details: ${err?.message}`,
);
ext.outputChannel.appendLine(
kdbOutputLog(
`Connection to server ${this.options.host}:${this.options.port} failed! Details: ${err?.message}`,
"ERROR",
);
return;
}
conn.addListener("close", () => {
ext.outputChannel.appendLine(
`Connection stopped from ${this.options.host}:${this.options.port}`,
kdbOutputLog(
`Connection closed: ${this.options.host}:${this.options.port}`,
"INFO",
);
this.connected = false;
});
Expand Down
25 changes: 15 additions & 10 deletions src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { Telemetry } from "../utils/telemetryClient";
import { LocalConnection } from "../classes/localConnection";
import { ConnectionManagementService } from "../services/connectionManagerService";
import { InsightsConnection } from "../classes/insightsConnection";
import { offerConnectAction } from "../utils/core";
import { kdbOutputLog, offerConnectAction } from "../utils/core";

export async function addDataSource(): Promise<void> {
const kdbDataSourcesFolderPath = createKdbDataSourcesFolder();
Expand Down Expand Up @@ -102,8 +102,9 @@ export async function populateScratchpad(
dataSourceForm!,
);
} else {
ext.outputChannel.appendLine(
`Invalid scratchpad output variable name: ${outputVariable}`,
kdbOutputLog(
`[DATASOURCE] Invalid scratchpad output variable name: ${outputVariable}`,
"ERROR",
);
}
});
Expand Down Expand Up @@ -135,7 +136,10 @@ export async function runDataSource(
dataSourceForm.insightsNode = getConnectedInsightsNode();
const fileContent = dataSourceForm;

ext.outputChannel.appendLine(`Running ${fileContent.name} datasource...`);
kdbOutputLog(
`[DATASOURCE] Running ${fileContent.name} datasource...`,
"INFO",
);
let res: any;
const selectedType = getSelectedType(fileContent);
ext.isDatasourceExecution = true;
Expand All @@ -162,7 +166,7 @@ export async function runDataSource(
window.showErrorMessage(res.error);
} else if (ext.resultsViewProvider.isVisible()) {
const resultCount = typeof res === "string" ? "0" : res.rows.length;
ext.outputChannel.appendLine(`Results: ${resultCount} rows`);
kdbOutputLog(`[DATASOURCE] Results: ${resultCount} rows`, "INFO");
writeQueryResultsToView(
res,
query,
Expand All @@ -172,8 +176,9 @@ export async function runDataSource(
selectedType,
);
} else {
ext.outputChannel.appendLine(
`Results is a string with length: ${res.length}`,
kdbOutputLog(
`[DATASOURCE] Results is a string with length: ${res.length}`,
"INFO",
);
writeQueryResultsToConsole(
res,
Expand All @@ -188,8 +193,8 @@ export async function runDataSource(
}
} catch (error) {
window.showErrorMessage((error as Error).message);
kdbOutputLog(`[DATASOURCE] ${(error as Error).message}`, "ERROR");
DataSourcesPanel.running = false;
//TODO ADD ERROR TO CONSOLE HERE
} finally {
DataSourcesPanel.running = false;
}
Expand Down Expand Up @@ -397,11 +402,11 @@ export function getQuery(
}
}

function parseError(error: GetDataError) {
export function parseError(error: GetDataError) {
if (error instanceof Object && error.buffer) {
return handleWSError(error.buffer);
} else {
ext.outputChannel.appendLine(`Error: ${error}`);
kdbOutputLog(`[DATASOURCE] Error: ${error}`, "ERROR");
return {
error,
};
Expand Down
Loading
Loading