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

[Multiple Datasource] Enhanced data source selector with default datasource shows as first choice #6293

Merged
merged 5 commits into from
Apr 3, 2024

Conversation

zhyuanqi
Copy link
Collaborator

@zhyuanqi zhyuanqi commented Mar 29, 2024

Description

  • add the default icon in the selector
  • Add a showDefault function that plugins team can use and get the Default datasource

Issues Resolved

Screenshot

Screen.Recording.2024-04-01.at.9.53.47.AM.mov

Testing the changes

Check List

  • All tests pass
    • yarn test:jest
    • yarn test:jest_integration
  • New functionality includes testing.
  • New functionality has been documented.
  • Update CHANGELOG.md
  • Commits are signed per the DCO using --signoff

Copy link

codecov bot commented Mar 29, 2024

Codecov Report

Attention: Patch coverage is 87.87879% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 67.50%. Comparing base (cb24346) to head (bace7c3).

Files Patch % Lines
...ents/data_source_selector/data_source_selector.tsx 81.81% 2 Missing ⚠️
.../data_source_management/public/components/utils.ts 88.88% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6293   +/-   ##
=======================================
  Coverage   67.50%   67.50%           
=======================================
  Files        3376     3376           
  Lines       65800    65830   +30     
  Branches    10639    10648    +9     
=======================================
+ Hits        44417    44441   +24     
- Misses      18800    18803    +3     
- Partials     2583     2586    +3     
Flag Coverage Δ
Linux_1 32.60% <ø> (ø)
Linux_2 55.60% <ø> (ø)
Linux_3 44.87% <87.87%> (+0.01%) ⬆️
Linux_4 34.99% <0.00%> (-0.03%) ⬇️
Windows_1 32.62% <ø> (ø)
Windows_2 55.57% <ø> (ø)
Windows_3 44.89% <87.87%> (+0.03%) ⬆️
Windows_4 34.99% <0.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zhyuanqi zhyuanqi force-pushed the selector branch 3 times, most recently from 9a8f88b to 01073e8 Compare April 1, 2024 04:01
@ZilongX
Copy link
Collaborator

ZilongX commented Apr 1, 2024

nit : add a prefix [Multiple Datasource] in the commit msg so all MD related changes would be easily grouped together, like [Multiple Datasource] - editdatasource selector to have default behavior

