Skip to content

Commit ea2362f

Browse files
shah-harshitclaude
andauthored
feat(ui): extract database/schema/table tab utils and config utils (open-metadata#28792)
* feat(ui): extract database/schema/table/ingestion tab and config utils (PR #13) Extract tab components and config utils from large source files into dedicated modules. Original files re-export all moved symbols for backward compatibility. New files: - Database/DatabaseDropdownOptions.tsx (database dropdown option builders) - Database/DatabaseTabsUtils.tsx (database page tab definitions) - DatabaseSchemaDropdownOptions.tsx (schema dropdown option builders) - DatabaseSchemaTabsUtils.tsx (schema page tab definitions) - TableDropdownOptions.tsx (table dropdown option builders) - TableTabsUtils.tsx (table page tab definitions) - IngestionConfigUtils.ts (ingestion configuration pure utils) - CronExpressionUtils.ts (cron schedule/expression utils) - DomainFilterUtils.ts (domain filter query builders) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: import getDayCron from CronExpressionUtils instead of SchedularUtils Removes transitive React/antd dependency from pure util IngestionConfigUtils.ts. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix checkstyle --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 388874a commit ea2362f

15 files changed

Lines changed: 2047 additions & 1729 deletions
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
/*
2+
* Copyright 2025 Collate.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
import { isUndefined, toNumber, toString } from 'lodash';
15+
16+
import type { RuleObject } from 'rc-field-form/es/interface';
17+
import type {
18+
Combination,
19+
StateValue,
20+
WorkflowExtraConfig,
21+
} from '../components/Settings/Services/AddIngestion/Steps/ScheduleInterval.interface';
22+
import {
23+
CRON_COMBINATIONS,
24+
DAY_OF_MONTH_PATTERN,
25+
DAY_OF_WEEK_PATTERN,
26+
DEFAULT_SCHEDULE_CRON_DAILY,
27+
DEFAULT_SCHEDULE_CRON_HOURLY,
28+
DEFAULT_SCHEDULE_CRON_MONTHLY,
29+
DEFAULT_SCHEDULE_CRON_WEEKLY,
30+
HOUR_PATTERN,
31+
MINUTE_PATTERN,
32+
MONTH_PATTERN,
33+
} from '../constants/Schedular.constants';
34+
import i18n from './i18next/LocalUtil';
35+
36+
export const getScheduleOptionsFromSchedules = (
37+
scheduleOptions: string[]
38+
): string[] => {
39+
return scheduleOptions.map((scheduleOption) => {
40+
switch (scheduleOption) {
41+
case 'run_once':
42+
return '';
43+
case 'hourly':
44+
return 'hour';
45+
case 'daily':
46+
return 'day';
47+
case 'weekly':
48+
return 'week';
49+
case 'monthly':
50+
return 'month';
51+
}
52+
53+
return '';
54+
});
55+
};
56+
57+
export const getRange = (n: number) => {
58+
return [...Array(n).keys()];
59+
};
60+
61+
export const getRangeOptions = (n: number) => {
62+
return getRange(n).map((v) => {
63+
return {
64+
label: `0${v}`.slice(-2),
65+
value: toString(v),
66+
};
67+
});
68+
};
69+
70+
export const getMinuteOptions = () => {
71+
return getRangeOptions(60);
72+
};
73+
74+
export const getHourOptions = () => {
75+
return getRangeOptions(24);
76+
};
77+
78+
export const getMinuteCron = (value: Partial<StateValue>) => {
79+
return `*/${value.min} * * * *`;
80+
};
81+
82+
export const getHourCron = (value: Partial<StateValue>) => {
83+
return `${value.min} * * * *`;
84+
};
85+
86+
export const getDayCron = (value: Partial<StateValue>) => {
87+
return `${value.min} ${value.hour} * * *`;
88+
};
89+
90+
export const getWeekCron = (value: Partial<StateValue>) => {
91+
return `${value.min} ${value.hour} * * ${value.dow}`;
92+
};
93+
94+
export const getMonthCron = (value: Partial<StateValue>) => {
95+
return `${value.min} ${value.hour} ${value.dom} * ${value.dow}`;
96+
};
97+
98+
export const getCron = (state: StateValue) => {
99+
const { selectedPeriod, cron } = state;
100+
101+
switch (selectedPeriod) {
102+
case 'hour':
103+
return getHourCron(state);
104+
case 'day':
105+
return getDayCron(state);
106+
case 'week':
107+
return getWeekCron(state);
108+
case 'month':
109+
return getMonthCron(state);
110+
default:
111+
return cron;
112+
}
113+
};
114+
115+
const getCronType = (cronStr: string) => {
116+
for (const c in CRON_COMBINATIONS) {
117+
if (CRON_COMBINATIONS[c as keyof Combination].test(cronStr)) {
118+
return c;
119+
}
120+
}
121+
122+
return 'custom';
123+
};
124+
125+
export const getStateValue = (value?: string, defaultValue?: string) => {
126+
const a = value?.split(' ');
127+
const d = a ?? defaultValue?.split(' ') ?? [];
128+
129+
const min = d[0];
130+
const hour = d[1];
131+
const dom = d[2];
132+
const dow = d[4];
133+
134+
const cronType = getCronType(value ?? defaultValue ?? '');
135+
136+
const stateVal: StateValue = {
137+
selectedPeriod: cronType,
138+
cron: value,
139+
min,
140+
hour,
141+
dow,
142+
dom,
143+
};
144+
145+
return stateVal;
146+
};
147+
148+
export const getCronDefaultValue = (appName: string) => {
149+
const value = {
150+
min: '0',
151+
hour: '0',
152+
};
153+
154+
let initialValue = getDayCron(value);
155+
156+
if (appName === 'DataInsightsReportApplication') {
157+
initialValue = getWeekCron({ ...value, dow: '0' });
158+
}
159+
160+
return initialValue;
161+
};
162+
163+
export const getDefaultScheduleValue = ({
164+
defaultSchedule,
165+
includePeriodOptions,
166+
allowNoSchedule = false,
167+
}: {
168+
defaultSchedule?: string;
169+
includePeriodOptions?: string[];
170+
allowNoSchedule?: boolean;
171+
}) => {
172+
if (isUndefined(includePeriodOptions)) {
173+
return allowNoSchedule
174+
? defaultSchedule
175+
: defaultSchedule || DEFAULT_SCHEDULE_CRON_DAILY;
176+
}
177+
178+
if (allowNoSchedule && isUndefined(defaultSchedule)) {
179+
return defaultSchedule;
180+
}
181+
182+
return getDefaultScheduleFromPeriod(includePeriodOptions);
183+
};
184+
185+
export const getDefaultScheduleFromPeriod = (
186+
includePeriodOptions: string[]
187+
) => {
188+
if (includePeriodOptions.includes('day')) {
189+
return DEFAULT_SCHEDULE_CRON_DAILY;
190+
} else if (includePeriodOptions.includes('week')) {
191+
return DEFAULT_SCHEDULE_CRON_WEEKLY;
192+
} else if (includePeriodOptions.includes('month')) {
193+
return DEFAULT_SCHEDULE_CRON_MONTHLY;
194+
} else if (includePeriodOptions.includes('hour')) {
195+
return DEFAULT_SCHEDULE_CRON_HOURLY;
196+
}
197+
198+
return DEFAULT_SCHEDULE_CRON_DAILY;
199+
};
200+
201+
export const getUpdatedStateFromFormState = <T>(
202+
currentState: StateValue,
203+
formValues: StateValue & WorkflowExtraConfig & T
204+
) => {
205+
try {
206+
const newState = { ...currentState, ...formValues };
207+
let { min, hour, dow, dom } = newState;
208+
209+
min = isNaN(toNumber(min)) ? '0' : min;
210+
hour = isNaN(toNumber(hour)) ? '0' : hour;
211+
const cronValue = newState.cron?.split(' ');
212+
213+
switch (newState.selectedPeriod) {
214+
case 'week':
215+
dow = isNaN(toNumber(dow)) ? '1' : dow;
216+
dom = '*';
217+
218+
break;
219+
case 'month':
220+
dom = isNaN(toNumber(dom)) ? '1' : dom;
221+
dow = '*';
222+
223+
break;
224+
case 'custom':
225+
min = cronValue?.[0] ?? '0';
226+
hour = cronValue?.[1] ?? '0';
227+
dom = cronValue?.[2] ?? '*';
228+
dow = cronValue?.[4] ?? '*';
229+
230+
break;
231+
}
232+
233+
return {
234+
...newState,
235+
min,
236+
hour,
237+
dow,
238+
dom,
239+
};
240+
} catch {
241+
return { ...currentState, ...formValues };
242+
}
243+
};
244+
245+
export const cronValidator = async (_: RuleObject, value: string) => {
246+
const trimmedValue = value.trim();
247+
248+
if (!trimmedValue) {
249+
return;
250+
}
251+
252+
const cronParts = trimmedValue.split(' ');
253+
254+
if (cronParts.length !== 5) {
255+
return Promise.reject(
256+
new Error(i18n.t('message.cron-invalid-field-count'))
257+
);
258+
}
259+
260+
const [minute, hour, dayOfMonth, month, dayOfWeek] = cronParts;
261+
262+
if (!MINUTE_PATTERN.test(minute)) {
263+
return Promise.reject(
264+
new Error(i18n.t('message.cron-invalid-minute-field'))
265+
);
266+
}
267+
if (!HOUR_PATTERN.test(hour)) {
268+
return Promise.reject(new Error(i18n.t('message.cron-invalid-hour-field')));
269+
}
270+
if (!DAY_OF_MONTH_PATTERN.test(dayOfMonth)) {
271+
return Promise.reject(
272+
new Error(i18n.t('message.cron-invalid-day-of-month-field'))
273+
);
274+
}
275+
if (!MONTH_PATTERN.test(month)) {
276+
return Promise.reject(
277+
new Error(i18n.t('message.cron-invalid-month-field'))
278+
);
279+
}
280+
if (!DAY_OF_WEEK_PATTERN.test(dayOfWeek)) {
281+
return Promise.reject(
282+
new Error(i18n.t('message.cron-invalid-day-of-week-field'))
283+
);
284+
}
285+
286+
try {
287+
const cronstrue = (await import('cronstrue/i18n')).default;
288+
const description = cronstrue.toString(trimmedValue);
289+
290+
const isFrequencyInMinutes = /Every \d* *minute/.test(description);
291+
const isFrequencyInSeconds = /Every \d* *second/.test(description);
292+
293+
if (isFrequencyInMinutes || isFrequencyInSeconds) {
294+
return Promise.reject(
295+
new Error(i18n.t('message.cron-less-than-hour-message'))
296+
);
297+
}
298+
299+
return Promise.resolve();
300+
} catch {
301+
return Promise.reject(new Error(i18n.t('message.cron-invalid-expression')));
302+
}
303+
};

0 commit comments

Comments
 (0)