Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix agg datatable type #90574

Merged
merged 4 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/plugins/data/common/search/aggs/agg_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { constant, noop, identity } from 'lodash';
import { i18n } from '@kbn/i18n';

import { ISearchSource } from 'src/plugins/data/public';
import { SerializedFieldFormat } from 'src/plugins/expressions/common';
import { DatatableColumnType, SerializedFieldFormat } from 'src/plugins/expressions/common';
import type { RequestAdapter } from 'src/plugins/inspector/common';

import { initParams } from './agg_params';
Expand All @@ -33,6 +33,7 @@ export interface AggTypeConfig<
ordered?: any;
hasNoDsl?: boolean;
params?: Array<Partial<TParam>>;
valueType?: DatatableColumnType;
getRequestAggs?: ((aggConfig: TAggConfig) => TAggConfig[]) | (() => TAggConfig[] | void);
getResponseAggs?: ((aggConfig: TAggConfig) => TAggConfig[]) | (() => TAggConfig[] | void);
customLabels?: boolean;
Expand Down Expand Up @@ -91,6 +92,11 @@ export class AggType<
* @type {string}
*/
title: string;
/**
* The type the values produced by this agg will have in the final data table.
* If not specified, the type of the field is used.
*/
valueType?: DatatableColumnType;
/**
* a function that will be called when this aggType is assigned to
* an aggConfig, and that aggConfig is being rendered (in a form, chart, etc.).
Expand Down Expand Up @@ -222,6 +228,7 @@ export class AggType<
this.dslName = config.dslName || config.name;
this.expressionName = config.expressionName;
this.title = config.title;
this.valueType = config.valueType;
this.makeLabel = config.makeLabel || constant(this.name);
this.ordered = config.ordered;
this.hasNoDsl = !!config.hasNoDsl;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/search/aggs/metrics/cardinality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface AggParamsCardinality extends BaseAggParams {
export const getCardinalityMetricAgg = () =>
new MetricAggType({
name: METRIC_TYPES.CARDINALITY,
valueType: 'number',
expressionName: aggCardinalityFnName,
title: uniqueCountTitle,
makeLabel(aggConfig: IMetricAggConfig) {
Expand Down
36 changes: 27 additions & 9 deletions src/plugins/data/common/search/tabify/response_writer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { TabbedAggResponseWriter } from './response_writer';
import { AggConfigs, BUCKET_TYPES } from '../aggs';
import { AggConfigs, BUCKET_TYPES, METRIC_TYPES } from '../aggs';
import { mockAggTypesRegistry } from '../aggs/test_helpers';
import { TabbedResponseWriterOptions } from './types';

Expand All @@ -23,7 +23,12 @@ describe('TabbedAggResponseWriter class', () => {
field: 'geo.src',
},
},
{ type: 'count' },
{
type: METRIC_TYPES.CARDINALITY,
params: {
field: 'machine.os.raw',
},
},
];

const twoSplitsAggConfig = [
Expand All @@ -39,7 +44,12 @@ describe('TabbedAggResponseWriter class', () => {
field: 'machine.os.raw',
},
},
{ type: 'count' },
{
type: METRIC_TYPES.CARDINALITY,
params: {
field: 'machine.os.raw',
},
},
];

const createResponseWritter = (aggs: any[] = [], opts?: Partial<TabbedResponseWriterOptions>) => {
Expand Down Expand Up @@ -174,19 +184,23 @@ describe('TabbedAggResponseWriter class', () => {
});

expect(response.columns[1]).toHaveProperty('id', 'col-1-2');
expect(response.columns[1]).toHaveProperty('name', 'Count');
expect(response.columns[1]).toHaveProperty('name', 'Unique count of machine.os.raw');
expect(response.columns[1]).toHaveProperty('meta', {
index: 'logstash-*',
field: 'machine.os.raw',
params: {
id: 'number',
},
source: 'esaggs',
sourceParams: {
appliedTimeRange: undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is added because count is handled in a unique way, but most metrics have this?

enabled: true,
id: '2',
indexPatternId: '1234',
params: {},
type: 'count',
params: {
field: 'machine.os.raw',
},
type: 'cardinality',
},
type: 'number',
});
Expand Down Expand Up @@ -231,19 +245,23 @@ describe('TabbedAggResponseWriter class', () => {
});

expect(response.columns[1]).toHaveProperty('id', 'col-1-2');
expect(response.columns[1]).toHaveProperty('name', 'Count');
expect(response.columns[1]).toHaveProperty('name', 'Unique count of machine.os.raw');
expect(response.columns[1]).toHaveProperty('meta', {
index: 'logstash-*',
field: 'machine.os.raw',
params: {
id: 'number',
},
source: 'esaggs',
sourceParams: {
appliedTimeRange: undefined,
enabled: true,
id: '2',
indexPatternId: '1234',
params: {},
type: 'count',
params: {
field: 'machine.os.raw',
},
type: 'cardinality',
},
type: 'number',
});
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/common/search/tabify/response_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export class TabbedAggResponseWriter {
id: column.id,
name: column.name,
meta: {
type: column.aggConfig.params.field?.type || 'number',
type:
column.aggConfig.type.valueType || column.aggConfig.params.field?.type || 'number',
field: column.aggConfig.params.field?.name,
index: column.aggConfig.getIndexPattern()?.title,
params: column.aggConfig.toSerializedFieldFormat(),
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as CSS from 'csstype';
import { Datatable as Datatable_2 } from 'src/plugins/expressions';
import { Datatable as Datatable_3 } from 'src/plugins/expressions/common';
import { DatatableColumn as DatatableColumn_2 } from 'src/plugins/expressions';
import { DatatableColumnType } from 'src/plugins/expressions/common';
import { DetailedPeerCertificate } from 'tls';
import { Ensure } from '@kbn/utility-types';
import { EnvironmentMode } from '@kbn/config';
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CoreStart as CoreStart_2 } from 'src/core/server';
import { Datatable } from 'src/plugins/expressions';
import { Datatable as Datatable_2 } from 'src/plugins/expressions/common';
import { DatatableColumn } from 'src/plugins/expressions';
import { DatatableColumnType } from 'src/plugins/expressions/common';
import { Duration } from 'moment';
import { ElasticsearchClient } from 'src/core/server';
import { ElasticsearchClient as ElasticsearchClient_2 } from 'kibana/server';
Expand Down