[TSVB] Add params._interval to mathjs (#14944)
* [TSVB] Add params._interval to mathjs * Fixing tests
This commit is contained in:
parent
0d92568229
commit
a258f1af44
|
@ -69,8 +69,9 @@ class MathAgg extends Component {
|
||||||
This field uses basic math expresions (see <a href="http://mathjs.org/docs/expressions/syntax.html" target="_blank">MathJS</a>) - Variables
|
This field uses basic math expresions (see <a href="http://mathjs.org/docs/expressions/syntax.html" target="_blank">MathJS</a>) - Variables
|
||||||
are keys on the <code>params</code> object, i.e. <code>params.<name></code> To access all the data use
|
are keys on the <code>params</code> object, i.e. <code>params.<name></code> To access all the data use
|
||||||
<code>params._all.<name>.values</code> for an array of the values and <code>params._all.<name>.timestamps</code>
|
<code>params._all.<name>.values</code> for an array of the values and <code>params._all.<name>.timestamps</code>
|
||||||
for an array of the timestamps. <code>params._timestamp</code> is available for the current bucket's timestamp
|
for an array of the timestamps. <code>params._timestamp</code> is available for the current bucket's timestamp,
|
||||||
and <code>params._index</code> is available for the current bucket's index.
|
<code>params._index</code> is available for the current bucket's index, and <code>params._interval</code>s available
|
||||||
|
for the interval in milliseconds.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -8,7 +8,8 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
aggregations: {
|
aggregations: {
|
||||||
SERIES: {
|
SERIES: {
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
SIBAGG: { value: 1 }
|
SIBAGG: { value: 1 },
|
||||||
|
meta: { bucketSize: 10 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -26,6 +27,7 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
{
|
{
|
||||||
id: 'SERIES',
|
id: 'SERIES',
|
||||||
label: 'Overall Average of Average of cpu',
|
label: 'Overall Average of Average of cpu',
|
||||||
|
meta: { bucketSize: 10 },
|
||||||
color: '#FF0000',
|
color: '#FF0000',
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
SIBAGG: { value: 1 }
|
SIBAGG: { value: 1 }
|
||||||
|
@ -48,7 +50,8 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
SIBAGG: { value: 2 }
|
SIBAGG: { value: 2 }
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
meta: { bucketSize: 10 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -69,6 +72,7 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
id: 'SERIES:example-01',
|
id: 'SERIES:example-01',
|
||||||
key: 'example-01',
|
key: 'example-01',
|
||||||
label: 'example-01',
|
label: 'example-01',
|
||||||
|
meta: { bucketSize: 10 },
|
||||||
color: '#FF0000',
|
color: '#FF0000',
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
SIBAGG: { value: 1 }
|
SIBAGG: { value: 1 }
|
||||||
|
@ -77,6 +81,7 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
id: 'SERIES:example-02',
|
id: 'SERIES:example-02',
|
||||||
key: 'example-02',
|
key: 'example-02',
|
||||||
label: 'example-02',
|
label: 'example-02',
|
||||||
|
meta: { bucketSize: 10 },
|
||||||
color: '#FF0000',
|
color: '#FF0000',
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
SIBAGG: { value: 2 }
|
SIBAGG: { value: 2 }
|
||||||
|
@ -99,7 +104,8 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
SIBAGG: { value: 2 }
|
SIBAGG: { value: 2 }
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
meta: { bucketSize: 10 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -120,6 +126,7 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
id: 'SERIES:example-01',
|
id: 'SERIES:example-01',
|
||||||
key: 'example-01',
|
key: 'example-01',
|
||||||
label: 'example-01',
|
label: 'example-01',
|
||||||
|
meta: { bucketSize: 10 },
|
||||||
color: '#FF0000',
|
color: '#FF0000',
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
SIBAGG: { value: 1 }
|
SIBAGG: { value: 1 }
|
||||||
|
@ -128,6 +135,7 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
id: 'SERIES:example-02',
|
id: 'SERIES:example-02',
|
||||||
key: 'example-02',
|
key: 'example-02',
|
||||||
label: 'example-02',
|
label: 'example-02',
|
||||||
|
meta: { bucketSize: 10 },
|
||||||
color: '#930000',
|
color: '#930000',
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
SIBAGG: { value: 2 }
|
SIBAGG: { value: 2 }
|
||||||
|
@ -146,7 +154,8 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
'filter-2': {
|
'filter-2': {
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
meta: { bucketSize: 10 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -168,6 +177,7 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
id: 'SERIES:filter-1',
|
id: 'SERIES:filter-1',
|
||||||
key: 'filter-1',
|
key: 'filter-1',
|
||||||
label: '200s',
|
label: '200s',
|
||||||
|
meta: { bucketSize: 10 },
|
||||||
color: '#F00',
|
color: '#F00',
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
},
|
},
|
||||||
|
@ -175,6 +185,7 @@ describe('getSplits(resp, panel, series)', () => {
|
||||||
id: 'SERIES:filter-2',
|
id: 'SERIES:filter-2',
|
||||||
key: 'filter-2',
|
key: 'filter-2',
|
||||||
label: '300s',
|
label: '300s',
|
||||||
|
meta: { bucketSize: 10 },
|
||||||
color: '#0F0',
|
color: '#0F0',
|
||||||
timeseries: { buckets: [] },
|
timeseries: { buckets: [] },
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import getLastMetric from './get_last_metric';
|
||||||
import getSplitColors from './get_split_colors';
|
import getSplitColors from './get_split_colors';
|
||||||
import { formatKey } from './format_key';
|
import { formatKey } from './format_key';
|
||||||
export default function getSplits(resp, panel, series) {
|
export default function getSplits(resp, panel, series) {
|
||||||
|
const meta = _.get(resp, `aggregations.${series.id}.meta`);
|
||||||
const color = new Color(series.color);
|
const color = new Color(series.color);
|
||||||
const metric = getLastMetric(series);
|
const metric = getLastMetric(series);
|
||||||
if (_.has(resp, `aggregations.${series.id}.buckets`)) {
|
if (_.has(resp, `aggregations.${series.id}.buckets`)) {
|
||||||
|
@ -16,6 +17,7 @@ export default function getSplits(resp, panel, series) {
|
||||||
bucket.id = `${series.id}:${bucket.key}`;
|
bucket.id = `${series.id}:${bucket.key}`;
|
||||||
bucket.label = formatKey(bucket.key, series);
|
bucket.label = formatKey(bucket.key, series);
|
||||||
bucket.color = panel.type === 'top_n' ? color.hex() : colors.shift();
|
bucket.color = panel.type === 'top_n' ? color.hex() : colors.shift();
|
||||||
|
bucket.meta = meta;
|
||||||
return bucket;
|
return bucket;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -27,6 +29,7 @@ export default function getSplits(resp, panel, series) {
|
||||||
bucket.key = filter.id;
|
bucket.key = filter.id;
|
||||||
bucket.color = filter.color;
|
bucket.color = filter.color;
|
||||||
bucket.label = filter.label || filter.filter || '*';
|
bucket.label = filter.label || filter.filter || '*';
|
||||||
|
bucket.meta = meta;
|
||||||
return bucket;
|
return bucket;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -46,7 +49,8 @@ export default function getSplits(resp, panel, series) {
|
||||||
id: series.id,
|
id: series.id,
|
||||||
label: series.label || calculateLabel(metric, series.metrics),
|
label: series.label || calculateLabel(metric, series.metrics),
|
||||||
color: color.hex(),
|
color: color.hex(),
|
||||||
...mergeObj
|
...mergeObj,
|
||||||
|
meta
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,11 @@ describe('dateHistogram(req, panel, series)', () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
bucketSize: 10,
|
||||||
|
intervalString: '10s',
|
||||||
|
timeField: '@timestamp'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,6 +81,11 @@ describe('dateHistogram(req, panel, series)', () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
bucketSize: 10,
|
||||||
|
intervalString: '10s',
|
||||||
|
timeField: '@timestamp'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,6 +115,11 @@ describe('dateHistogram(req, panel, series)', () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
bucketSize: 20,
|
||||||
|
intervalString: '20s',
|
||||||
|
timeField: 'timestamp'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { set } from 'lodash';
|
||||||
export default function dateHistogram(req, panel, series) {
|
export default function dateHistogram(req, panel, series) {
|
||||||
return next => doc => {
|
return next => doc => {
|
||||||
const { timeField, interval } = getIntervalAndTimefield(panel, series);
|
const { timeField, interval } = getIntervalAndTimefield(panel, series);
|
||||||
const { intervalString } = getBucketSize(req, interval);
|
const { bucketSize, intervalString } = getBucketSize(req, interval);
|
||||||
const { from, to } = offsetTime(req, series.offset_time);
|
const { from, to } = offsetTime(req, series.offset_time);
|
||||||
const { timezone: time_zone } = req.payload.timerange;
|
const { timezone: time_zone } = req.payload.timerange;
|
||||||
|
|
||||||
|
@ -19,6 +19,11 @@ export default function dateHistogram(req, panel, series) {
|
||||||
max: to.valueOf()
|
max: to.valueOf()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
set(doc, `aggs.${series.id}.meta`, {
|
||||||
|
timeField,
|
||||||
|
intervalString,
|
||||||
|
bucketSize
|
||||||
|
});
|
||||||
return next(doc);
|
return next(doc);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import _ from 'lodash';
|
import { set } from 'lodash';
|
||||||
import getBucketSize from '../../helpers/get_bucket_size';
|
import getBucketSize from '../../helpers/get_bucket_size';
|
||||||
import getIntervalAndTimefield from '../../get_interval_and_timefield';
|
import getIntervalAndTimefield from '../../get_interval_and_timefield';
|
||||||
import getTimerange from '../../helpers/get_timerange';
|
import getTimerange from '../../helpers/get_timerange';
|
||||||
|
@ -7,11 +7,11 @@ import { calculateAggRoot } from './calculate_agg_root';
|
||||||
export default function dateHistogram(req, panel) {
|
export default function dateHistogram(req, panel) {
|
||||||
return next => doc => {
|
return next => doc => {
|
||||||
const { timeField, interval } = getIntervalAndTimefield(panel);
|
const { timeField, interval } = getIntervalAndTimefield(panel);
|
||||||
const { intervalString } = getBucketSize(req, interval);
|
const { bucketSize, intervalString } = getBucketSize(req, interval);
|
||||||
const { from, to } = getTimerange(req);
|
const { from, to } = getTimerange(req);
|
||||||
panel.series.forEach(column => {
|
panel.series.forEach(column => {
|
||||||
const aggRoot = calculateAggRoot(doc, column);
|
const aggRoot = calculateAggRoot(doc, column);
|
||||||
_.set(doc, `${aggRoot}.timeseries.date_histogram`, {
|
set(doc, `${aggRoot}.timeseries.date_histogram`, {
|
||||||
field: timeField,
|
field: timeField,
|
||||||
interval: intervalString,
|
interval: intervalString,
|
||||||
min_doc_count: 0,
|
min_doc_count: 0,
|
||||||
|
@ -20,6 +20,11 @@ export default function dateHistogram(req, panel) {
|
||||||
max: to.valueOf()
|
max: to.valueOf()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
set(doc, aggRoot.replace(/\.aggs$/, '.meta'), {
|
||||||
|
timeField,
|
||||||
|
intervalString,
|
||||||
|
bucketSize
|
||||||
|
});
|
||||||
});
|
});
|
||||||
return next(doc);
|
return next(doc);
|
||||||
};
|
};
|
||||||
|
|
|
@ -83,7 +83,15 @@ export function mathAgg(resp, panel, series) {
|
||||||
const someNull = values(params).some(v => v == null);
|
const someNull = values(params).some(v => v == null);
|
||||||
if (someNull) return [ts, null];
|
if (someNull) return [ts, null];
|
||||||
// calculate the result based on the user's script and return the value
|
// calculate the result based on the user's script and return the value
|
||||||
const result = limitedEval(mathMetric.script, { params: { ...params, _index: index, _timestamp: ts, _all: all } });
|
const result = limitedEval(mathMetric.script, {
|
||||||
|
params: {
|
||||||
|
...params,
|
||||||
|
_index: index,
|
||||||
|
_timestamp: ts,
|
||||||
|
_all: all,
|
||||||
|
_interval: split.meta.bucketSize * 1000
|
||||||
|
}
|
||||||
|
});
|
||||||
// if the result is an object (usually when the user is working with maps and functions) flatten the results and return the last value.
|
// if the result is an object (usually when the user is working with maps and functions) flatten the results and return the last value.
|
||||||
if (typeof result === 'object') return [ts, last(flatten(result.valueOf()))];
|
if (typeof result === 'object') return [ts, last(flatten(result.valueOf()))];
|
||||||
return [ts, result];
|
return [ts, result];
|
||||||
|
|
|
@ -99,6 +99,11 @@ describe('buildRequestBody(req)', () => {
|
||||||
'filter': {
|
'filter': {
|
||||||
'match_all': {}
|
'match_all': {}
|
||||||
},
|
},
|
||||||
|
'meta': {
|
||||||
|
'timeField': '@timestamp',
|
||||||
|
'bucketSize': 10,
|
||||||
|
'intervalString': '10s'
|
||||||
|
},
|
||||||
'aggs': {
|
'aggs': {
|
||||||
'timeseries': {
|
'timeseries': {
|
||||||
'date_histogram': {
|
'date_histogram': {
|
||||||
|
|
Loading…
Reference in a new issue