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

Refactor read-only component to cover more edge cases #6416

Merged
merged 5 commits into from
Apr 16, 2024

Conversation

zhyuanqi
Copy link
Collaborator

@zhyuanqi zhyuanqi commented Apr 11, 2024

Description

Refactor read-only component to cover more edge cases

Issues Resolved

#6393

Screenshot

Testing the changes

  1. activeOption={[{id: ''}]} with hideLocalCluster is set to true in yaml
    Toast: Data source is not available
Screenshot 2024-04-12 at 2 36 37 PM
  1. activeOption={[{id: 'invalid'}]}
    Toast: Failed to fetch data source
Screenshot 2024-04-12 at 2 37 25 PM
  1. activeOption={[{id: ''}]} with hideLocalCluster is set to false in yaml
    Show local cluster
Screenshot 2024-04-12 at 2 38 25 PM
  1. activeOption={[{id: 'valid id'}]}
    show selected cluster
Screenshot 2024-04-12 at 2 39 39 PM
  1. activeOption={[{id: 'valid id', iabel:"show label"}]}
    show selected cluster with label name
Screenshot 2024-04-12 at 2 40 20 PM
  1. When valid id pass in, but it has been filtered out
    activeOption: [{ id: '6304d1d0-f826-11ee-ab60-439c91fde80b', label:'show label' }],
    dataSourceFilter: (ds) => {return ds.id != '6304d1d0-f826-11ee-ab60-439c91fde80b'},
    toast: datasource is not available
Screenshot 2024-04-12 at 2 40 37 PM

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

@@ -130,6 +142,9 @@ export async function getDataSourceById(
savedObjectsClient: SavedObjectsClientContract
) {
return savedObjectsClient.get('data-source', id).then((response) => {
if (!response || response.error) {
throw new Error('Unable to find data source');
Copy link
Collaborator

Choose a reason for hiding this comment

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

does the get data source API throw exception when failed to get data source or do we need to handle it this way?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does not throw exception if we don't handle it. i have done test on this. So if we put an invalid id such as invalid id, it would return back something as below:

Object { client: {…}, attributes: {}, _version: undefined, id: "invalidID", type: "data-source", migrationVersion: undefined, error: {…}, references: [], updated_at: undefined }

Copy link
Collaborator

Choose a reason for hiding this comment

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

If that's the case, then we don't need the try catch block, right?

Copy link
Collaborator Author

@zhyuanqi zhyuanqi Apr 15, 2024

Choose a reason for hiding this comment

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

Sure . would do

}

try {
const selectedDataSource = await getDataSourceById(optionId, this.props.savedObjectsClient!);
Copy link
Collaborator

Choose a reason for hiding this comment

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

we only do this when label is not passed in

Copy link
Collaborator Author

@zhyuanqi zhyuanqi Apr 12, 2024

Choose a reason for hiding this comment

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

I need to use this DatasourceById to find if it is an invalid ID. If the ID is invalid, getDataSourceByID would fail and throw exception. This would be caught and return as a warning message

Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need to check each time, when label is passed in, we will accept it, if it is not passed it, we will look for it. This component is used internally by developers, no need for over defensive programming

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.

});

test('should add warning with error message when error is provided', () => {
const error = 'Failed to fetch data';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const error = 'Failed to fetch data';
const errorMessage= 'Failed to fetch data';

or const error = new Error("Filed to fetch data")?

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

@BionIT
Copy link
Collaborator

BionIT commented Apr 12, 2024

Can we add the screenshots? Also test failed, could we address https://github.com/opensearch-project/OpenSearch-Dashboards/actions/runs/8667700544/job/23771246546?pr=6416

@zhyuanqi zhyuanqi force-pushed the readonly-logic branch 4 times, most recently from 83368ff to 57979c1 Compare April 12, 2024 22:07
@@ -44,9 +44,11 @@ export const DataSourceComponentType = {
export type DataSourceComponentType = typeof DataSourceComponentType[keyof typeof DataSourceComponentType];

export interface DataSourceViewConfig extends DataSourceBaseConfig {
onSelectedDataSources?: (dataSources: DataSourceOption[]) => void;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's move optional fields below required

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


interface DataSourceViewProps {
fullWidth: boolean;
selectedOption: DataSourceOption[];
savedObjectsClient?: SavedObjectsClientContract;
notifications?: ToastsStart;
uiSettings?: IUiSettingsClient;
hideLocalCluster: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's move optional fields below required

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

okay

}

try {
const selectedDataSource = await getDataSourceById(optionId, this.props.savedObjectsClient!);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need to check each time, when label is passed in, we will accept it, if it is not passed it, we will look for it. This component is used internally by developers, no need for over defensive programming

@@ -130,6 +142,9 @@ export async function getDataSourceById(
savedObjectsClient: SavedObjectsClientContract
) {
return savedObjectsClient.get('data-source', id).then((response) => {
if (!response || response.error) {
throw new Error('Unable to find data source');
Copy link
Collaborator

Choose a reason for hiding this comment

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

If that's the case, then we don't need the try catch block, right?


export function handleNoAvailableDataSourceError(notifications: ToastsStart) {
notifications.addWarning(
i18n.translate('dataSource.fetchDataSourceError', {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
i18n.translate('dataSource.fetchDataSourceError', {
i18n.translate('dataSource.noAvailableDataSourceError', {

@zhyuanqi zhyuanqi force-pushed the readonly-logic branch 2 times, most recently from fcf50dd to 688df3c Compare April 15, 2024 17:42
Copy link

codecov bot commented Apr 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 32.72%. Comparing base (e785fd3) to head (018c6d5).
Report is 1 commits behind head on main.

❗ Current head 018c6d5 differs from pull request most recent head f08ebe4. Consider uploading reports for the commit f08ebe4 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #6416       +/-   ##
===========================================
- Coverage   49.55%   32.72%   -16.84%     
===========================================
  Files        2670     2252      -418     
  Lines       54290    45574     -8716     
  Branches     8878     7150     -1728     
===========================================
- Hits        26906    14913    -11993     
- Misses      25720    29962     +4242     
+ Partials     1664      699      -965     
Flag Coverage Δ
Linux_1 32.72% <ø> (?)
Windows_1 ?
Windows_3 ?

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.

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>
@zhyuanqi zhyuanqi force-pushed the readonly-logic branch 2 times, most recently from 5a698ce to ae48c41 Compare April 15, 2024 19:14
Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>
zhongnansu
zhongnansu previously approved these changes Apr 15, 2024
Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>
@zhongnansu zhongnansu merged commit b9acf57 into opensearch-project:main Apr 16, 2024
67 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 16, 2024
* Refactor read-only component to cover more edge case

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

* fix test

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

* fix snapshot

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

---------

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>
(cherry picked from commit b9acf57)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
zhongnansu pushed a commit that referenced this pull request Apr 16, 2024
* Refactor read-only component to cover more edge case

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

* fix test

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

* fix snapshot

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

---------

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>
(cherry picked from commit b9acf57)
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 16, 2024
* Refactor read-only component to cover more edge case

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

* fix test

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

* fix snapshot

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>

---------

Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>
(cherry picked from commit b9acf57)
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.

None yet

3 participants