Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOP-22993] add transfer schedule #61

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"clsx": "2.1.1",
"dayjs": "1.11.13",
"dotenv-webpack": "8.1.0",
"rc-picker": "4.9.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-error-boundary": "4.0.13",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Form, Input, Switch } from 'antd';
import { CronSelect } from '@shared/ui';
import { Form, Switch } from 'antd';
import React, { useState } from 'react';

export const TransferSchedule = () => {
Expand All @@ -16,7 +17,7 @@ export const TransferSchedule = () => {
</Form.Item>
{isScheduled && (
<Form.Item label="Schedule" name="schedule" rules={[{ required: true }]}>
<Input size="large" />
<CronSelect />
</Form.Item>
)}
</>
Expand Down
12 changes: 6 additions & 6 deletions src/features/transfer/TransferDetailInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { Descriptions } from 'antd';
import { Link } from 'react-router-dom';
import { CronService } from '@shared/services';

import { TransferDetailInfoProps } from './types';
import classes from './styles.module.less';
Expand Down Expand Up @@ -32,12 +33,11 @@ export const TransferDetailInfo = ({
<Descriptions.Item label="Queue" span={3}>
<Link to={`/queues/${queue.id}`}>{queue.name}</Link>
</Descriptions.Item>
<Descriptions.Item label="Is scheduled" span={3}>
{transfer.is_scheduled ? 'Yes' : 'No'}
</Descriptions.Item>
<Descriptions.Item label="Schedule" span={3}>
{transfer.schedule}
</Descriptions.Item>
{transfer.is_scheduled && (
<Descriptions.Item label="Schedule" span={3}>
{new CronService(transfer.schedule).getSchedule()}
</Descriptions.Item>
)}
<Descriptions.Item label="Strategy params" span={3}>
{transfer.strategy_params.type}
</Descriptions.Item>
Expand Down
10 changes: 10 additions & 0 deletions src/shared/services/cronService/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CronSegmentKey, CronSegmentValue, DayOfWeekName } from './types';

export const CRON_VALUE_DEFAULT = new Map<CronSegmentKey, CronSegmentValue>([
['minute', new Date().getMinutes()],
['hour', new Date().getHours()],
['date', null],
['day', null],
]);

export const DAYS_OF_WEEK = Object.values(DayOfWeekName);
158 changes: 158 additions & 0 deletions src/shared/services/cronService/cronService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { getOrdinalNumber } from '@shared/utils';

import { CRON_VALUE_DEFAULT, DAYS_OF_WEEK } from './constants';
import { CronSegmentKey, CronSegmentValue, Period } from './types';

/** Class for convenient handling cron settings */
export class CronService {
private initialValueLength = 5;

private period: Period;

private value: Map<CronSegmentKey, CronSegmentValue>;

constructor(initialValue?: string) {
this.value = this.transformInitialValueToMap(initialValue);
this.period = this.initPeriod();
}

private transformInitialValueToMap(initialValue?: string) {
const splittedValue = initialValue?.split(' ');
if (splittedValue?.length !== this.initialValueLength) {
return CRON_VALUE_DEFAULT;
}

const cronValue = splittedValue.map((segment) => {
const parsedValue = parseInt(segment);
if (Number.isInteger(parsedValue)) {
return parsedValue;
}
return null;
});

return new Map<CronSegmentKey, CronSegmentValue>([
['minute', cronValue[0]],
['hour', cronValue[1]],
['date', cronValue[2]],
['day', cronValue[4]],
]);
}

private initPeriod() {
if (this.getMonthDay() === null && this.getWeekDay() === null) {
return Period.DAY;
}
if (this.getMonthDay()) {
return Period.MONTH;
}
return Period.WEEK;
}

getPeriod() {
return this.period;
}

getMinute(): number {
return this.value.get('minute')!;
}

getHour(): number {
return this.value.get('hour')!;
}

getTime() {
return `${this.getHour()}:${this.getMinute()}`;
}

getMonthDay(): CronSegmentValue {
return this.value.get('date') ?? null;
}

getWeekDay(): CronSegmentValue {
return this.value.get('day') ?? null;
}

setPeriod(period: Period) {
this.period = period;
switch (period) {
case Period.DAY:
this.setMonthDay(null);
this.setWeekDay(null);
break;
case Period.WEEK:
this.setWeekDay(new Date().getDay());
this.setMonthDay(null);
break;
case Period.MONTH:
this.setWeekDay(null);
this.setMonthDay(new Date().getDate());
}
}

setMinute(value: number) {
if (value < 0 || value > 59) {
jellySWATy marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Invalid value');
}
this.value.set('minute', value);
}

setHour(value: number) {
if (value < 0 || value > 23) {
throw new Error('Invalid value');
}
this.value.set('hour', value);
}

setTime(hour?: number, minute?: number) {
this.setHour(hour ?? new Date().getHours());
this.setMinute(minute ?? new Date().getMinutes());
}

setMonthDay(value: CronSegmentValue) {
if (value === null) {
this.value.set('date', null);
return;
}
if (value < 1 || value > 31) {
jellySWATy marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Invalid value');
}
this.value.set('date', value);
}

setWeekDay(value: CronSegmentValue) {
if (value === null) {
this.value.set('day', null);
return;
}
if (value < 0 || value > 6) {
jellySWATy marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Invalid value');
}
this.value.set('day', value);
}

toString() {
const minute = this.getMinute();
const hour = this.getHour();
const date = this.getMonthDay() ?? '*';
const day = this.getWeekDay() ?? '*';
return `${minute} ${hour} ${date} * ${day}`;
}

getSchedule() {
const time = this.getTime();
const day = this.getWeekDay();
const date = this.getMonthDay();

let schedule = `Every ${this.period} `;

if (day !== null) {
schedule += `on ${DAYS_OF_WEEK[day]} `;
} else if (date) {
schedule += `${getOrdinalNumber(date)} `;
}

schedule += `at ${time}`;

return schedule;
}
}
2 changes: 2 additions & 0 deletions src/shared/services/cronService/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './cronService';
export * from './types';
29 changes: 29 additions & 0 deletions src/shared/services/cronService/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export type CronSegmentValue = number | null;

export type CronSegmentKey = 'date' | 'day' | 'hour' | 'minute';

export enum Period {
DAY = 'day',
WEEK = 'week',
MONTH = 'month',
}

export enum DayOfWeek {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
}

export enum DayOfWeekName {
SUNDAY = 'Sunday',
MONDAY = 'Monday',
TUESDAY = 'Tuesday',
WEDNESDAY = 'Wednesday',
THURSDAY = 'Thursday',
FRIDAY = 'Friday',
SATURDAY = 'Saturday',
}
1 change: 1 addition & 0 deletions src/shared/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cronService';
5 changes: 5 additions & 0 deletions src/shared/ui/Calendar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Dayjs } from 'dayjs';
import dayjsGenerateConfig from 'rc-picker/lib/generate/dayjs';
import generateCalendar from 'antd/es/calendar/generateCalendar';

export const Calendar = generateCalendar<Dayjs>(dayjsGenerateConfig);
39 changes: 39 additions & 0 deletions src/shared/ui/CronSelect/components/DynamicSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { Select } from 'antd';
import { getOrdinalNumber } from '@shared/utils';
import { Period } from '@shared/services';

import classes from '../../styles.module.less';
import { DAYS_OF_MONTH_SELECT_OPTIONS, DAYS_OF_WEEK_SELECT_OPTIONS } from '../../constants';

import { DynamicSelectProps } from './types';

export const DynamicSelect = ({ period, weekDay, monthDay, onChangeWeekDay, onChangeMonthDay }: DynamicSelectProps) => {
switch (period) {
case Period.WEEK:
return (
<Select
className={classes.day}
size="large"
onChange={onChangeWeekDay}
options={DAYS_OF_WEEK_SELECT_OPTIONS}
value={weekDay}
/>
);
case Period.MONTH:
return (
<div className={classes.month}>
<Select
className={classes.date}
size="large"
onChange={onChangeMonthDay}
options={DAYS_OF_MONTH_SELECT_OPTIONS}
value={monthDay}
/>
<span>{getOrdinalNumber(monthDay!, true)}</span>
</div>
);
default:
return null;
}
};
15 changes: 15 additions & 0 deletions src/shared/ui/CronSelect/components/DynamicSelect/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CronSegmentValue, Period } from '@shared/services';

/** Interface as Props for component "DynamicSelect" */
export interface DynamicSelectProps {
/** Value of period Select */
period: Period;
/** Value of week day Select */
weekDay: CronSegmentValue;
/** Value of month day Select */
monthDay: CronSegmentValue;
/** Callback for changing value of week day Select */
onChangeWeekDay: (value: CronSegmentValue) => void;
/** Callback for changing value of month day Select */
onChangeMonthDay: (value: CronSegmentValue) => void;
}
1 change: 1 addition & 0 deletions src/shared/ui/CronSelect/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DynamicSelect';
28 changes: 28 additions & 0 deletions src/shared/ui/CronSelect/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DayOfWeek, DayOfWeekName, Period } from '@shared/services';
import { prepareOptionsForSelect } from '@shared/ui';

