zhaoyongjie commented on code in PR #20728: URL: https://github.com/apache/superset/pull/20728#discussion_r927762353
########## superset-frontend/src/dashboard/components/DatasourceResultsPane/index.tsx: ########## @@ -0,0 +1,232 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import React, { + useState, + useEffect, + useMemo, + useCallback, + useRef, +} from 'react'; +import { + BinaryQueryObjectFilterClause, + css, + Datasource, + ensureIsArray, + GenericDataType, + t, + useTheme, +} from '@superset-ui/core'; +import Loading from 'src/components/Loading'; +import { EmptyStateMedium } from 'src/components/EmptyState'; +import TableView, { EmptyWrapperType } from 'src/components/TableView'; +import { useTableColumns } from 'src/explore/components/DataTableControl'; +import { getDatasourceSamples } from 'src/components/Chart/chartAction'; +import TableControls from './TableControls'; + +const PAGE_SIZE = 50; + +export default function DatasourceResultsPane({ + datasource, + initialFilters, +}: { + datasource: Datasource; + initialFilters?: BinaryQueryObjectFilterClause[]; +}) { + const theme = useTheme(); + const [pageIndex, setPageIndex] = useState(0); + const lastPageIndex = useRef(pageIndex); + const [filters, setFilters] = useState(initialFilters || []); + const [isLoading, setIsLoading] = useState(false); + const [responseError, setResponseError] = useState(''); + const [resultsPages, setResultsPages] = useState< + Map< + number, + { + total: number; + data: Record<string, any>[]; + colNames: string[]; + colTypes: GenericDataType[]; + } + > + >(new Map()); + + // Get string identifier for dataset + const datasourceId = useMemo( + () => `${datasource.id}__${datasource.type}`, + [datasource], + ); + + // Get page of results + const resultsPage = useMemo(() => { + const nextResultsPage = resultsPages.get(pageIndex); + if (nextResultsPage) { + lastPageIndex.current = pageIndex; + return nextResultsPage; + } + + return resultsPages.get(lastPageIndex.current); + }, [pageIndex, resultsPages]); + + // Clear cache and reset page index if filters change + useEffect(() => { + setResultsPages(new Map()); + setPageIndex(0); + }, [filters]); + + // Download page of results if not already in cache + useEffect(() => { + if (!resultsPages.has(pageIndex)) { + setIsLoading(true); + getDatasourceSamples( + datasource.type, + datasource.id, + true, + filters.length ? { filters } : null, + { page: pageIndex + 1, perPage: PAGE_SIZE }, + ) + .then(response => { + setResultsPages( + new Map([ + ...resultsPages.entries(), + [ + pageIndex, + { + total: response.total_count, + data: response.data, + colNames: ensureIsArray(response.colnames), + colTypes: ensureIsArray(response.coltypes), + }, + ], + ]), + ); + setResponseError(''); + }) + .catch(error => { + setResponseError(`${error.name}: ${error.message}`); + }) + .finally(() => { + setIsLoading(false); + }); + } + }, [ + datasource.id, + datasource.type, + filters, + pageIndex, + resultsPage, + resultsPages, + ]); + + // this is to preserve the order of the columns, even if there are integer values, + // while also only grabbing the first column's keys + const columns = useTableColumns( + resultsPage?.colNames, + resultsPage?.colTypes, + resultsPage?.data, + datasourceId, + ); + + const sortDisabledColumns = columns.map(column => ({ + ...column, + disableSortBy: true, + })); + + // Update page index on pagination click + const onServerPagination = useCallback(({ pageIndex }) => { + setPageIndex(pageIndex); + }, []); + + // Clear cache on reload button click + const handleReload = useCallback(() => { + setResultsPages(new Map()); + }, []); + + // Render error if page download failed + if (responseError) { + return ( + <pre + css={css` + margin-top: ${theme.gridUnit * 4}px; + `} + > + {responseError} + </pre> + ); + } + + // Render loading if first page hasn't loaded + if (!resultsPages.size) { + return ( + <div + css={css` + height: ${theme.gridUnit * 25}px; + `} + > + <Loading /> + </div> + ); + } + + // Render empty state if no results are returned for page + if (resultsPage?.total === 0) { + const title = t('No rows were returned for this dataset'); + return <EmptyStateMedium image="document.svg" title={title} />; + } + + // Render chart if at least one page has successfully loaded + return ( + <div + css={css` + display: flex; + flex-direction: column; + `} + > + <TableControls + filters={filters} + setFilters={setFilters} + totalCount={resultsPage?.total} + onReload={handleReload} + /> + <div> Review Comment: `<div>...</div>` can be removed ########## superset-frontend/src/dashboard/components/DatasourceResultsPane/index.tsx: ########## @@ -0,0 +1,232 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import React, { + useState, + useEffect, + useMemo, + useCallback, + useRef, +} from 'react'; +import { + BinaryQueryObjectFilterClause, + css, + Datasource, + ensureIsArray, + GenericDataType, + t, + useTheme, +} from '@superset-ui/core'; +import Loading from 'src/components/Loading'; +import { EmptyStateMedium } from 'src/components/EmptyState'; +import TableView, { EmptyWrapperType } from 'src/components/TableView'; +import { useTableColumns } from 'src/explore/components/DataTableControl'; +import { getDatasourceSamples } from 'src/components/Chart/chartAction'; +import TableControls from './TableControls'; + +const PAGE_SIZE = 50; + +export default function DatasourceResultsPane({ + datasource, + initialFilters, +}: { + datasource: Datasource; + initialFilters?: BinaryQueryObjectFilterClause[]; +}) { + const theme = useTheme(); + const [pageIndex, setPageIndex] = useState(0); + const lastPageIndex = useRef(pageIndex); + const [filters, setFilters] = useState(initialFilters || []); + const [isLoading, setIsLoading] = useState(false); + const [responseError, setResponseError] = useState(''); + const [resultsPages, setResultsPages] = useState< Review Comment: I understand your point - want to be able to cache the viewed samples, but this cache can become infinitely large, and the browser may crash. Do we consider not caching here?(Not working is better than being slow), or setting a length for the map. ########## superset-frontend/src/dashboard/components/DatasourceResultsPane/index.tsx: ########## @@ -0,0 +1,232 @@ +/** Review Comment: For the naming, the ResultPane has used for the `results` tab, so we have to pick a new name for this. Another nitpicky request is that instead of writing logic in the `index.[t|j]sx?`, we should create a new file where the index file only export modules.  -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
