Skip to content

Commit

Permalink
Enable sample data with Multiple datasource frontend
Browse files Browse the repository at this point in the history
Signed-off-by: Kristen Tian <tyarong@amazon.com>
  • Loading branch information
kristenTian committed Jun 27, 2023
1 parent a44b09f commit 59958c9
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export class SampleDataSetCard extends React.Component {
};

install = () => {
this.props.onInstall(this.props.id);
this.props.onInstall(this.props.id, this.props.dataSourceId);
};

uninstall = () => {
this.props.onUninstall(this.props.id);
this.props.onUninstall(this.props.id, this.props.dataSourceId);
};

renderBtn = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,21 @@ export class SampleDataSetCards extends React.Component {
this.loadSampleDataSets();
}

loadSampleDataSets = async () => {
componentDidUpdate(prevProps) {
if (this.props.isDataSourceEnabled) {
this._isMounted = true;
if (prevProps && prevProps.dataSourceId !== this.props.dataSourceId) {
this.setState({ dataSourceId: this.props.dataSourceId }, () =>
this.loadSampleDataSets(this.state.dataSourceId)
);
}
}
}

loadSampleDataSets = async (dataSourceId) => {
let sampleDataSets;
try {
sampleDataSets = await listSampleDataSets();
sampleDataSets = await listSampleDataSets(dataSourceId);
} catch (fetchError) {
this.toastNotifications.addDanger({
title: i18n.translate('home.sampleDataSet.unableToLoadListErrorMessage', {
Expand All @@ -93,7 +104,7 @@ export class SampleDataSetCards extends React.Component {
});
};

install = async (id) => {
install = async (id, dataSourceId) => {
const targetSampleDataSet = this.state.sampleDataSets.find((sampleDataSet) => {
return sampleDataSet.id === id;
});
Expand All @@ -103,7 +114,7 @@ export class SampleDataSetCards extends React.Component {
}));

try {
await installSampleDataSet(id, targetSampleDataSet.defaultIndex);
await installSampleDataSet(id, targetSampleDataSet.defaultIndex, dataSourceId);
} catch (fetchError) {
if (this._isMounted) {
this.setState((prevState) => ({
Expand Down Expand Up @@ -141,7 +152,7 @@ export class SampleDataSetCards extends React.Component {
});
};

uninstall = async (id) => {
uninstall = async (id, dataSourceId) => {
const targetSampleDataSet = this.state.sampleDataSets.find((sampleDataSet) => {
return sampleDataSet.id === id;
});
Expand All @@ -151,7 +162,7 @@ export class SampleDataSetCards extends React.Component {
}));

try {
await uninstallSampleDataSet(id, targetSampleDataSet.defaultIndex);
await uninstallSampleDataSet(id, targetSampleDataSet.defaultIndex, dataSourceId);
} catch (fetchError) {
if (this._isMounted) {
this.setState((prevState) => ({
Expand Down Expand Up @@ -213,6 +224,7 @@ export class SampleDataSetCards extends React.Component {
previewUrl={this.props.addBasePath(this.lightOrDarkImage(sampleDataSet))}
onInstall={this.install}
onUninstall={this.uninstall}
dataSourceId={this.state.dataSourceId}
/>
</EuiFlexItem>
);
Expand Down
121 changes: 109 additions & 12 deletions src/plugins/home/public/application/components/tutorial_directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { getServices } from '../opensearch_dashboards_services';

import {
EuiPage,
EuiPanel,
EuiTabs,
EuiTab,
EuiFlexItem,
Expand All @@ -45,10 +46,10 @@ import {
EuiSpacer,
EuiTitle,
EuiPageBody,
EuiComboBox,
} from '@elastic/eui';

import { getTutorials } from '../load_tutorials';

import { injectI18n, FormattedMessage } from '@osd/i18n/react';
import { i18n } from '@osd/i18n';

Expand Down Expand Up @@ -81,6 +82,7 @@ class TutorialDirectoryUi extends React.Component {
selectedTabId: openTab,
tutorialCards: [],
notices: getServices().tutorialService.getDirectoryNotices(),
isDataSourceEnabled: !!getServices().dataSource,
};
}

Expand Down Expand Up @@ -152,6 +154,30 @@ class TutorialDirectoryUi extends React.Component {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});

if (this.state.isDataSourceEnabled) {
this.getDataSources(getServices().savedObjectsClient)
.then((fetchedDataSources) => {
if (fetchedDataSources?.length) {
const dataSourceOptions = fetchedDataSources.map((dataSource) => ({
id: dataSource.id,
label: dataSource.title,
}));

this.setState({
// eslint-disable-line react/no-did-mount-set-state
dataSources: dataSourceOptions,
});
}
})
.catch(() => {
getServices().toastNotifications.addWarning(
i18n.translate('devTool.devToolWrapper.fetchDataSourceError', {
defaultMessage: 'Unable to fetch existing data sources',
})
);
});
}

this.setState({
// eslint-disable-line react/no-did-mount-set-state
tutorialCards: tutorialCards,
Expand All @@ -164,6 +190,12 @@ class TutorialDirectoryUi extends React.Component {
});
};

onSelectedDataSourceChange = (e) => {
this.setState({ selectedOption: e });
const dataSourceId = e[0] ? e[0].id : undefined;
this.setState({ selectedDataSourceId: dataSourceId });
};

renderTabs = () => {
return this.tabs.map((tab, index) => (
<EuiTab
Expand All @@ -178,7 +210,13 @@ class TutorialDirectoryUi extends React.Component {

renderTabContent = () => {
if (this.state.selectedTabId === SAMPLE_DATA_TAB_ID) {
return <SampleDataSetCards addBasePath={this.props.addBasePath} />;
return (
<SampleDataSetCards
addBasePath={this.props.addBasePath}
dataSourceId={this.state.selectedDataSourceId}
isDataSourceEnabled={this.state.isDataSourceEnabled}
/>
);
}

return (
Expand Down Expand Up @@ -210,6 +248,30 @@ class TutorialDirectoryUi extends React.Component {
);
};

renderDataSourceSelector = () => {
const { isDataSourceEnabled, dataSources, selectedOption } = this.state;

return isDataSourceEnabled ? (
<div className="sampledataSourcePicker">
<EuiComboBox
aria-label={i18n.translate('sampleData.DataSourceComboBoxAriaLabel', {
defaultMessage: 'Select a Data Source',
})}
placeholder={i18n.translate('sampleData.DataSourceComboBoxPlaceholder', {
defaultMessage: 'Select a Data Source',
})}
singleSelection={{ asPlainText: true }}
options={dataSources}
selectedOptions={selectedOption}
onChange={this.onSelectedDataSourceChange}
prepend="DataSource"
compressed
isDisabled={!isDataSourceEnabled}
/>
</div>
) : null;
};

renderNotices = () => {
const notices = getServices().tutorialService.getDirectoryNotices();
return notices.length ? (
Expand Down Expand Up @@ -260,19 +322,54 @@ class TutorialDirectoryUi extends React.Component {
);
};

render() {
renderPageBody = () => {
return (
<EuiPage restrictWidth={1200}>
<EuiPageBody component="main">
{this.renderHeader()}
<EuiSpacer size="m" />
<EuiTabs>{this.renderTabs()}</EuiTabs>
<EuiSpacer />
{this.renderTabContent()}
</EuiPageBody>
</EuiPage>
<EuiPageBody component="main">
{this.renderHeader()}
<EuiSpacer size="m" />
{this.renderDataSourceSelector()}
<EuiTabs>{this.renderTabs()}</EuiTabs>
<EuiSpacer />
{this.renderTabContent()}
</EuiPageBody>
);
};

render() {
const { isDataSourceEnabled } = this.state;

return isDataSourceEnabled ? (
<EuiPanel paddingSize={'l'} style={{ width: '70%', margin: '50px auto' }}>
{this.renderPageBody()}
</EuiPanel>
) : (
<EuiPage restrictWidth={1200}>{this.renderPageBody()}</EuiPage>
);
}

getDataSources = (savedObjectsClient) => {
return savedObjectsClient
.find({
type: 'data-source',
fields: ['id', 'description', 'title'],
perPage: 10000,
})
.then(
(response) =>
response?.savedObjects?.map?.((source) => {
const id = source.id;
const title = source.get('title');
const description = source.get('description');

return {
id,
title,
description,
sort: `${title}`,
};
}) || []
);
};
}

TutorialDirectoryUi.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { FeatureCatalogueRegistry } from '../services/feature_catalogue';
import { EnvironmentService } from '../services/environment';
import { ConfigSchema } from '../../config';
import { HomePluginBranding } from '..';
import { DataSourcePluginStart } from '../../../data_source/public';

export interface HomeOpenSearchDashboardsServices {
indexPatternService: any;
Expand All @@ -71,6 +72,7 @@ export interface HomeOpenSearchDashboardsServices {
getInjectedVar: (name: string, defaultValue?: any) => unknown;
getBranding: () => HomePluginBranding;
};
dataSource?: DataSourcePluginStart;
}

let services: HomeOpenSearchDashboardsServices | null = null;
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/home/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ import { UsageCollectionSetup } from '../../usage_collection/public';
import { UrlForwardingSetup, UrlForwardingStart } from '../../url_forwarding/public';
import { AppNavLinkStatus } from '../../../core/public';
import { PLUGIN_ID, HOME_APP_BASE_PATH } from '../common/constants';
import { DataSourcePluginStart } from '../../data_source/public';

export interface HomePluginStartDependencies {
data: DataPublicPluginStart;
telemetry?: TelemetryPluginStart;
urlForwarding: UrlForwardingStart;
dataSource?: DataSourcePluginStart;
}

export interface HomePluginSetupDependencies {
Expand Down Expand Up @@ -96,7 +98,7 @@ export class HomePublicPlugin
: () => {};
const [
coreStart,
{ telemetry, data, urlForwarding: urlForwardingStart },
{ telemetry, data, urlForwarding: urlForwardingStart, dataSource },
] = await core.getStartServices();
setServices({
trackUiMetric,
Expand All @@ -119,6 +121,7 @@ export class HomePublicPlugin
tutorialService: this.tutorialService,
featureCatalogue: this.featuresCatalogueRegistry,
injectedMetadata: coreStart.injectedMetadata,
dataSource,
});
coreStart.chrome.docTitle.change(
i18n.translate('home.pageTitle', { defaultMessage: 'Home' })
Expand Down

0 comments on commit 59958c9

Please sign in to comment.