Skip to content

Commit

Permalink
[5.x] Backport #12243 and #12250 (#12531)
Browse files Browse the repository at this point in the history
* [uiSettings] make service request based (#12243)

* [server/uiSettings] make uiSettings service request based

* [server/uiSettings] disambiguate UiSettings/Service

* [server/uiSettings] link to PR in removal error

* [server/uiSettings] await _read before hydrating

* [server/uiSettings] focus tests, remove server integration

* [server/uiSettings] add tests for readInterceptor() arg

* [server/uiSettings] add server integration tests

* [server/uiExports] fix replaceInjectedVars tests

* [server/uiSettings] convert all methods to use async/await

* [uiSettings/serviceFactory] fix doc block

* [uiSettings/service] fix doc block

* [uiSettings/tests/callClusterStub] stop tracking state needlessly

* [uiSettings/tests] remove invalid tests and pointless promise helpers

* [uiSettings/forRequest] fix typo

* [uiSettings/tests] remove mixture of arrow and function expressions

* [uiSettings/tests/callClusterStub] leverage sinon.calledWithExactly

* [uiSettings/mixin/tests] add exception for eslint import/no-duplicates

* [uiSettings/mixin/tests] wrap single args in parens

(cherry picked from commit 65d6b5d)

* [uiSettings] support defining settings with uiExports (#12250)

* [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

(cherry picked from commit 7bec60c)
  • Loading branch information
spalger authored Jun 28, 2017
1 parent d7c4ee8 commit cb674df
Show file tree
Hide file tree
Showing 26 changed files with 1,016 additions and 693 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 @@ -12,6 +12,7 @@ import scripts from './server/routes/api/scripts';
import { registerSuggestionsApi } from './server/routes/api/suggestions';
import * as systemApi from './server/lib/system_api';
import mappings from './mappings.json';
import { getUiSettingDefaults } from './ui_setting_defaults';

const mkdirp = Promise.promisify(mkdirpNode);

Expand Down Expand Up @@ -130,7 +131,9 @@ module.exports = 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
Expand Up @@ -6,11 +6,12 @@ export default function registerDelete(server) {
method: 'DELETE',
handler: function (req, reply) {
const { key } = req.params;
const uiSettings = server.uiSettings();
const uiSettings = req.getUiSettingsService();

uiSettings
.remove(req, key)
.remove(key)
.then(() => uiSettings
.getUserProvided(req)
.getUserProvided()
.then(settings => reply({ settings }).type('application/json'))
)
.catch(err => reply(Boom.wrap(err, err.statusCode)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default function registerGet(server) {
path: '/api/kibana/settings',
method: 'GET',
handler: function (req, reply) {
server
.uiSettings()
.getUserProvided(req)
req
.getUiSettingsService()
.getUserProvided()
.then(settings => reply({ settings }).type('application/json'))
.catch(err => reply(Boom.wrap(err, err.statusCode)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export default function registerSet(server) {
handler: function (req, reply) {
const { key } = req.params;
const { value } = req.payload;
const uiSettings = server.uiSettings();
const uiSettings = req.getUiSettingsService();

uiSettings
.set(req, key, value)
.set(key, value)
.then(() => uiSettings
.getUserProvided(req)
.getUserProvided()
.then(settings => reply({ settings }).type('application/json'))
)
.catch(err => reply(Boom.wrap(err, err.statusCode)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ export default function registerSet(server) {
method: 'POST',
handler: function (req, reply) {
const { changes } = req.payload;
const uiSettings = server.uiSettings();
const uiSettings = req.getUiSettingsService();

uiSettings
.setMany(req, changes)
.setMany(changes)
.then(() => uiSettings
.getUserProvided(req)
.getUserProvided()
.then(settings => reply({ settings }).type('application/json'))
)
.catch(err => reply(Boom.wrap(err, err.statusCode)));
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 @@ module.exports = 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
2 changes: 1 addition & 1 deletion src/core_plugins/timelion/server/routes/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = (server) => {
path: '/api/timelion/run',
handler: async (request, reply) => {
try {
const uiSettings = await server.uiSettings().getAll(request);
const uiSettings = await request.getUiSettingsService().getAll();

const tlConfig = require('../handlers/lib/tl_config.js')({
server,
Expand Down
3 changes: 1 addition & 2 deletions src/core_plugins/timelion/server/routes/validate_es.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module.exports = function (server) {
method: 'GET',
path: '/api/timelion/validate/es',
handler: function (request, reply) {
return server.uiSettings().getAll(request).then((uiSettings) => {

return request.getUiSettingsService().getAll().then((uiSettings) => {
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');

const timefield = uiSettings['timelion:es.timefield'];
Expand Down
4 changes: 2 additions & 2 deletions src/functional_test_runner/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ async function run() {
const failureCount = await functionalTestRunner.run();
process.exitCode = failureCount ? 1 : 0;
} catch (err) {
// await teardown(err);
await teardown(err);
} finally {
// await teardown();
await teardown();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ module.exports = async function (kbnServer, server, config) {
const url = await shortUrlLookup.getUrl(request.params.urlId, request);
shortUrlAssertValid(url);

const uiSettings = server.uiSettings();
const stateStoreInSessionStorage = await uiSettings.get(request, 'state:storeInSessionStorage');
const uiSettings = request.getUiSettingsService();
const stateStoreInSessionStorage = await uiSettings.get('state:storeInSessionStorage');
if (!stateStoreInSessionStorage) {
reply().redirect(config.get('server.basePath') + url);
return;
Expand Down
4 changes: 0 additions & 4 deletions src/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import pluginsCheckEnabledMixin from './plugins/check_enabled';
import pluginsCheckVersionMixin from './plugins/check_version';
import configCompleteMixin from './config/complete';
import uiMixin from '../ui';
import { uiSettingsMixin } from '../ui';
import optimizeMixin from '../optimize';
import pluginsInitializeMixin from './plugins/initialize';
import { indexPatternsMixin } from './index_patterns';
Expand Down Expand Up @@ -63,9 +62,6 @@ module.exports = 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
2 changes: 1 addition & 1 deletion src/ui/__tests__/ui_exports_replace_injected_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('UiExports', function () {

await kbnServer.ready();
kbnServer.status.get('ui settings').state = 'green';
kbnServer.server.decorate('server', 'uiSettings', () => {
kbnServer.server.decorate('request', 'getUiSettingsService', () => {
return { getDefaults: noop, getUserProvided: noop };
});
});
Expand Down
8 changes: 5 additions & 3 deletions src/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,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 Expand Up @@ -67,7 +69,7 @@ export default async (kbnServer, server, config) => {
});

async function getKibanaPayload({ app, request, includeUserProvidedConfig, injectedVarsOverrides }) {
const uiSettings = server.uiSettings();
const uiSettings = request.getUiSettingsService();
const translations = await uiI18n.getTranslationsForRequest(request);

return {
Expand All @@ -83,7 +85,7 @@ export default async (kbnServer, server, config) => {
translations: translations,
uiSettings: await props({
defaults: uiSettings.getDefaults(),
user: includeUserProvidedConfig && uiSettings.getUserProvided(request)
user: includeUserProvidedConfig && uiSettings.getUserProvided()
}),
vars: await reduceAsync(
uiExports.injectedVarsReplacers,
Expand Down
Loading

0 comments on commit cb674df

Please sign in to comment.