EnxDev commented on code in PR #44982:
URL: https://github.com/apache/superset/pull/44982#discussion_r4189066970
##########
superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.test.tsx:
##########
@@ -38,3 +41,95 @@ test('getRefreshWarningMessage normalizes refreshLimit', ()
=> {
expect(getRefreshWarningMessage(5, 10, 'warn')).toBe('warn');
expect(getRefreshWarningMessage(15, 10000, 'warn')).toBeNull();
});
+
+test('getRefreshFrequencyOptions honours the configured intervals', () => {
+ const options = getRefreshFrequencyOptions([
+ [0, "Don't refresh"],
+ [600, '10 minutes'],
+ [1800, '30 minutes'],
+ [3600, '1 hour'],
+ ]);
+
+ expect(options).toEqual([
+ { value: 0, label: "Don't refresh" },
+ { value: 600, label: '10 minutes' },
+ { value: 1800, label: '30 minutes' },
+ { value: 3600, label: '1 hour' },
+ { value: CUSTOM_REFRESH_FREQUENCY, label: 'Custom' },
+ ]);
+});
+
+test('getRefreshFrequencyOptions falls back to the built-in list when the
config is absent', () => {
+ expect(getRefreshFrequencyOptions(undefined)).toEqual(
+ REFRESH_FREQUENCY_OPTIONS,
+ );
+ expect(getRefreshFrequencyOptions([])).toEqual(REFRESH_FREQUENCY_OPTIONS);
+ expect(getRefreshFrequencyOptions('10 seconds')).toEqual(
+ REFRESH_FREQUENCY_OPTIONS,
+ );
+});
+
+test('getRefreshFrequencyOptions drops malformed entries instead of rendering
them', () => {
+ const options = getRefreshFrequencyOptions([
+ [600],
+ ['not-a-number', 'Ten minutes'],
+ [-5, 'Negative'],
+ [1800, ' '],
+ [1800, null],
+ [3600, '1 hour'],
+ ]);
+
+ expect(options).toEqual([
+ { value: 3600, label: '1 hour' },
+ { value: CUSTOM_REFRESH_FREQUENCY, label: 'Custom' },
+ ]);
+});
+
+test('getRefreshFrequencyOptions keeps configured labels verbatim, including
untranslated ones', () => {
Review Comment:
This passes with `t(label)` too, since no language pack is loaded in tests,
so it doesn't pin the behaviour the docstring describes.
If you switch to `t()` (see the note on line 121), worth swapping this for a
test that loads a translation for `10 seconds` and checks it comes through.
##########
superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.tsx:
##########
@@ -62,16 +72,73 @@ export const REFRESH_FREQUENCY_OPTIONS = [
{ value: 21600, label: t('6 hours') },
{ value: 43200, label: t('12 hours') },
{ value: 86400, label: t('24 hours') },
- { value: -1, label: t('Custom') },
+ { value: CUSTOM_REFRESH_FREQUENCY, label: t('Custom') },
];
-const isPresetValue = (frequency: number) =>
- REFRESH_FREQUENCY_OPTIONS.some(
- option => option.value === frequency && option.value !== -1,
+const isPresetValue = (frequency: number, options: RefreshFrequencyOption[]) =>
+ options.some(
+ option =>
+ option.value === frequency && option.value !== CUSTOM_REFRESH_FREQUENCY,
+ );
+
+// `Number(null)`, `Number('')` and `Number([])` all yield 0, and 0 is a real
+// interval, so a bare Number() would smuggle malformed entries in as valid.
+const toIntervalSeconds = (value: unknown): number => {
+ const isNumeric = typeof value === 'number' || typeof value === 'string';
+ return isNumeric && String(value).trim() !== '' ? Number(value) : Number.NaN;
+};
+
+/**
+ * Builds the interval list from DASHBOARD_AUTO_REFRESH_INTERVALS, which
reaches
+ * the browser as a list of `[seconds, label]` pairs. Labels stay verbatim: the
+ * deployment authored them, so translating them here would rewrite operator
+ * wording. Anything that is not a well-formed non-empty list falls back to the
+ * built-in options rather than rendering an empty selector.
+ */
+export const getRefreshFrequencyOptions = (
+ configuredIntervals?: unknown,
+): RefreshFrequencyOption[] => {
+ const options: RefreshFrequencyOption[] = [];
+ const seen = new Set<number>();
+
+ (Array.isArray(configuredIntervals) ? configuredIntervals : []).forEach(
+ pair => {
+ if (!Array.isArray(pair)) {
+ return;
+ }
+ const [value, label] = pair;
+ const seconds = toIntervalSeconds(value);
+ const isLabelled = typeof label === 'string' && label.trim() !== '';
+ if (!Number.isFinite(seconds) || seconds < 0 || !isLabelled) {
+ return;
+ }
+ // Deployment overrides are unvalidated; a repeated interval would render
+ // duplicate React keys and two simultaneously-checked radios.
+ if (seen.has(seconds)) {
+ return;
+ }
+ seen.add(seconds);
+ options.push({ value: seconds, label });
Review Comment:
`config.py` ships these labels in English, and both 6.0
(`t(predefinedValue[1])`) and the 2022 version (`t(option[1])`) rendered them
through `t()`. As is, a German or French UI on the default config shows "Don't
refresh" and "10 seconds" in English.
`t()` returns strings it has no entry for unchanged, so `10 Minuten`
survives either way, and the literal `t()` calls in `REFRESH_FREQUENCY_OPTIONS`
keep the msgids extracted:
```suggestion
options.push({ value: seconds, label: t(label) });
```
##########
superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.test.tsx:
##########
@@ -38,3 +41,95 @@ test('getRefreshWarningMessage normalizes refreshLimit', ()
=> {
expect(getRefreshWarningMessage(5, 10, 'warn')).toBe('warn');
expect(getRefreshWarningMessage(15, 10000, 'warn')).toBeNull();
});
+
+test('getRefreshFrequencyOptions honours the configured intervals', () => {
+ const options = getRefreshFrequencyOptions([
+ [0, "Don't refresh"],
+ [600, '10 minutes'],
+ [1800, '30 minutes'],
+ [3600, '1 hour'],
+ ]);
+
+ expect(options).toEqual([
+ { value: 0, label: "Don't refresh" },
+ { value: 600, label: '10 minutes' },
+ { value: 1800, label: '30 minutes' },
+ { value: 3600, label: '1 hour' },
+ { value: CUSTOM_REFRESH_FREQUENCY, label: 'Custom' },
+ ]);
+});
+
+test('getRefreshFrequencyOptions falls back to the built-in list when the
config is absent', () => {
+ expect(getRefreshFrequencyOptions(undefined)).toEqual(
+ REFRESH_FREQUENCY_OPTIONS,
+ );
+ expect(getRefreshFrequencyOptions([])).toEqual(REFRESH_FREQUENCY_OPTIONS);
+ expect(getRefreshFrequencyOptions('10 seconds')).toEqual(
+ REFRESH_FREQUENCY_OPTIONS,
+ );
+});
+
+test('getRefreshFrequencyOptions drops malformed entries instead of rendering
them', () => {
+ const options = getRefreshFrequencyOptions([
+ [600],
+ ['not-a-number', 'Ten minutes'],
+ [-5, 'Negative'],
+ [1800, ' '],
+ [1800, null],
+ [3600, '1 hour'],
+ ]);
+
+ expect(options).toEqual([
+ { value: 3600, label: '1 hour' },
+ { value: CUSTOM_REFRESH_FREQUENCY, label: 'Custom' },
+ ]);
+});
+
+test('getRefreshFrequencyOptions keeps configured labels verbatim, including
untranslated ones', () => {
+ const options = getRefreshFrequencyOptions([[600, '10 Minuten']]);
+
+ expect(options[0].label).toBe('10 Minuten');
+});
+
+test('getRefreshFrequencyOptions does not append a second Custom entry', () =>
{
+ const options = getRefreshFrequencyOptions([
+ [600, '10 minutes'],
+ [CUSTOM_REFRESH_FREQUENCY, 'Custom'],
Review Comment:
Nit, take it or leave it. `[-1, 'Custom']` is dropped by the `seconds < 0`
guard before `hasCustom` ever sees it, so this passes whatever `hasCustom` does.
On the configured path `hasCustom` can't be true, and the component renders
its own `t('Custom')` anyway. Could point this at the fallback list, or drop it.
--
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]