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

[Rollups] Add useNormalizedEsInterval param to date histogram bucket to allow rollups to avoid normalizing ES interval units #24428

Merged
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: 7 additions & 2 deletions src/ui/public/agg_types/buckets/date_histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export const dateHistogramBucketAgg = new BucketAggType({
default: null,
write: _.noop,
},
{
name: 'useNormalizedEsInterval',
default: true,
write: _.noop,
},
{
name: 'interval',
type: 'optioned',
Expand All @@ -124,8 +129,8 @@ export const dateHistogramBucketAgg = new BucketAggType({
write: function (agg, output, aggs) {
setBounds(agg, true);
agg.buckets.setInterval(getInterval(agg));

const interval = agg.buckets.getInterval();
const { useNormalizedEsInterval } = agg.params;
const interval = agg.buckets.getInterval(useNormalizedEsInterval);
output.bucketInterval = interval;
output.params.interval = interval.expression;

Expand Down
12 changes: 11 additions & 1 deletion src/ui/public/time_buckets/calc_es_interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import dateMath from '@kbn/datemath';
import { parseEsInterval } from 'ui/utils/parse_es_interval';

const unitsDesc = dateMath.unitsDesc;
const largeMax = unitsDesc.indexOf('M');
Expand All @@ -30,7 +31,7 @@ const largeMax = unitsDesc.indexOf('M');
* @param {moment.duration} duration
* @return {object}
*/
export function calcEsInterval(duration) {
export function convertDurationToNormalizedEsInterval(duration) {
for (let i = 0; i < unitsDesc.length; i++) {
const unit = unitsDesc[i];
const val = duration.as(unit);
Expand Down Expand Up @@ -59,3 +60,12 @@ export function calcEsInterval(duration) {
expression: ms + 'ms'
};
}

export function convertIntervalToEsInterval(interval) {
const { value, unit } = parseEsInterval(interval);
return {
value,
unit,
expression: interval,
};
}
19 changes: 14 additions & 5 deletions src/ui/public/time_buckets/time_buckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import moment from 'moment';
import chrome from '../chrome';
import { parseInterval } from '../utils/parse_interval';
import { calcAutoInterval } from './calc_auto_interval';
import { calcEsInterval } from './calc_es_interval';
import {
convertDurationToNormalizedEsInterval,
convertIntervalToEsInterval,
} from './calc_es_interval';
import { fieldFormats } from '../registry/field_formats';

const config = chrome.getUiSettingsClient();
Expand Down Expand Up @@ -152,6 +155,10 @@ TimeBuckets.prototype.getDuration = function () {
* @param {object|string|moment.duration} input - see desc
*/
TimeBuckets.prototype.setInterval = function (input) {
// Preserve the original units because they're lost when the interval is converted to a
// moment duration object.
this.originalInterval = input;

let interval = input;

// selection object -> val
Expand Down Expand Up @@ -215,7 +222,7 @@ TimeBuckets.prototype.setInterval = function (input) {
*
* @return {[type]} [description]
*/
TimeBuckets.prototype.getInterval = function () {
TimeBuckets.prototype.getInterval = function (useNormalizedEsInterval = true) {
const self = this;
const duration = self.getDuration();
return decorateInterval(maybeScaleInterval(readInterval()));
Expand Down Expand Up @@ -253,7 +260,9 @@ TimeBuckets.prototype.getInterval = function () {

// append some TimeBuckets specific props to the interval
function decorateInterval(interval) {
const esInterval = calcEsInterval(interval);
const esInterval = useNormalizedEsInterval
? convertDurationToNormalizedEsInterval(interval)
: convertIntervalToEsInterval(self.originalInterval);
interval.esValue = esInterval.value;
interval.esUnit = esInterval.unit;
interval.expression = esInterval.expression;
Expand Down Expand Up @@ -341,12 +350,12 @@ TimeBuckets.__cached__ = function (self) {

function cachedGetter(prop) {
return {
value: function cachedGetter() {
value: function cachedGetter(...rest) {
if (cache.hasOwnProperty(prop)) {
return cache[prop];
}

return cache[prop] = self[prop]();
return cache[prop] = self[prop](...rest);
}
};
}
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/rollup/public/visualize/editor_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ editorConfigProviders.register((aggType, indexPattern, aggConfig) => {
interval: {
fixedValue: 'custom',
},
useNormalizedEsInterval: {
fixedValue: false,
},
customInterval: {
default: interval,
timeBase: interval,
Expand Down