sonfire186 opened a new pull request, #42849:
URL: https://github.com/apache/superset/pull/42849
**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 properties** → **Advanced** → **JSON Metadata**:
```json
{
"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.
--
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]