Skip to content

Commit

Permalink
Merge branch 'dev' into test-semantic-release
Browse files Browse the repository at this point in the history
  • Loading branch information
nfarrell-kx committed Jun 18, 2024
2 parents f0a4060 + ae82a48 commit 763c257
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
96 changes: 96 additions & 0 deletions src/classes/insightsConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ import {
kdbOutputLog,
tokenUndefinedError,
} from "../utils/core";
import { InsightsConfig, InsightsEndpoints } from "../models/config";

export class InsightsConnection {
public connected: boolean;
public connLabel: string;
public node: InsightsNode;
public meta?: MetaObject;
public config?: InsightsConfig;
public insightsVersion?: string;
public connEndpoints?: InsightsEndpoints;

constructor(connLabel: string, node: InsightsNode) {
this.connected = false;
Expand All @@ -52,6 +56,7 @@ export class InsightsConnection {
).then(async (token) => {
this.connected = token ? true : false;
if (token) {
await this.getConfig();
await this.getMeta();
}
});
Expand Down Expand Up @@ -98,6 +103,97 @@ export class InsightsConnection {
return undefined;
}

public async getConfig() {
if (this.connected) {
const configUrl = new url.URL(
ext.insightsAuthUrls.configURL,
this.node.details.server,
);
const token = await getCurrentToken(
this.node.details.server,
this.node.details.alias,
this.node.details.realm || "insights",
);

if (token === undefined) {
tokenUndefinedError(this.connLabel);
return undefined;
}

const options = {
headers: { Authorization: `Bearer ${token.accessToken}` },
};

const configResponse = await axios.get(configUrl.toString(), options);
this.config = configResponse.data;
this.getInsightsVersion();
this.defineEndpoints();
}
}

public getInsightsVersion() {
const match = this.config?.version.match(/-\d+(\.\d+){2}(-|$)/);
const version = match ? match[0].replace(/-/g, "") : null;
if (version) {
const [major, minor, _path] = version.split(".");
this.insightsVersion = `${major}.${minor}`;
}
}

public defineEndpoints() {
if (this.insightsVersion) {
switch (this.insightsVersion) {
case "1.11":
this.connEndpoints = {
scratchpad: {
scratchpad: "scratchpad-manager/api/v1/execute/display",
import: "scratchpad-manager/api/v1/execute/import/data",
importSql: "scratchpad-manager/api/v1/execute/import/sql",
importQsql: "scratchpad-manager/api/v1/execute/import/qsql",
reset: "scratchpad-manager/api/v1/execute/reset",
},
serviceGateway: {
meta: "servicegateway/meta",
data: "servicegateway/data",
sql: "servicegateway/qe/sql",
qsql: "servicegateway/qe/qsql",
},
};
break;
default:
this.connEndpoints = {
scratchpad: {
scratchpad: "servicebroker/scratchpad/display",
import: "servicebroker/scratchpad/import/data",
importSql: "servicebroker/scratchpad/import/sql",
importQsql: "servicebroker/scratchpad/import/qsql",
reset: "servicebroker/scratchpad/reset",
},
serviceGateway: {
meta: "servicegateway/meta",
data: "servicegateway/data",
sql: "servicegateway/qe/sql",
qsql: "servicegateway/qe/qsql",
},
};
break;
}
}
}

public retrieveEndpoints(
parentKey: "scratchpad" | "serviceGateway",
childKey: string,
): string | undefined {
if (this.connEndpoints) {
const parent = this.connEndpoints[parentKey];
if (parent) {
return parent[childKey as keyof typeof parent];
}
return undefined;
}
}

public async getDataInsights(
targetUrl: string,
body: string,
Expand Down
1 change: 1 addition & 0 deletions src/extensionVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export namespace ext {
revoke: "auth/realms/insights/protocol/openid-connect/revoke",
tokenURL: "auth/realms/insights/protocol/openid-connect/token",
scratchpadURL: "servicebroker/scratchpad/display",
configURL: "kxicontroller/config",
};

export const insightsScratchpadUrls = {
Expand Down
37 changes: 37 additions & 0 deletions src/models/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 1998-2023 Kx Systems Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

export type InsightsConfig = {
description?: string;
encryptionInFlight?: boolean;
installSize?: any;
restricted?: boolean;
storage?: any;
version: string;
};

export type InsightsEndpoints = {
scratchpad: {
scratchpad: string;
import: string;
importSql: string;
importQsql: string;
reset: string;
};
serviceGateway: {
meta: string;
data: string;
sql: string;
qsql: string;
};
};
12 changes: 10 additions & 2 deletions src/services/connectionManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export class ConnectionManagementService {
}
if (conn) {
kdbOutputLog(
`[CONNECTION] Connection established successfully to: ${connLabel}`,
"INFO",
`Connection established successfully to: ${connLabel}`,
"CONNECTION",
);

Telemetry.sendEvent("Connection.Connected.QProcess");
Expand All @@ -115,6 +115,14 @@ export class ConnectionManagementService {
await insightsConn.connect();
if (insightsConn.connected) {
Telemetry.sendEvent("Connection.Connected.Insights");
kdbOutputLog(
`Connection established successfully to: ${connLabel}`,
"CONNECTION",
);
kdbOutputLog(
`${connLabel} connection insights version: ${insightsConn.insightsVersion}`,
"CONNECTION",
);
ext.connectedConnectionList.push(insightsConn);
this.isConnectedBehaviour(connection);
} else {
Expand Down

0 comments on commit 763c257

Please sign in to comment.