CHANGELOG.md Outdated
@@ -162,6 +162,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Discover] Enhanced the data source selector with added sorting functionality ([#5719](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5719))
- [Multiple Datasource] Add datasource picker component and use it in devtools and tutorial page when multiple datasource is enabled ([#5756](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5756))
- [Multiple Datasource] Add datasource picker to import saved object flyout when multiple data source is enabled ([#5781](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5781))
- [Multiple Datasource] Add show default datasource for selector picker ([#6293](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6293/files))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit : add the changelog entry in the middle (say line 163) instead of append it in the last line would save some conflict resolve efforts in case any

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Good advise! It is ver helpful. Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct the PR link to remove the "file" at the end?

Suggested change
- [Multiple Datasource] Add show default datasource for selector picker ([#6293](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6293/files))
- [Multiple Datasource] Add show default datasource for selector picker ([#6293](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6293))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix the position of the changelog, it is now under 2.12 section

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

});

it('should return local cluster if it exists and no default options in the data sources', () => {
mockUiSettingsCalls(uiSettings, 'get', null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null, Is it correct argument which passed here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. So if the uiSettings could not find the defaultDataSource in the config, it will return null.

} else if (
uiSettings &&
uiSettings?.get('defaultDataSource', null) !== null &&
dataSources.find((dataSource) => dataSource.id === uiSettings?.get('defaultDataSource'))
Copy link
Member

@Flyingliuhub Flyingliuhub Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two locations were used uiSettings?.get('defaultDataSource'), is it possible to use const defaultDataSourceId = uiSettings?.get('defaultDataSource'); to simplify the code

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I can work on this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

) {
let defaultShowingDataSource: DataSourceOption;

if (defaultOption && dataSources.find((dataSource) => dataSource.id === defaultOption?.[0].id)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be we can define the those value at the beginning, make the logic more clear

if (defaultDataSource) {
    ...
  } else if (uiSettingsDataSource) {
    ....
  } else if (!hideLocalCluster) {
    ....
  } else {
    ...
  }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, let me check on this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will check on this.

Comment on lines 82 to 108
if (this.props.showDefault) {
const dataSources = getFilterDataSources(
this.state.allDataSources,
this.props.dataSourceFilter
);
const defaultDataSource = getDefaultDataSource(
dataSources,
LocalCluster,
this.props.uiSettings,
this.props.hideLocalCluster,
this.props.defaultOption
);
this.props.showDefault(defaultDataSource);
}
Copy link
Member

@xinruiba xinruiba Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this change.
I remember @BionIT have a PR to Use data source filter function before rendering

And in this line: https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6175/files#diff-ec5b6ef64504177e50cad326dcc07ada2135faafe924d25b0f4df088bc7b4823L77 she move filter function call from componentDidMount() to render().

Are there any concerns to follow the same format by also moving this logic into render() function?

Thanks

Copy link
Collaborator Author

@zhyuanqi zhyuanqi Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi This showDefault should be use as a callback function that plugins team can use to get from the defaultShowing datasource. Below is how plugin team use it.

  const [defaultDataSource, setDefaultDataSource] = React.useState<DataSourceOption>();
  const showDefault = (source: DataSourceOption) => {
    setDefaultDataSource(source);
  };

  <DataSourceSelector
      .......
       showDefault={showDefault}
/>

As componentDidMount should perform data fetching and initialization where render focus on rendering the UI based on component's state.Let me look into this more

@@ -78,6 +79,41 @@ export async function setFirstDataSourceAsDefault(
}
}

export function getFilterDataSources(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would getFilteredDataSources be a better naming?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will rename it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhongnansu @zhyuanqi , would you use suggested fix feature for quick apply the suggestion, like below

Suggested change
export function getFilterDataSources(
export function getFilteredDataSources(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good suggestion, will do!

import { AuthenticationMethod, AuthenticationMethodRegistry } from '../auth_registry';
import { deepEqual } from 'assert';
import { DataSourceAttributes } from 'src/plugins/data_source/common/data_sources'; // Import the missing 'SigV4Content' here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to import SigV4Content? can we remove the comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me remove this line here.

@@ -29,6 +30,8 @@ export interface DataSourceSelectorProps {
removePrepend?: boolean;
dataSourceFilter?: (dataSource: SavedObject<DataSourceAttributes>) => boolean;
compressed?: boolean;
uiSettings?: IUiSettingsClient;
showDefault?: (source: DataSourceOption) => void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be a dumb question, but why do we want those 2 param to be optional? shouldn't we always show default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uiSettings, for plugins, we want to pass it during plugin setup, so i set it as optional. The showDefault is a callback function, depends if plugin wants to call this the see what is the default or not. So it is optional

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thx Ella

CHANGELOG.md Outdated
@@ -162,6 +162,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Discover] Enhanced the data source selector with added sorting functionality ([#5719](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5719))
- [Multiple Datasource] Add datasource picker component and use it in devtools and tutorial page when multiple datasource is enabled ([#5756](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5756))
- [Multiple Datasource] Add datasource picker to import saved object flyout when multiple data source is enabled ([#5781](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5781))
- [Multiple Datasource] Add show default datasource for selector picker ([#6293](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6293/files))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix the position of the changelog, it is now under 2.12 section

@@ -76,6 +79,20 @@ export class DataSourceSelector extends React.Component<
allDataSources: fetchedDataSources,
});
}
if (this.props.showDefault) {
const dataSources = getFilterDataSources(
this.state.allDataSources,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can just pass fetchedDataSources

@@ -76,6 +79,20 @@ export class DataSourceSelector extends React.Component<
allDataSources: fetchedDataSources,
});
}
if (this.props.showDefault) {
const dataSources = getFilterDataSources(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be filteredDataSources

this.props.hideLocalCluster,
this.props.defaultOption
);
this.props.showDefault(defaultDataSource);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we reuse the existing callback function? onSelectedDataSource

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make the change. Just worry about if it would break any existing behavior. Let me change on this

const dataSources = this.props.dataSourceFilter
? this.state.allDataSources.filter((ds) => this.props.dataSourceFilter!(ds))
: this.state.allDataSources;
const dataSources = getFilterDataSources(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having 2 separate functions and have one of them called twice, would it be better to have one function which takes the filter function and does the filtering logic and store the default data source in the state here, so that we just need to call the function once here?

@@ -140,6 +159,16 @@ export class DataSourceSelector extends React.Component<
isDisabled={this.props.disabled}
fullWidth={this.props.fullWidth || false}
data-test-subj={'dataSourceSelectorComboBox'}
renderOption={(option) => (
<EuiFlexGroup alignItems="center">
<EuiFlexItem grow={1}>{option.label}</EuiFlexItem>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test?

} else if (
uiSettings &&
uiSettings?.get('defaultDataSource', null) !== null &&
dataSources.find((dataSource) => dataSource.id === uiSettings?.get('defaultDataSource'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@BionIT BionIT mentioned this pull request Apr 3, 2024
7 tasks
@zhyuanqi zhyuanqi force-pushed the selector branch 2 times, most recently from 9073369 to 3bc74ae Compare April 3, 2024 04:32
}: DevToolsWrapperProps) {
const mountedTool = useRef<MountedDevToolDescriptor | null>(null);
const [isLoading, setIsLoading] = React.useState(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should isLoading be true initially, then set to false once it is loaded?

zhyuanqi and others added 3 commits April 3, 2024 17:34
…ce_selector/data_source_selector.tsx

Co-authored-by: Lu Yu <[email protected]>
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
Co-authored-by: Lu Yu <[email protected]>
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
@zhyuanqi zhyuanqi force-pushed the selector branch 2 times, most recently from d26ef5f to 3bf9adf Compare April 3, 2024 18:05
BionIT
BionIT previously approved these changes Apr 3, 2024
@BionIT BionIT changed the title edit datasource selector to have default behavior [Multiple Datasource] Enhanced data source selector with default datasource shows as first choice Apr 3, 2024
@BionIT BionIT added multiple datasource multiple datasource project v2.14.0 backport 2.x labels Apr 3, 2024
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
@ZilongX ZilongX self-requested a review April 3, 2024 23:36
@ZilongX ZilongX merged commit 557fbcf into opensearch-project:main Apr 3, 2024
69 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 3, 2024
…source shows as first choice (#6293)

* edit datasource selector to show dafault datasource and return default to customer

Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>

* Update src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx

Co-authored-by: Lu Yu <[email protected]>
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>

* Apply suggestions from code review

Co-authored-by: Lu Yu <[email protected]>
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>

* fix syntax

Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>

---------

Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
Co-authored-by: Lu Yu <[email protected]>
(cherry picked from commit 557fbcf)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
BionIT pushed a commit that referenced this pull request Apr 4, 2024
…source shows as first choice (#6293) (#6339)

* edit datasource selector to show dafault datasource and return default to customer

Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>

* Update src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx

Co-authored-by: Lu Yu <[email protected]>
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>

* Apply suggestions from code review

Co-authored-by: Lu Yu <[email protected]>
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>

* fix syntax

Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>

---------

Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
Co-authored-by: Lu Yu <[email protected]>
(cherry picked from commit 557fbcf)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants