Fixes #12602 - Change TSVB Fields API to use fieldCaps API (#12611)

* Fixes #12602 - Change TSVB Fields API to use fieldCaps API

* Removing obsolete test

* updating tests

* removing the extra sortBy
This commit is contained in:
Chris Cowan 2017-07-24 09:25:08 -07:00 committed by GitHub
parent 37fc9495c6
commit 190cd5f3f6
6 changed files with 25 additions and 140 deletions

View file

@ -6,12 +6,7 @@ describe('generateByTypeFilter()', () => {
describe('numeric', () => {
const fn = generateByTypeFilter('numeric');
[
'scaled_float',
'half_float',
'integer',
'float',
'long',
'double'
'number'
].forEach((type) => {
it(`should return true for ${type}`, () => expect(fn({ type })).to.equal(true));
});
@ -34,12 +29,7 @@ describe('generateByTypeFilter()', () => {
describe('all', () => {
const fn = generateByTypeFilter('all');
[
'scaled_float',
'half_float',
'integer',
'float',
'long',
'double',
'number',
'string',
'text',
'keyword',

View file

@ -4,12 +4,7 @@ export default function byType(type) {
switch (type) {
case 'numeric':
return _.includes([
'scaled_float',
'half_float',
'integer',
'float',
'long',
'double'
'number'
], field.type);
case 'string':
return _.includes([

View file

@ -1,84 +0,0 @@
import { expect } from 'chai';
import { getParams, handleResponse } from '../get_fields';
describe('getFields', () => {
describe('getParams', () => {
it('returns a valid params object', () => {
const req = { query: { index: 'metricbeat-*' } };
expect(getParams(req)).to.eql({
index: 'metricbeat-*',
fields: ['*'],
ignoreUnavailable: false,
allowNoIndices: false,
includeDefaults: true
});
});
});
describe('handleResponse', () => {
it('returns a valid response', () => {
const resp = {
'foo': {
'mappings': {
'bar': {
'@timestamp': {
'full_name': '@timestamp',
'mapping': {
'@timestamp': {
'type': 'date'
}
}
}
}
}
},
'twitter': {
'mappings': {
'tweet': {
'message': {
'full_name': 'message',
'mapping': {
'message': {
'type': 'text',
'fields': {
'keyword': {
'type': 'keyword',
'ignore_above': 256
}
}
}
}
},
'@timestamp': {
'full_name': '@timestamp',
'mapping': {
'@timestamp': {
'type': 'date'
}
}
},
'id.keyword': {
'full_name': 'id.keyword',
'mapping': {
'keyword': {
'type': 'keyword',
'ignore_above': 256
}
}
}
}
}
}
};
expect(handleResponse(resp)).to.eql([
{ name: '@timestamp', type: 'date' },
{ name: 'id.keyword', type: 'keyword' },
{ name: 'message', type: 'text' }
]);
});
});
});

View file

@ -1,40 +1,9 @@
import _ from 'lodash';
import { uniq } from 'lodash';
export function getParams(req) {
export async function getFields(req) {
const { indexPatternsService } = req.pre;
const index = req.query.index || '*';
return {
index,
fields: ['*'],
ignoreUnavailable: false,
allowNoIndices: false,
includeDefaults: true
};
const resp = await indexPatternsService.getFieldsForWildcard({ pattern: index });
const fields = resp.filter(field => field.aggregatable);
return uniq(fields, field => field.name);
}
export function handleResponse(resp) {
return _.reduce(resp, (acc, index) => {
_.each(index.mappings, (type) => {
_.each(type, (field, fullName) => {
const name = _.last(fullName.split(/\./));
const enabled = _.get(field, `mapping.${name}.enabled`, true);
const fieldType = _.get(field, `mapping.${name}.type`);
if (enabled && fieldType) {
acc.push({
name: _.get(field, 'full_name', fullName),
type: fieldType
});
}
});
});
return _(acc).sortBy('name').uniq(row => row.name).value();
}, []);
}
function getFields(req) {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data');
const params = getParams(req);
return callWithRequest(req, 'indices.getFieldMapping', params).then(handleResponse);
}
export default getFields;

View file

@ -0,0 +1,11 @@
import { IndexPatternsService } from '../../../../server/index_patterns/service';
export const getIndexPatternService = {
assign: 'indexPatternsService',
method(req, reply) {
const dataCluster = req.server.plugins.elasticsearch.getCluster('data');
const callDataCluster = (...args) => {
return dataCluster.callWithRequest(req, ...args);
};
reply(new IndexPatternsService(callDataCluster));
}
};

View file

@ -1,7 +1,11 @@
import getFields from '../lib/get_fields';
import { getFields } from '../lib/get_fields';
import { getIndexPatternService } from '../lib/get_index_pattern_service';
export default (server) => {
server.route({
config: {
pre: [getIndexPatternService]
},
path: '/api/metrics/fields',
method: 'GET',
handler: (req, reply) => {