Skip to content

Commit

Permalink
Merge pull request #192 from KxSystems/KXI-29938
Browse files Browse the repository at this point in the history
KXI-29938 - Scratchpad - support execution of Python code from vscode
  • Loading branch information
Philip-Carneiro-KX authored Nov 30, 2023
2 parents 1219c84 + ceae583 commit 4662ffb
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 69 deletions.
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@
"command": "kdb.execute.fileQuery",
"title": "Execute Entire File",
"when": "kdb.connected"
},
{
"command": "kdb.execute.pythonScratchpadQuery",
"title": "Execute Current Selection in Insights Scratchpad",
"when": "kdb.connected"
},
{
"command": "kdb.execute.pythonFileScratchpadQuery",
"title": "Execute Entire File in Insights Scratchpad",
"when": "kdb.connected"
}
],
"keybindings": [
Expand All @@ -309,6 +319,18 @@
"mac": "cmd+shift+d",
"when": "editorLangId == q && kdb.connected"
},
{
"command": "kdb.execute.pythonScratchpadQuery",
"key": "ctrl+d",
"mac": "cmd+d",
"when": "editorLangId == python && kdb.insightsConnected"
},
{
"command": "kdb.execute.pythonFileScratchpadQuery",
"key": "ctrl+shift+d",
"mac": "cmd+shift+d",
"when": "editorLangId == python && kdb.insightsConnected"
},
{
"command": "kdb.terminal.run",
"key": "ctrl+shift+r",
Expand Down Expand Up @@ -565,6 +587,11 @@
"command": "kdb.execute.fileQuery",
"when": "resourceLangId == q && kdb.connected",
"group": "navigation"
},
{
"command": "kdb.execute.pythonFileScratchpadQuery",
"group": "navigation",
"when": "editorLangId == python && kdb.insightsConnected"
}
],
"editor/context": [
Expand All @@ -582,6 +609,16 @@
"command": "kdb.execute.fileQuery",
"group": "q",
"when": "editorLangId == q && kdb.connected"
},
{
"command": "kdb.execute.pythonScratchpadQuery",
"group": "q",
"when": "editorLangId == python && kdb.insightsConnected"
},
{
"command": "kdb.execute.pythonFileScratchpadQuery",
"group": "q",
"when": "editorLangId == python && kdb.insightsConnected"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ sonar.qualitygate.wait=true
sonar.sources=src,server
sonar.tests=test
sonar.javascript.lcov.reportPaths=lcov.info
sonar.coverage.exclusions=server/src/utils/antlrGrammars/**,server/src/utils/parserUtils.ts,src/ipc/**,src/models/**
sonar.coverage.exclusions=server/src/utils/antlrGrammars/**,server/src/utils/parserUtils.ts,src/ipc/**,src/models/**,src/extension.ts
6 changes: 5 additions & 1 deletion src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ export async function runDataSource(
}

if (ext.resultsViewProvider.isVisible()) {
writeQueryResultsToView(res, selectedType);
writeQueryResultsToView(
res,
getQuery(fileContent, selectedType),
selectedType
);
} else {
writeQueryResultsToConsole(
res,
Expand Down
105 changes: 68 additions & 37 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { JwtUser } from "../models/jwt_user";
import { MetaObject, MetaObjectPayload } from "../models/meta";
import { queryConstants } from "../models/queryResult";
import { ScratchpadResult } from "../models/scratchpadResult";
import { Server } from "../models/server";
import { Server, ServerType } from "../models/server";
import { ServerObject } from "../models/serverObject";
import { DataSourcesPanel } from "../panels/datasource";
import { getCurrentToken } from "../services/kdbInsights/codeFlowLogin";
Expand All @@ -70,7 +70,7 @@ import {
} from "../utils/core";
import { refreshDataSourcesPanel } from "../utils/dataSource";
import { decodeQUTF } from "../utils/decode";
import { ExecutionConsole } from "../utils/executionConsole";
import { ExecutionConsole, addQueryHistory } from "../utils/executionConsole";
import { openUrl } from "../utils/openUrl";
import {
handleScratchpadTableRes,
Expand Down Expand Up @@ -586,7 +586,8 @@ export async function importScratchpad(

export async function getScratchpadQuery(
query: string,
context?: string
context?: string,
isPython?: boolean
): Promise<any | undefined> {
if (ext.connectionNode instanceof InsightsNode) {
const isTableView = ext.resultsViewProvider.isVisible();
Expand Down Expand Up @@ -614,7 +615,7 @@ export async function getScratchpadQuery(
const body = {
expression: query,
isTableView,
language: "q",
language: !isPython ? "q" : "python",
context: context || ".",
sampleFn: "first",
sampleSize: 10000,
Expand Down Expand Up @@ -789,7 +790,8 @@ export async function disconnect(): Promise<void> {

export async function executeQuery(
query: string,
context?: string
context?: string,
isPython?: boolean
): Promise<void> {
const queryConsole = ExecutionConsole.start();

Expand All @@ -806,8 +808,8 @@ export async function executeQuery(
// set context for root nodes
if (insightsNode) {
query = sanitizeQuery(query);
const queryRes = await getScratchpadQuery(query, context);
writeScratchpadResult(queryRes, query);
const queryRes = await getScratchpadQuery(query, context, isPython);
writeScratchpadResult(queryRes, query, isPython);
} else if (ext.connection !== undefined && ext.connection.connected) {
query = sanitizeQuery(query);

Expand Down Expand Up @@ -869,31 +871,43 @@ export function getQueryContext(lineNum?: number): string {

export function runQuery(type: ExecutionTypes, rerunQuery?: string) {
const editor = window.activeTextEditor;
if (editor) {
let context;
let query;
switch (type) {
case ExecutionTypes.QuerySelection:
query = editor?.document.getText(
new Range(editor.selection.start, editor.selection.end)
);
if (!editor) {
return false;
}
let context;
let query;
let isPython = false;
const insightsNode = ext.kdbinsightsNodes.find((n) =>
ext.connectionNode instanceof InsightsNode
? n === ext.connectionNode?.details.alias + " (connected)"
: false
);
switch (type) {
case ExecutionTypes.QuerySelection:
case ExecutionTypes.PythonQuerySelection:
const selection = editor.selection;
query = selection.isEmpty
? editor.document.lineAt(selection.active.line).text
: editor.document.getText(selection);
context = getQueryContext(selection.end.line);
if (type === ExecutionTypes.PythonQuerySelection && insightsNode) {
isPython = true;
}
break;

if (query === "") {
const docLine = editor.selection.active.line;
query = editor.document.lineAt(docLine).text;
context = getQueryContext(docLine);
} else {
context = getQueryContext(editor.selection.end.line);
}
break;
case ExecutionTypes.QueryFile:
case ExecutionTypes.ReRunQuery:
default:
query = rerunQuery ? rerunQuery : editor.document.getText();
context = getQueryContext();
}
executeQuery(query, context);
case ExecutionTypes.QueryFile:
case ExecutionTypes.ReRunQuery:
case ExecutionTypes.PythonQueryFile:
default:
query = rerunQuery || editor.document.getText();
context = getQueryContext();

if (type === ExecutionTypes.PythonQueryFile && insightsNode) {
isPython = true;
}
break;
}
executeQuery(query, context, isPython);
}

export async function loadServerObjects(): Promise<ServerObject[]> {
Expand Down Expand Up @@ -924,7 +938,8 @@ export async function loadServerObjects(): Promise<ServerObject[]> {
export function writeQueryResultsToConsole(
result: string | string[],
query: string,
dataSourceType?: string
dataSourceType?: string,
isPython?: boolean
): void {
const queryConsole = ExecutionConsole.start();
const res = Array.isArray(result)
Expand All @@ -938,28 +953,44 @@ export function writeQueryResultsToConsole(
res,
query,
ext.connectionNode?.label ? ext.connectionNode.label : "",
dataSourceType
dataSourceType,
isPython
);
} else {
queryConsole.appendQueryError(
query,
res.substring(queryConstants.error.length),
!!ext.connection,
ext.connectionNode?.label ? ext.connectionNode.label : ""
ext.connectionNode?.label ? ext.connectionNode.label : "",
isPython
);
}
}

export function writeQueryResultsToView(
result: any,
dataSourceType?: string
query: string,
dataSourceType?: string,
isPython?: boolean
): void {
commands.executeCommand("kdb.resultsPanel.update", result, dataSourceType);
const connectionType: ServerType =
ext.connectionNode instanceof KdbNode
? ServerType.KDB
: ServerType.INSIGHTS;
addQueryHistory(
query,
ext.connectionNode?.label ? ext.connectionNode.label : "",
connectionType,
true,
isPython
);
}

export function writeScratchpadResult(
result: ScratchpadResult,
query: string
query: string,
isPython?: boolean
): void {
const queryConsole = ExecutionConsole.start();

Expand All @@ -972,9 +1003,9 @@ export function writeScratchpadResult(
);
} else {
if (ext.resultsViewProvider.isVisible()) {
writeQueryResultsToView(result.data, "SCRATCHPAD");
writeQueryResultsToView(result.data, query, "SCRATCHPAD", isPython);
} else {
writeQueryResultsToConsole(result.data, query);
writeQueryResultsToConsole(result.data, query, undefined, isPython);
}
}
}
13 changes: 12 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,18 @@ export async function activate(context: ExtensionContext) {
commands.registerCommand("kdb.execute.fileQuery", async () => {
runQuery(ExecutionTypes.QueryFile);
ext.connection?.update();
})
}),
commands.registerCommand("kdb.execute.pythonScratchpadQuery", async () => {
runQuery(ExecutionTypes.PythonQuerySelection);
ext.connection?.update();
}),
commands.registerCommand(
"kdb.execute.pythonFileScratchpadQuery",
async () => {
runQuery(ExecutionTypes.PythonQueryFile);
ext.connection?.update();
}
)
);

const lastResult: QueryResult | undefined = undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/models/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export enum ExecutionTypes {
QuerySelection,
QueryFile,
ReRunQuery,
PythonQuerySelection,
PythonQueryFile,
}
1 change: 1 addition & 0 deletions src/models/queryHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface QueryHistory {
query: string;
time: string;
success: boolean;
language?: string;
}
22 changes: 16 additions & 6 deletions src/utils/executionConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class ExecutionConsole {
output: string | string[],
query = "",
serverName: string,
dataSourceType?: string
dataSourceType?: string,
isPhython?: boolean
): void {
getHideDetailedConsoleQueryOutput();
const hideDetails = ext.hideDetailedConsoleQueryOutput;
Expand All @@ -92,7 +93,7 @@ export class ExecutionConsole {
ext.connectionNode instanceof KdbNode
? ServerType.KDB
: ServerType.INSIGHTS;
addQueryHistory(query, serverName, connectionType, true);
addQueryHistory(query, serverName, connectionType, true, isPhython);

//TODO: this._console.clear(); Add an option in the future to clear or not the console
const date = new Date();
Expand Down Expand Up @@ -120,7 +121,8 @@ export class ExecutionConsole {
query: string,
result: string,
isConnected: boolean,
serverName: string
serverName: string,
isPython?: boolean
): void {
getHideDetailedConsoleQueryOutput();
const hideDetails = ext.hideDetailedConsoleQueryOutput;
Expand All @@ -143,12 +145,18 @@ export class ExecutionConsole {
} else {
this._console.appendLine(`Error: ${result}`);
}
addQueryHistory(query, serverName, connectionType, false);
addQueryHistory(query, serverName, connectionType, false, isPython);
} else {
window.showErrorMessage(`Please connect to a kdb+ server`);
this._console.appendLine(`Please connect to a kdb+ server`);
commands.executeCommand("kdb.disconnect");
addQueryHistory(query, "No connection", ServerType.undefined, false);
addQueryHistory(
query,
"No connection",
ServerType.undefined,
false,
isPython
);
}
if (!hideDetails) {
this._console.appendLine(`<<< >>>`);
Expand All @@ -165,14 +173,16 @@ export function addQueryHistory(
query: string,
connectionName: string,
connectionType: ServerType,
success: boolean
success: boolean,
isPython?: boolean
) {
const newQueryHistory: QueryHistory = {
query: query,
time: new Date().toLocaleString(),
success,
connectionName,
connectionType,
language: isPython ? "python" : "q",
};

ext.kdbQueryHistoryList.unshift(newQueryHistory);
Expand Down
Loading

0 comments on commit 4662ffb

Please sign in to comment.