Skip to content

Commit

Permalink
[TSVB2Lens] Support top_hit of size 1 aggregation (#125766)
Browse files Browse the repository at this point in the history
* [TSVB2Lens] Support top_hit of size 1 aggregation

* Add restriction to math aggregation

* Support with quick function

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
stratoula and kibanamachine authored Feb 17, 2022
1 parent d9a3e5e commit 0d23491
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,42 @@ describe('getSeries', () => {
]);
});

test('should return the correct formula config for a top_hit size 1 aggregation', () => {
const metric = [
{
id: '12345',
type: 'top_hit',
field: 'day_of_week_i',
size: 1,
order_by: 'timestamp',
},
] as Metric[];
const config = getSeries(metric);
expect(config).toStrictEqual([
{
agg: 'last_value',
fieldName: 'day_of_week_i',
isFullReference: false,
params: {
sortField: 'timestamp',
},
},
]);
});

test('should return null for a top_hit size >1 aggregation', () => {
const metric = [
{
id: '12345',
type: 'top_hit',
field: 'day_of_week_i',
size: 2,
},
] as Metric[];
const config = getSeries(metric);
expect(config).toBeNull();
});

test('should return the correct formula for the math aggregation with percentiles as variables', () => {
const metric = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ export const getSeries = (metrics: Metric[]): VisualizeEditorLayersContext['metr
continue;
}
const currentMetric = metrics[layerMetricIdx];
// We can only support top_hit with size 1
if (
(currentMetric.type === 'top_hit' &&
currentMetric?.size &&
Number(currentMetric?.size) !== 1) ||
currentMetric?.order === 'asc'
) {
return null;
}

// should treat percentiles differently
if (currentMetric.type === 'percentile') {
Expand Down Expand Up @@ -152,6 +161,29 @@ export const getSeries = (metrics: Metric[]): VisualizeEditorLayersContext['metr
metricsArray = getFormulaSeries(formula);
break;
}
case 'top_hit': {
const currentMetric = metrics[metricIdx];
// We can only support top_hit with size 1
if (
(currentMetric?.size && Number(currentMetric?.size) !== 1) ||
currentMetric?.order === 'asc'
) {
return null;
}
const timeScale = getTimeScale(currentMetric);
metricsArray = [
{
agg: aggregationMap.name,
isFullReference: aggregationMap.isFullReference,
fieldName: fieldName ?? 'document',
params: {
...(timeScale && { timeScale }),
...(currentMetric?.order_by && { sortField: currentMetric?.order_by }),
},
},
];
break;
}
default: {
const timeScale = getTimeScale(metrics[metricIdx]);
metricsArray = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export const SUPPORTED_METRICS: { [key: string]: AggOptions } = {
name: 'filter_ratio',
isFullReference: false,
},
top_hit: {
name: 'last_value',
isFullReference: false,
},
math: {
name: 'formula',
isFullReference: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export const lastValueOperation: OperationDefinition<LastValueIndexPatternColumn
return errorMessages.length ? errorMessages : undefined;
},
buildColumn({ field, previousColumn, indexPattern }, columnParams) {
const lastValueParams = columnParams as LastValueIndexPatternColumn['params'];
const sortField = isTimeFieldNameDateField(indexPattern)
? indexPattern.timeFieldName
: indexPattern.fields.find((f) => f.type === 'date')?.name;
Expand All @@ -188,7 +189,7 @@ export const lastValueOperation: OperationDefinition<LastValueIndexPatternColumn
filter: getFilter(previousColumn, columnParams),
timeShift: columnParams?.shift || previousColumn?.timeShift,
params: {
sortField,
sortField: lastValueParams?.sortField || sortField,
...getFormatFromPreviousColumn(previousColumn),
},
};
Expand Down

0 comments on commit 0d23491

Please sign in to comment.