Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[utils] add util for converting between es and kibana types #11967

Merged
merged 12 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import angular from 'angular';
import rison from 'rison-node';
import { savedObjectManagementRegistry } from 'plugins/kibana/management/saved_object_registry';
import objectViewHTML from 'plugins/kibana/management/sections/objects/_view.html';
import { IndexPatternsCastMappingTypeProvider } from 'ui/index_patterns/_cast_mapping_type';
import uiRoutes from 'ui/routes';
import { uiModules } from 'ui/modules';
import { castEsToKbnFieldTypeName } from '../../../../../../utils';

uiRoutes
.when('/management/kibana/objects/:service/:id', {
Expand All @@ -16,9 +16,8 @@ uiModules.get('apps/management')
.directive('kbnManagementObjectsView', function (kbnIndex, Notifier, confirmModal) {
return {
restrict: 'E',
controller: function ($scope, $injector, $routeParams, $location, $window, $rootScope, esAdmin, Private) {
controller: function ($scope, $injector, $routeParams, $location, $window, $rootScope, esAdmin) {
const notify = new Notifier({ location: 'SavedObject view' });
const castMappingType = Private(IndexPatternsCastMappingTypeProvider);
const serviceObj = savedObjectManagementRegistry.get($routeParams.service);
const service = $injector.get(serviceObj.service);

Expand Down Expand Up @@ -81,7 +80,7 @@ uiModules.get('apps/management')
fields.push({
name: name,
type: (function () {
switch (castMappingType(esType)) {
switch (castEsToKbnFieldTypeName(esType)) {
case 'string': return 'text';
case 'number': return 'number';
case 'boolean': return 'boolean';
Expand Down
38 changes: 22 additions & 16 deletions src/fixtures/logstash_fields.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import { castEsToKbnFieldTypeName } from '../utils';

function stubbedLogstashFields() {
return [
// |indexed
// | |analyzed
// | | |aggregatable
// | | | |searchable
// name type | | | | |metadata
['bytes', 'number', true, true, true, true, { count: 10, docValues: true } ],
// name esType | | | | |metadata
['bytes', 'long', true, true, true, true, { count: 10, docValues: true } ],
['ssl', 'boolean', true, true, true, true, { count: 20 } ],
['@timestamp', 'date', true, true, true, true, { count: 30 } ],
['time', 'date', true, true, true, true, { count: 30 } ],
['@tags', 'string', true, true, true, true ],
['@tags', 'keyword', true, true, true, true ],
['utc_time', 'date', true, true, true, true ],
['phpmemory', 'number', true, true, true, true ],
['phpmemory', 'integer', true, true, true, true ],
['ip', 'ip', true, true, true, true ],
['request_body', 'attachment', true, true, true, true ],
['point', 'geo_point', true, true, true, true ],
['area', 'geo_shape', true, true, true, true ],
['hashed', 'murmur3', true, true, false, true ],
['geo.coordinates', 'geo_point', true, true, true, true ],
['extension', 'string', true, true, true, true ],
['machine.os', 'string', true, true, true, true ],
['machine.os.raw', 'string', true, false, true, true, { docValues: true } ],
['geo.src', 'string', true, true, true, true ],
['_id', 'string', false, false, true, true ],
['_type', 'string', false, false, true, true ],
['_source', 'string', false, false, true, true ],
['non-filterable', 'string', false, false, true, false],
['non-sortable', 'string', false, false, false, false],
['extension', 'keyword', true, true, true, true ],
['machine.os', 'text', true, true, true, true ],
['machine.os.raw', 'keyword', true, false, true, true, { docValues: true } ],
['geo.src', 'keyword', true, true, true, true ],
['_id', 'keyword', false, false, true, true ],
['_type', 'keyword', false, false, true, true ],
['_source', 'keyword', false, false, true, true ],
['non-filterable', 'text', false, false, true, false],
['non-sortable', 'text', false, false, false, false],
['custom_user_field', 'conflict', false, false, true, true ],
['script string', 'string', false, false, true, false, { script: '\'i am a string\'' } ],
['script number', 'number', false, false, true, false, { script: '1234' } ],
['script string', 'text', false, false, true, false, { script: '\'i am a string\'' } ],
['script number', 'long', false, false, true, false, { script: '1234' } ],
['script date', 'date', false, false, true, false, { script: '1234', lang: 'painless' } ],
['script murmur3', 'murmur3', false, false, true, false, { script: '1234' } ],
].map(function (row) {
const [
name,
type,
esType,
indexed,
analyzed,
aggregatable,
Expand All @@ -51,6 +53,10 @@ function stubbedLogstashFields() {
scripted = !!script,
} = metadata;

// the conflict type is actually a kbnFieldType, we
// don't have any other way to represent it here
const type = esType === 'conflict' ? esType : castEsToKbnFieldTypeName(esType);

return {
name,
type,
Expand Down
23 changes: 13 additions & 10 deletions src/fixtures/stubbed_logstash_index_pattern.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import _ from 'lodash';
import TestUtilsStubIndexPatternProvider from 'test_utils/stub_index_pattern';
import { IndexPatternsFieldTypesProvider } from 'ui/index_patterns/_field_types';
import FixturesLogstashFieldsProvider from 'fixtures/logstash_fields';
import { getKbnFieldType } from '../utils';

export default function stubbedLogstashIndexPatternService(Private) {
const StubIndexPattern = Private(TestUtilsStubIndexPatternProvider);
const fieldTypes = Private(IndexPatternsFieldTypesProvider);
const mockLogstashFields = Private(FixturesLogstashFieldsProvider);


const fields = mockLogstashFields.map(function (field) {
field.displayName = field.name;
const type = fieldTypes.byName[field.type];
if (!type) throw new TypeError('unknown type ' + field.type);
if (!_.has(field, 'sortable')) field.sortable = type.sortable;
if (!_.has(field, 'filterable')) field.filterable = type.filterable;
return field;
const kbnType = getKbnFieldType(field.type);

if (kbnType.name === 'unknown') {
throw new TypeError(`unknown type ${field.type}`);
}

return {
...field,
sortable: ('sortable' in field) ? !!field.sortable : kbnType.sortable,
filterable: ('filterable' in field) ? !!field.filterable : kbnType.filterable,
displayName: field.name,
};
});

const indexPattern = new StubIndexPattern('logstash-*', 'time', fields);
Expand Down
32 changes: 26 additions & 6 deletions src/ui/public/field_editor/__tests__/field_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ describe('FieldEditor directive', function () {
let $el;

let $httpBackend;
let getScriptedLangsResponse;

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function ($compile, $injector, Private) {
$httpBackend = $injector.get('$httpBackend');
$httpBackend
.when('GET', '/api/kibana/scripts/languages')
.respond(['expression', 'painless']);
getScriptedLangsResponse = $httpBackend.when('GET', '/api/kibana/scripts/languages');
getScriptedLangsResponse.respond(['expression', 'painless']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference in beforeEach: if it gets overridden in any test, it should always be handled within each test (so I only beforeEach something if it concerns all the tests it covers, otherwise I find it just becomes more difficult to track the state of things)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do something like this at first but it just spiraled out of control and changed way too much stuff.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, just skip it if it adds noise to this PR


$rootScope = $injector.get('$rootScope');
Field = Private(IndexPatternsFieldProvider);
Expand Down Expand Up @@ -153,12 +153,15 @@ describe('FieldEditor directive', function () {
expect(field.lang).to.be('expression');
});

it('provides lang options based on what is enabled for inline use in ES', function () {
it('limits lang options to "expression" and "painless"', function () {
getScriptedLangsResponse
.respond(['expression', 'painless', 'groovy']);

$httpBackend.flush();
expect(_.isEqual(editor.scriptingLangs, ['expression', 'painless'])).to.be.ok();
expect(editor.scriptingLangs).to.eql(['expression', 'painless']);
});

it('provides curated type options based on language', function () {
it('provides specific type when language is painless', function () {
$rootScope.$apply();
expect(editor.fieldTypes).to.have.length(1);
expect(editor.fieldTypes[0]).to.be('number');
Expand All @@ -170,6 +173,23 @@ describe('FieldEditor directive', function () {
expect(_.isEqual(editor.fieldTypes, ['number', 'string', 'date', 'boolean'])).to.be.ok();
});

it('provides all kibana types when language is groovy (only possible in 5.x)', function () {
$rootScope.$apply();
expect(editor.fieldTypes).to.have.length(1);
expect(editor.fieldTypes[0]).to.be('number');

editor.field.lang = 'groovy';
$rootScope.$apply();

expect(editor.fieldTypes).to.contain('number');
expect(editor.fieldTypes).to.contain('string');
expect(editor.fieldTypes).to.contain('geo_point');
expect(editor.fieldTypes).to.contain('ip');
expect(editor.fieldTypes).to.not.contain('text');
expect(editor.fieldTypes).to.not.contain('keyword');
expect(editor.fieldTypes).to.not.contain('attachement');
});

it('updates formatter options based on field type', function () {
field.lang = 'painless';

Expand Down
4 changes: 2 additions & 2 deletions src/ui/public/field_editor/field_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { RegistryFieldFormatsProvider } from 'ui/registry/field_formats';
import { IndexPatternsFieldProvider } from 'ui/index_patterns/_field';
import { uiModules } from 'ui/modules';
import fieldEditorTemplate from 'ui/field_editor/field_editor.html';
import { IndexPatternsCastMappingTypeProvider } from 'ui/index_patterns/_cast_mapping_type';
import { documentationLinks } from '../documentation_links/documentation_links';
import './field_editor.less';
import { GetEnabledScriptingLanguagesProvider, getSupportedScriptingLanguages } from '../scripting_languages';
import { getKbnTypeNames } from '../../../utils';

uiModules
.get('kibana', ['colorpicker.module'])
Expand All @@ -21,7 +21,7 @@ uiModules
const fieldTypesByLang = {
painless: ['number', 'string', 'date', 'boolean'],
expression: ['number'],
default: _.keys(Private(IndexPatternsCastMappingTypeProvider).types.byType)
default: getKbnTypeNames()
};

return {
Expand Down
68 changes: 0 additions & 68 deletions src/ui/public/index_patterns/__tests__/_cast_mapping_type.js

This file was deleted.

1 change: 0 additions & 1 deletion src/ui/public/index_patterns/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import './_index_pattern';
import './_cast_mapping_type';
import './_map_field';
import './_pattern_to_wildcard';
import './_get_computed_fields';
Expand Down
45 changes: 0 additions & 45 deletions src/ui/public/index_patterns/_cast_mapping_type.js

This file was deleted.

7 changes: 3 additions & 4 deletions src/ui/public/index_patterns/_field.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ObjDefine } from 'ui/utils/obj_define';
import { IndexPatternsFieldFormatProvider } from 'ui/index_patterns/_field_format/field_format';
import { IndexPatternsFieldTypesProvider } from 'ui/index_patterns/_field_types';
import { RegistryFieldFormatsProvider } from 'ui/registry/field_formats';
import { getKbnFieldType } from '../../../utils';

export function IndexPatternsFieldProvider(Private, shortDotsFilter, $rootScope, Notifier) {
const notify = new Notifier({ location: 'IndexPattern Field' });
const FieldFormat = Private(IndexPatternsFieldFormatProvider);
const fieldTypes = Private(IndexPatternsFieldTypesProvider);
const fieldFormats = Private(RegistryFieldFormatsProvider);

function Field(indexPattern, spec) {
Expand All @@ -23,7 +22,7 @@ export function IndexPatternsFieldProvider(Private, shortDotsFilter, $rootScope,
}

// find the type for this field, fallback to unknown type
let type = fieldTypes.byName[spec.type];
let type = getKbnFieldType(spec.type);
if (spec.type && !type) {
notify.error(
'Unknown field type "' + spec.type + '"' +
Expand All @@ -32,7 +31,7 @@ export function IndexPatternsFieldProvider(Private, shortDotsFilter, $rootScope,
);
}

if (!type) type = fieldTypes.byName.unknown;
if (!type) type = getKbnFieldType('unknown');

let format = spec.format;
if (!format || !(format instanceof FieldFormat)) {
Expand Down
24 changes: 0 additions & 24 deletions src/ui/public/index_patterns/_field_types.js

This file was deleted.

Loading