apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
63.06k stars 13.96k forks source link

Pagination is not reset on charts after updating an applied filter #30244

Open vineetdpnd opened 2 months ago

vineetdpnd commented 2 months ago

Bug description

When server pagination is enabled on a chart and the page is navigated to a higher page number (e.g., 10th page), applying a new filter with a shorter time range (which would result in fewer pages) causes the chart to not load data. It seems that the pagination is not being reset after the filter is updated, causing the chart to request data for a page that no longer exists.

How to reproduce the bug

  1. Enable server pagination on any chart with a significant amount of data for a specific time period (e.g., 30 days) and configure pagination with a maximum of 100 records per page.
  2. The chart will load with several pages of data.
  3. Navigate to a higher page (e.g., the 10th page).
  4. Update the filter to select a shorter time range (e.g., 1 day), assuming the new data set would not have as many pages as the previously selected time period.
  5. The chart fails to load any data, likely because it is still attempting to fetch data for the previous page number.

Screenshots/recordings

No response

Superset version

4.0.2

Python version

3.11

Node version

16

Browser

Chrome

Additional context

No response

Checklist

dosubot[bot] commented 2 months ago

Hey @vineetdpnd! 👋 I'm here to help you with any bugs, questions, or contributions you have for Superset. Let's tackle this together!

To address the issue of pagination not resetting on charts after updating an applied filter, you can use the useEffect hook to clear the cache and reset the page index whenever the filters change. This is already implemented in the Superset codebase as follows:

// Clear cache and reset page index if filters change
useEffect(() => {
  setResponseError('');
  setResultsPages(new Map());
  setPageIndex(0);
}, [filters]);

This useEffect hook ensures that whenever the filters state changes, the responseError is cleared, the resultsPages cache is reset, and the pageIndex is set to 0. This effectively resets the pagination to the first page, avoiding loading issues [1].

Additionally, in the buildQuery function, there is logic to reset the row_offset to 0 if the filters have changed, ensuring that the pagination starts from the first page:

if (
  formData.server_pagination &&
  options?.extras?.cachedChanges?.[formData.slice_id] &&
  JSON.stringify(options?.extras?.cachedChanges?.[formData.slice_id]) !==
    JSON.stringify(queryObject.filters)
) {
  queryObject = { ...queryObject, row_offset: 0 };
  updateExternalFormData(
    options?.hooks?.setDataMask,
    0,
    queryObject.row_limit ?? 0,
  );
}

This ensures that the pagination is reset on the server side as well, providing a consistent experience when filters are updated [2].

To continue talking to Dosu, mention @dosu.

vineetdpnd commented 2 months ago

We have checked it, the limit & offset value is not reset on the queries post change of the applied filters, so it does seem to be bug