Skip to content

Commit

Permalink
Merge pull request #533 from KxSystems/KXI-58282
Browse files Browse the repository at this point in the history
Kxi 58282 - Run UDAs with optional parameters not defined in getMeta
  • Loading branch information
Philip-Carneiro-KX authored Mar 6, 2025
2 parents 0acb809 + 99b2e7f commit a51c8ff
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 32 deletions.
6 changes: 4 additions & 2 deletions src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ export async function runDataSource(
`[DATASOURCE] Results is a string with length: ${res.length}`,
"INFO",
);
} else if (res.errorMsg) {
res = res.errorMsg;
} else if (res.error) {
res = res.errorMsg ? res.errorMsg : res.error;
}

await writeQueryResultsToConsole(
Expand Down Expand Up @@ -510,6 +510,8 @@ export function getQuery(
return `GetData - table: ${fileContent.dataSource.api.table}`;
case "QSQL":
return fileContent.dataSource.qsql.query;
case "UDA":
return `Executed UDA: ${fileContent.dataSource.uda?.name}`;
case "SQL":
default:
return fileContent.dataSource.sql.query;
Expand Down
1 change: 1 addition & 0 deletions src/models/uda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface UDAParam {
selectedMultiTypeString?: string;
value?: any;
isVisible?: boolean;
isDistinguised?: boolean;
}

export interface UDAReturn {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/queryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export function checkIfIsDatasource(
if (dataSourceType === undefined) {
return false;
}
const validTypes = ["API", "QSQL", "SQL"];
const validTypes = ["API", "QSQL", "SQL", "UDA"];
return validTypes.includes(dataSourceType);
}

Expand Down
165 changes: 137 additions & 28 deletions src/webview/components/kdbDataSourceView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,74 @@ import {
import { MetaObjectPayload } from "../../models/meta";
import { DataSourceCommand, DataSourceMessage2 } from "../../models/messages";
import { dataSourceStyles, kdbStyles, shoelaceStyles } from "./styles";
import { UDA, UDAParam } from "../../models/uda";
import { ParamFieldType, UDA, UDAParam } from "../../models/uda";

const MAX_RULES = 32;
const UDA_DISTINGUISED_PARAMS: UDAParam[] = [
{
name: "table",
description: "Table to target.",
isReq: false,
type: [-11],
isVisible: false,
fieldType: ParamFieldType.Text,
isDistinguised: true,
},
{
name: "labels",
description: "A dictionary describing DAP labels to target,",
isReq: false,
type: [99],
isVisible: false,
fieldType: ParamFieldType.JSON,
isDistinguised: true,
},
{
name: "scope",
description: "A dictionary describing what RC and/or DAPs to target.",
isReq: false,
type: [99],
fieldType: ParamFieldType.JSON,
isDistinguised: true,
},
{
name: "startTS",
description: "Inclusive start time of the request.",
isReq: false,
type: [-19],
isVisible: false,
fieldType: ParamFieldType.Timestamp,
isDistinguised: true,
},
{
name: "endTS",
description: "Exclusive end time of the request.",
isReq: false,
type: [-19],
isVisible: false,
fieldType: ParamFieldType.Timestamp,
isDistinguised: true,
},
{
name: "inputTZ",
description: "Timezone of startTS and endTS (default: UTC).",
isReq: false,
type: [-11],
isVisible: false,
fieldType: ParamFieldType.Text,
isDistinguised: true,
},
{
name: "outputTZ",
description:
"Timezone of the final result (.kxi.getData only). No effect on routing.",
isReq: false,
type: [-11],
isVisible: false,
fieldType: ParamFieldType.Text,
isDistinguised: true,
},
];

@customElement("kdb-data-source-view")
export class KdbDataSourceView extends LitElement {
Expand Down Expand Up @@ -954,7 +1019,6 @@ export class KdbDataSourceView extends LitElement {
${this.renderUDAOptions()}
</sl-select>
${this.renderUDADetails()} ${this.renderUDAParams()}
${this.userSelectedUDA ? this.renderUDAAddParamButton() : null}
</div>
`;
}
Expand Down Expand Up @@ -1015,26 +1079,36 @@ export class KdbDataSourceView extends LitElement {
const visibleParams = this.renderVisibleUDAParams();
const noParams = this.renderUDANoParams();
const invalidParams = this.renderUDAInvalidParams();
const addParamsBtn = this.renderUDAAddParamButton();

if (invalidParams) {
params.push(invalidParams);
} else {
params.push(...(visibleParams.length ? visibleParams : [noParams]));
params.push(
...(visibleParams.length ? visibleParams : [noParams]),
...[addParamsBtn],
);
}

return params;
}

renderUDAAddParamButton() {
return html`
<sl-dropdown
class="udaDropdown"
@sl-select="${this.handleUDAAddParamSelect}">
<sl-button slot="trigger" variant="neutral" class="width-200-px" caret>
+ Add Parameter
</sl-button>
${this.renderUDAAddParamBtnOptions()}
</sl-dropdown>
<div class="width-98-pct">
<sl-dropdown
class="udaDropdown width-30-pct"
@sl-select="${this.handleUDAAddParamSelect}">
<sl-button
slot="trigger"
class="width-100-pct"
variant="neutral"
caret>
+ Add Parameter
</sl-button>
${this.renderUDAAddParamBtnOptions()}
</sl-dropdown>
</div>
`;
}

Expand All @@ -1051,7 +1125,7 @@ export class KdbDataSourceView extends LitElement {

renderUDAAddParamBtnOptions() {
return html`
<sl-menu class="width-200-px">
<sl-menu class="width-100-pct">
${this.renderUDAOptionalParamsOpts()}
</sl-menu>
`;
Expand All @@ -1064,26 +1138,61 @@ export class KdbDataSourceView extends LitElement {
`;
}

const optParamTxtHtml = html`
<small class="btn-opt-text"><strong>OPTIONAL PARAMETERS:</strong></small>
`;
const distParamTxtHtml = html`
<small class="btn-opt-text"
><strong>DISTINGUISHED PARAMETERS:</strong></small
>
`;

const optionalParams = this.userSelectedUDA.params.filter(
(param) => !param.isReq,
);
const filteredDistinguisedParam = UDA_DISTINGUISED_PARAMS.filter(
(param) =>
!optionalParams.some(
(optionalParam) => param.name === optionalParam.name,
),
);

if (optionalParams.length === 0) {
return html`
<sl-menu-item disabled>No optional parameters available</sl-menu-item>
`;
const renderParams = (params: any[]) =>
params.map(
(param: {
isVisible: unknown;
name: unknown;
description: unknown;
}) => html`
<sl-menu-item
.type=${param.isVisible ? "checkbox" : "normal"}
.disabled=${!!param.isVisible}
.value=${param.name as string}>
${param.name}<br /><small>${param.description}</small>
</sl-menu-item>
`,
);

const optionalParamsHtml = [
optParamTxtHtml,
...(optionalParams.length
? renderParams(optionalParams)
: [
html`<sl-menu-item disabled
>No optional parameters available</sl-menu-item
>`,
]),
];

if (filteredDistinguisedParam.length > 0) {
optionalParamsHtml.push(
html`<hr class="btn-opt-divider" />`,
distParamTxtHtml,
...renderParams(filteredDistinguisedParam),
);
}

return optionalParams.map(
(param) => html`
<sl-menu-item
.type=${param.isVisible ? "checkbox" : "normal"}
.disabled=${param.isVisible === true}
.value=${param.name}>
${param.name}<br /><small>${param.description}</small>
</sl-menu-item>
`,
);
return optionalParamsHtml;
}

renderDeleteUDAParamButton(param: UDAParam) {
Expand Down Expand Up @@ -1115,7 +1224,7 @@ export class KdbDataSourceView extends LitElement {
multitype: "multitype",
text: "text",
};
return inputTypes[type || "text"] || "text";
return inputTypes[type ?? "text"] || "text";
}

renderVisibleUDAParams() {
Expand Down Expand Up @@ -1200,7 +1309,7 @@ export class KdbDataSourceView extends LitElement {
<sl-select
class="reset-widths-limit width-30-pct"
label="${param.name}"
.helpText="Select a type"
help-text="Select a type"
.value="${live(value)}"
@sl-change="${(event: Event) => {
param.selectedMultiTypeString = (
Expand Down
12 changes: 12 additions & 0 deletions src/webview/components/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ export const dataSourceStyles = css`
margin-top: 28px;
}
.btn-opt-text {
margin-left: 1rem;
}
.btn-opt-divider {
border: solid var(--sl-panel-border-width) var(--vscode-focusBorder) !important;
}
sl-menu {
border: solid var(--sl-panel-border-width) var(--vscode-focusBorder) !important;
}
Expand Down Expand Up @@ -325,6 +333,10 @@ export const kdbStyles = css`
width: 97%;
}
.width-98-pct {
width: 98%;
}
.width-100-pct {
width: 100%;
}
Expand Down
1 change: 0 additions & 1 deletion test/suite/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,6 @@ describe("queryHistoryProvider", () => {
TreeItemCollapsibleState.None,
);
const result = queryHistoryTreeItem.defineQueryIcon(true);
console.log(JSON.stringify(queryHistoryTreeItem));
assert.strictEqual(
result,
sucessIcon,
Expand Down
88 changes: 88 additions & 0 deletions test/suite/webview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,94 @@ describe("KdbDataSourceView", () => {
assert.ok(!result);
});
});

describe("renderUDAOptionalParamsOpts", () => {
it("should return 'No optional parameters available' if userSelectedUDA is not set", () => {
view.userSelectedUDA = undefined;
const result = view.renderUDAOptionalParamsOpts();
assert.strictEqual(Array.isArray(result) ? result.length : 0, 0);
const resultString = Array.isArray(result)
? result.map((item) => item.strings.join("")).join("")
: result.strings.join("");
assert.ok(resultString.includes("No optional parameters available"));
});

it("should return 'No optional parameters available' if there are no optional parameters", () => {
view.userSelectedUDA = {
name: "test",
description: "test description",
params: [
{
name: "param",
type: 10,
description: "param description",
isReq: true,
},
],
return: {
type: ["99"],
description: "test return description",
},
};
const result = view.renderUDAOptionalParamsOpts();
assert.strictEqual(Array.isArray(result) ? result.length : 0, 11);
const resultString = Array.isArray(result)
? result.map((item) => item.strings.join("")).join("")
: result.strings.join("");
assert.ok(resultString.includes("No optional parameters available"));
});

it("should render optional parameters if they exist", () => {
view.userSelectedUDA = {
name: "test",
description: "test description",
params: [
{
name: "optionalParam",
type: 10,
description: "optional param description",
isReq: false,
},
],
return: {
type: ["99"],
description: "test return description",
},
};
const result = view.renderUDAOptionalParamsOpts();
assert.strictEqual(Array.isArray(result) ? result.length : 0, 11);
const resultString = Array.isArray(result)
? result.map((item) => item.strings.join("")).join("")
: result.strings.join("");
assert.ok(resultString.includes("OPTIONAL PARAMETERS"));
});

it("should render distinguished parameters if they exist", () => {
view.userSelectedUDA = {
name: "test",
description: "test description",
params: [
{
name: "optionalParam",
type: 10,
description: "optional param description",
isReq: false,
},
],
return: {
type: ["99"],
description: "test return description",
},
};
const result = view.renderUDAOptionalParamsOpts();
assert.strictEqual(Array.isArray(result) ? result.length : 0, 11);
const resultString = Array.isArray(result)
? result.map((item) => item.strings.join("")).join("")
: result.strings.join("");

assert.ok(resultString.includes("DISTINGUISHED PARAMETERS:"));
});
});
});

describe("KdbNewConnectionView", () => {
Expand Down

0 comments on commit a51c8ff

Please sign in to comment.