-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ensureDefaultIndexPattern into data plugin (#63100)
* Move ensure_default_index_pattern into data plugin * Update docs to include ensureDefaultIndexPattern * Fix translations * Move helper into index_patterns service * Update docs * Remove mock * Add mock Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
0dcefe8
commit 9bda6bd
Showing
16 changed files
with
133 additions
and
134 deletions.
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
1 change: 1 addition & 0 deletions
1
...terns/create_index_pattern_wizard/__snapshots__/create_index_pattern_wizard.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
98 changes: 98 additions & 0 deletions
98
src/plugins/data/public/index_patterns/index_patterns/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,98 @@ | ||
/* | ||
* 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) => { | ||
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(this: IndexPatternsContract, history: History) { | ||
const patterns = await this.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.indexPatterns.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(() => {}); | ||
} | ||
}; | ||
}; |
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
98 changes: 0 additions & 98 deletions
98
src/plugins/kibana_utils/public/history/ensure_default_index_pattern.tsx
This file was deleted.
Oops, something went wrong.
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
1 change: 1 addition & 0 deletions
1
...t/public/management_section/objects_table/__snapshots__/saved_objects_table.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.