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

Simplify date histogram meta and apply interval scaling to all levels #95206

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<b>Signature:</b>

```typescript
fieldIsTimeField(): boolean | "" | undefined;
fieldIsTimeField(): boolean;
```
<b>Returns:</b>

`boolean | "" | undefined`
`boolean`

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export declare class AggConfigs
| [aggs](./kibana-plugin-plugins-data-public.aggconfigs.aggs.md) | | <code>IAggConfig[]</code> | |
| [createAggConfig](./kibana-plugin-plugins-data-public.aggconfigs.createaggconfig.md) | | <code>&lt;T extends AggConfig = AggConfig&gt;(params: CreateAggConfigParams, { addToAggConfigs }?: {</code><br/><code> addToAggConfigs?: boolean &#124; undefined;</code><br/><code> }) =&gt; T</code> | |
| [indexPattern](./kibana-plugin-plugins-data-public.aggconfigs.indexpattern.md) | | <code>IndexPattern</code> | |
| [timeFields](./kibana-plugin-plugins-data-public.aggconfigs.timefields.md) | | <code>string[]</code> | |
| [timeRange](./kibana-plugin-plugins-data-public.aggconfigs.timerange.md) | | <code>TimeRange</code> | |

## Methods
Expand All @@ -43,6 +44,7 @@ export declare class AggConfigs
| [getResponseAggs()](./kibana-plugin-plugins-data-public.aggconfigs.getresponseaggs.md) | | Gets the AggConfigs (and possibly ResponseAggConfigs) that represent the values that will be produced when all aggs are run.<!-- -->With multi-value metric aggs it is possible for a single agg request to result in multiple agg values, which is why the length of a vis' responseValuesAggs may be different than the vis' aggs {<!-- -->array\[AggConfig\]<!-- -->} |
| [jsonDataEquals(aggConfigs)](./kibana-plugin-plugins-data-public.aggconfigs.jsondataequals.md) | | Data-by-data comparison of this Aggregation Ignores the non-array indexes |
| [onSearchRequestStart(searchSource, options)](./kibana-plugin-plugins-data-public.aggconfigs.onsearchrequeststart.md) | | |
| [setTimeFields(timeFields)](./kibana-plugin-plugins-data-public.aggconfigs.settimefields.md) | | |
| [setTimeRange(timeRange)](./kibana-plugin-plugins-data-public.aggconfigs.settimerange.md) | | |
| [toDsl(hierarchical)](./kibana-plugin-plugins-data-public.aggconfigs.todsl.md) | | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [AggConfigs](./kibana-plugin-plugins-data-public.aggconfigs.md) &gt; [setTimeFields](./kibana-plugin-plugins-data-public.aggconfigs.settimefields.md)

## AggConfigs.setTimeFields() method

<b>Signature:</b>

```typescript
setTimeFields(timeFields: string[] | undefined): void;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| timeFields | <code>string[] &#124; undefined</code> | |

<b>Returns:</b>

`void`

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [AggConfigs](./kibana-plugin-plugins-data-public.aggconfigs.md) &gt; [timeFields](./kibana-plugin-plugins-data-public.aggconfigs.timefields.md)

## AggConfigs.timeFields property

<b>Signature:</b>

```typescript
timeFields?: string[];
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ search: {
intervalOptions: ({
display: string;
val: string;
enabled(agg: import("../common").IBucketAggConfig): boolean | "" | undefined;
enabled(agg: import("../common").IBucketAggConfig): boolean;
} | {
display: string;
val: string;
Expand Down Expand Up @@ -47,6 +47,11 @@ search: {
intervalLabel: string;
})[];
getNumberHistogramIntervalByDatatableColumn: (column: import("../../expressions").DatatableColumn) => number | undefined;
getDateHistogramMetaDataByDatatableColumn: (column: import("../../expressions").DatatableColumn) => {
interval: string | undefined;
timeZone: string | undefined;
timeRange: import("../common").TimeRange | undefined;
} | undefined;
};
getRequestInspectorStats: typeof getRequestInspectorStats;
getResponseInspectorStats: typeof getResponseInspectorStats;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ search: {
intervalOptions: ({
display: string;
val: string;
enabled(agg: import("../common").IBucketAggConfig): boolean | "" | undefined;
enabled(agg: import("../common").IBucketAggConfig): boolean;
} | {
display: string;
val: string;
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/data/common/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,14 @@ export class AggConfig {
}

fieldIsTimeField() {
const indexPattern = this.getIndexPattern();
if (!indexPattern) return false;
const timeFieldName = indexPattern.timeFieldName;
return timeFieldName && this.fieldName() === timeFieldName;
const defaultTimeField = this.getIndexPattern()?.getTimeField?.()?.name;
const defaultTimeFields = defaultTimeField ? [defaultTimeField] : [];
const allTimeFields =
this.aggConfigs.timeFields && this.aggConfigs.timeFields.length > 0
? this.aggConfigs.timeFields
Copy link
Member

Choose a reason for hiding this comment

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

will this contain the default one? or we don't want the default one if this was provided ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It overwrites the default one - this is the same logic as esaggs uses for building the filter

: defaultTimeFields;
const currentFieldName = this.fieldName();
return allTimeFields.includes(currentFieldName);
}

public get type() {
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/data/common/search/aggs/agg_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type IAggConfigs = AggConfigs;
export class AggConfigs {
public indexPattern: IndexPattern;
public timeRange?: TimeRange;
public timeFields?: string[];
private readonly typesRegistry: AggTypesRegistryStart;

aggs: IAggConfig[];
Expand All @@ -83,6 +84,10 @@ export class AggConfigs {
configStates.forEach((params: any) => this.createAggConfig(params));
}

setTimeFields(timeFields: string[] | undefined) {
this.timeFields = timeFields;
}

setTimeRange(timeRange: TimeRange) {
this.timeRange = timeRange;

Expand Down
3 changes: 1 addition & 2 deletions src/plugins/data/common/search/aggs/aggs_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ describe('Aggs service', () => {
describe('start()', () => {
test('exposes proper contract', () => {
const start = service.start(startDeps);
expect(Object.keys(start).length).toBe(5);
expect(Object.keys(start).length).toBe(4);
expect(start).toHaveProperty('calculateAutoTimeExpression');
expect(start).toHaveProperty('getDateMetaByDatatableColumn');
expect(start).toHaveProperty('createAggConfigs');
expect(start).toHaveProperty('types');
expect(start).toHaveProperty('datatableUtilities');
Expand Down
7 changes: 0 additions & 7 deletions src/plugins/data/common/search/aggs/aggs_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
getCalculateAutoTimeExpression,
} from './';
import { AggsCommonSetup, AggsCommonStart } from './types';
import { getDateMetaByDatatableColumn } from './utils/time_column_meta';
import { getDatatableColumnUtilities } from './utils/datatable_column_meta';

/** @internal */
Expand Down Expand Up @@ -89,12 +88,6 @@ export class AggsCommonService {

return {
calculateAutoTimeExpression,
getDateMetaByDatatableColumn: getDateMetaByDatatableColumn({
calculateAutoTimeExpression,
getIndexPattern,
getConfig,
isDefaultTimezone,
}),
datatableUtilities: getDatatableColumnUtilities({
getIndexPattern,
createAggConfigs,
Expand Down
29 changes: 29 additions & 0 deletions src/plugins/data/common/search/aggs/buckets/date_histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export interface AggParamsDateHistogram extends BaseAggParams {
useNormalizedEsInterval?: boolean;
scaleMetricValues?: boolean;
interval?: string;
used_interval?: string;
time_zone?: string;
used_time_zone?: string;
drop_partials?: boolean;
format?: string;
min_doc_count?: number;
Expand Down Expand Up @@ -220,6 +222,21 @@ export const getDateHistogramBucketAgg = ({
}
},
},
{
name: 'used_interval',
default: autoInterval,
shouldShow() {
return false;
},
write: () => {},
serialize(val, agg) {
if (!agg) return undefined;
const { useNormalizedEsInterval } = agg.params;
const interval = agg.buckets.getInterval(useNormalizedEsInterval);
return interval.expression;
},
toExpressionAst: () => undefined,
},
{
name: 'time_zone',
default: undefined,
Expand All @@ -232,6 +249,18 @@ export const getDateHistogramBucketAgg = ({
output.params.time_zone = tz;
},
},
{
name: 'used_timezone',
shouldShow() {
return false;
},
write: () => {},
serialize(val, agg) {
if (!agg) return undefined;
return inferTimeZone(agg.params, agg.getIndexPattern(), isDefaultTimezone, getConfig);
},
toExpressionAst: () => undefined,
},
{
name: 'drop_partials',
default: false,
Expand Down
14 changes: 0 additions & 14 deletions src/plugins/data/common/search/aggs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import { Assign } from '@kbn/utility-types';
import { DatatableColumn } from 'src/plugins/expressions';
import { IndexPattern } from '../../index_patterns/index_patterns/index_pattern';
import { TimeRange } from '../../query';
import {
aggAvg,
aggBucketAvg,
Expand Down Expand Up @@ -106,19 +105,6 @@ export interface AggsCommonSetup {
/** @internal */
export interface AggsCommonStart {
calculateAutoTimeExpression: ReturnType<typeof getCalculateAutoTimeExpression>;
/**
* Helper function returning meta data about use date intervals for a data table column.
* If the column is not a column created by a date histogram aggregation of the esaggs data source,
* this function will return undefined.
*
* Otherwise, it will return the following attributes in an object:
* * `timeZone` time zone used to create the buckets (important e.g. for DST),
* * `timeRange` total time range of the fetch data (to infer partial buckets at the beginning and end of the data)
* * `interval` Interval used on elasticsearch (`auto` resolved to the actual interval)
*/
getDateMetaByDatatableColumn: (
column: DatatableColumn
) => Promise<undefined | { timeZone: string; timeRange?: TimeRange; interval: string }>;
datatableUtilities: {
getIndexPattern: (column: DatatableColumn) => Promise<IndexPattern | undefined>;
getAggConfig: (column: DatatableColumn) => Promise<AggConfig | undefined>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { DatatableColumn } from 'src/plugins/expressions/common';
import { TimeRange } from '../../../types';
import type { AggParamsDateHistogram } from '../buckets';
import { BUCKET_TYPES } from '../buckets/bucket_agg_types';

/**
* Helper function returning the used interval, used time zone and applied time filters for data table column created by the date_histogramm agg type.
* "auto" will get expanded to the actually used interval.
* If the column is not a column created by a date_histogram aggregation of the esaggs data source,
* this function will return undefined.
*/
export const getDateHistogramMetaDataByDatatableColumn = (column: DatatableColumn) => {
if (column.meta.source !== 'esaggs') return;
if (column.meta.sourceParams?.type !== BUCKET_TYPES.DATE_HISTOGRAM) return;
const params = (column.meta.sourceParams.params as unknown) as AggParamsDateHistogram;

let interval: string | undefined;
if (params.used_interval && params.used_interval !== 'auto') {
interval = params.used_interval;
}

return {
interval,
timeZone: params.used_time_zone,
timeRange: column.meta.sourceParams.appliedTimeRange as TimeRange | undefined,
};
};
1 change: 1 addition & 0 deletions src/plugins/data/common/search/aggs/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

export * from './calculate_auto_time_expression';
export { getNumberHistogramIntervalByDatatableColumn } from './get_number_histogram_interval';
export { getDateHistogramMetaDataByDatatableColumn } from './get_date_histogram_meta';
export * from './date_interval_utils';
export * from './get_format_with_aggs';
export * from './ipv4_address';
Expand Down
Loading