Adds find by value embeddables helper (#89629)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Corey Robertson 2021-01-29 13:38:18 -05:00 committed by GitHub
parent a6fe0a2de7
commit 2055cb96ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 0 deletions

View file

@ -25,3 +25,4 @@ export function plugin(initializerContext: PluginInitializerContext) {
}
export { DashboardPluginSetup, DashboardPluginStart } from './types';
export { findByValueEmbeddables } from './usage/find_by_value_embeddables';

View file

@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/
import { SavedDashboardPanel730ToLatest } from '../../common';
import { findByValueEmbeddables } from './find_by_value_embeddables';
const visualizationByValue = ({
embeddableConfig: {
value: 'visualization-by-value',
},
type: 'visualization',
} as unknown) as SavedDashboardPanel730ToLatest;
const mapByValue = ({
embeddableConfig: {
value: 'map-by-value',
},
type: 'map',
} as unknown) as SavedDashboardPanel730ToLatest;
const embeddableByRef = ({
panelRefName: 'panel_ref_1',
} as unknown) as SavedDashboardPanel730ToLatest;
describe('findByValueEmbeddables', () => {
it('finds the by value embeddables for the given type', async () => {
const savedObjectsResult = {
saved_objects: [
{
attributes: {
panelsJSON: JSON.stringify([visualizationByValue, mapByValue, embeddableByRef]),
},
},
{
attributes: {
panelsJSON: JSON.stringify([embeddableByRef, mapByValue, visualizationByValue]),
},
},
],
};
const savedObjectClient = { find: jest.fn().mockResolvedValue(savedObjectsResult) };
const maps = await findByValueEmbeddables(savedObjectClient, 'map');
expect(maps.length).toBe(2);
expect(maps[0]).toEqual(mapByValue.embeddableConfig);
expect(maps[1]).toEqual(mapByValue.embeddableConfig);
const visualizations = await findByValueEmbeddables(savedObjectClient, 'visualization');
expect(visualizations.length).toBe(2);
expect(visualizations[0]).toEqual(visualizationByValue.embeddableConfig);
expect(visualizations[1]).toEqual(visualizationByValue.embeddableConfig);
});
});

View file

@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/
import { ISavedObjectsRepository, SavedObjectAttributes } from 'kibana/server';
import { SavedDashboardPanel730ToLatest } from '../../common';
export const findByValueEmbeddables = async (
savedObjectClient: Pick<ISavedObjectsRepository, 'find'>,
embeddableType: string
) => {
const dashboards = await savedObjectClient.find<SavedObjectAttributes>({
type: 'dashboard',
});
return dashboards.saved_objects
.map((dashboard) => {
try {
return (JSON.parse(
dashboard.attributes.panelsJSON as string
) as unknown) as SavedDashboardPanel730ToLatest[];
} catch (exception) {
return [];
}
})
.flat()
.filter((panel) => (panel as Record<string, any>).panelRefName === undefined)
.filter((panel) => panel.type === embeddableType)
.map((panel) => panel.embeddableConfig);
};