export const PERIOD_SELECT_OPTIONS = prepareOptionsForSelect({
data: Object.values(Period),
renderLabel: (data) => data,
renderValue: (data) => data,
});

export const DAYS_OF_WEEK_SELECT_OPTIONS = prepareOptionsForSelect({
data: [
{ value: DayOfWeek.MONDAY, label: DayOfWeekName.MONDAY },
{ value: DayOfWeek.TUESDAY, label: DayOfWeekName.TUESDAY },
{ value: DayOfWeek.WEDNESDAY, label: DayOfWeekName.WEDNESDAY },
{ value: DayOfWeek.THURSDAY, label: DayOfWeekName.THURSDAY },
{ value: DayOfWeek.FRIDAY, label: DayOfWeekName.FRIDAY },
{ value: DayOfWeek.SATURDAY, label: DayOfWeekName.SATURDAY },
{ value: DayOfWeek.SUNDAY, label: DayOfWeekName.SUNDAY },
],
renderLabel: (data) => data.label,
renderValue: (data) => data.value,
});

export const DAYS_OF_MONTH_SELECT_OPTIONS = prepareOptionsForSelect({
data: Array.from({ length: 31 }, (_, index) => index + 1),
renderLabel: (data) => data,
renderValue: (data) => data,
});
1 change: 1 addition & 0 deletions src/shared/ui/CronSelect/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useCron';
2 changes: 2 additions & 0 deletions src/shared/ui/CronSelect/hooks/useCron/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useCron';
export * from './types';
7 changes: 7 additions & 0 deletions src/shared/ui/CronSelect/hooks/useCron/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** Interface as Props for hook "useCron" */
export interface UseCronProps {
/** Value of cron expression like "* * * * *" */
value?: string;
/** Callback for changing value of cron expression */
onChange?: (value: string) => void;
}
Loading
Loading