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-46633] Add confirm dialog to connect #317

Merged
merged 12 commits into from
May 23, 2024
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 11 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@
"command": "kdb.connect",
"title": "Connect server"
},
{
"category": "KX",
"command": "kdb.connect.via.dialog",
"title": "Connect server"
},
{
"category": "KX",
"command": "kdb.active.connection",
Expand All @@ -292,11 +297,6 @@
"command": "kdb.enableTLS",
"title": "Enable TLS"
},
{
"category": "KX",
"command": "kdb.insightsConnect",
"title": "Connect to Insights"
},
{
"category": "KX",
"command": "kdb.insightsRemove",
Expand Down Expand Up @@ -521,10 +521,6 @@
],
"menus": {
"commandPalette": [
{
"command": "kdb.insightsConnect",
"when": "false"
},
{
"command": "kdb.insightsRemove",
"when": "false"
Expand All @@ -549,6 +545,10 @@
"command": "kdb.connect",
"when": "false"
},
{
"command": "kdb.connect.via.dialog",
"when": "false"
},
{
"command": "kdb.active.connection",
"when": "false"
Expand Down Expand Up @@ -634,7 +634,7 @@
"view/item/context": [
{
"command": "kdb.connect",
"when": "view == kdb-servers && viewItem not in kdb.connected && viewItem in kdb.rootNodes",
"when": "view == kdb-servers && viewItem not in kdb.connected",
"group": "connection@1"
},
{
Expand All @@ -652,11 +652,6 @@
"when": "view == kdb-servers && viewItem not in kdb.insightsNodes && viewItem in kdb.kdbNodesWithoutTls && viewItem not in kdb.local",
"group": "connection@4"
},
{
"command": "kdb.insightsConnect",
"when": "view == kdb-servers && viewItem not in kdb.connected && viewItem in kdb.insightsNodes",
"group": "connection@0"
},
{
"command": "kdb.insightsRemove",
"when": "view == kdb-servers && viewItem in kdb.insightsNodes",
Expand Down Expand Up @@ -837,7 +832,7 @@
"@vscode/webview-ui-toolkit": "^1.4.0",
"@windozer/node-q": "^2.6.0",
"ag-grid-community": "^31.3.1",
"axios": "^1.7.0",
"axios": "^1.7.2",
"chevrotain": "^10.5.0",
"csv-parser": "^3.0.0",
"extract-zip": "^2.0.1",
Expand Down
6 changes: 4 additions & 2 deletions src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +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";

export async function addDataSource(): Promise<void> {
const kdbDataSourcesFolderPath = createKdbDataSourcesFolder();
Expand Down Expand Up @@ -92,7 +93,7 @@ export async function populateScratchpad(
selectedConnection instanceof LocalConnection ||
!selectedConnection
) {
window.showErrorMessage("No Insights active connection found");
offerConnectAction(connLabel);
DataSourcesPanel.running = false;
return;
}
Expand Down Expand Up @@ -123,7 +124,8 @@ export async function runDataSource(

try {
if (selectedConnection instanceof LocalConnection || !selectedConnection) {
throw new Error("The selected Insights Connection is not connected");
offerConnectAction(connLabel);
return;
}
selectedConnection.getMeta();
if (!selectedConnection?.meta?.payload.assembly) {
Expand Down
11 changes: 6 additions & 5 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,14 @@ export async function removeConnection(viewItem: KdbNode | InsightsNode) {
await connMngService.removeConnection(viewItem);
}

export async function connect(viewItem: KdbNode | InsightsNode): Promise<void> {
export async function connect(connLabel: string): Promise<void> {
const connMngService = new ConnectionManagementService();
commands.executeCommand("kdb-results.focus");
ExecutionConsole.start();
// handle cleaning up existing connection
if (ext.activeConnection !== undefined) {
DataSourcesPanel.close();
const viewItem = connMngService.retrieveConnection(connLabel);
if (viewItem === undefined) {
window.showErrorMessage("Connection not found");
return;
}

const isKdbNode = viewItem instanceof KdbNode;
Expand Down Expand Up @@ -334,7 +335,7 @@ export async function executeQuery(
if (connLabel === "") {
if (ext.activeConnection === undefined) {
window.showErrorMessage(
"No active connection founded. Connect to one connection.",
"No active connection found. Connect to one connection.",
);
//TODO ADD ERROR TO CONSOLE HERE
return undefined;
Expand Down
6 changes: 6 additions & 0 deletions src/commands/workspaceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { InsightsNode, KdbNode } from "../services/kdbTreeProvider";
import { runQuery } from "./serverCommand";
import { ExecutionTypes } from "../models/execution";
import { importOldDsFiles, oldFilesExists } from "../utils/dataSource";
import { offerConnectAction } from "../utils/core";

const connectionService = new ConnectionManagementService();

Expand Down Expand Up @@ -228,6 +229,7 @@ export async function activateConnectionForServer(server: string) {
/* istanbul ignore next */
export async function runActiveEditor(type?: ExecutionTypes) {
if (ext.activeTextEditor) {
const connMngService = new ConnectionManagementService();
const uri = ext.activeTextEditor.document.uri;
const isWorkbook = uri.path.endsWith(".kdb.q");

Expand All @@ -238,6 +240,10 @@ export async function runActiveEditor(type?: ExecutionTypes) {
if (!server) {
server = "";
}
if (!connMngService.isConnected(server) && isScratchpad(uri)) {
offerConnectAction(server);
return;
}
const executorName = ext.activeTextEditor.document.fileName
.split("/")
.pop();
Expand Down
21 changes: 12 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,18 @@ export async function activate(context: ExtensionContext) {
commands.registerCommand("kdb.datasource.import.ds", async () => {
await importOldDSFiles();
}),
commands.registerCommand("kdb.connect", async (viewItem: KdbNode) => {
await connect(viewItem);
}),
commands.registerCommand(
"kdb.connect",
async (viewItem: KdbNode | InsightsNode) => {
await connect(viewItem.label);
},
),
commands.registerCommand(
"kdb.connect.via.dialog",
async (connLabel: string) => {
await connect(connLabel);
},
),
commands.registerCommand(
"kdb.active.connection",
async (viewItem: KdbNode) => {
Expand All @@ -197,12 +206,6 @@ export async function activate(context: ExtensionContext) {
commands.registerCommand("kdb.enableTLS", async (viewItem: KdbNode) => {
await enableTLS(viewItem.children[0]);
}),
commands.registerCommand(
"kdb.insightsConnect",
async (viewItem: InsightsNode) => {
await connect(viewItem);
},
),
commands.registerCommand(
"kdb.insightsRemove",
async (viewItem: InsightsNode) => {
Expand Down
9 changes: 8 additions & 1 deletion src/services/connectionManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export class ConnectionManagementService {
}
}

public async refreshGetMetas(): Promise<void> {
public async refreshAllGetMetas(): Promise<void> {
if (ext.connectedConnectionList.length > 0) {
const promises = ext.connectedConnectionList.map(async (connection) => {
if (connection instanceof InsightsConnection) {
Expand All @@ -339,4 +339,11 @@ export class ConnectionManagementService {
await Promise.all(promises);
}
}

public async refreshGetMeta(connLabel: string): Promise<void> {
const connection = this.retrieveConnectedConnection(connLabel);
if (connection instanceof InsightsConnection) {
await connection.getMeta();
}
}
}
8 changes: 7 additions & 1 deletion src/services/dataSourceEditorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
import { InsightsConnection } from "../classes/insightsConnection";
import { MetaObjectPayload } from "../models/meta";
import { ConnectionManagementService } from "./connectionManagerService";
import { offerConnectAction } from "../utils/core";

export class DataSourceEditorProvider implements CustomTextEditorProvider {
public filenname = "";
Expand Down Expand Up @@ -134,6 +135,7 @@ export class DataSourceEditorProvider implements CustomTextEditorProvider {
changeDocumentSubscription.dispose();
});

/* istanbul ignore next */
webview.onDidReceiveMessage(async (msg: DataSourceMessage2) => {
switch (msg.command) {
case DataSourceCommand.Server:
Expand All @@ -156,8 +158,12 @@ export class DataSourceEditorProvider implements CustomTextEditorProvider {
break;
case DataSourceCommand.Refresh:
const connMngService = new ConnectionManagementService();
await connMngService.refreshGetMetas();
const selectedServer = getServerForUri(document.uri) || "";
if (!connMngService.isConnected(selectedServer)) {
offerConnectAction(selectedServer);
break;
}
await connMngService.refreshGetMeta(selectedServer);
this.cache.delete(selectedServer);
updateWebview();
break;
Expand Down
15 changes: 15 additions & 0 deletions src/utils/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,21 @@ export function getServerAlias(serverList: ServerDetails[]): void {
});
}

/* istanbul ignore next */
export function offerConnectAction(connLabel: string): void {
window
.showInformationMessage(
`You aren't connected to ${connLabel}, would you like to connect?`,
"Connect",
"Cancel",
)
.then(async (result) => {
if (result === "Connect") {
await commands.executeCommand("kdb.connect.via.dialog", connLabel);
}
});
}

export function getInsightsAlias(insightsList: InsightDetails[]): void {
insightsList.forEach((x) => {
ext.kdbConnectionAliasList.push(x.alias);
Expand Down
22 changes: 22 additions & 0 deletions test/suite/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,28 @@ describe("serverCommand", () => {
assert.ok(updateServersStub.notCalled);
}).timeout(5000);
});

describe("connect", () => {
const connService = new ConnectionManagementService();
const _console = vscode.window.createOutputChannel("q Console Output");
const executionConsole = new ExecutionConsole(_console);
let windowErrorStub, retrieveConnectionStub: sinon.SinonStub;

beforeEach(() => {
windowErrorStub = sinon.stub(vscode.window, "showErrorMessage");
retrieveConnectionStub = sinon.stub(connService, "retrieveConnection");
});

afterEach(() => {
sinon.restore();
});

it("should show error message if connection not found", async () => {
retrieveConnectionStub.returns(undefined);
await serverCommand.connect("test");
windowErrorStub.calledOnce;
});
});
});

describe("walkthroughCommand", () => {
Expand Down
20 changes: 15 additions & 5 deletions test/suite/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ describe("connectionManagerService", () => {
});
});

describe("refreshGetMetas", () => {
describe("refreshAllGetMetas && refreshGetMeta", () => {
let getMetaStub: sinon.SinonStub;
beforeEach(() => {
getMetaStub = sinon.stub(insightsConn, "getMeta");
Expand All @@ -1063,14 +1063,24 @@ describe("connectionManagerService", () => {
ext.connectedConnectionList.length = 0;
});

it("Should not refresh getMetas if connection is not InsightsConnection", async () => {
await connectionManagerService.refreshGetMetas();
it("Should not refreshAllgetMetas if connection is not InsightsConnection", async () => {
await connectionManagerService.refreshAllGetMetas();
sinon.assert.notCalled(getMetaStub);
});

it("Should refresh getMetas if connection is InsightsConnection", async () => {
it("Should refreshAllgetMetas if connection is InsightsConnection", async () => {
ext.connectedConnectionList.push(insightsConn);
await connectionManagerService.refreshGetMetas();
await connectionManagerService.refreshAllGetMetas();
sinon.assert.calledOnce(getMetaStub);
});
it("Should not refreshGetMeta if connection is not InsightsConnection", async () => {
await connectionManagerService.refreshGetMeta("test");
sinon.assert.notCalled(getMetaStub);
});

it("Should refreshGetMeta if connection is InsightsConnection", async () => {
ext.connectedConnectionList.push(insightsConn);
await connectionManagerService.refreshGetMeta(insightsConn.connLabel);
sinon.assert.calledOnce(getMetaStub);
});
});
Expand Down
Loading