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

Clean up date histogram agg type. #58805

Merged
merged 13 commits into from
Mar 5, 2020
1 change: 1 addition & 0 deletions src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export {
siblingPipelineType,
termsAggFilter,
toAbsoluteDates,
updateTimeBuckets,
// search_source
getRequestInspectorStats,
getResponseInspectorStats,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import { dateHistogramInterval } from '../../../../common';
import { writeParams } from '../agg_params';
import { isMetricAggType } from '../metrics/metric_agg_type';

import { fieldFormats, KBN_FIELD_TYPES } from '../../../../../../../plugins/data/public';
import {
fieldFormats,
KBN_FIELD_TYPES,
TimefilterContract,
} from '../../../../../../../plugins/data/public';
import {
getFieldFormats,
getQueryService,
Expand All @@ -41,13 +45,12 @@ import {
const detectedTimezone = moment.tz.guess();
const tzOffset = moment().format('Z');

const updateTimeBuckets = (
export const updateTimeBuckets = (
agg: IBucketDateHistogramAggConfig,
timefilter: TimefilterContract,
customBuckets?: IBucketDateHistogramAggConfig['buckets']
) => {
const { timefilter } = getQueryService().timefilter;
const bounds = agg.params.timeRange ? timefilter.calculateBounds(agg.params.timeRange) : null;

const buckets = customBuckets || agg.buckets;
buckets.setBounds(agg.fieldIsTimeField() && bounds);
buckets.setInterval(agg.params.interval);
Expand Down Expand Up @@ -104,8 +107,9 @@ export const dateHistogramBucketAgg = new BucketAggType<IBucketDateHistogramAggC
get() {
if (buckets) return buckets;

const { timefilter } = getQueryService().timefilter;
buckets = new TimeBuckets({ uiSettings });
updateTimeBuckets(this, buckets);
updateTimeBuckets(this, timefilter, buckets);

return buckets;
},
Expand Down Expand Up @@ -176,7 +180,8 @@ export const dateHistogramBucketAgg = new BucketAggType<IBucketDateHistogramAggC
default: 'auto',
options: intervalOptions,
write(agg, output, aggs) {
updateTimeBuckets(agg);
const { timefilter } = getQueryService().timefilter;
lukeelmers marked this conversation as resolved.
Show resolved Hide resolved
updateTimeBuckets(agg, timefilter);

const { useNormalizedEsInterval, scaleMetricValues } = agg.params;
const interval = agg.buckets.getInterval(useNormalizedEsInterval);
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/data/public/search/aggs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export {
export { AggParamType } from './param_types/agg';
export { AggGroupNames, aggGroupNamesMap } from './agg_groups';
export { intervalOptions } from './buckets/_interval_options'; // only used in Discover
export { isDateHistogramBucketAggConfig } from './buckets/date_histogram';
export { isDateHistogramBucketAggConfig, updateTimeBuckets } from './buckets/date_histogram';
export { termsAggFilter } from './buckets/terms';
export { isType, isStringType } from './buckets/migrate_include_exclude_format';
export { CidrMask } from './buckets/lib/cidr_mask';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import {
TimefilterContract,
} from '../../../../../../../plugins/data/public';
import { Vis, VisParams } from '../types';
import { IAggConfig, isDateHistogramBucketAggConfig } from '../../../../../data/public';
import {
IAggConfig,
isDateHistogramBucketAggConfig,
updateTimeBuckets,
} from '../../../../../data/public';

interface SchemaConfigParams {
precision?: number;
Expand Down Expand Up @@ -86,12 +90,16 @@ const getSchemas = (
vis: Vis,
opts: {
timeRange?: any;
timefilter: TimefilterContract;
}
): Schemas => {
const { timeRange } = opts;
const createSchemaConfig = (accessor: number, agg: IAggConfig): SchemaConfig => {
if (isDateHistogramBucketAggConfig(agg)) {
agg.params.timeRange = timeRange;
// TODO: This is only place this is used outside the `data` plugin.
// We should find a way to get rid of it so it can stay internal to `data`.
updateTimeBuckets(agg, opts.timefilter);
lukeelmers marked this conversation as resolved.
Show resolved Hide resolved
}

const hasSubAgg = [
Expand Down Expand Up @@ -445,6 +453,7 @@ export const buildVislibDimensions = async (
) => {
const schemas = getSchemas(vis, {
timeRange: params.timeRange,
timefilter: params.timefilter,
});
const dimensions = {
x: schemas.segment ? schemas.segment[0] : null,
Expand All @@ -463,12 +472,8 @@ export const buildVislibDimensions = async (
dimensions.x.params.interval = moment.duration(esValue, esUnit);
dimensions.x.params.intervalESValue = esValue;
dimensions.x.params.intervalESUnit = esUnit;
const bounds = xAgg.params.timeRange
? params.timefilter.calculateBounds(xAgg.params.timeRange)
: null;
dimensions.x.params.bounds = xAgg.fieldIsTimeField() && bounds;
xAgg.buckets.setBounds(bounds);
dimensions.x.params.format = xAgg.buckets.getScaledDateFormat();
dimensions.x.params.bounds = xAgg.buckets.getBounds();
} else if (xAgg.type.name === 'histogram') {
const intervalParam = xAgg.type.paramByName('interval');
const output = { params: {} as any };
Expand Down Expand Up @@ -524,6 +529,7 @@ export const buildPipeline = async (

const schemas = getSchemas(vis, {
timeRange: params.timeRange,
timefilter: params.timefilter,
});
if (buildPipelineVisFunction[vis.type.name]) {
pipeline += buildPipelineVisFunction[vis.type.name](visState, schemas, uiState, {
Expand Down