Skip to content

Commit

Permalink
[TSVB] Math params._interval is incorrect when using entire timerange…
Browse files Browse the repository at this point in the history
… mode (#100775)

* [TSVB] Math params._interval is incorrect when using entire timerange mode

Closes: #100615

* fix jest

* rename get -> overwrite

* apply fix for "bucket script"

* Update date_histogram.js

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
alexwizp and kibanamachine authored Jun 3, 2021
1 parent 8097f36 commit 83b47c1
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
*/

import { getBucketsPath } from './get_buckets_path';
import { parseInterval } from './parse_interval';
import { set } from '@elastic/safer-lodash-set';
import { isEmpty } from 'lodash';
import { i18n } from '@kbn/i18n';
import { MODEL_SCRIPTS } from './moving_fn_scripts';
import { convertIntervalToUnit } from './unit_to_seconds';

function checkMetric(metric, fields) {
fields.forEach((field) => {
Expand Down Expand Up @@ -161,19 +161,24 @@ export const bucketTransform = {
};
},

derivative: (bucket, metrics, bucketSize) => {
derivative: (bucket, metrics, intervalString) => {
checkMetric(bucket, ['type', 'field']);

const body = {
derivative: {
buckets_path: getBucketsPath(bucket.field, metrics),
gap_policy: 'skip', // seems sane
unit: bucketSize,
unit: intervalString,
},
};

if (bucket.gap_policy) body.derivative.gap_policy = bucket.gap_policy;
if (bucket.unit) {
body.derivative.unit = /^([\d]+)([shmdwMy]|ms)$/.test(bucket.unit) ? bucket.unit : bucketSize;
body.derivative.unit = /^([\d]+)([shmdwMy]|ms)$/.test(bucket.unit)
? bucket.unit
: intervalString;
}

return body;
},

Expand Down Expand Up @@ -214,8 +219,10 @@ export const bucketTransform = {
};
},

calculation: (bucket, metrics, bucketSize) => {
calculation: (bucket, metrics, intervalString) => {
checkMetric(bucket, ['variables', 'script']);
const inMsInterval = convertIntervalToUnit(intervalString, 'ms');

const body = {
bucket_script: {
buckets_path: bucket.variables.reduce((acc, row) => {
Expand All @@ -226,7 +233,7 @@ export const bucketTransform = {
source: bucket.script,
lang: 'painless',
params: {
_interval: parseInterval(bucketSize).asMilliseconds(),
_interval: inMsInterval?.value,
},
},
gap_policy: 'skip', // seems sane
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ export function dateHistogram(
const barTargetUiSettings = await uiSettings.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET);

const { timeField, interval, maxBars } = getIntervalAndTimefield(panel, series, seriesIndex);
const { bucketSize, intervalString } = getBucketSize(
req,
interval,
capabilities,
maxBars ? Math.min(maxBarsUiSettings, maxBars) : barTargetUiSettings
);
const { from, to } = offsetTime(req, series.offset_time);

const getDateHistogramForLastBucketMode = () => {
const { from, to } = offsetTime(req, series.offset_time);
let bucketInterval;

const overwriteDateHistogramForLastBucketMode = () => {
const { timezone } = capabilities;

const { intervalString } = getBucketSize(
req,
interval,
capabilities,
maxBars ? Math.min(maxBarsUiSettings, maxBars) : barTargetUiSettings
);

overwrite(doc, `aggs.${series.id}.aggs.timeseries.date_histogram`, {
field: timeField,
min_doc_count: 0,
Expand All @@ -50,25 +53,29 @@ export function dateHistogram(
},
...dateHistogramInterval(intervalString),
});

bucketInterval = intervalString;
};

const getDateHistogramForEntireTimerangeMode = () =>
const overwriteDateHistogramForEntireTimerangeMode = () => {
overwrite(doc, `aggs.${series.id}.aggs.timeseries.auto_date_histogram`, {
field: timeField,
buckets: 1,
});

bucketInterval = `${to.valueOf() - from.valueOf()}ms`;
};

isLastValueTimerangeMode(panel, series)
? getDateHistogramForLastBucketMode()
: getDateHistogramForEntireTimerangeMode();
? overwriteDateHistogramForLastBucketMode()
: overwriteDateHistogramForEntireTimerangeMode();

overwrite(doc, `aggs.${series.id}.meta`, {
timeField,
intervalString,
bucketSize,
panelId: panel.id,
seriesId: series.id,
intervalString: bucketInterval,
index: panel.use_kibana_indexes ? seriesIndex.indexPattern?.id : undefined,
panelId: panel.id,
});

return next(doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ describe('dateHistogram(req, panel, series)', () => {
},
},
meta: {
bucketSize: 10,
intervalString: '10s',
timeField: '@timestamp',
seriesId: 'test',
Expand Down Expand Up @@ -128,7 +127,6 @@ describe('dateHistogram(req, panel, series)', () => {
},
},
meta: {
bucketSize: 10,
intervalString: '10s',
timeField: '@timestamp',
seriesId: 'test',
Expand Down Expand Up @@ -173,7 +171,6 @@ describe('dateHistogram(req, panel, series)', () => {
},
},
meta: {
bucketSize: 20,
intervalString: '20s',
timeField: 'timestamp',
seriesId: 'test',
Expand All @@ -185,8 +182,11 @@ describe('dateHistogram(req, panel, series)', () => {
});

describe('dateHistogram for entire time range mode', () => {
test('should ignore entire range mode for timeseries', async () => {
beforeEach(() => {
panel.time_range_mode = 'entire_time_range';
});

test('should ignore entire range mode for timeseries', async () => {
panel.type = 'timeseries';

const next = (doc) => doc;
Expand All @@ -204,9 +204,36 @@ describe('dateHistogram(req, panel, series)', () => {
expect(doc.aggs.test.aggs.timeseries.date_histogram).toBeDefined();
});

test('should returns valid date histogram for entire range mode', async () => {
panel.time_range_mode = 'entire_time_range';
test('should set meta values', async () => {
// set 15 minutes (=== 900000ms) interval;
req.body.timerange = {
min: '2021-01-01T00:00:00Z',
max: '2021-01-01T00:15:00Z',
};

const next = (doc) => doc;
const doc = await dateHistogram(
req,
panel,
series,
config,
indexPattern,
capabilities,
uiSettings
)(next)({});

expect(doc.aggs.test.meta).toMatchInlineSnapshot(`
Object {
"index": undefined,
"intervalString": "900000ms",
"panelId": "panelId",
"seriesId": "test",
"timeField": "@timestamp",
}
`);
});

test('should returns valid date histogram for entire range mode', async () => {
const next = (doc) => doc;
const doc = await dateHistogram(
req,
Expand All @@ -232,8 +259,7 @@ describe('dateHistogram(req, panel, series)', () => {
meta: {
timeField: '@timestamp',
seriesId: 'test',
bucketSize: 10,
intervalString: '10s',
intervalString: '3600000ms',
panelId: 'panelId',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,17 @@
*/

import { overwrite } from '../../helpers';
import { getBucketSize } from '../../helpers/get_bucket_size';
import { bucketTransform } from '../../helpers/bucket_transform';
import { getIntervalAndTimefield } from '../../get_interval_and_timefield';
import { UI_SETTINGS } from '../../../../../../data/common';
import { get } from 'lodash';

export function metricBuckets(
req,
panel,
series,
esQueryConfig,
seriesIndex,
capabilities,
uiSettings
) {
export function metricBuckets(req, panel, series) {
return (next) => async (doc) => {
const barTargetUiSettings = await uiSettings.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET);

const { interval } = getIntervalAndTimefield(panel, series, seriesIndex);
const { intervalString } = getBucketSize(req, interval, capabilities, barTargetUiSettings);

series.metrics
.filter((row) => !/_bucket$/.test(row.type) && !/^series/.test(row.type))
.forEach((metric) => {
const fn = bucketTransform[metric.type];
const intervalString = get(doc, `aggs.${series.id}.meta.intervalString`);

if (fn) {
try {
const bucket = fn(metric, series.metrics, intervalString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,28 @@
* Side Public License, v 1.
*/

import { get } from 'lodash';
import { overwrite } from '../../helpers';
import { getBucketSize } from '../../helpers/get_bucket_size';
import { bucketTransform } from '../../helpers/bucket_transform';
import { getIntervalAndTimefield } from '../../get_interval_and_timefield';
import { UI_SETTINGS } from '../../../../../../data/common';

export function siblingBuckets(
req,
panel,
series,
esQueryConfig,
seriesIndex,
capabilities,
uiSettings
) {
export function siblingBuckets(req, panel, series) {
return (next) => async (doc) => {
const barTargetUiSettings = await uiSettings.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET);
const { interval } = getIntervalAndTimefield(panel, series, seriesIndex);
const { bucketSize } = getBucketSize(req, interval, capabilities, barTargetUiSettings);

series.metrics
.filter((row) => /_bucket$/.test(row.type))
.forEach((metric) => {
const fn = bucketTransform[metric.type];
const intervalString = get(doc, `aggs.${series.id}.meta.intervalString`);

if (fn) {
try {
const bucket = fn(metric, series.metrics, bucketSize);
const bucket = fn(metric, series.metrics, intervalString);
overwrite(doc, `aggs.${series.id}.aggs.${metric.id}`, bucket);
} catch (e) {
// meh
}
}
});

return next(doc);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@ export function dateHistogram(req, panel, esQueryConfig, seriesIndex, capabiliti
return (next) => async (doc) => {
const barTargetUiSettings = await uiSettings.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET);
const { timeField, interval } = getIntervalAndTimefield(panel, {}, seriesIndex);
const { from, to } = getTimerange(req);

const meta = {
timeField,
index: panel.use_kibana_indexes ? seriesIndex.indexPattern?.id : undefined,
panelId: panel.id,
};

const getDateHistogramForLastBucketMode = () => {
const { bucketSize, intervalString } = getBucketSize(
req,
interval,
capabilities,
barTargetUiSettings
);
const { from, to } = getTimerange(req);
const overwriteDateHistogramForLastBucketMode = () => {
const { intervalString } = getBucketSize(req, interval, capabilities, barTargetUiSettings);
const { timezone } = capabilities;

panel.series.forEach((column) => {
Expand All @@ -54,12 +49,13 @@ export function dateHistogram(req, panel, esQueryConfig, seriesIndex, capabiliti
overwrite(doc, aggRoot.replace(/\.aggs$/, '.meta'), {
...meta,
intervalString,
bucketSize,
});
});
};

const getDateHistogramForEntireTimerangeMode = () => {
const overwriteDateHistogramForEntireTimerangeMode = () => {
const intervalString = `${to.valueOf() - from.valueOf()}ms`;

panel.series.forEach((column) => {
const aggRoot = calculateAggRoot(doc, column);

Expand All @@ -68,13 +64,16 @@ export function dateHistogram(req, panel, esQueryConfig, seriesIndex, capabiliti
buckets: 1,
});

overwrite(doc, aggRoot.replace(/\.aggs$/, '.meta'), meta);
overwrite(doc, aggRoot.replace(/\.aggs$/, '.meta'), {
...meta,
intervalString,
});
});
};

isLastValueTimerangeMode(panel)
? getDateHistogramForLastBucketMode()
: getDateHistogramForEntireTimerangeMode();
? overwriteDateHistogramForLastBucketMode()
: overwriteDateHistogramForEntireTimerangeMode();

return next(doc);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
* Side Public License, v 1.
*/

import { get } from 'lodash';
import { overwrite } from '../../helpers';
import { getBucketSize } from '../../helpers/get_bucket_size';
import { bucketTransform } from '../../helpers/bucket_transform';
import { getIntervalAndTimefield } from '../../get_interval_and_timefield';
import { calculateAggRoot } from './calculate_agg_root';
import { UI_SETTINGS } from '../../../../../../data/common';

export function metricBuckets(req, panel, esQueryConfig, seriesIndex, capabilities, uiSettings) {
export function metricBuckets(req, panel) {
return (next) => async (doc) => {
const barTargetUiSettings = await uiSettings.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET);
const { interval } = getIntervalAndTimefield(panel, {}, seriesIndex);
const { intervalString } = getBucketSize(req, interval, capabilities, barTargetUiSettings);

panel.series.forEach((column) => {
const aggRoot = calculateAggRoot(doc, column);
column.metrics
Expand All @@ -27,7 +21,9 @@ export function metricBuckets(req, panel, esQueryConfig, seriesIndex, capabiliti
const fn = bucketTransform[metric.type];
if (fn) {
try {
const intervalString = get(doc, aggRoot.replace(/\.aggs$/, '.meta.intervalString'));
const bucket = fn(metric, column.metrics, intervalString);

overwrite(doc, `${aggRoot}.timeseries.aggs.${metric.id}`, bucket);
} catch (e) {
// meh
Expand Down
Loading

0 comments on commit 83b47c1

Please sign in to comment.