-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Move ensureDefaultIndexPattern into data plugin #63100
Merged
sulemanof
merged 14 commits into
elastic:master
from
sulemanof:np/move_ensure_index_pattern
Apr 24, 2020
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2716e33
Move ensure_default_index_pattern into data plugin
sulemanof 6d0a4e0
Update docs to include ensureDefaultIndexPattern
sulemanof 3bfa875
Fix translations
sulemanof 9519da7
Merge branch 'master' into np/move_ensure_index_pattern
elasticmachine 78f4c97
Merge remote-tracking branch 'upstream/master' into np/move_ensure_in…
sulemanof d0532d7
Move helper into index_patterns service
sulemanof 1df31de
Update docs
sulemanof e1a0cf7
Merge branch 'np/move_ensure_index_pattern' of github.com:sulemanof/k…
sulemanof 64ba52f
Remove mock
sulemanof 162d8d7
Merge branch 'master' into np/move_ensure_index_pattern
elasticmachine 095edcf
Merge remote-tracking branch 'upstream/master' into np/move_ensure_in…
sulemanof a7d2a20
Merge branch 'np/move_ensure_index_pattern' of github.com:sulemanof/k…
sulemanof 16ad08a
Add mock
sulemanof 14cb75a
Merge remote-tracking branch 'upstream/master' into np/move_ensure_in…
sulemanof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/plugins/data/public/ui/ensure_default_index_pattern/ensure_default_index_pattern.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { contains } from 'lodash'; | ||
import React from 'react'; | ||
import { History } from 'history'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiCallOut } from '@elastic/eui'; | ||
import { CoreStart } from 'kibana/public'; | ||
import { toMountPoint } from '../../../../kibana_react/public'; | ||
import { IndexPatternsContract } from '../../index_patterns'; | ||
|
||
export type EnsureDefaultIndexPattern = (history: History) => Promise<unknown> | undefined; | ||
|
||
export const createEnsureDefaultIndexPattern = ( | ||
core: CoreStart, | ||
indexPatterns: IndexPatternsContract | ||
) => { | ||
let bannerId: string; | ||
let timeoutId: NodeJS.Timeout | undefined; | ||
|
||
/** | ||
* Checks whether a default index pattern is set and exists and defines | ||
* one otherwise. | ||
* | ||
* If there are no index patterns, redirect to management page and show | ||
* banner. In this case the promise returned from this function will never | ||
* resolve to wait for the URL change to happen. | ||
*/ | ||
return async function ensureDefaultIndexPattern(history: History) { | ||
const patterns = await indexPatterns.getIds(); | ||
let defaultId = core.uiSettings.get('defaultIndex'); | ||
let defined = !!defaultId; | ||
const exists = contains(patterns, defaultId); | ||
|
||
if (defined && !exists) { | ||
core.uiSettings.remove('defaultIndex'); | ||
defaultId = defined = false; | ||
} | ||
|
||
if (defined) { | ||
return; | ||
} | ||
|
||
// If there is any index pattern created, set the first as default | ||
if (patterns.length >= 1) { | ||
defaultId = patterns[0]; | ||
core.uiSettings.set('defaultIndex', defaultId); | ||
} else { | ||
const canManageIndexPatterns = core.application.capabilities.management.kibana.index_patterns; | ||
const redirectTarget = canManageIndexPatterns ? '/management/kibana/index_pattern' : '/home'; | ||
|
||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
} | ||
|
||
// Avoid being hostile to new users who don't have an index pattern setup yet | ||
// give them a friendly info message instead of a terse error message | ||
bannerId = core.overlays.banners.replace( | ||
bannerId, | ||
toMountPoint( | ||
<EuiCallOut | ||
color="warning" | ||
iconType="iInCircle" | ||
title={i18n.translate('data.ensureDefaultIndexPattern.bannerLabel', { | ||
defaultMessage: | ||
"In order to visualize and explore data in Kibana, you'll need to create an index pattern to retrieve data from Elasticsearch.", | ||
})} | ||
/> | ||
) | ||
); | ||
|
||
// hide the message after the user has had a chance to acknowledge it -- so it doesn't permanently stick around | ||
timeoutId = setTimeout(() => { | ||
core.overlays.banners.remove(bannerId); | ||
timeoutId = undefined; | ||
}, 15000); | ||
|
||
history.push(redirectTarget); | ||
|
||
// return never-resolving promise to stop resolving and wait for the url change | ||
return new Promise(() => {}); | ||
} | ||
}; | ||
}; |
23 changes: 23 additions & 0 deletions
23
src/plugins/data/public/ui/ensure_default_index_pattern/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { | ||
createEnsureDefaultIndexPattern, | ||
EnsureDefaultIndexPattern, | ||
} from './ensure_default_index_pattern'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should not be exported from here.
It's not really a UI component, and the naming makes it extra unclear.
It looks more like it should be a
useDefaultIndexPattern
hook, that is exported fromkibana_react
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be the best way to implement this (except the thing about showing the warning banner, it would be disappeared right after a plugin is destroyed),
but unfortunately, right now it's only used across angular driven plugins and I guess there is no way to integrate this pattern well there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lizozom What do you think about exporting this from
IndexPatternsService
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the ergonomics of this would be better. The only problem is that the way this helper is currently written (with
bannerId
andtimeoutId
being stateful), means we really have no choice but to put it somewhere in the runtime contract of thedata
plugin, askibana_utils
andkibana_react
are only imported statically.I don't have strong feelings on whether it goes in
ui
orIndexPatternsService
, but since it contains state that needs to be shared across apps, it seems those are our only options outside of refactoring the function altogether and storing that state elsewhere (e.g. sessionStorage).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what would be the final decision?
Should it stay in
ui
or moved intoIndexPatternsService
, or even postponed in respect of #57401 ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If @lizozom doesn't like the idea of putting it in
ui
, then IMHO it's fine to put it onIndexPatternsService
. She should be available tomorrow and can hopefully chime in if she has any other ideas.I don't think we should wait on that issue to fix this bug. That discussion is still in early stages & I don't know when the actual implementation will be prioritized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sulemanof
I think moving it to the index pattern service is better.
It's not an actual UI component.
Thanks!