Skip to content

Commit

Permalink
[Lens] Create a filter with field:value when last value metric is use…
Browse files Browse the repository at this point in the history
…d on a datatable
  • Loading branch information
mbondyra committed Jun 28, 2023
1 parent 4064e2b commit 644969e
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 19 deletions.
93 changes: 81 additions & 12 deletions src/plugins/data/common/search/aggs/metrics/filtered_metric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,36 @@
*/

import { AggConfigs, IAggConfigs } from '../agg_configs';
import { mockAggTypesRegistry } from '../test_helpers';
import { AggTypesDependencies } from '../agg_types';
import { mockAggTypesDependencies, mockAggTypesRegistry } from '../test_helpers';
import { getFilteredMetricAgg } from './filtered_metric';
import { IMetricAggConfig } from './metric_agg_type';
import { METRIC_TYPES } from './metric_agg_types';

describe('filtered metric agg type', () => {
let aggConfigs: IAggConfigs;
let aggTypesDependencies: AggTypesDependencies;
const typesRegistry = mockAggTypesRegistry();
const field = {
name: 'bytes',
filterable: true,
};
const indexPattern = {
id: '1234',
title: 'logstash-*',
fields: {
getByName: () => field,
filter: () => [field],
find: () => field,
},
} as any;

beforeEach(() => {
const typesRegistry = mockAggTypesRegistry();
const field = {
name: 'bytes',
jest.resetAllMocks();
aggTypesDependencies = {
...mockAggTypesDependencies,
getConfig: jest.fn(),
};
const indexPattern = {
id: '1234',
title: 'logstash-*',
fields: {
getByName: () => field,
filter: () => [field],
},
} as any;

aggConfigs = new AggConfigs(
indexPattern,
Expand Down Expand Up @@ -76,4 +87,62 @@ describe('filtered metric agg type', () => {

expect(agg.getResponseId()).toEqual('filtered_metric-bucket');
});
it('returns phrase filter for filtered metric on top metrics', () => {
const topMetricsAggConfigs = new AggConfigs(
indexPattern,
[
{
id: METRIC_TYPES.FILTERED_METRIC,
type: METRIC_TYPES.FILTERED_METRIC,
schema: 'metric',
params: {
customBucket: {
type: 'filter',
params: {
filter: { language: 'kuery', query: 'a: b' },
},
},
customMetric: {
type: 'top_metrics',
params: {
field: 'bytes',
},
},
},
},
],
{
typesRegistry,
},
jest.fn()
);

expect(
getFilteredMetricAgg(aggTypesDependencies).createFilter!(
topMetricsAggConfigs.aggs[0] as IMetricAggConfig,
10
)
).toEqual({
meta: { index: '1234' },
query: { match_phrase: { bytes: 10 } },
});
});
it('returns filter from the custom bucket filter parameter for metric', () => {
expect(
getFilteredMetricAgg(aggTypesDependencies).createFilter!(
aggConfigs.aggs[0] as IMetricAggConfig,
'10'
)
).toEqual({
query: {
bool: {
must: [],
filter: [{ bool: { should: [{ match: { bytes: 'b' } }], minimum_should_match: 1 } }],
should: [],
must_not: [],
},
},
meta: { index: '1234', alias: 'a: b' },
});
});
});
17 changes: 10 additions & 7 deletions src/plugins/data/common/search/aggs/metrics/filtered_metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ export const getFilteredMetricAgg = ({ getConfig }: FiltersMetricAggDependencies
hasNoDslParams: true,
getSerializedFormat,
createFilter: (agg, inputState) => {
const indexPattern = agg.getIndexPattern();
if (!indexPattern) return;
if (
agg.params.customMetric.type.name === 'top_hits' ||
agg.params.customMetric.type.name === 'top_metrics'
) {
return agg.params.customMetric.createFilter(inputState);
}
if (!agg.params.customBucket.params.filter) return;
const esQueryConfigs = getEsQueryConfig({ get: getConfig });
return buildQueryFilter(
buildEsQuery(
agg.getIndexPattern(),
[agg.params.customBucket.params.filter],
[],
esQueryConfigs
),
agg.getIndexPattern().id!,
buildEsQuery(indexPattern, [agg.params.customBucket.params.filter], [], esQueryConfigs),
indexPattern.id!,
agg.params.customBucket.params.filter.query
);
},
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/top_hit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,5 +402,11 @@ describe('Top hit metric', () => {
});
});
});
it('returns phrase filter', () => {
expect(getTopHitMetricAgg().createFilter!(aggConfig, '10')).toEqual({
meta: { index: '1234' },
query: { match_phrase: { bytes: 10 } },
});
});
});
});
7 changes: 7 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/top_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { DataViewField } from '@kbn/data-views-plugin/common';
import { buildPhraseFilter } from '@kbn/es-query';
import { aggTopHitFnName } from './top_hit_fn';
import { IMetricAggConfig, MetricAggType } from './metric_agg_type';
import { METRIC_TYPES } from './metric_agg_types';
Expand Down Expand Up @@ -256,5 +257,11 @@ export const getTopHitMetricAgg = () => {
}
return values;
},
createFilter: (agg, key) => {
const field = agg.getField();
if (field) {
return buildPhraseFilter(field, key, agg.getIndexPattern());
}
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,11 @@ describe('Top metrics metric', () => {
init({ fieldName: 'bytes' });
expect(getTopMetricsMetricAgg().getValue(aggConfig, bucket)).toEqual([1024, 512, 256]);
});
it('returns phrase filter', () => {
expect(getTopMetricsMetricAgg().createFilter!(aggConfig, '10')).toEqual({
meta: { index: '1234' },
query: { match_phrase: { bytes: 10 } },
});
});
});
});
7 changes: 7 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/top_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import _ from 'lodash';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { i18n } from '@kbn/i18n';
import { buildPhraseFilter } from '@kbn/es-query';
import { aggTopMetricsFnName } from './top_metrics_fn';
import { IMetricAggConfig, MetricAggType } from './metric_agg_type';
import { METRIC_TYPES } from './metric_agg_types';
Expand Down Expand Up @@ -162,5 +163,11 @@ export const getTopMetricsMetricAgg = () => {
if (results.length === 1) return results[0];
return results;
},
createFilter: (agg, key) => {
const field = agg.getField();
if (field) {
return buildPhraseFilter(field, key, agg.getIndexPattern());
}
},
});
};

0 comments on commit 644969e

Please sign in to comment.