Skip to content

Add pre-filter option to Time Grain dashboard control - #42849

Open
sonfire186 wants to merge 6 commits into
apache:masterfrom
sonfire186:patch-3
Open

Add pre-filter option to Time Grain dashboard control#42849
sonfire186 wants to merge 6 commits into
apache:masterfrom
sonfire186:patch-3

Conversation

@sonfire186

@sonfire186 sonfire186 commented Aug 6, 2026

Copy link
Copy Markdown

Title: feat(dashboard): add per-dashboard time grain allowlist via time_grain_allowlist in JSON metadata

Description:

This MR introduces the ability to restrict available time grains at the dashboard level by adding a time_grain_allowlist field to the dashboard JSON metadata. When set, all Time Grain native filters on that dashboard will only display the specified time grains, regardless of what the underlying datasource provides.

Motivation

Currently, Superset allows global time grain restrictions via TIME_GRAIN_DENYLIST in superset_config.py, but there is no way to limit time grains per dashboard. Users have requested the ability to show different time grain options on different dashboards (e.g., hourly/monthly on Dashboard A, monthly/yearly on Dashboard B) without manually editing each filter's settings.

Changes

1. Backend — superset/dashboards/schemas.py

  • Added time_grain_allowlist to DashboardJSONMetadataSchema as a List(Str) field with allow_none=True.
  • This allows the dashboard API (PUT /api/v1/dashboard/{id}) to accept and validate the new field in json_metadata without throwing "Unknown field.".

2. Frontend — TimeGrainFilterPlugin.tsx

  • Reads dashboardInfo.metadata.time_grain_allowlist from the Redux store via ReactReduxContext (safe for plugin context where useSelector may fail).
  • Filters options based on the dashboard allowlist. Priority:
    1. Dashboard time_grain_allowlist
    2. Filter-level timeGrains (existing behavior)
    3. All datasource time grains (fallback)
  • Uses useRef (hasInitRef) to apply defaultValue exactly once after options are ready, preventing race conditions and "Cannot load filter" errors on initial render.
  • validValue is computed directly from filterState.value intersected with options, ensuring the Select component never receives a value that does not exist in its option list.

Usage

Edit a dashboard → Edit propertiesAdvancedJSON Metadata:

{
  "time_grain_allowlist": ["PT1H", "P1M"],
  "native_filter_configuration": [ ... ]
}

All Time Grain filters on this dashboard will now show only Hour and Month.

Supported values

Value Label
PT1S Second
PT1M Minute
PT5M 5 Minutes
PT10M 10 Minutes
PT15M 15 Minutes
PT30M 30 Minutes
PT1H Hour
P1D Day
P1W Week
P1W/1970-01-05T00:00:00Z Week starting Monday
1969-12-29T00:00:00Z/P1W Week ending Sunday
P1M Month
P3M Quarter
P1Y Year

Backward compatibility

  • If time_grain_allowlist is absent from dashboard metadata, behavior is unchanged.
  • If present but empty [], all time grains are shown (same as no restriction).

Testing

  • Verify PUT /api/v1/dashboard/{id} accepts json_metadata with time_grain_allowlist.
  • Verify Time Grain filter shows only allowed grains when time_grain_allowlist is set.
  • Verify filter works normally when time_grain_allowlist is absent.
  • Verify no "Cannot load filter" error on first dashboard load with default value.

@bito-code-review

bito-code-review Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Bito Review Skipped - Source Branch Not Found

Bito didn’t review this change because the pull request is no longer valid. It may have been merged, or the source/target branch may no longer exist.

@dosubot dosubot Bot added change:backend Requires changing the backend change:frontend Requires changing the frontend dashboard:native-filters Related to the native filters of the Dashboard labels Aug 6, 2026
@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.68293% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.73%. Comparing base (de93a19) to head (be42b3e).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
...ers/components/TimeGrain/TimeGrainFilterPlugin.tsx 92.50% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #42849   +/-   ##
=======================================
  Coverage   65.73%   65.73%           
=======================================
  Files        2843     2843           
  Lines      162659   162677   +18     
  Branches    37239    37244    +5     
=======================================
+ Hits       106921   106939   +18     
- Misses      53645    53646    +1     
+ Partials     2093     2092    -1     
Flag Coverage Δ
hive 37.96% <100.00%> (+<0.01%) ⬆️
javascript 72.09% <92.50%> (+<0.01%) ⬆️
mysql 57.79% <100.00%> (+<0.01%) ⬆️
postgres 57.84% <100.00%> (+<0.01%) ⬆️
presto 39.87% <100.00%> (+<0.01%) ⬆️
python 59.21% <100.00%> (+<0.01%) ⬆️
sqlite 57.48% <100.00%> (+<0.01%) ⬆️
unit 100.00% <ø> (ø)

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

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sadpandajoe
sadpandajoe requested review from hainenber and a lite review from Copilot August 6, 2026 17:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@bito-code-review

bito-code-review Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #23ff5f

Actionable Suggestions - 0
Additional Suggestions - 1
  • superset-frontend/src/filters/components/TimeGrain/TimeGrainFilterPlugin.tsx - 1
    • CWE-862: Unverified Context Access · Line 54-57
      Line 54 uses `useContext(ReactReduxContext)` and `store.getState()` — an unusual pattern inconsistent with the rest of the codebase. All other components in `src/dashboard` use `useSelector` to access `dashboardInfo.metadata` (confirmed across 65+ usages). This manual context + getState call could break if Provider setup changes and is harder to test.
Review Details
  • Files reviewed - 2 · Commit Range: 201039d..be42b3e
    • superset-frontend/src/filters/components/TimeGrain/TimeGrainFilterPlugin.tsx
    • superset/dashboards/schemas.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend change:frontend Requires changing the frontend dashboard:native-filters Related to the native filters of the Dashboard size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants