Merge pull request #5434 from epixa/5432-format-field-stats

Send epoch_millis format with field stats constraints
This commit is contained in:
Joe Fleming 2015-11-18 14:22:45 -07:00
commit 8c7b7e54bb
2 changed files with 45 additions and 8 deletions

View file

@ -12,11 +12,13 @@ describe('ui/index_patterns/_calculate_indices', () => {
let response;
let config;
let constraints;
let indices;
beforeEach(ngMock.module('kibana', ($provide) => {
response = {
indices: {
'mock-*': 'irrelevant, is ignored'
'mock-*': { fields: { '@something': {} } },
'ignore-*': { fields: {} }
}
};
@ -37,7 +39,9 @@ describe('ui/index_patterns/_calculate_indices', () => {
}));
function run({ start = undefined, stop = undefined } = {}) {
calculateIndices('wat-*-no', '@something', start, stop);
calculateIndices('wat-*-no', '@something', start, stop).then(value => {
indices = value;
});
$rootScope.$apply();
config = _.first(es.fieldStats.lastCall.args);
constraints = config.body.index_constraints;
@ -72,6 +76,9 @@ describe('ui/index_patterns/_calculate_indices', () => {
it('max_value is set to original if not a moment object', () => {
expect(constraints['@something'].max_value.gte).to.equal('1234567890');
});
it('max_value format is set to epoch_millis', () => {
expect(constraints['@something'].max_value.format).to.equal('epoch_millis');
});
it('max_value is set to moment.valueOf if given a moment object', () => {
const start = moment();
run({ start });
@ -90,6 +97,9 @@ describe('ui/index_patterns/_calculate_indices', () => {
it('min_value is set to original if not a moment object', () => {
expect(constraints['@something'].min_value.lte).to.equal('1234567890');
});
it('min_value format is set to epoch_millis', () => {
expect(constraints['@something'].min_value.format).to.equal('epoch_millis');
});
it('max_value is set to moment.valueOf if given a moment object', () => {
const stop = moment();
run({ stop });
@ -98,6 +108,14 @@ describe('ui/index_patterns/_calculate_indices', () => {
});
});
describe('response filtering', () => {
it('filters out any indices that have empty fields', () => {
run();
expect(_.includes(indices, 'mock-*')).to.be(true);
expect(_.includes(indices, 'ignore-*')).to.be(false);
});
});
describe('response sorting', function () {
require('testUtils/noDigestPromises').activateForSuite();

View file

@ -7,6 +7,23 @@ define(function (require) {
return moment.isMoment(val) ? val.valueOf() : val;
}
// returns a properly formatted millisecond timestamp index constraint
function msConstraint(comparison, value) {
return {
[comparison]: timeValue(value),
format: 'epoch_millis'
};
}
// returns a new object with any indexes removed that do not include the
// time field
//
// fixme: this really seems like a bug that needs to be fixed in
// elasticsearch itself, but this workaround will do for now
function omitIndicesWithoutTimeField(indices, timeFieldName) {
return _.pick(indices, index => index.fields[timeFieldName]);
}
return function CalculateIndicesFactory(Promise, es) {
// Uses the field stats api to determine the names of indices that need to
@ -14,7 +31,9 @@ define(function (require) {
// given time range
function calculateIndices(pattern, timeFieldName, start, stop, sortDirection) {
return getFieldStats(pattern, timeFieldName, start, stop)
.then(resp => sortIndexStats(resp, timeFieldName, sortDirection));
.then(resp => resp.indices)
.then(indices => omitIndicesWithoutTimeField(indices, timeFieldName))
.then(indices => sortIndexStats(indices, timeFieldName, sortDirection));
};
// creates the configuration hash that must be passed to the elasticsearch
@ -22,10 +41,10 @@ define(function (require) {
function getFieldStats(pattern, timeFieldName, start, stop) {
const constraints = {};
if (start) {
constraints.max_value = { gte: timeValue(start) };
constraints.max_value = msConstraint('gte', start);
}
if (stop) {
constraints.min_value = { lte: timeValue(stop) };
constraints.min_value = msConstraint('lte', stop);
}
return es.fieldStats({
@ -40,14 +59,14 @@ define(function (require) {
});
}
function sortIndexStats(resp, timeFieldName, sortDirection) {
if (!sortDirection) return _.keys(resp.indices);
function sortIndexStats(indices, timeFieldName, sortDirection) {
if (!sortDirection) return _.keys(indices);
// FIXME: Once https://github.com/elastic/elasticsearch/issues/14404 is closed
// this should be sorting based on the sortable value of a field.
const edgeKey = sortDirection === 'desc' ? 'max_value' : 'min_value';
return _(resp.indices)
return _(indices)
.map((stats, index) => (
{ index, edge: stats.fields[timeFieldName][edgeKey] }
))