Skip to content
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

[Manual Backport 2.x] Use savedObjects client to fetch notebook visualizations (#2241) #2249

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
*/

import {
EuiSmallButton,
EuiSmallButtonIcon,
EuiComboBoxOptionOption,
EuiCompressedFormRow,
EuiContextMenu,
EuiContextMenuPanelDescriptor,
EuiFlexGroup,
EuiFlexItem,
EuiCompressedFormRow,
EuiHorizontalRule,
EuiIcon,
EuiLink,
EuiPanel,
EuiPopover,
EuiSmallButton,
EuiSmallButtonIcon,
EuiSpacer,
EuiText,
htmlIdGenerator,
} from '@elastic/eui';
import _ from 'lodash';
import filter from 'lodash/filter';
import moment from 'moment';
import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { CoreStart, MountPoint, SavedObjectsStart } from '../../../../../../../src/core/public';
import {
CoreStart,
MountPoint,
SavedObjectsFindOptions,
SavedObjectsStart,
} from '../../../../../../../src/core/public';
import {
DashboardContainerInput,
DashboardStart,
Expand All @@ -40,6 +45,7 @@
import { ParaType } from '../../../../../common/types/notebooks';
import { uiSettingsService } from '../../../../../common/utils';
import { dataSourceFilterFn } from '../../../../../common/utils/shared';
import { coreRefs } from '../../../../framework/core_refs';
import PPLService from '../../../../services/requests/ppl';
import { SavedObjectsActions } from '../../../../services/saved_objects/saved_object_client/saved_objects_actions';
import { ObservabilitySavedVisualization } from '../../../../services/saved_objects/saved_object_client/types';
Expand Down Expand Up @@ -160,45 +166,78 @@
}));

const fetchVisualizations = async () => {
let opt1: EuiComboBoxOptionOption[] = [];
let opt2: EuiComboBoxOptionOption[] = [];
await http
.get(`${NOTEBOOKS_API_PREFIX}/visualizations/${dataSourceMDSId ?? ''}`)
.then((res) => {
opt1 = res.savedVisualizations.map((vizObject) => ({
label: vizObject.label,
key: vizObject.key,
className: 'VISUALIZATION',
}));
})
.catch((err) => console.error('Fetching dashboard visualization issue', err.body.message));
if (dataSourceEnabled) {
let opts: EuiComboBoxOptionOption[] = [];
const vizOptions: SavedObjectsFindOptions = {
type: 'visualization',
...(coreRefs.application?.capabilities?.workspaces?.enabled
? { workspaces: coreRefs.workspaces?.currentWorkspace$.getValue()?.name }
: {}),
};
await coreRefs.savedObjectsClient
?.find(vizOptions)
.then((res) => {
opts = res.savedObjects.map((vizObject) => ({
label: vizObject.attributes.title,
key: vizObject.id,
className: 'VISUALIZATION',
}));
})
.catch((error) => {
console.error('Failed to fetch visualizations', error);
});

const allVisualizations = [{ label: 'Dashboards Visualizations', options: opts }];
setVisOptions(allVisualizations);

await SavedObjectsActions.getBulk<ObservabilitySavedVisualization>({
objectType: ['savedVisualization'],
})
.then((res) => {
opt2 = res.observabilityObjectList
.filter((visualization) => !visualization.savedVisualization.application_id)
.map((visualization) => ({
label: visualization.savedVisualization.name,
key: visualization.objectId,
className: 'OBSERVABILITY_VISUALIZATION',
const selectedObject = filter([...opts], {
key: para.visSavedObjId,
});
if (selectedObject.length > 0) {
setVisType(selectedObject[0].className ?? 'VISUALIZATION');
setSelectedVisOption(selectedObject);
}
} else {
let opt1: EuiComboBoxOptionOption[] = [];
let opt2: EuiComboBoxOptionOption[] = [];
await http
.get(`${NOTEBOOKS_API_PREFIX}/visualizations/${dataSourceMDSId ?? ''}`)
.then((res) => {
opt1 = res.savedVisualizations.map((vizObject) => ({
label: vizObject.label,
key: vizObject.key,
className: 'VISUALIZATION',
}));
})
.catch((err) => console.error('Fetching dashboard visualization issue', err.body.message));

await SavedObjectsActions.getBulk<ObservabilitySavedVisualization>({
objectType: ['savedVisualization'],
})
.catch((err) => console.error('Fetching observability visualization issue', err));
.then((res) => {
opt2 = res.observabilityObjectList
.filter((visualization) => !visualization.savedVisualization.application_id)
.map((visualization) => ({
label: visualization.savedVisualization.name,
key: visualization.objectId,
className: 'OBSERVABILITY_VISUALIZATION',
}));
})
.catch((err) => console.error('Fetching observability visualization issue', err));

const allVisualizations = [
{ label: 'Dashboards Visualizations', options: opt1 },
{ label: 'Observability Visualizations', options: opt2 },
];
setVisOptions(allVisualizations);
const allVisualizations = [
{ label: 'Dashboards Visualizations', options: opt1 },
{ label: 'Observability Visualizations', options: opt2 },
];
setVisOptions(allVisualizations);

const selectedObject = _.filter([...opt1, ...opt2], {
key: para.visSavedObjId,
});
if (selectedObject.length > 0) {
setVisType(selectedObject.className);
setSelectedVisOption(selectedObject);
const selectedObject = filter([...opt1, ...opt2], {
key: para.visSavedObjId,
});
if (selectedObject.length > 0) {
setVisType(selectedObject[0].className ?? 'VISUALIZATION');
setSelectedVisOption(selectedObject);
}
}
};

Expand All @@ -211,7 +250,7 @@

fetchVisualizations();
}
}, [dataSourceMDSId]);

Check warning on line 253 in public/components/notebooks/components/paragraph_components/paragraphs.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has missing dependencies: 'fetchVisualizations', 'para.isVizualisation', 'para.visSavedObjId', and 'para.vizObjectInput'. Either include them or remove the dependency array

const createDashboardVizObject = (objectId: string) => {
const vizUniqueId = htmlIdGenerator()();
Expand Down
Loading