Skip to content

Commit

Permalink
fix active connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip-Carneiro-KX committed Apr 10, 2024
1 parent 28ea800 commit 034cf75
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"onCommand:kdb.newConnection.createNewBundledConnection",
"onCommand:kdb.removeConnection",
"onCommand:kdb.connect",
"onCommand:kdb.active.connection",
"onCommand:kdb.disconnect",
"onCommand:kdb.addAuthentication",
"onCommand:kdb.enableTLS",
Expand Down Expand Up @@ -219,6 +220,10 @@
"command": "kdb.connect",
"title": "Connect server"
},
{
"command": "kdb.active.connection",
"title": "Active connection"
},
{
"command": "kdb.addAuthentication",
"title": "Add Authentication",
Expand Down Expand Up @@ -470,6 +475,10 @@
"command": "kdb.connect",
"when": "false"
},
{
"command": "kdb.active.connection",
"when": "false"
},
{
"command": "kdb.disconnect",
"when": "false"
Expand Down Expand Up @@ -532,6 +541,11 @@
"when": "view == kdb-servers && viewItem not in kdb.connected && viewItem in kdb.rootNodes",
"group": "connection@1"
},
{
"command": "kdb.active.connection",
"when": "view == kdb-servers && viewItem in kdb.connected && (viewItem in kdb.rootNodes || viewItem in kdb.insightsNodes)",
"group": "connection@1"
},
{
"command": "kdb.addAuthentication",
"when": "view == kdb-servers && viewItem not in kdb.insightsNodes && viewItem in kdb.kdbNodesWithoutAuth",
Expand Down
7 changes: 7 additions & 0 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,13 @@ export async function connect(viewItem: KdbNode): Promise<void> {
ext.serverProvider.reload();
}

export function activeConnection(viewItem: KdbNode | InsightsNode): void {
const connMngService = new ConnectionManagementService();
connMngService.setActiveConnection(viewItem);
refreshDataSourcesPanel();
ext.serverProvider.reload();
}

export async function disconnect(connLabel?: string): Promise<void> {
const insightsNode = ext.kdbinsightsNodes.find((n) =>
ext.connectionNode instanceof InsightsNode
Expand Down
7 changes: 7 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
stopLocalProcessByServerName,
} from "./commands/installTools";
import {
activeConnection,
addInsightsConnection,
addKdbConnection,
addNewConnection,
Expand Down Expand Up @@ -164,6 +165,12 @@ export async function activate(context: ExtensionContext) {
commands.registerCommand("kdb.connect", async (viewItem: KdbNode) => {
await connect(viewItem);
}),
commands.registerCommand(
"kdb.active.connection",
async (viewItem: KdbNode) => {
activeConnection(viewItem);
},
),

commands.registerCommand("kdb.enableTLS", async (viewItem: KdbNode) => {
await enableTLS(viewItem.children[0]);
Expand Down
1 change: 1 addition & 0 deletions src/extensionVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export namespace ext {
export let connection: LocalConnection | undefined;
export let activeConnection: LocalConnection | undefined;
export const connectedConnectionList: Array<LocalConnection> = [];
export const connectedContextStrings: Array<string> = [];
export const connectionsList: Array<KdbNode | InsightsNode> = [];
export let hideDetailedConsoleQueryOutput: boolean;
export let connectionNode: KdbNode | InsightsNode | undefined;
Expand Down
41 changes: 29 additions & 12 deletions src/services/connectionManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export class ConnectionManagementService {
return connection.details.serverName + ":" + connection.details.serverPort;
}

public removeConnectionFromContextString(connLabel: string): void {
const index = ext.connectedContextStrings.indexOf(connLabel);
if (index > -1) {
ext.connectedContextStrings.splice(index, 1);
}
}

public async connect(connLabel: string): Promise<void> {
// check if connection exists
const connection = this.retrieveConnection(connLabel);
Expand All @@ -64,7 +71,7 @@ export class ConnectionManagementService {
authCredentials ? authCredentials.split(":") : undefined,
connection.details.tls,
);
await localConnection.connect((err, conn) => {
localConnection.connect((err, conn) => {
if (err) {
window.showErrorMessage(err.message);
return;
Expand All @@ -75,31 +82,35 @@ export class ConnectionManagementService {
`Connection established successfully to: ${connLabel}`,
);

commands.executeCommand("setContext", "kdb.connected", [
`${connLabel}` + " (connected)",
]);
ext.connectedContextStrings.push(connLabel);

commands.executeCommand(
"setContext",
"kdb.connected",
ext.connectedContextStrings,
);

ext.connectionNode = connection;
Telemetry.sendEvent("Connection.Connected.QProcess");
ext.serverProvider.reload();

this.setActiveConnection(connLabel);
this.setActiveConnection(connection);
ext.connectedConnectionList.push(localConnection);
ext.activeConnection = localConnection;
}
});
ext.connectedConnectionList.push(localConnection);
ext.activeConnection = localConnection;
} else {
// work with Insights nodes
}
}

public setActiveConnection(connLabel: string): void {
const connection = this.retrieveConnectedConnection(connLabel);
public setActiveConnection(node: KdbNode | InsightsNode): void {
const connection = this.retrieveConnectedConnection(node.label);
if (!connection) {
return;
}
commands.executeCommand("setContext", "kdb.connected.active", [
`${connLabel}` + " (active)",
`${node.label}`,
]);
Telemetry.sendEvent("Connection.Connected.Active");
ext.activeConnection = connection;
Expand All @@ -108,19 +119,25 @@ export class ConnectionManagementService {

public disconnect(connLabel: string): void {
const connection = this.retrieveConnectedConnection(connLabel);
const connectionNode = this.retrieveConnection(connLabel);
if (!connection) {
return;
}
const isLocal = this.isLocalConnection();
if (isLocal) {
if (isLocal && connectionNode) {
connection.getConnection()?.close(() => {
ext.connectedConnectionList.splice(
ext.connectedConnectionList.indexOf(connection),
1,
);
ext.activeConnection = undefined;
ext.connectionNode = undefined;
commands.executeCommand("setContext", "kdb.connected", false);
this.removeConnectionFromContext(connectionNode);

Check failure on line 135 in src/services/connectionManagerService.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Property 'removeConnectionFromContext' does not exist on type 'ConnectionManagementService'. Did you mean 'removeConnectionFromContextString'?

Check failure on line 135 in src/services/connectionManagerService.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Property 'removeConnectionFromContext' does not exist on type 'ConnectionManagementService'. Did you mean 'removeConnectionFromContextString'?
commands.executeCommand(
"setContext",
"kdb.connected",
ext.connectedContextStrings,
);
commands.executeCommand("setContext", "kdb.connected.active", false);
Telemetry.sendEvent("Connection.Disconnected");
ext.outputChannel.appendLine(
Expand Down

0 comments on commit 034cf75

Please sign in to comment.