Skip to content

Commit

Permalink
[uiSettings] support defining settings with uiExports (#12250)
Browse files Browse the repository at this point in the history
* [uiSettings] add `uiSettingDefaults` export type

* [uiSettings/uiExportsConsumer] ensure that there are not conflicts

* use `sinon.assert.calledWithExactly()`

* describe the UiExportsConsumer class

* make uiExport consumers filtering intention more clear

* fix typo

* fix typos

* add note about why getDefaults() is a function
  • Loading branch information
spalger authored Jun 13, 2017
1 parent dedef14 commit 7bec60c
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 149 deletions.
5 changes: 4 additions & 1 deletion src/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { registerSuggestionsApi } from './server/routes/api/suggestions';
import * as systemApi from './server/lib/system_api';
import handleEsError from './server/lib/handle_es_error';
import mappings from './mappings.json';
import { getUiSettingDefaults } from './ui_setting_defaults';

import { injectVars } from './inject_vars';

Expand Down Expand Up @@ -106,7 +107,9 @@ export default function (kibana) {
translations: [
resolve(__dirname, './translations/en.json')
],
mappings

mappings,
uiSettingDefaults: getUiSettingDefaults(),
},

preInit: async function (server) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import moment from 'moment-timezone';

export function getDefaultSettings() {
export function getUiSettingDefaults() {
const weekdays = moment.weekdays().slice();
const [defaultWeekday] = weekdays;

Expand Down Expand Up @@ -283,47 +283,6 @@ export function getDefaultSettings() {
value: 2000,
description: 'The maximum number of buckets a single datasource can return'
},
// Timelion stuff
'timelion:showTutorial': {
value: false,
description: 'Should I show the tutorial by default when entering the timelion app?'
},
'timelion:es.timefield': {
value: '@timestamp',
description: 'Default field containing a timestamp when using .es()'
},
'timelion:es.default_index': {
value: '_all',
description: 'Default elasticsearch index to search with .es()'
},
'timelion:target_buckets': {
value: 200,
description: 'The number of buckets to shoot for when using auto intervals'
},
'timelion:max_buckets': {
value: 2000,
description: 'The maximum number of buckets a single datasource can return'
},
'timelion:default_columns': {
value: 2,
description: 'Number of columns on a timelion sheet by default'
},
'timelion:default_rows': {
value: 2,
description: 'Number of rows on a timelion sheet by default'
},
'timelion:min_interval': {
value: '1ms',
description: 'The smallest interval that will be calculated when using "auto"'
},
'timelion:graphite.url': {
value: 'https://www.hostedgraphite.com/UID/ACCESS_KEY/graphite',
description: '<em>[experimental]</em> The URL of your graphite host'
},
'timelion:quandl.key': {
value: 'someKeyHere',
description: '<em>[experimental]</em> Your API key from www.quandl.com'
},
'state:storeInSessionStorage': {
value: false,
description: 'The URL can sometimes grow to be too large for some browsers to ' +
Expand Down
7 changes: 5 additions & 2 deletions src/core_plugins/tests_bundle/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { union } from 'lodash';
import { getDefaultSettings } from '../../ui/ui_settings/defaults';
import findSourceFiles from './find_source_files';
import { fromRoot } from '../../utils';

Expand Down Expand Up @@ -57,7 +56,11 @@ export default (kibana) => {
});
}

env.defaultUiSettings = getDefaultSettings();
env.defaultUiSettings = plugins.kbnServer.uiExports.consumers
// find the first uiExportsConsumer that has a getUiSettingDefaults method
// See src/ui/ui_settings/ui_exports_consumer.js
.find(consumer => typeof consumer.getUiSettingDefaults === 'function')
.getUiSettingDefaults();

return new UiBundle({
id: 'tests',
Expand Down
45 changes: 44 additions & 1 deletion src/core_plugins/timelion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,50 @@ export default function (kibana) {
visTypes: [
'plugins/timelion/vis'
],
mappings: require('./mappings.json')
mappings: require('./mappings.json'),

uiSettingDefaults: {
'timelion:showTutorial': {
value: false,
description: 'Should I show the tutorial by default when entering the timelion app?'
},
'timelion:es.timefield': {
value: '@timestamp',
description: 'Default field containing a timestamp when using .es()'
},
'timelion:es.default_index': {
value: '_all',
description: 'Default elasticsearch index to search with .es()'
},
'timelion:target_buckets': {
value: 200,
description: 'The number of buckets to shoot for when using auto intervals'
},
'timelion:max_buckets': {
value: 2000,
description: 'The maximum number of buckets a single datasource can return'
},
'timelion:default_columns': {
value: 2,
description: 'Number of columns on a timelion sheet by default'
},
'timelion:default_rows': {
value: 2,
description: 'Number of rows on a timelion sheet by default'
},
'timelion:min_interval': {
value: '1ms',
description: 'The smallest interval that will be calculated when using "auto"'
},
'timelion:graphite.url': {
value: 'https://www.hostedgraphite.com/UID/ACCESS_KEY/graphite',
description: '<em>[experimental]</em> The URL of your graphite host'
},
'timelion:quandl.key': {
value: 'someKeyHere',
description: '<em>[experimental]</em> Your API key from www.quandl.com'
}
}
},
init: require('./init.js'),
});
Expand Down
5 changes: 1 addition & 4 deletions src/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import pluginsScanMixin from './plugins/scan';
import pluginsCheckEnabledMixin from './plugins/check_enabled';
import pluginsCheckVersionMixin from './plugins/check_version';
import configCompleteMixin from './config/complete';
import uiMixin, { uiSettingsMixin } from '../ui';
import uiMixin from '../ui';
import optimizeMixin from '../optimize';
import pluginsInitializeMixin from './plugins/initialize';
import { indexPatternsMixin } from './index_patterns';
Expand Down Expand Up @@ -66,9 +66,6 @@ export default class KbnServer {
// setup saved object routes
savedObjectsMixin,

// setup server.uiSettings
uiSettingsMixin,

// ensure that all bundles are built, or that the
// lazy bundle server is running
optimizeMixin,
Expand Down
4 changes: 3 additions & 1 deletion src/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import UiBundleCollection from './ui_bundle_collection';
import UiBundlerEnv from './ui_bundler_env';
import { UiI18n } from './ui_i18n';

export { uiSettingsMixin } from './ui_settings';
import { uiSettingsMixin } from './ui_settings';

export default async (kbnServer, server, config) => {
const uiExports = kbnServer.uiExports = new UiExports({
urlBasePath: config.get('server.basePath')
});

await kbnServer.mixin(uiSettingsMixin);

const uiI18n = kbnServer.uiI18n = new UiI18n(config.get('i18n.defaultLocale'));
uiI18n.addUiExportConsumer(uiExports);

Expand Down
17 changes: 13 additions & 4 deletions src/ui/ui_settings/__tests__/ui_settings_mixin_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('uiSettingsMixin()', () => {
const kbnServer = {
server,
config,
uiExports: { addConsumer: sinon.stub() },
status: new ServerStatus(server),
ready: sinon.stub().returns(readyPromise),
};
Expand Down Expand Up @@ -133,9 +134,14 @@ describe('uiSettingsMixin()', () => {

sandbox.stub(uiSettingsServiceFactoryNS, 'uiSettingsServiceFactory');
sinon.assert.notCalled(uiSettingsServiceFactory);
const football = {};
decorations.server.uiSettingsServiceFactory(football);
sinon.assert.calledWith(uiSettingsServiceFactory, server, football);
decorations.server.uiSettingsServiceFactory({
foo: 'bar'
});
sinon.assert.calledOnce(uiSettingsServiceFactory);
sinon.assert.calledWithExactly(uiSettingsServiceFactory, server, {
foo: 'bar',
getDefaults: sinon.match.func,
});
});
});

Expand Down Expand Up @@ -168,7 +174,10 @@ describe('uiSettingsMixin()', () => {
sandbox.stub(getUiSettingsServiceForRequestNS, 'getUiSettingsServiceForRequest');
decorations.request.getUiSettingsService();

const readInterceptor = getUiSettingsServiceForRequest.firstCall.args[2];
const options = getUiSettingsServiceForRequest.firstCall.args[2];
expect(options).to.have.property('readInterceptor');

const { readInterceptor } = options;
expect(readInterceptor).to.be.a('function');

status.green();
Expand Down
Loading

0 comments on commit 7bec60c

Please sign in to comment.