[Data] Update esaggs function to use count aggregation by default (#110095)

* Update default value of the aggs argument to use count aggregation
* Fix datatable canvas element to use column id instead of name
This commit is contained in:
Michael Dokolin 2021-08-30 17:10:14 +02:00 committed by GitHub
parent 035937a5c1
commit 57dc0c1ff5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 6 deletions

View file

@ -9,12 +9,13 @@
import { i18n } from '@kbn/i18n';
import { Observable } from 'rxjs';
import { Datatable, ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
import type { Datatable, ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
import { buildExpressionFunction } from '../../../../../../plugins/expressions/common';
import { IndexPatternExpressionType } from '../../../index_patterns/expressions';
import { IndexPatternsContract } from '../../../index_patterns/index_patterns';
import { AggsStart, AggExpressionType } from '../../aggs';
import { AggsStart, AggExpressionType, aggCountFnName } from '../../aggs';
import { ISearchStartSearchSource } from '../../search_source';
import { KibanaContext } from '../kibana_context_type';
@ -67,7 +68,7 @@ export const getEsaggsMeta: () => Omit<EsaggsExpressionFunctionDefinition, 'fn'>
aggs: {
types: ['agg_type'],
multi: true,
default: [],
default: `{${buildExpressionFunction(aggCountFnName, {}).toString()}}`,
help: i18n.translate('data.search.functions.esaggs.aggConfigs.help', {
defaultMessage: 'List of aggs configured with agg_type functions',
}),

View file

@ -6,6 +6,7 @@
* Side Public License, v 1.
*/
import { omit } from 'lodash';
import { of as mockOf } from 'rxjs';
import type { MockedKeys } from '@kbn/utility-types/jest';
import type { ExecutionContext } from 'src/plugins/expressions/public';
@ -90,6 +91,12 @@ describe('esaggs expression function - public', () => {
);
});
test('calls aggs.createAggConfigs with the empty aggs array when not provided', async () => {
await definition().fn(null, omit(args, 'aggs'), mockHandlers).toPromise();
expect(startDependencies.aggs.createAggConfigs).toHaveBeenCalledWith({}, []);
});
test('calls getEsaggsMeta to retrieve meta', () => {
const result = definition();

View file

@ -44,7 +44,7 @@ export function getFunctionDefinition({
const indexPattern = await indexPatterns.create(args.index.value, true);
const aggConfigs = aggs.createAggConfigs(
indexPattern,
args.aggs!.map((agg) => agg.value)
args.aggs?.map((agg) => agg.value) ?? []
);
aggConfigs.hierarchical = args.metricsAtAllLevels;

View file

@ -6,6 +6,7 @@
* Side Public License, v 1.
*/
import { omit } from 'lodash';
import { of as mockOf } from 'rxjs';
import type { MockedKeys } from '@kbn/utility-types/jest';
import { KibanaRequest } from 'src/core/server';
@ -98,6 +99,12 @@ describe('esaggs expression function - server', () => {
);
});
test('calls aggs.createAggConfigs with the empty aggs array when not provided', async () => {
await definition().fn(null, omit(args, 'aggs'), mockHandlers).toPromise();
expect(startDependencies.aggs.createAggConfigs).toHaveBeenCalledWith({}, []);
});
test('calls getEsaggsMeta to retrieve meta', () => {
const result = definition();

View file

@ -56,7 +56,7 @@ export function getFunctionDefinition({
const indexPattern = await indexPatterns.create(args.index.value, true);
const aggConfigs = aggs.createAggConfigs(
indexPattern,
args.aggs!.map((agg) => agg.value)
args.aggs?.map((agg) => agg.value) ?? []
);
aggConfigs.hierarchical = args.metricsAtAllLevels;

View file

@ -40,6 +40,8 @@ const getIcon = (type: DatatableColumnType | null) => {
const getColumnName = (col: DatatableColumn) => (typeof col === 'string' ? col : col.name);
const getColumnId = (col: DatatableColumn) => (typeof col === 'string' ? col : col.id);
const getColumnType = (col: DatatableColumn) => col.meta?.type || null;
const getFormattedValue = (val: any, type: any) => {
@ -85,7 +87,7 @@ export const Datatable: FC<Props> = ({
<tr key={i} className="canvasDataTable__tr">
{datatable.columns.map((col) => (
<td key={`row-${i}-${getColumnName(col)}`} className="canvasDataTable__td">
{getFormattedValue(row[getColumnName(col)], getColumnType(col))}
{getFormattedValue(row[getColumnId(col)], getColumnType(col))}
</td>
))}
</tr>