[8.x] [Lens] Fix generation of on-click filters for generated ESQL Lens (#218223) (#218407)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Lens] Fix generation of on-click filters for generated ESQL Lens
(#218223)](https://github.com/elastic/kibana/pull/218223)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Marco
Vettorello","email":"marco.vettorello@elastic.co"},"sourceCommit":{"committedDate":"2025-04-16T10:32:11Z","message":"[Lens]
Fix generation of on-click filters for generated ESQL Lens
(#218223)","sha":"19feb59109b2cbc53667042e04ceba4f4e92c901","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Visualizations","release_note:skip","Feature:Lens","backport:version","v9.1.0","v8.19.0","v9.0.1"],"title":"[Lens]
Fix generation of on-click filters for generated ESQL
Lens","number":218223,"url":"https://github.com/elastic/kibana/pull/218223","mergeCommit":{"message":"[Lens]
Fix generation of on-click filters for generated ESQL Lens
(#218223)","sha":"19feb59109b2cbc53667042e04ceba4f4e92c901"}},"sourceBranch":"main","suggestedTargetBranches":["8.x","9.0"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/218223","number":218223,"mergeCommit":{"message":"[Lens]
Fix generation of on-click filters for generated ESQL Lens
(#218223)","sha":"19feb59109b2cbc53667042e04ceba4f4e92c901"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.0","label":"v9.0.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Marco Vettorello <marco.vettorello@elastic.co>
This commit is contained in:
Kibana Machine 2025-04-16 14:33:27 +02:00 committed by GitHub
parent 024367f726
commit b935520b3b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 27 deletions

View file

@ -189,12 +189,13 @@ export const buildRangeFilter = (
export const buildSimpleNumberRangeFilter = (
fieldName: string,
fieldType: 'number' | 'date',
params: RangeFilterParams,
value: string,
dataViewId: string
) => {
return buildRangeFilter(
{ name: fieldName, type: 'number' },
{ name: fieldName, type: fieldType },
params,
{ id: dataViewId, title: dataViewId },
value

View file

@ -121,7 +121,9 @@ describe('createFiltersFromClickEvent', () => {
meta: {
type: 'number',
sourceParams: {
indexPattern: 'logs*',
sourceField: 'bytes',
operationType: 'sum',
},
},
},
@ -148,24 +150,54 @@ describe('createFiltersFromClickEvent', () => {
expect(filter).toEqual([]);
});
test('handles an event when operation type is a date histogram', async () => {
(table.columns[0].meta.sourceParams as any).operationType = 'date_histogram';
test('ignores event when sourceField.indexPattern is missing', async () => {
(table.columns[0].meta.sourceParams as any).indexPattern = null;
const filter = await createFilterESQL(table, 0, 0);
expect(filter).toMatchInlineSnapshot(`Array []`);
expect(filter).toEqual([]);
});
test('handles an event when operation type is a date histogram', async () => {
(table.columns[0].meta.sourceParams as any).operationType = 'date_histogram';
(table.columns[0].meta.sourceParams as any).sourceField = '@timestamp';
(table.columns[0].meta.sourceParams as any).interval = 1000;
(table.columns[0].meta as any).type = 'date';
table.rows[0]['1-1'] = 1696118400000;
const filter = await createFilterESQL(table, 0, 0);
expect(filter).toEqual([
{
meta: { field: '@timestamp', formattedValue: 1696118400000, index: 'logs*', params: {} },
query: {
range: {
'@timestamp': {
format: 'strict_date_optional_time',
gte: 1696118400000,
lt: 1696118401000,
},
},
},
},
]);
});
test('handles an event when operation type is histogram', async () => {
(table.columns[0].meta.sourceParams as any).operationType = 'histogram';
const filter = await createFilterESQL(table, 0, 0);
expect(filter).toMatchInlineSnapshot(`Array []`);
expect(filter).toEqual([
{
meta: { field: 'bytes', formattedValue: '2048', index: 'logs*', params: {} },
query: { range: { bytes: { gte: 2048, lt: 20480 } } },
},
]);
});
test('handles an event when operation type is not date histogram', async () => {
const filter = await createFilterESQL(table, 0, 0);
expect(filter).toMatchInlineSnapshot(`Array []`);
expect(filter.length).toBe(1);
});
});

View file

@ -162,10 +162,11 @@ export const createFilterESQL = async (
filters.push(
buildSimpleNumberRangeFilter(
sourceField,
operationType === 'date_histogram' ? 'date' : 'number',
{
gte: value,
lt: value + (interval || 0),
...(operationType === 'date_hisotgram' ? { format: 'strict_date_optional_time' } : {}),
lt: value + (interval ?? 0),
...(operationType === 'date_histogram' ? { format: 'strict_date_optional_time' } : {}),
},
value,
indexPattern
@ -185,25 +186,22 @@ export const createFiltersFromValueClickAction = async ({
}: ValueClickDataContext) => {
const filters: Filter[] = [];
await Promise.all(
data
.filter((point) => point)
.map(async (val) => {
const { table, column, row } = val;
const filter =
table.meta?.type === 'es_ql'
? await createFilterESQL(table, column, row)
: (await createFilter(table, column, row)) || [];
if (filter) {
filter.forEach((f) => {
if (negate) {
f = toggleFilterNegated(f);
}
filters.push(f);
});
}
})
);
for (const value of data) {
if (!value) {
continue;
}
const { table, column, row } = value;
const filter =
table.meta?.type === 'es_ql'
? await createFilterESQL(table, column, row)
: (await createFilter(table, column, row)) ?? [];
filter.forEach((f) => {
if (negate) {
f = toggleFilterNegated(f);
}
filters.push(f);
});
}
return _.uniqWith(mapAndFlattenFilters(filters), (a, b) =>
compareFilters(a, b, COMPARE_ALL_OPTIONS)