Skip to content

Commit

Permalink
simplify date histogram meta and apply interval scaling to all levels
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Mar 23, 2021
1 parent 8961f85 commit df22cae
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 284 deletions.
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 @@ -437,10 +437,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
: 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 @@ -57,6 +57,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 @@ -76,6 +77,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 @@ -192,9 +192,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
30 changes: 30 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,22 @@ export const getDateHistogramBucketAgg = ({
}
},
},
{
name: 'used_interval',
default: autoInterval,
shouldShow() {
return false;
},
write: () => {},
serialize(val, agg) {
if (!agg) return undefined;
updateTimeBuckets(agg, calculateBounds);
const { useNormalizedEsInterval } = agg.params;
const interval = agg.buckets.getInterval(useNormalizedEsInterval);
return interval.expression;
},
toExpressionAst: () => undefined,
},
{
name: 'time_zone',
default: undefined,
Expand All @@ -232,6 +250,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 @@ -104,19 +103,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 for data table column created by the histogramm agg type.
* "auto" will get expanded to the actually used interval.
* If the column is not a column created by a 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
147 changes: 0 additions & 147 deletions src/plugins/data/common/search/aggs/utils/time_column_meta.test.ts

This file was deleted.

57 changes: 0 additions & 57 deletions src/plugins/data/common/search/aggs/utils/time_column_meta.ts

This file was deleted.

Loading

0 comments on commit df22cae

Please sign in to comment.