-
Notifications
You must be signed in to change notification settings - Fork 909
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
[Table Vis] move format table logic to table render and consolidate types #3397
Conversation
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more @@ Coverage Diff @@
## main #3397 +/- ##
==========================================
- Coverage 66.44% 66.40% -0.04%
==========================================
Files 3208 3226 +18
Lines 61744 61998 +254
Branches 9537 9585 +48
==========================================
+ Hits 41023 41167 +144
- Misses 18431 18525 +94
- Partials 2290 2306 +16
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 17 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
return uiState.sort.colIndex !== undefined && | ||
columns[uiState.sort.colIndex].id && | ||
uiState.sort.direction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return uiState.sort.colIndex !== undefined && | |
columns[uiState.sort.colIndex].id && | |
uiState.sort.direction | |
return columns[uiState.sort.colIndex]?.id !== undefined && | |
uiState.sort.direction |
... since checking uiState.sort.colIndex
proves nothing and all we care for is columns[uiState.sort.colIndex].id
to exist. if uiState.sort.colIndex = 999
and there is not such index, columns[uiState.sort.colIndex].id
would throw exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uiState.sort.colIndex is optional... shouldn't we request it to be not undefined?
if not check this, there is a type error
return uiState.sort.colIndex && | ||
dataGridColumns[uiState.sort.colIndex].id && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return uiState.sort.colIndex && | |
dataGridColumns[uiState.sort.colIndex].id && | |
return dataGridColumns[uiState.sort.colIndex]?.id !== undefined && |
src/plugins/vis_type_table/public/components/table_vis_component_group.tsx
Outdated
Show resolved
Hide resolved
src/plugins/vis_type_table/public/table_vis_response_handler.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple minor questions and comments, otherwise I agree with Miki's suggestions.
src/plugins/vis_type_table/public/table_vis_response_handler.ts
Outdated
Show resolved
Hide resolved
src/plugins/vis_type_table/public/components/table_vis_component_group.tsx
Outdated
Show resolved
Hide resolved
1a04a50
to
2611e06
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ananzh This is looking great so far! I've only reviewed the utils
, but I figured I'd share my comments so far.
@@ -20,20 +20,23 @@ export const usePagination = (visConfig: TableVisConfig, nRow: number) => { | |||
]); | |||
|
|||
useEffect(() => { | |||
const perPage = visConfig.perPage || 10; | |||
const perPage = visParams.perPage || 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the change in the fallback value? It seems like 0 items per page is an invalid page size, and 10 is a more reasonable default.
Is there also a way we can avoid doing this fallback twice (here and on L12)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I never explain this thoroughly but keeping changing this value. My bad. The line const perPage = visParams.perPage || 0
; is setting the default value for perPage
to 0 if visParams.perPage
is not set or is a falsy value (e.g., null, undefined, or 0). The reason for using 0 as the default value is because the pagination functionality is designed to be optional. When the value of perPage is set to 0, it indicates that pagination is not enabled, and the table should display all rows without pagination. If we set the default value to 10, it would mean that pagination would be enabled by default, displaying only 10 rows per page even if the user has not explicitly set a perPage value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So setting const perPage = visParams.perPage || 0
makes logic sense but it makes the next line const maxiPageIndex = Math.ceil(nRow / perPage) - 1;
vulnerable. There are two edge cases we should consider 1) visParams.perPage
is 0. Well this is not possible. The Max rows per page
option (src/plugins/vis_type_table/public/components/table_vis_options.tsx) forbids being set to 0. The NumberInputOption
component for the Max rows per page
option has a min property set to 1, which means the minimum allowed value for this input field is 1. 2) visParams.perPage = ''
. This is possible if Max rows per page
option is empty. This would set maxiPageIndex to Infinity. The setPagination function will be called, but since maxiPageIndex is Infinity, the condition p.pageIndex > maxiPageIndex
will always be false, and the pageIndex will not be updated. This is also reflected in the unit test:
it('should not set pagination if perPage is empty string', () => {
visParams.perPage = '';
const { result } = renderHook(() => usePagination(visParams, 20));
expect(result.current).toEqual(undefined);
});
However, the code still does not break. Even though maxiPageIndex becomes Infinity, the setPagination function will update the pageIndex value to be the same as the current pageIndex value, which means that the pagination state remains unchanged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary for approval, but I think this is a case where it just makes sense to abstract it out as a function, that can have its own test cases and comment explanations:
const getPerPageCount = (perPageConfig, nRow) => {
// if no perPageConfig specified, set to 0 to disable pagination
const perPage = perPageConfig || 0;
return Math.min(perPage, nRow);
}
src/plugins/vis_type_table/public/utils/ui_state_management.test.ts
Outdated
Show resolved
Hide resolved
src/plugins/vis_type_table/public/utils/add_percentage_col.test.ts
Outdated
Show resolved
Hide resolved
sumTotal: 5, | ||
}, | ||
{ | ||
title: 'count percentages', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of scope, but it's a little odd that this function allows the title
of the percentage column to be set independently from the title of the source column.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only some minor questions and suggestions, but nothing blocking.
src/plugins/vis_type_table/public/table_vis_response_handler.ts
Outdated
Show resolved
Hide resolved
src/plugins/vis_type_table/public/components/table_vis_cell.tsx
Outdated
Show resolved
Hide resolved
src/plugins/vis_type_table/public/components/table_vis_component_group.test.tsx
Outdated
Show resolved
Hide resolved
all checks pass, but needs changelog conflict resolution. |
@ananzh You should merge or rebase on |
@ananzh The visbuilder tablevis functional test is failing:
|
.visTable { | ||
display: flex; | ||
flex-direction: column; | ||
flex: 1 0 0; | ||
overflow: auto; | ||
|
||
@include euiScrollBar; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting - fine for this PR, but this usage is something we'll want to document/revisit in OUI - I think we'd prefer to avoid these stylings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am using a consistent @include euiScrollBar;
as shown in other scss files in our repo. Why we want to avoid it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #3803 (comment) - we're aiming for a state where there are no/minimal SASS files in OSD. One difference is that EUI encouraged direct usage of styles (as done here), whereas OUI prefers only using React components as a way to access those styles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But don't worry about it here - I'll open an issue.
src/plugins/vis_type_table/public/components/table_vis_cell.tsx
Outdated
Show resolved
Hide resolved
const sortColumnId = | ||
sort.colIndex !== null && sort.colIndex !== undefined | ||
? formattedColumns[sort.colIndex]?.id | ||
: undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - this ternary is unnecessary:
const sortColumnId = | |
sort.colIndex !== null && sort.colIndex !== undefined | |
? formattedColumns[sort.colIndex]?.id | |
: undefined; | |
const sortColumnId = formattedColumns[sort.colIndex]?.id; |
Simple demo:
const fc = [{id: 'foo'}, {id: 'bar'}];
> undefined
const s = {}
> undefined
fc[s.colIndex]?.id
> undefined
s.colIndex = 1
> 1
fc[s.colIndex]?.id
> "bar"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think I tried before. But it will give a type error
(property) ColumnSort.colIndex?: number | undefined
Type 'undefined' cannot be used as an index type.ts(2538)
src/plugins/vis_type_table/public/components/table_vis_component.tsx
Outdated
Show resolved
Hide resolved
sort.colIndex !== null && | ||
sort.colIndex !== undefined && | ||
dataGridColumns[sort.colIndex].id && | ||
sort.direction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
sort.colIndex !== null && | |
sort.colIndex !== undefined && | |
dataGridColumns[sort.colIndex].id && | |
sort.direction | |
dataGridColumns[sort.colIndex]?.id !== undefined && | |
sort.direction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yah I think I wrote to solve the type error Type 'undefined' cannot be used as an index type.ts(2538)
<EuiTitle size="xs" className="visTable__component__title"> | ||
<h3>{title}</h3> | ||
</EuiTitle> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - There's a preferred way to center a title with OUI components:
<EuiTitle size="xs" className="visTable__component__title"> | |
<h3>{title}</h3> | |
</EuiTitle> | |
<EuiTitle size="xs"> | |
<EuiTextAlign textAlign="center"> | |
<h3>{title}</h3> | |
</EuiTextAlign> | |
</EuiTitle> |
src/plugins/vis_type_table/public/components/table_vis_component_group.tsx
Outdated
Show resolved
Hide resolved
@@ -20,20 +20,23 @@ export const usePagination = (visConfig: TableVisConfig, nRow: number) => { | |||
]); | |||
|
|||
useEffect(() => { | |||
const perPage = visConfig.perPage || 10; | |||
const perPage = visParams.perPage || 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary for approval, but I think this is a case where it just makes sense to abstract it out as a function, that can have its own test cases and comment explanations:
const getPerPageCount = (perPageConfig, nRow) => {
// if no perPageConfig specified, set to 0 to disable pagination
const perPage = perPageConfig || 0;
return Math.min(perPage, nRow);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more nits - but nothing blocking.
Currently, table data is formatted by a until function convertToFormattedData in TableVisComponent. In this PR, we moved the formatting data process to table_vis_response_handler.ts to combine with other data process logics. In this way, component render and data handling logics are completely isolated. This PR also solidate some types. Issue Resolved: opensearch-project#3395 opensearch-project#2856 Signed-off-by: Anan Zhuang <ananzh@amazon.com>
.visTable { | ||
display: flex; | ||
flex-direction: column; | ||
flex: 1 0 0; | ||
overflow: auto; | ||
|
||
@include euiScrollBar; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But don't worry about it here - I'll open an issue.
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/backport-2.x 2.x
# Navigate to the new working tree
pushd ../.worktrees/backport-2.x
# Create a new branch
git switch --create backport/backport-3397-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 fc195ea580844dea47e21c4854ac7767f2513531
# Push it to GitHub
git push --set-upstream origin backport/backport-3397-to-2.x
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/backport-2.x Then, create a pull request where the |
…d unit tests Currently, table data is formatted by a until function convertToFormattedData in TableVisComponent. In this PR, we moved the formatting data process to table_vis_response_handler.ts to combine with other data process logics. In this way, component render and data handling logics are completely isolated. This PR also solidate some types. Backport PR: opensearch-project#3397 Issue Resolved: opensearch-project#3395 opensearch-project#2856 Signed-off-by: Anan Zhuang <ananzh@amazon.com>
…d unit tests (#3869) Currently, table data is formatted by a until function convertToFormattedData in TableVisComponent. In this PR, we moved the formatting data process to table_vis_response_handler.ts to combine with other data process logics. In this way, component render and data handling logics are completely isolated. This PR also solidate some types. Backport PR: #3397 Issue Resolved: #3395 #2856 Signed-off-by: Anan Zhuang <ananzh@amazon.com>
Description
Currently, table data is formatted by a until function convertToFormattedData in TableVisComponent. In this PR, we moved the formatting data process to table_vis_response_handler.ts to combine with other data process logics. In this way, component render and data handling logics are completely isolated. This PR also solidate some types.
Issue Resolved:
fixes #3395
Check List
yarn test:jest
yarn test:jest_integration
yarn test:ftr