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

open meta json from node tree #354

Merged
merged 5 commits into from
Jun 12, 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
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@
"command": "kdb.active.connection",
"title": "Active connection"
},
{
"category": "KX",
"command": "kdb.open.meta",
"title": "Open meta object"
},
{
"category": "KX",
"command": "kdb.addAuthentication",
Expand Down Expand Up @@ -564,6 +569,10 @@
"command": "kdb.connect",
"when": "false"
},
{
"command": "kdb.open.meta",
"when": "false"
},
{
"command": "kdb.connect.via.dialog",
"when": "false"
Expand Down
40 changes: 39 additions & 1 deletion src/classes/insightsConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ext } from "../extensionVariables";
import axios, { AxiosRequestConfig } from "axios";
import { ProgressLocation, window } from "vscode";
import * as url from "url";
import { MetaObject, MetaObjectPayload } from "../models/meta";
import { MetaInfoType, MetaObject, MetaObjectPayload } from "../models/meta";
import { getCurrentToken } from "../services/kdbInsights/codeFlowLogin";
import { InsightsNode } from "../services/kdbTreeProvider";
import { GetDataObjectPayload } from "../models/data";
Expand Down Expand Up @@ -424,4 +424,42 @@ export class InsightsConnection {
return false;
}
}

public returnMetaObject(metaType: MetaInfoType): string {
if (!this.meta) {
kdbOutputLog(
`Meta data is undefined for connection ${this.connLabel}`,
"ERROR",
);
return "";
}

let objectToReturn;

switch (metaType) {
case MetaInfoType.META:
objectToReturn = this.meta.payload;
break;
case MetaInfoType.SCHEMA:
objectToReturn = this.meta.payload.schema;
break;
case MetaInfoType.API:
objectToReturn = this.meta.payload.api;
break;
case MetaInfoType.AGG:
objectToReturn = this.meta.payload.agg;
break;
case MetaInfoType.DAP:
objectToReturn = this.meta.payload.dap;
break;
case MetaInfoType.RC:
objectToReturn = this.meta.payload.rc;
break;
default:
kdbOutputLog(`Invalid meta type: ${metaType}`, "ERROR");
return "";
}

return JSON.stringify(objectToReturn);
}
}
37 changes: 35 additions & 2 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
import { readFileSync } from "fs-extra";
import { join } from "path";
import * as url from "url";
import { Position, Range, commands, window } from "vscode";
import {
Position,
Range,
Uri,
ViewColumn,
commands,
window,
workspace,
} from "vscode";
import { ext } from "../extensionVariables";
import { DataSourceFiles } from "../models/dataSource";
import { ExecutionTypes } from "../models/execution";
Expand All @@ -24,7 +32,12 @@ import { ScratchpadResult } from "../models/scratchpadResult";
import { Server, ServerDetails, ServerType } from "../models/server";
import { ServerObject } from "../models/serverObject";
import { DataSourcesPanel } from "../panels/datasource";
import { InsightsNode, KdbNode } from "../services/kdbTreeProvider";
import {
InsightsMetaNode,
InsightsNode,
KdbNode,
MetaObjectPayloadNode,
} from "../services/kdbTreeProvider";
import {
addLocalConnectionContexts,
checkOpenSslInstalled,
Expand Down Expand Up @@ -53,6 +66,7 @@ import { NewConnectionPannel } from "../panels/newConnection";
import { Telemetry } from "../utils/telemetryClient";
import { ConnectionManagementService } from "../services/connectionManagerService";
import { InsightsConnection } from "../classes/insightsConnection";
import { MetaContentProvider } from "../services/metaContentProvider";

export async function addNewConnection(): Promise<void> {
NewConnectionPannel.render(ext.context.extensionUri);
Expand Down Expand Up @@ -574,6 +588,25 @@ export async function loadServerObjects(): Promise<ServerObject[]> {
}
}

export async function openMeta(node: MetaObjectPayloadNode | InsightsMetaNode) {
const metaContentProvider = new MetaContentProvider();
workspace.registerTextDocumentContentProvider("meta", metaContentProvider);
const connMngService = new ConnectionManagementService();
const doc = connMngService.retrieveMetaContent(node.connLabel, node.label);
if (doc && doc !== "") {
const formattedDoc = JSON.stringify(JSON.parse(doc), null, 2);
const uri = Uri.parse(`meta:${node.connLabel} - ${node.label}.json`);
metaContentProvider.update(uri, formattedDoc);
const document = await workspace.openTextDocument(uri);
await window.showTextDocument(document, {
preview: false,
viewColumn: ViewColumn.One,
});
} else {
kdbOutputLog("[META] Meta content not found", "ERROR");
}
}

export function writeQueryResultsToConsole(
result: string | string[],
query: string,
Expand Down
9 changes: 9 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
connect,
disconnect,
enableTLS,
openMeta,
refreshGetMeta,
removeConnection,
rerunQuery,
Expand All @@ -58,9 +59,11 @@ import { InsightDetails, Insights } from "./models/insights";
import { QueryResult } from "./models/queryResult";
import { Server, ServerDetails } from "./models/server";
import {
InsightsMetaNode,
InsightsNode,
KdbNode,
KdbTreeProvider,
MetaObjectPayloadNode,
} from "./services/kdbTreeProvider";
import {
QueryHistoryProvider,
Expand Down Expand Up @@ -223,6 +226,12 @@ export async function activate(context: ExtensionContext) {
await disconnect(connLabel);
},
),
commands.registerCommand(
"kdb.open.meta",
async (viewItem: InsightsMetaNode | MetaObjectPayloadNode) => {
await openMeta(viewItem);
},
),
commands.registerCommand("kdb.addConnection", async () => {
await addNewConnection();
}),
Expand Down
9 changes: 9 additions & 0 deletions src/models/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
* specific language governing permissions and limitations under the License.
*/

export enum MetaInfoType {
META = "meta",
SCHEMA = "schema",
API = "api",
AGG = "agg",
DAP = "dap",
RC = "rc",
}

export type MetaRC = {
rc: string;
labels: {
Expand Down
36 changes: 36 additions & 0 deletions src/services/connectionManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import { Insights } from "../models/insights";
import { Server } from "../models/server";
import { refreshDataSourcesPanel } from "../utils/dataSource";
import { MetaInfoType } from "../models/meta";

export class ConnectionManagementService {
public retrieveConnection(
Expand Down Expand Up @@ -340,4 +341,39 @@ export class ConnectionManagementService {
await connection.getMeta();
}
}

public getMetaInfoType(value: string): MetaInfoType | undefined {
return MetaInfoType[value as keyof typeof MetaInfoType];
}

public retrieveMetaContent(
connLabel: string,
metaTypeString: string,
): string {
const metaType = this.getMetaInfoType(metaTypeString.toUpperCase());
if (!metaType) {
kdbOutputLog(
"[META] The meta info type that you try to open is not valid",
"ERROR",
);
return "";
}
const connection = this.retrieveConnectedConnection(connLabel);
if (!connection) {
kdbOutputLog(
"[META] The connection that you try to open meta info is not connected",
"ERROR",
);
return "";
}
if (connection instanceof LocalConnection) {
kdbOutputLog(
"[META] The connection that you try to open meta info is not an Insights connection",
"ERROR",
);
return "";
}

return connection.returnMetaObject(metaType);
}
}
10 changes: 10 additions & 0 deletions src/services/kdbTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ export class KdbTreeProvider implements TreeDataProvider<TreeItem> {
) {
ext.isBundleQCreated = true;
}
if (
element instanceof InsightsMetaNode ||
element instanceof MetaObjectPayloadNode
) {
element.command = {
command: "kdb.open.meta",
title: "Open Meta Object",
arguments: [element],
};
}
return element;
}

Expand Down
30 changes: 30 additions & 0 deletions src/services/metaContentProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

import * as vscode from "vscode";

export class MetaContentProvider implements vscode.TextDocumentContentProvider {
private _onDidChange = new vscode.EventEmitter<vscode.Uri>();
public readonly onDidChange = this._onDidChange.event;

private content: string = "";

public update(uri: vscode.Uri, content: string) {
this.content = content;
this._onDidChange.fire(uri);
}

provideTextDocumentContent(uri: vscode.Uri): string {
return this.content;
}
}
Loading
Loading