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

Ping insights #285

Merged
merged 2 commits into from
May 4, 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
66 changes: 66 additions & 0 deletions src/classes/insightsConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,70 @@ export class InsightsConnection {
return false;
}
}

public async pingInsights(): Promise<boolean> {
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: {},
};

return await window.withProgress(
{
location: ProgressLocation.Notification,
cancellable: false,
},
async (progress, token) => {
token.onCancellationRequested(() => {
ext.outputChannel.appendLine("User cancelled the ping request.");
return false;
});

progress.report({ message: "Pinging insights..." });

const res = await axios
.request({
method: "post",
url: pingURL.toString(),
data: body,
headers: { Authorization: `Bearer ${userToken.accessToken}` },
timeout: 1500,
})
.then((response: any) => {
console.log(response);
Telemetry.sendEvent("Insights.Pinged");
return true;
})
.catch((error: any) => {
console.log(error);
window.showErrorMessage(
`Error ocurried while pinging insights connection: ${this.connLabel}, the connection disconnected.`,
);
return false;
});

return res;
},
);
} else {
return false;
}
}
}
1 change: 1 addition & 0 deletions src/extensionVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export namespace ext {
data: "servicegateway/data",
sql: "servicegateway/qe/sql",
qsql: "servicegateway/qe/qsql",
ping: "servicegateway/kxi/ping",
};

export const insightsGrantType = {
Expand Down
36 changes: 36 additions & 0 deletions src/services/connectionManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

import { LocalConnection } from "../classes/localConnection";
import * as os from "os";
import { window, commands } from "vscode";
import { ext } from "../extensionVariables";
import { InsightsNode, KdbNode } from "./kdbTreeProvider";
Expand Down Expand Up @@ -97,6 +98,9 @@ export class ConnectionManagementService {
);

Telemetry.sendEvent("Connection.Connected.QProcess");
if (ext.connectedConnectionList.length === 0) {
this.startMonitoringConn();
}

ext.connectedConnectionList.push(localConnection);

Expand All @@ -112,6 +116,9 @@ export class ConnectionManagementService {
await insightsConn.connect();
if (insightsConn.connected) {
Telemetry.sendEvent("Connection.Connected.Insights");
if (ext.connectedConnectionList.length === 0) {
this.startMonitoringConn();
}
ext.connectedConnectionList.push(insightsConn);
this.isConnectedBehaviour(connection);
} else {
Expand Down Expand Up @@ -315,4 +322,33 @@ export class ConnectionManagementService {
);
}
}

public async checkInsightsConnectionIsAlive(): Promise<void> {
for (const connection of ext.connectedConnectionList) {
if (connection instanceof InsightsConnection) {
const res = await connection.pingInsights();
if (res === false) {
this.disconnect(connection.connLabel);
}
}
}
}

/* istanbul ignore next */
public async startMonitoringConn() {
let previousNetworkState = os.networkInterfaces();
const intervalId = setInterval(async () => {
const currentNetworkState = os.networkInterfaces();
if (
JSON.stringify(previousNetworkState) !==
JSON.stringify(currentNetworkState)
) {
previousNetworkState = currentNetworkState;
await this.checkInsightsConnectionIsAlive();
}
if (ext.connectedConnectionList.length === 0) {
clearInterval(intervalId);
}
}, 15000);
}
}
41 changes: 41 additions & 0 deletions test/suite/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,4 +1010,45 @@ describe("connectionManagerService", () => {
sinon.assert.notCalled(resetScratchpadStub);
});
});

describe("checkInsightsConnectionIsAlive()", () => {
let pingInsightsStub, disconnectStub: sinon.SinonStub;
beforeEach(() => {
pingInsightsStub = sinon.stub(insightsConn, "pingInsights");
disconnectStub = sinon.stub(connectionManagerService, "disconnect");
ext.connectedConnectionList.length = 0;
});

afterEach(() => {
ext.connectedConnectionList.length = 0;
sinon.restore();
});

it("should not call pingInsights if connection is not an instance of InsightsConnection", async () => {
ext.connectedConnectionList.push(localConn);
await connectionManagerService.checkInsightsConnectionIsAlive();
sinon.assert.notCalled(pingInsightsStub);
});

it("should not call pingInsights if there is no connection connected", async () => {
await connectionManagerService.checkInsightsConnectionIsAlive();
sinon.assert.notCalled(pingInsightsStub);
});

it("should call pingInsights if connection is an instance of InsightsConnection", async () => {
ext.connectedConnectionList.push(insightsConn);
pingInsightsStub.resolves(true);
await connectionManagerService.checkInsightsConnectionIsAlive();
sinon.assert.calledOnce(pingInsightsStub);
sinon.assert.notCalled(disconnectStub);
});

it("should call disconnect if pingInsights returns false", async () => {
ext.connectedConnectionList.push(insightsConn);
pingInsightsStub.resolves(false);
await connectionManagerService.checkInsightsConnectionIsAlive();
sinon.assert.calledOnce(pingInsightsStub);
sinon.assert.calledOnce(disconnectStub);
});
});
});
Loading