mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
Improve logic for dashboard duplication title computation (#185056)
## Summary follow up to https://github.com/elastic/kibana/pull/184777#discussion_r1631353032 Previously, if a user has cloned a dashboard N times, in the worst case that the next duplication attempt is initiated from the initial dashboard N network calls would be required to find a unique title for the next uniquely titled clone. This update short circuits that process by querying for the last created dashboard, getting it's duplicationId, increment it and proposing it as the next dashboard duplicate title, with this approach there's still a chance we might suggest a dashboard title that's been claimed if the user modifies the dashboard title to a higher duplication Id after creation, especially that we are querying for the last created and this is situation we can't correct for, the resolution for the appropriate title in that case would be on the user. ## How to test - Create couple of duplicated dashboards, the titles should follow the normal increment progression ie. n+1 - Create some that skip the expected pattern by any increment of choice, on attempting to duplicate this dashboard the suggested title should be previous + 1 - Choose to duplicate any previously created dashboard the suggested title should be the increment of the last duplicated dashboard ### Checklist <!-- Delete any items that are not applicable to this PR. --> <!-- - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials --> - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios <!-- - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [ ] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### Risk Matrix Delete this section if it is not applicable to this PR. Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release. When forming the risk matrix, consider some of the following examples and how they may potentially impact the change: | Risk | Probability | Severity | Mitigation/Notes | |---------------------------|-------------|----------|-------------------------| | Multiple Spaces—unexpected behavior in non-default Kibana Space. | Low | High | Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces. | | Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. | High | Low | Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure. | | Code should gracefully handle cases when feature X or plugin Y are disabled. | Medium | High | Unit tests will verify that any feature flag or plugin combination still results in our service operational. | | [See more potential risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) | ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --> --------- Co-authored-by: Catherine Liu <catherine.liu@elastic.co>
This commit is contained in:
parent
946a255bc5
commit
641a71bb68
4 changed files with 177 additions and 20 deletions
|
@ -275,21 +275,18 @@ export async function runInteractiveSave(this: DashboardContainer, interactionMo
|
|||
|
||||
if (lastSavedId) {
|
||||
const [baseTitle, baseCount] = extractTitleAndCount(newTitle);
|
||||
let copyCount = baseCount + 1;
|
||||
newTitle = `${baseTitle} (${copyCount})`;
|
||||
|
||||
// increment count until we find a unique title
|
||||
while (
|
||||
!(await checkForDuplicateDashboardTitle({
|
||||
title: newTitle,
|
||||
lastSavedTitle: currentState.title,
|
||||
copyOnSave: true,
|
||||
isTitleDuplicateConfirmed: false,
|
||||
}))
|
||||
) {
|
||||
copyCount++;
|
||||
newTitle = `${baseTitle} (${copyCount})`;
|
||||
}
|
||||
newTitle = `${baseTitle} (${baseCount + 1})`;
|
||||
|
||||
await checkForDuplicateDashboardTitle({
|
||||
title: newTitle,
|
||||
lastSavedTitle: currentState.title,
|
||||
copyOnSave: true,
|
||||
isTitleDuplicateConfirmed: false,
|
||||
onTitleDuplicate(speculativeSuggestion) {
|
||||
newTitle = speculativeSuggestion;
|
||||
},
|
||||
});
|
||||
|
||||
switch (interactionMode) {
|
||||
case ViewMode.EDIT: {
|
||||
|
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0 and the Server Side Public License, v 1; you may not use this file except
|
||||
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
||||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ContentClient } from '@kbn/content-management-plugin/public';
|
||||
import { checkForDuplicateDashboardTitle } from './check_for_duplicate_dashboard_title';
|
||||
import { extractTitleAndCount } from '../../../dashboard_container/embeddable/api/lib/extract_title_and_count';
|
||||
|
||||
type ContentManagementStart = Parameters<typeof checkForDuplicateDashboardTitle>[1];
|
||||
|
||||
describe('checkForDuplicateDashboardTitle', () => {
|
||||
const mockedContentManagementClient = {
|
||||
search: jest.fn(),
|
||||
} as unknown as ContentClient;
|
||||
|
||||
const newTitle = 'Shiny dashboard (1)';
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('will only search using the dashboard basename', async () => {
|
||||
const [baseDashboardName] = extractTitleAndCount(newTitle);
|
||||
|
||||
const pageResults = [
|
||||
{
|
||||
attributes: {
|
||||
title: baseDashboardName,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
(
|
||||
mockedContentManagementClient.search as jest.MockedFunction<ContentClient['search']>
|
||||
).mockImplementationOnce(() =>
|
||||
Promise.resolve({
|
||||
hits: pageResults,
|
||||
pagination: {
|
||||
total: pageResults.length,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
await checkForDuplicateDashboardTitle(
|
||||
{
|
||||
title: newTitle,
|
||||
lastSavedTitle: baseDashboardName,
|
||||
copyOnSave: true,
|
||||
isTitleDuplicateConfirmed: false,
|
||||
},
|
||||
{ client: mockedContentManagementClient } as ContentManagementStart
|
||||
);
|
||||
|
||||
expect(mockedContentManagementClient.search).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
query: expect.objectContaining({
|
||||
text: `${baseDashboardName}*`,
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('invokes onTitleDuplicate with a speculative collision free value when the new title provided is a duplicate match', async () => {
|
||||
const [baseDashboardName] = extractTitleAndCount(newTitle);
|
||||
|
||||
const userTitleInput = `${baseDashboardName} (10)`;
|
||||
|
||||
const pageResults = [
|
||||
{
|
||||
attributes: {
|
||||
title: baseDashboardName,
|
||||
},
|
||||
},
|
||||
].concat(
|
||||
Array.from(new Array(5)).map((_, idx) => ({
|
||||
attributes: {
|
||||
title: `${baseDashboardName} (${10 + idx})`,
|
||||
},
|
||||
}))
|
||||
);
|
||||
|
||||
const onTitleDuplicate = jest.fn();
|
||||
|
||||
(
|
||||
mockedContentManagementClient.search as jest.MockedFunction<ContentClient['search']>
|
||||
).mockImplementationOnce(() =>
|
||||
Promise.resolve({
|
||||
hits: pageResults,
|
||||
pagination: {
|
||||
total: pageResults.length,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
await checkForDuplicateDashboardTitle(
|
||||
{
|
||||
title: userTitleInput,
|
||||
lastSavedTitle: baseDashboardName,
|
||||
copyOnSave: true,
|
||||
isTitleDuplicateConfirmed: false,
|
||||
onTitleDuplicate,
|
||||
},
|
||||
{ client: mockedContentManagementClient } as ContentManagementStart
|
||||
);
|
||||
|
||||
expect(mockedContentManagementClient.search).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
query: expect.objectContaining({
|
||||
text: 'Shiny dashboard*',
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
expect(onTitleDuplicate).toHaveBeenCalledWith(`${baseDashboardName} (15)`);
|
||||
});
|
||||
});
|
|
@ -9,12 +9,16 @@
|
|||
import { DashboardStartDependencies } from '../../../plugin';
|
||||
import { DASHBOARD_CONTENT_ID } from '../../../dashboard_constants';
|
||||
import { DashboardCrudTypes } from '../../../../common/content_management';
|
||||
import { extractTitleAndCount } from '../../../dashboard_container/embeddable/api/lib/extract_title_and_count';
|
||||
|
||||
export interface DashboardDuplicateTitleCheckProps {
|
||||
title: string;
|
||||
copyOnSave: boolean;
|
||||
lastSavedTitle: string;
|
||||
onTitleDuplicate?: () => void;
|
||||
/**
|
||||
* invokes the onTitleDuplicate function if provided with a speculative title that should be collision free
|
||||
*/
|
||||
onTitleDuplicate?: (speculativeSuggestion: string) => void;
|
||||
isTitleDuplicateConfirmed: boolean;
|
||||
}
|
||||
|
||||
|
@ -33,6 +37,11 @@ export async function checkForDuplicateDashboardTitle(
|
|||
}: DashboardDuplicateTitleCheckProps,
|
||||
contentManagement: DashboardStartDependencies['contentManagement']
|
||||
): Promise<boolean> {
|
||||
// Don't check if the title is an empty string
|
||||
if (!title) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Don't check for duplicates if user has already confirmed save with duplicate title
|
||||
if (isTitleDuplicateConfirmed) {
|
||||
return true;
|
||||
|
@ -44,21 +53,37 @@ export async function checkForDuplicateDashboardTitle(
|
|||
return true;
|
||||
}
|
||||
|
||||
const [baseDashboardName] = extractTitleAndCount(title);
|
||||
|
||||
const { hits } = await contentManagement.client.search<
|
||||
DashboardCrudTypes['SearchIn'],
|
||||
DashboardCrudTypes['SearchOut']
|
||||
>({
|
||||
contentTypeId: DASHBOARD_CONTENT_ID,
|
||||
query: {
|
||||
text: title ? `${title}*` : undefined,
|
||||
limit: 10,
|
||||
text: `${baseDashboardName}*`,
|
||||
limit: 20,
|
||||
},
|
||||
options: {
|
||||
onlyTitle: true,
|
||||
},
|
||||
options: { onlyTitle: true },
|
||||
});
|
||||
const duplicate = hits.find((hit) => hit.attributes.title.toLowerCase() === title.toLowerCase());
|
||||
|
||||
const duplicate = Boolean(
|
||||
hits.find((hit) => hit.attributes.title.toLowerCase() === title.toLowerCase())
|
||||
);
|
||||
|
||||
if (!duplicate) {
|
||||
return true;
|
||||
}
|
||||
onTitleDuplicate?.();
|
||||
|
||||
const [largestDuplicationId] = hits
|
||||
.map((hit) => extractTitleAndCount(hit.attributes.title)[1])
|
||||
.sort((a, b) => b - a);
|
||||
|
||||
const speculativeCollisionFreeTitle = `${baseDashboardName} (${largestDuplicationId + 1})`;
|
||||
|
||||
onTitleDuplicate?.(speculativeCollisionFreeTitle);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -49,5 +49,20 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
|
|||
await PageObjects.dashboard.gotoDashboardLandingPage();
|
||||
await listingTable.searchAndExpectItemsCount('dashboard', `${dashboardName} (2)`, 1);
|
||||
});
|
||||
|
||||
it('Clone should always increment from the last duplicated dashboard with a unique title', async function () {
|
||||
await PageObjects.dashboard.loadSavedDashboard(clonedDashboardName);
|
||||
// force dashboard duplicate id to increment out of logical progression bounds
|
||||
await PageObjects.dashboard.duplicateDashboard(`${dashboardName} (20)`);
|
||||
await PageObjects.dashboard.gotoDashboardLandingPage();
|
||||
await listingTable.searchAndExpectItemsCount('dashboard', `${dashboardName} (20)`, 1);
|
||||
// load dashboard with duplication id 1
|
||||
await PageObjects.dashboard.loadSavedDashboard(clonedDashboardName);
|
||||
// run normal clone
|
||||
await PageObjects.dashboard.duplicateDashboard();
|
||||
await PageObjects.dashboard.gotoDashboardLandingPage();
|
||||
// clone gets duplication id, that picks off from last duplicated dashboard
|
||||
await listingTable.searchAndExpectItemsCount('dashboard', `${dashboardName} (21)`, 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue