Skip to content

Commit 19feb59

Browse files
authored
[Lens] Fix generation of on-click filters for generated ESQL Lens (#218223)
1 parent eaa1978 commit 19feb59

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

src/platform/packages/shared/kbn-es-query/src/filters/build_filters/range_filter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,13 @@ export const buildRangeFilter = (
189189

190190
export const buildSimpleNumberRangeFilter = (
191191
fieldName: string,
192+
fieldType: 'number' | 'date',
192193
params: RangeFilterParams,
193194
value: string,
194195
dataViewId: string
195196
) => {
196197
return buildRangeFilter(
197-
{ name: fieldName, type: 'number' },
198+
{ name: fieldName, type: fieldType },
198199
params,
199200
{ id: dataViewId, title: dataViewId },
200201
value

src/platform/plugins/shared/data/public/actions/filters/create_filters_from_value_click.test.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ describe('createFiltersFromClickEvent', () => {
121121
meta: {
122122
type: 'number',
123123
sourceParams: {
124+
indexPattern: 'logs*',
124125
sourceField: 'bytes',
126+
operationType: 'sum',
125127
},
126128
},
127129
},
@@ -148,24 +150,54 @@ describe('createFiltersFromClickEvent', () => {
148150
expect(filter).toEqual([]);
149151
});
150152

153+
test('ignores event when sourceField.indexPattern is missing', async () => {
154+
(table.columns[0].meta.sourceParams as any).indexPattern = null;
155+
const filter = await createFilterESQL(table, 0, 0);
156+
157+
expect(filter).toEqual([]);
158+
});
159+
151160
test('handles an event when operation type is a date histogram', async () => {
152161
(table.columns[0].meta.sourceParams as any).operationType = 'date_histogram';
162+
(table.columns[0].meta.sourceParams as any).sourceField = '@timestamp';
163+
(table.columns[0].meta.sourceParams as any).interval = 1000;
164+
(table.columns[0].meta as any).type = 'date';
165+
table.rows[0]['1-1'] = 1696118400000;
166+
153167
const filter = await createFilterESQL(table, 0, 0);
154168

155-
expect(filter).toMatchInlineSnapshot(`Array []`);
169+
expect(filter).toEqual([
170+
{
171+
meta: { field: '@timestamp', formattedValue: 1696118400000, index: 'logs*', params: {} },
172+
query: {
173+
range: {
174+
'@timestamp': {
175+
format: 'strict_date_optional_time',
176+
gte: 1696118400000,
177+
lt: 1696118401000,
178+
},
179+
},
180+
},
181+
},
182+
]);
156183
});
157184

158185
test('handles an event when operation type is histogram', async () => {
159186
(table.columns[0].meta.sourceParams as any).operationType = 'histogram';
160187
const filter = await createFilterESQL(table, 0, 0);
161188

162-
expect(filter).toMatchInlineSnapshot(`Array []`);
189+
expect(filter).toEqual([
190+
{
191+
meta: { field: 'bytes', formattedValue: '2048', index: 'logs*', params: {} },
192+
query: { range: { bytes: { gte: 2048, lt: 20480 } } },
193+
},
194+
]);
163195
});
164196

165197
test('handles an event when operation type is not date histogram', async () => {
166198
const filter = await createFilterESQL(table, 0, 0);
167199

168-
expect(filter).toMatchInlineSnapshot(`Array []`);
200+
expect(filter.length).toBe(1);
169201
});
170202
});
171203

src/platform/plugins/shared/data/public/actions/filters/create_filters_from_value_click.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ export const createFilterESQL = async (
162162
filters.push(
163163
buildSimpleNumberRangeFilter(
164164
sourceField,
165+
operationType === 'date_histogram' ? 'date' : 'number',
165166
{
166167
gte: value,
167-
lt: value + (interval || 0),
168-
...(operationType === 'date_hisotgram' ? { format: 'strict_date_optional_time' } : {}),
168+
lt: value + (interval ?? 0),
169+
...(operationType === 'date_histogram' ? { format: 'strict_date_optional_time' } : {}),
169170
},
170171
value,
171172
indexPattern
@@ -185,25 +186,22 @@ export const createFiltersFromValueClickAction = async ({
185186
}: ValueClickDataContext) => {
186187
const filters: Filter[] = [];
187188

188-
await Promise.all(
189-
data
190-
.filter((point) => point)
191-
.map(async (val) => {
192-
const { table, column, row } = val;
193-
const filter =
194-
table.meta?.type === 'es_ql'
195-
? await createFilterESQL(table, column, row)
196-
: (await createFilter(table, column, row)) || [];
197-
if (filter) {
198-
filter.forEach((f) => {
199-
if (negate) {
200-
f = toggleFilterNegated(f);
201-
}
202-
filters.push(f);
203-
});
204-
}
205-
})
206-
);
189+
for (const value of data) {
190+
if (!value) {
191+
continue;
192+
}
193+
const { table, column, row } = value;
194+
const filter =
195+
table.meta?.type === 'es_ql'
196+
? await createFilterESQL(table, column, row)
197+
: (await createFilter(table, column, row)) ?? [];
198+
filter.forEach((f) => {
199+
if (negate) {
200+
f = toggleFilterNegated(f);
201+
}
202+
filters.push(f);
203+
});
204+
}
207205

208206
return _.uniqWith(mapAndFlattenFilters(filters), (a, b) =>
209207
compareFilters(a, b, COMPARE_ALL_OPTIONS)

0 commit comments

Comments
 (0)