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-50022 - fix some bugs related to conenctions #394

Merged
merged 4 commits into from
Aug 8, 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
12 changes: 9 additions & 3 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import { Telemetry } from "../utils/telemetryClient";
import { ConnectionManagementService } from "../services/connectionManagerService";
import { InsightsConnection } from "../classes/insightsConnection";
import { MetaContentProvider } from "../services/metaContentProvider";
import { handleLabelsConnMap } from "../utils/connLabel";
import { handleLabelsConnMap, removeConnFromLabels } from "../utils/connLabel";

export async function addNewConnection(): Promise<void> {
NewConnectionPannel.close();
Expand Down Expand Up @@ -138,7 +138,7 @@ export async function addInsightsConnection(
const newInsights = getInsights();
if (newInsights != undefined) {
if (labels && labels.length > 0) {
handleLabelsConnMap(labels, insightsData.alias);
await handleLabelsConnMap(labels, insightsData.alias);
}
ext.serverProvider.refreshInsights(newInsights);
Telemetry.sendEvent("Connection.Created.Insights");
Expand Down Expand Up @@ -193,6 +193,7 @@ export async function editInsightsConnection(
const oldKey = getKeyForServerName(oldAlias);
const newKey = insightsData.alias;
if (insights[oldKey] && oldAlias !== insightsData.alias) {
removeConnFromLabels(oldAlias);
const uInsights = Object.keys(insights).filter((insight) => {
return insight !== oldKey;
});
Expand Down Expand Up @@ -225,6 +226,8 @@ export async function editInsightsConnection(
if (newInsights != undefined) {
if (labels && labels.length > 0) {
await handleLabelsConnMap(labels, insightsData.alias);
} else {
removeConnFromLabels(insightsData.alias);
}
ext.serverProvider.refreshInsights(newInsights);
Telemetry.sendEvent("Connection.Edited.Insights");
Expand Down Expand Up @@ -384,7 +387,7 @@ export async function addKdbConnection(
const newServers = getServers();
if (newServers != undefined) {
if (labels && labels.length > 0) {
handleLabelsConnMap(labels, kdbData.serverAlias);
await handleLabelsConnMap(labels, kdbData.serverAlias);
}
Telemetry.sendEvent("Connection.Created.QProcess");
ext.serverProvider.refresh(newServers);
Expand Down Expand Up @@ -487,6 +490,8 @@ export async function editKdbConnection(
if (newServers != undefined) {
if (labels && labels.length > 0) {
await handleLabelsConnMap(labels, kdbData.serverAlias);
} else {
removeConnFromLabels(kdbData.serverAlias);
}
ext.serverProvider.refresh(newServers);
Telemetry.sendEvent("Connection.Edited.KDB");
Expand All @@ -499,6 +504,7 @@ export async function editKdbConnection(
`Edited KDB connection: ${kdbData.serverAlias}`,
);
if (oldKey !== newKey) {
removeConnFromLabels(oldKey);
removeAuthConnection(oldKey);
if (kdbData.auth) {
addAuthConnection(newKey, kdbData.username!, kdbData.password!);
Expand Down
15 changes: 13 additions & 2 deletions src/services/connectionManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ export class ConnectionManagementService {
): LocalConnection | InsightsConnection | undefined {
return ext.connectedConnectionList.find(
(connection: LocalConnection | InsightsConnection) => {
if (!connLabel) {
return false;
}
const escapedConnLabel = connLabel.replace(
/[-[\]{}()*+?.,\\^$|#\s]/g,
"\\$&",
);
const regex = new RegExp(
`\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+ \\[${connLabel}\\]`,
`\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+ \\[${escapedConnLabel}\\]`,
);
return (
connLabel === connection.connLabel || regex.test(connection.connLabel)
Expand All @@ -62,8 +69,12 @@ export class ConnectionManagementService {
}

public isConnected(connLabel: string): boolean {
const escapedConnLabel = connLabel.replace(
/[-[\]{}()*+?.,\\^$|#\s]/g,
"\\$&",
);
const regex = new RegExp(
`\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+ \\[${connLabel}\\]`,
`\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+ \\[${escapedConnLabel}\\]`,
);
return (
ext.connectedContextStrings.includes(connLabel) ||
Expand Down
44 changes: 23 additions & 21 deletions src/webview/components/kdbNewConnectionView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ export class KdbNewConnectionView extends LitElement {
this.editAuth = !this.editAuth;
}

renderServerNameDesc() {
return this.isBundledQ
renderServerNameDesc(isBundleQ?: boolean) {
return isBundleQ
? html`<span
>Name your server "local"; this name has been reserved for use by the
packaged q in the kdb VS Code extension and must be used to access
Expand All @@ -149,8 +149,8 @@ export class KdbNewConnectionView extends LitElement {
>`;
}

renderServerNameField(serverType: ServerType) {
return this.isBundledQ
renderServerNameField(serverType: ServerType, isBundleQ?: boolean) {
return isBundleQ
? html`<vscode-text-field
class="text-field larger option-title"
value="${this.bundledServer.serverAlias}"
Expand Down Expand Up @@ -180,17 +180,19 @@ export class KdbNewConnectionView extends LitElement {
</vscode-text-field>`;
}

renderServerName(serverType: ServerType) {
renderServerName(serverType: ServerType, isBundleQ?: boolean) {
return html`
<div class="row">${this.renderServerNameField(serverType)}</div>
<div class="row">
${this.renderServerNameField(serverType, isBundleQ)}
</div>
<div class="row option-description option-help">
${this.renderServerNameDesc()}
${this.renderServerNameDesc(isBundleQ)}
</div>
`;
}

renderPortNumberDesc() {
return this.isBundledQ
renderPortNumberDesc(isBundleQ?: boolean) {
return isBundleQ
? html`<span
>Ensure the port number you use does not conflict with another
port.</span
Expand All @@ -201,40 +203,40 @@ export class KdbNewConnectionView extends LitElement {
>`;
}

renderPortNumber() {
renderPortNumber(isBundleQ?: boolean) {
return html`
<div class="row">
<vscode-text-field
class="text-field larger option-title"
value="${this.isBundledQ
value="${isBundleQ
? this.bundledServer.serverPort
: this.kdbServer.serverPort}"
@input="${(event: Event) => {
const value = (event.target as HTMLSelectElement).value;
this.isBundledQ
isBundleQ
? (this.bundledServer.serverPort = value)
: (this.kdbServer.serverPort = value);
}}"
>Set port number</vscode-text-field
>
</div>
<div class="row option-description option-help">
${this.renderPortNumberDesc()}
${this.renderPortNumberDesc(isBundleQ)}
</div>
`;
}

renderConnAddDesc(serverType: ServerType) {
return this.isBundledQ
renderConnAddDesc(serverType: ServerType, isBundleQ?: boolean) {
return isBundleQ
? html`The localhost connection is already set up for you.`
: serverType === ServerType.KDB
? html`Set the IP of your kdb+ database connection.`
: html`Set the IP of your kdb+ database connection, your Insights
connection must be deployed for kdb VS Code to access.`;
}

renderConnAddress(serverType: ServerType) {
return this.isBundledQ
renderConnAddress(serverType: ServerType, isBundleQ?: boolean) {
return isBundleQ
? html`
<div class="row">
<vscode-text-field
Expand Down Expand Up @@ -490,16 +492,16 @@ export class KdbNewConnectionView extends LitElement {
<div class="col">
<div class="row">
<div class="col gap-0">
${this.renderServerName(ServerType.KDB)}
${this.renderServerName(ServerType.KDB, true)}
</div>
</div>
<div class="row">
<div class="col gap-0">
${this.renderConnAddress(ServerType.KDB)}
${this.renderConnAddress(ServerType.KDB, true)}
</div>
</div>
<div class="row">
<div class="col gap-0">${this.renderPortNumber()}</div>
<div class="col gap-0">${this.renderPortNumber(true)}</div>
</div>
${this.renderConnectionLabelsSection()}
</div>
Expand Down Expand Up @@ -826,7 +828,7 @@ export class KdbNewConnectionView extends LitElement {

render() {
if (!this.connectionData) {
return this.renderNewConnectionForm();
return html` ${this.renderNewConnectionForm()} `;
} else {
return html` ${this.renderEditConnectionForm()} `;
}
Expand Down
4 changes: 2 additions & 2 deletions test/suite/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ describe("serverCommand", () => {
});
it("should add new Insights connection", async () => {
getInsightsStub.returns({});
await serverCommand.addInsightsConnection(insightsData);
await serverCommand.addInsightsConnection(insightsData, ["lblTest"]);
sinon.assert.calledOnce(updateInsightsStub);
windowMock
.expects("showInformationMessage")
Expand Down Expand Up @@ -1035,7 +1035,7 @@ describe("serverCommand", () => {

it("should add new Kdb connection", async () => {
getServersStub.returns({});
await serverCommand.addKdbConnection(kdbData);
await serverCommand.addKdbConnection(kdbData, false, ["lblTest"]);
sinon.assert.calledOnce(updateServersStub);
windowMock
.expects("showInformationMessage")
Expand Down
19 changes: 7 additions & 12 deletions test/suite/webview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,7 @@ describe("KdbNewConnectionView", () => {
});

it("should render port number desc for KDB server", () => {
view.isBundledQ = false;
const result = view.renderPortNumberDesc(ServerType.KDB);
const result = view.renderPortNumberDesc();
assert.strictEqual(
JSON.stringify(result).includes("<b>Set port number</b>"),
true,
Expand All @@ -393,8 +392,7 @@ describe("KdbNewConnectionView", () => {
});

it("should render port number for KDB server", () => {
view.isBundledQ = false;
const result = view.renderPortNumber(ServerType.KDB);
const result = view.renderPortNumber();
assert.strictEqual(
JSON.stringify(result).includes("<b>Set port number</b>"),
true,
Expand All @@ -421,9 +419,10 @@ describe("KdbNewConnectionView", () => {
});

it("should render connection address for Bundled q", () => {
const result = view.renderConnAddDesc(ServerType.KDB);
const result = view.renderConnAddDesc(ServerType.KDB, true);
console.log(JSON.stringify(result));
assert.strictEqual(
result.strings[0].includes("lready set up for you"),
result.strings[0].includes("already set up for you"),
true,
);
});
Expand All @@ -439,9 +438,8 @@ describe("KdbNewConnectionView", () => {
);
});

it("should render connection address for bundled q", () => {
view.isBundledQ = true;
const result = view.renderConnAddress(ServerType.KDB);
it("should render connection address for Bundled q", () => {
const result = view.renderConnAddress(ServerType.KDB, true);
assert.strictEqual(
JSON.stringify(result).includes("127.0.0.1 or localhost"),
false,
Expand Down Expand Up @@ -691,7 +689,6 @@ describe("KdbNewConnectionView", () => {
serverName: "local",
};
const result = view.renderBundleQEditForm();
console.log(JSON.stringify(result));
assert.ok(result.strings[0].includes('<div class="col gap-0">'));
assert.ok(result.strings[1].includes('<div class="col gap-0">'));
assert.ok(result.strings[2].includes('<div class="col gap-0">'));
Expand All @@ -716,7 +713,6 @@ describe("KdbNewConnectionView", () => {
serverName: "local",
};
const result = view.renderMyQEditForm();
console.log(JSON.stringify(result));
assert.ok(result.strings[0].includes('<div class="col gap-0">'));
assert.ok(result.strings[1].includes('<div class="col gap-0">'));
assert.ok(result.strings[2].includes('<div class="col gap-0">'));
Expand All @@ -741,7 +737,6 @@ describe("KdbNewConnectionView", () => {
serverName: "local",
};
const result = view.renderInsightsEditForm();
console.log(JSON.stringify(result));
assert.ok(result.strings[0].includes('<div class="col gap-0">'));
assert.ok(result.strings[1].includes('<div class="col gap-0">'));
assert.ok(result.strings[2].includes('<div class="col gap-0">'));
Expand Down