Skip to content

Commit

Permalink
Use SavedObjectsClient for Courier Index Pattern (#12719)
Browse files Browse the repository at this point in the history
* Index pattern is created, by default, with a random ID by Elasticsearch
* Updated all references requiring the pattern itself to use indexPattern.title
* Advanced options toggle added to index pattern creation page to provide a specified ID
* If an index pattern does not exist, the user is given a link to create a pattern with the referenced ID.
  • Loading branch information
tylersmalley authored Jul 11, 2017
1 parent dd80489 commit 0d502ae
Show file tree
Hide file tree
Showing 46 changed files with 449 additions and 367 deletions.
2 changes: 1 addition & 1 deletion src/core_plugins/kibana/public/dashboard/panel/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

<div ng-if="error" class="load-error">
<span aria-hidden="true" class="kuiIcon fa-exclamation-triangle"></span>
<span ng-bind="error"></span>
<span ng-bind-html="error | markdown"></span>
</div>

<visualize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'ui/private';
import 'plugins/kibana/discover/components/field_chooser/field_chooser';
import FixturesHitsProvider from 'fixtures/hits';
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
import { SavedObject } from 'ui/saved_objects';

// Load the kibana app dependencies.

Expand Down Expand Up @@ -70,7 +71,11 @@ describe('discover field chooser directives', function () {
beforeEach(ngMock.inject(function (Private) {
hits = Private(FixturesHitsProvider);
indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider);
indexPatternList = [ 'b', 'a', 'c' ];
indexPatternList = [
new SavedObject(undefined, { id: '0', attributes: { title: 'b' } }),
new SavedObject(undefined, { id: '1', attributes: { title: 'a' } }),
new SavedObject(undefined, { id: '2', attributes: { title: 'c' } })
];

const fieldCounts = _.transform(hits, function (counts, hit) {
_.keys(indexPattern.flattenHit(hit)).forEach(function (key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
class="index-pattern-selection"
ng-model="selectedIndexPattern"
on-select="setIndexPattern($item)"
ng-init="selectedIndexPattern = indexPattern.id"
ng-init="selectedIndexPattern = indexPattern"
>
<ui-select-match>
{{$select.selected}}
{{$select.selected.title}}
</ui-select-match>
<ui-select-choices repeat="id in indexPatternList | filter:$select.search | orderBy">
<div ng-bind-html="id | highlight: $select.search"></div>
<ui-select-choices repeat="pattern in indexPatternList | filter:$select.search">
<div ng-bind-html="pattern.get('title') | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
</div>
Expand All @@ -20,8 +20,9 @@
class="index-pattern-label"
id="index_pattern_id"
tabindex="0"
css-truncate
>{{ indexPattern.id }}</h2>
css-truncate>

{{ indexPattern.title }}</h2>
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { uiModules } from 'ui/modules';
import fieldChooserTemplate from 'plugins/kibana/discover/components/field_chooser/field_chooser.html';
const app = uiModules.get('apps/discover');



app.directive('discFieldChooser', function ($location, globalState, config, $route, Private) {
const FieldList = Private(IndexPatternsFieldListProvider);

Expand All @@ -32,8 +30,9 @@ app.directive('discFieldChooser', function ($location, globalState, config, $rou
},
template: fieldChooserTemplate,
link: function ($scope) {
$scope.setIndexPattern = function (id) {
$scope.state.index = id;
$scope.indexPatternList = _.sortBy($scope.indexPatternList, o => o.get('title'));
$scope.setIndexPattern = function (pattern) {
$scope.state.index = pattern.id;
$scope.state.save();
};

Expand Down
21 changes: 13 additions & 8 deletions src/core_plugins/kibana/public/discover/controllers/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { uiModules } from 'ui/modules';
import indexTemplate from 'plugins/kibana/discover/index.html';
import { StateProvider } from 'ui/state_management/state';
import { documentationLinks } from 'ui/documentation_links/documentation_links';
import { SavedObjectsClientProvider } from 'ui/saved_objects';

const app = uiModules.get('apps/discover', [
'kibana/notify',
Expand All @@ -46,8 +47,14 @@ uiRoutes
resolve: {
ip: function (Promise, courier, config, $location, Private) {
const State = Private(StateProvider);
return courier.indexPatterns.getIds()
.then(function (list) {
const savedObjectsClient = Private(SavedObjectsClientProvider);

return savedObjectsClient.find({
type: 'index-pattern',
fields: ['title'],
perPage: 10000
})
.then(({ savedObjects }) => {
/**
* In making the indexPattern modifiable it was placed in appState. Unfortunately,
* the load order of AppState conflicts with the load order of many other things
Expand All @@ -60,12 +67,12 @@ uiRoutes
const state = new State('_a', {});

const specified = !!state.index;
const exists = _.contains(list, state.index);
const exists = _.findIndex(savedObjects, o => o.id === state.index) > -1;
const id = exists ? state.index : config.get('defaultIndex');
state.destroy();

return Promise.props({
list: list,
list: savedObjects,
loaded: courier.indexPatterns.get(id),
stateVal: state.index,
stateValFound: specified && exists
Expand Down Expand Up @@ -225,7 +232,7 @@ function discoverController(
const body = await searchSource.getSearchRequestBody();
return {
searchRequest: {
index: searchSource.get('index').id,
index: searchSource.get('index').title,
body
},
fields: selectFields,
Expand Down Expand Up @@ -266,8 +273,6 @@ function discoverController(
$scope.opts = {
// number of records to fetch, then paginate through
sampleSize: config.get('discover:sampleSize'),
// Index to match
index: $scope.indexPattern.id,
timefield: $scope.indexPattern.timeFieldName,
savedSearch: savedSearch,
indexPatternList: $route.current.locals.ip.list,
Expand Down Expand Up @@ -691,7 +696,7 @@ function discoverController(

if (own && !stateVal) return own;
if (stateVal && !stateValFound) {
const err = '"' + stateVal + '" is not a configured pattern. ';
const err = '"' + stateVal + '" is not a configured pattern ID. ';
if (own) {
notify.warning(err + ' Using the saved index pattern: "' + own.id + '"');
return own;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('createIndexPattern UI', () => {
current: {
params: {},
locals: {
indexPatternIds: []
indexPatterns: []
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@
class="kuiVerticalRhythm"
ng-submit="controller.createIndexPattern()"
>

<!-- Index pattern input -->
<div class="kuiVerticalRhythm">
<label
class="kuiLabel kuiVerticalRhythmSmall"
translate="KIBANA-INDEX_NAME_OR_PATTERN"
></label>
<label class="kuiLabel kuiVerticalRhythmSmall">
<span translate="KIBANA-INDEX_PATTERN"></span>

<small>
<a
class="kuiLink"
ng-click="controller.toggleAdvancedIndexOptions();"
translate="KIBANA-ADVANCED_OPTIONS"
></a>
</small>
</label>

<div class="kuiVerticalRhythm kuiVerticalRhythmSmall">
<input
Expand Down Expand Up @@ -66,6 +74,35 @@
</div>
</div>

<!-- Index pattern id input -->
<div class="kuiVerticalRhythm" ng-if="controller.showAdvancedOptions">
<label
class="kuiLabel kuiVerticalRhythmSmall"
translate="KIBANA-INDEX_PATTERN_ID">
</label>

<div class="kuiVerticalRhythm kuiVerticalRhythmSmall">
<input
class="kuiTextInput kuiTextInput--large"
data-test-subj="createIndexPatternIdInput"
ng-model="controller.formValues.id"
ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }"
validate-index-name
allow-wildcard
name="id"
type="text"
>
</div>

<!-- ID help text -->
<div class="kuiVerticalRhythm">
<p
class="kuiSubText kuiVerticalRhythmSmall"
translate="KIBANA-INDEX_PATTERN_SPECIFY_ID"
></p>
</div>
</div>

<!-- Time field select -->
<div class="kuiVerticalRhythm">
<label class="kuiLabel kuiVerticalRhythmSmall">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import _ from 'lodash';
import { IndexPatternMissingIndices } from 'ui/errors';
import 'ui/directives/validate_index_name';
import 'ui/directives/auto_select_if_only_one';
import { RefreshKibanaIndex } from '../refresh_kibana_index';
import uiRoutes from 'ui/routes';
import { uiModules } from 'ui/modules';
import template from './create_index_pattern.html';
Expand All @@ -15,13 +14,24 @@ uiRoutes
});

uiModules.get('apps/management')
.controller('managementIndicesCreate', function ($scope, kbnUrl, Private, Notifier, indexPatterns, es, config, Promise, $translate) {
.controller('managementIndicesCreate', function (
$scope,
$routeParams,
kbnUrl,
Private,
Notifier,
indexPatterns,
es,
config,
Promise,
$translate
) {
const notify = new Notifier();
const refreshKibanaIndex = Private(RefreshKibanaIndex);
let loadingCount = 0;

// Configure the new index pattern we're going to create.
this.formValues = {
id: $routeParams.id ? decodeURIComponent($routeParams.id) : undefined,
name: config.get('indexPattern:placeholder'),
expandWildcard: false,
timeFieldOption: null,
Expand All @@ -30,6 +40,7 @@ uiModules.get('apps/management')
// UI state.
this.timeFieldOptions = [];
this.timeFieldOptionsError = null;
this.showAdvancedOptions = $routeParams.id || false;

const getTimeFieldOptions = () => {
loadingCount += 1;
Expand Down Expand Up @@ -191,14 +202,17 @@ uiModules.get('apps/management')
});
};

this.toggleAdvancedIndexOptions = () => {
this.showAdvancedOptions = !!!this.showAdvancedOptions;
};

this.createIndexPattern = () => {
const {
id,
name,
timeFieldOption,
} = this.formValues;

const id = name;

const timeFieldName = timeFieldOption
? timeFieldOption.fieldName
: undefined;
Expand All @@ -210,24 +224,23 @@ uiModules.get('apps/management')
loadingCount += 1;
sendCreateIndexPatternRequest(indexPatterns, {
id,
name,
timeFieldName,
notExpandable,
}).then(createdId => {
if (!createdId) {
return;
}

refreshKibanaIndex().then(() => {
if (!config.get('defaultIndex')) {
config.set('defaultIndex', id);
}
if (!config.get('defaultIndex')) {
config.set('defaultIndex', createdId);
}

indexPatterns.cache.clear(id);
kbnUrl.change(`/management/kibana/indices/${id}`);
indexPatterns.cache.clear(createdId);
kbnUrl.change(`/management/kibana/indices/${createdId}`);

// force loading while kbnUrl.change takes effect
loadingCount = Infinity;
});
// force loading while kbnUrl.change takes effect
loadingCount = Infinity;
}).catch(err => {
if (err instanceof IndexPatternMissingIndices) {
return notify.error($translate.instant('KIBANA-NO_INDICES_MATCHING_PATTERN'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export function sendCreateIndexPatternRequest(indexPatterns, {
id,
name,
timeFieldName,
notExpandable,
}) {
// get an empty indexPattern to start
return indexPatterns.get()
.then(indexPattern => {
// set both the id and title to the same value
indexPattern.id = indexPattern.title = id;

Object.assign(indexPattern, {
id,
title: name,
timeFieldName,
notExpandable,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</p>

<p class="kuiText kuiVerticalRhythm">
This page lists every field in the <strong>{{::indexPattern.id}}</strong>
This page lists every field in the <strong>{{::indexPattern.title}}</strong>
index and the field's associated core type as recorded by Elasticsearch.
While this list allows you to view the core type of each field, changing
field types must be done using Elasticsearch's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import './indexed_fields_table';
import './scripted_fields_table';
import './scripted_field_editor';
import './source_filters_table';
import { RefreshKibanaIndex } from '../refresh_kibana_index';
import { KbnUrlProvider } from 'ui/url';
import { IndicesEditSectionsProvider } from './edit_sections';
import uiRoutes from 'ui/routes';
Expand Down Expand Up @@ -44,12 +43,14 @@ uiModules.get('apps/management')
$scope, $location, $route, config, courier, Notifier, Private, AppState, docTitle, confirmModal) {
const notify = new Notifier();
const $state = $scope.state = new AppState();
const refreshKibanaIndex = Private(RefreshKibanaIndex);

$scope.kbnUrl = Private(KbnUrlProvider);
$scope.indexPattern = $route.current.locals.indexPattern;
docTitle.change($scope.indexPattern.id);
const otherIds = _.without($route.current.locals.indexPatternIds, $scope.indexPattern.id);

const otherPatterns = _.filter($route.current.locals.indexPatterns, pattern => {
return pattern.id !== $scope.indexPattern.id;
});

$scope.$watch('indexPattern.fields', function () {
$scope.editSections = Private(IndicesEditSectionsProvider)($scope.indexPattern);
Expand Down Expand Up @@ -104,13 +105,13 @@ uiModules.get('apps/management')
function doRemove() {
if ($scope.indexPattern.id === config.get('defaultIndex')) {
config.remove('defaultIndex');
if (otherIds.length) {
config.set('defaultIndex', otherIds[0]);

if (otherPatterns.length) {
config.set('defaultIndex', otherPatterns[0].id);
}
}

courier.indexPatterns.delete($scope.indexPattern)
.then(refreshKibanaIndex)
.then(function () {
$location.url('/management/kibana/index');
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ng-if="defaultIndex === indexPattern.id"
class="kuiIcon fa-star"
></span>
{{indexPattern.id}}
{{indexPattern.title}}
</h1>
</div>

Expand Down
Loading

0 comments on commit 0d502ae

Please sign in to comment.