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

[discuss][Drilldowns][URL drilldown] Expose "event.row" in VALUE_CLICK_TRIGGER #79157

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ export class UrlDrilldown implements Drilldown<Config, UrlTrigger, ActionFactory
config: Config,
context: ActionFactoryContext
): config is Config => {
const { isValid } = urlDrilldownValidateUrlTemplate(config.url, this.buildEditorScope(context));
const { isValid } = urlDrilldownValidateUrlTemplate(
config.url,
this.buildEditorScope(context),
{ validateVariables: false }
);
return isValid;
};

public readonly isCompatible = async (config: Config, context: ActionContext) => {
const { isValid, error } = urlDrilldownValidateUrlTemplate(
config.url,
await this.buildRuntimeScope(context)
await this.buildRuntimeScope(context),
{ validateVariables: true }
);

if (!isValid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,25 @@ describe('VALUE_CLICK_TRIGGER', () => {
]) as ValueClickTriggerEventScope;
expect(mockEventScope.points.length).toBeGreaterThan(3);
expect(mockEventScope.points).toMatchInlineSnapshot(`
Array [
Object {
"key": "event.points.0.key",
"value": "event.points.0.value",
},
Object {
"key": "event.points.1.key",
"value": "event.points.1.value",
},
Object {
"key": "event.points.2.key",
"value": "event.points.2.value",
},
Object {
"key": "event.points.3.key",
"value": "event.points.3.value",
},
]
`);
Array [
Object {
"key": "event.points.[0].key",
"value": "event.points.[0].value",
},
Object {
"key": "event.points.[1].key",
"value": "event.points.[1].value",
},
Object {
"key": "event.points.[2].key",
"value": "event.points.[2].value",
},
Object {
"key": "event.points.[3].key",
"value": "event.points.[3].value",
},
]
`);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface ValueClickTriggerEventScope {
value: Primitive;
negate: boolean;
points: Array<{ key?: string; value: Primitive }>;
row: Primitive[];
}
export interface RangeSelectTriggerEventScope {
key: string;
Expand All @@ -131,7 +132,7 @@ function getEventScopeFromRangeSelectTriggerContext(
const { table, column: columnIndex, range } = eventScopeInput.data;
const column = table.columns[columnIndex];
return cleanEmptyKeys({
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field) as string,
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field ?? column?.name) as string,
from: toPrimitiveOrUndefined(range[0]) as string | number | undefined,
to: toPrimitiveOrUndefined(range[range.length - 1]) as string | number | undefined,
});
Expand All @@ -141,19 +142,29 @@ function getEventScopeFromValueClickTriggerContext(
eventScopeInput: ValueClickContext
): ValueClickTriggerEventScope {
const negate = eventScopeInput.data.negate ?? false;

const points = eventScopeInput.data.data.map(({ table, value, column: columnIndex }) => {
const column = table.columns[columnIndex];
return {
value: toPrimitiveOrUndefined(value) as Primitive,
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field) as string | undefined,
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field ?? column?.name) as
| string
| undefined,
};
});

const dataPoint = eventScopeInput.data.data[0];

const row = dataPoint.table.columns.map(
({ id }) => dataPoint.table.rows[dataPoint.row][id]
) as Primitive[];

return cleanEmptyKeys({
key: points[0]?.key,
value: points[0]?.value,
negate,
points,
row,
});
}

Expand All @@ -174,14 +185,19 @@ export function getMockEventScope([trigger]: UrlTrigger[]): UrlDrilldownEventSco
// should be larger or equal of any possible data points length emitted by VALUE_CLICK_TRIGGER
const nPoints = 4;
const points = new Array(nPoints).fill(0).map((_, index) => ({
key: `event.points.${index}.key`,
value: `event.points.${index}.value`,
key: `event.points.[${index}].key`,
value: `event.points.[${index}].value`,
}));

// number of mock columns to generate
const nColumns = 4;
const row = new Array(nColumns).fill(0).map((_, index) => `event.row.[${index}]`);
return {
key: `event.key`,
value: `event.value`,
key: points[0].key,
value: points[0].value,
negate: false,
points,
row,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ handlebars.registerHelper('date', (...args) => {
return format ? momentDate.format(format) : momentDate.toISOString();
});

export function compile(url: string, context: object): string {
const template = handlebars.compile(url, { strict: true, noEscape: true });
export function compile(
url: string,
context: object,
{ strict = true }: { strict: boolean } = { strict: true }
): string {
const template = handlebars.compile(url, { strict, noEscape: true });
return encodeURI(template(context));
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export function validateUrl(url: string): { isValid: boolean; error?: string } {

export function validateUrlTemplate(
urlTemplate: UrlDrilldownConfig['url'],
scope: UrlDrilldownScope
scope: UrlDrilldownScope,
{ validateVariables = true }: { validateVariables: boolean } = { validateVariables: true }
): { isValid: boolean; error?: string } {
if (!urlTemplate.template)
return {
Expand All @@ -60,7 +61,7 @@ export function validateUrlTemplate(
};

try {
const compiledUrl = compile(urlTemplate.template, scope);
const compiledUrl = compile(urlTemplate.template, scope, { strict: validateVariables });
return validateUrl(compiledUrl);
} catch (e) {
return {
Expand Down