[TSVB] Add params._interval to mathjs (#14944)

* [TSVB] Add params._interval to mathjs

* Fixing tests
This commit is contained in:
Chris Cowan 2017-11-27 11:23:51 -07:00 committed by GitHub
parent 0d92568229
commit a258f1af44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 12 deletions

View file

@ -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
are keys on the <code>params</code> object, i.e. <code>params.&lt;name&gt;</code> To access all the data use
<code>params._all.&lt;name&gt;.values</code> for an array of the values and <code>params._all.&lt;name&gt;.timestamps</code>
for an array of the timestamps. <code>params._timestamp</code> is available for the current bucket&apos;s timestamp
and <code>params._index</code> is available for the current bucket&apos;s index.
for an array of the timestamps. <code>params._timestamp</code> is available for the current bucket&apos;s timestamp,
<code>params._index</code> is available for the current bucket&apos;s index, and <code>params._interval</code>s available
for the interval in milliseconds.
</div>
</div>
</div>

View file

@ -8,7 +8,8 @@ describe('getSplits(resp, panel, series)', () => {
aggregations: {
SERIES: {
timeseries: { buckets: [] },
SIBAGG: { value: 1 }
SIBAGG: { value: 1 },
meta: { bucketSize: 10 }
}
}
};
@ -26,6 +27,7 @@ describe('getSplits(resp, panel, series)', () => {
{
id: 'SERIES',
label: 'Overall Average of Average of cpu',
meta: { bucketSize: 10 },
color: '#FF0000',
timeseries: { buckets: [] },
SIBAGG: { value: 1 }
@ -48,7 +50,8 @@ describe('getSplits(resp, panel, series)', () => {
timeseries: { buckets: [] },
SIBAGG: { value: 2 }
}
]
],
meta: { bucketSize: 10 }
}
}
};
@ -69,6 +72,7 @@ describe('getSplits(resp, panel, series)', () => {
id: 'SERIES:example-01',
key: 'example-01',
label: 'example-01',
meta: { bucketSize: 10 },
color: '#FF0000',
timeseries: { buckets: [] },
SIBAGG: { value: 1 }
@ -77,6 +81,7 @@ describe('getSplits(resp, panel, series)', () => {
id: 'SERIES:example-02',
key: 'example-02',
label: 'example-02',
meta: { bucketSize: 10 },
color: '#FF0000',
timeseries: { buckets: [] },
SIBAGG: { value: 2 }
@ -99,7 +104,8 @@ describe('getSplits(resp, panel, series)', () => {
timeseries: { buckets: [] },
SIBAGG: { value: 2 }
}
]
],
meta: { bucketSize: 10 }
}
}
};
@ -120,6 +126,7 @@ describe('getSplits(resp, panel, series)', () => {
id: 'SERIES:example-01',
key: 'example-01',
label: 'example-01',
meta: { bucketSize: 10 },
color: '#FF0000',
timeseries: { buckets: [] },
SIBAGG: { value: 1 }
@ -128,6 +135,7 @@ describe('getSplits(resp, panel, series)', () => {
id: 'SERIES:example-02',
key: 'example-02',
label: 'example-02',
meta: { bucketSize: 10 },
color: '#930000',
timeseries: { buckets: [] },
SIBAGG: { value: 2 }
@ -146,7 +154,8 @@ describe('getSplits(resp, panel, series)', () => {
'filter-2': {
timeseries: { buckets: [] },
}
}
},
meta: { bucketSize: 10 }
}
}
};
@ -168,6 +177,7 @@ describe('getSplits(resp, panel, series)', () => {
id: 'SERIES:filter-1',
key: 'filter-1',
label: '200s',
meta: { bucketSize: 10 },
color: '#F00',
timeseries: { buckets: [] },
},
@ -175,6 +185,7 @@ describe('getSplits(resp, panel, series)', () => {
id: 'SERIES:filter-2',
key: 'filter-2',
label: '300s',
meta: { bucketSize: 10 },
color: '#0F0',
timeseries: { buckets: [] },
}

View file

@ -5,6 +5,7 @@ import getLastMetric from './get_last_metric';
import getSplitColors from './get_split_colors';
import { formatKey } from './format_key';
export default function getSplits(resp, panel, series) {
const meta = _.get(resp, `aggregations.${series.id}.meta`);
const color = new Color(series.color);
const metric = getLastMetric(series);
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.label = formatKey(bucket.key, series);
bucket.color = panel.type === 'top_n' ? color.hex() : colors.shift();
bucket.meta = meta;
return bucket;
});
}
@ -27,6 +29,7 @@ export default function getSplits(resp, panel, series) {
bucket.key = filter.id;
bucket.color = filter.color;
bucket.label = filter.label || filter.filter || '*';
bucket.meta = meta;
return bucket;
});
}
@ -46,7 +49,8 @@ export default function getSplits(resp, panel, series) {
id: series.id,
label: series.label || calculateLabel(metric, series.metrics),
color: color.hex(),
...mergeObj
...mergeObj,
meta
}
];
}

View file

@ -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'
}
}
}

View file

@ -5,7 +5,7 @@ import { set } from 'lodash';
export default function dateHistogram(req, panel, series) {
return next => doc => {
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 { timezone: time_zone } = req.payload.timerange;
@ -19,6 +19,11 @@ export default function dateHistogram(req, panel, series) {
max: to.valueOf()
}
});
set(doc, `aggs.${series.id}.meta`, {
timeField,
intervalString,
bucketSize
});
return next(doc);
};
}

View file

@ -1,4 +1,4 @@
import _ from 'lodash';
import { set } from 'lodash';
import getBucketSize from '../../helpers/get_bucket_size';
import getIntervalAndTimefield from '../../get_interval_and_timefield';
import getTimerange from '../../helpers/get_timerange';
@ -7,11 +7,11 @@ import { calculateAggRoot } from './calculate_agg_root';
export default function dateHistogram(req, panel) {
return next => doc => {
const { timeField, interval } = getIntervalAndTimefield(panel);
const { intervalString } = getBucketSize(req, interval);
const { bucketSize, intervalString } = getBucketSize(req, interval);
const { from, to } = getTimerange(req);
panel.series.forEach(column => {
const aggRoot = calculateAggRoot(doc, column);
_.set(doc, `${aggRoot}.timeseries.date_histogram`, {
set(doc, `${aggRoot}.timeseries.date_histogram`, {
field: timeField,
interval: intervalString,
min_doc_count: 0,
@ -20,6 +20,11 @@ export default function dateHistogram(req, panel) {
max: to.valueOf()
}
});
set(doc, aggRoot.replace(/\.aggs$/, '.meta'), {
timeField,
intervalString,
bucketSize
});
});
return next(doc);
};

View file

@ -83,7 +83,15 @@ export function mathAgg(resp, panel, series) {
const someNull = values(params).some(v => v == null);
if (someNull) return [ts, null];
// 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 (typeof result === 'object') return [ts, last(flatten(result.valueOf()))];
return [ts, result];

View file

@ -99,6 +99,11 @@ describe('buildRequestBody(req)', () => {
'filter': {
'match_all': {}
},
'meta': {
'timeField': '@timestamp',
'bucketSize': 10,
'intervalString': '10s'
},
'aggs': {
'timeseries': {
'date_histogram': {