Skip to content

Commit

Permalink
AIP-38 Add logical date to trigger DagRun form and remove dataInterval (
Browse files Browse the repository at this point in the history
apache#46471)

* AIP-38 Add logical date to trigger DagRun form

* Automatically fill fields based on conditions

* Remove dataIntervalEnd and dataIntervalStart
  • Loading branch information
pierrejeambrun authored Feb 11, 2025
1 parent 67c4f77 commit c490605
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 78 deletions.
47 changes: 6 additions & 41 deletions airflow/ui/src/components/TriggerDag/TriggerDAGForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,21 @@ type TriggerDAGFormProps = {
export type DagRunTriggerParams = {
conf: string;
dagRunId: string;
dataIntervalEnd: string;
dataIntervalStart: string;
logicalDate: string;
note: string;
};

const TriggerDAGForm = ({ dagId, onClose, open }: TriggerDAGFormProps) => {
const [errors, setErrors] = useState<{ conf?: string; date?: unknown }>({});
const initialParamsDict = useDagParams(dagId, open);
const {
dateValidationError,
error: errorTrigger,
isPending,
triggerDagRun,
} = useTrigger({ dagId, onSuccessConfirm: onClose });
const { error: errorTrigger, isPending, triggerDagRun } = useTrigger({ dagId, onSuccessConfirm: onClose });
const { conf, setConf } = useParamStore();

const { control, handleSubmit, reset, watch } = useForm<DagRunTriggerParams>({
const { control, handleSubmit, reset } = useForm<DagRunTriggerParams>({
defaultValues: {
conf,
dagRunId: "",
dataIntervalEnd: "",
dataIntervalStart: "",
logicalDate: "",
note: "",
},
});
Expand All @@ -75,15 +68,6 @@ const TriggerDAGForm = ({ dagId, onClose, open }: TriggerDAGFormProps) => {
}
}, [conf, reset]);

useEffect(() => {
if (Boolean(dateValidationError)) {
setErrors((prev) => ({ ...prev, date: dateValidationError }));
}
}, [dateValidationError]);

const dataIntervalStart = watch("dataIntervalStart");
const dataIntervalEnd = watch("dataIntervalEnd");

const onSubmit = (data: DagRunTriggerParams) => {
triggerDagRun(data);
};
Expand Down Expand Up @@ -136,31 +120,12 @@ const TriggerDAGForm = ({ dagId, onClose, open }: TriggerDAGFormProps) => {
<Box p={5}>
<Controller
control={control}
name="dataIntervalStart"
name="logicalDate"
render={({ field }) => (
<Field.Root invalid={Boolean(errors.date)}>
<Field.Label fontSize="md">Data Interval Start Date</Field.Label>
<Input
{...field}
max={dataIntervalEnd || undefined}
onBlur={resetDateError}
placeholder="yyyy-mm-ddThh:mm"
size="sm"
type="datetime-local"
/>
</Field.Root>
)}
/>

<Controller
control={control}
name="dataIntervalEnd"
render={({ field }) => (
<Field.Root invalid={Boolean(errors.date)} mt={6}>
<Field.Label fontSize="md">Data Interval End Date</Field.Label>
<Field.Label fontSize="md">Logical Date</Field.Label>
<Input
{...field}
min={dataIntervalStart || undefined}
onBlur={resetDateError}
placeholder="yyyy-mm-ddThh:mm"
size="sm"
Expand Down
42 changes: 5 additions & 37 deletions airflow/ui/src/queries/useTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ export const useTrigger = ({ dagId, onSuccessConfirm }: { dagId: string; onSucce
const queryClient = useQueryClient();
const [error, setError] = useState<unknown>(undefined);

const [dateValidationError, setDateValidationError] = useState<unknown>(undefined);

const onSuccess = async () => {
const queryKeys = [
[useDagServiceGetDagsKey],
Expand Down Expand Up @@ -65,38 +63,10 @@ export const useTrigger = ({ dagId, onSuccessConfirm }: { dagId: string; onSucce
const triggerDagRun = (dagRunRequestBody: DagRunTriggerParams) => {
const parsedConfig = JSON.parse(dagRunRequestBody.conf) as Record<string, unknown>;

const DataIntervalStart = dagRunRequestBody.dataIntervalStart
? new Date(dagRunRequestBody.dataIntervalStart)
: undefined;
const DataIntervalEnd = dagRunRequestBody.dataIntervalEnd
? new Date(dagRunRequestBody.dataIntervalEnd)
: undefined;

if (Boolean(DataIntervalStart) !== Boolean(DataIntervalEnd)) {
setDateValidationError({
body: {
detail:
"Either both Data Interval Start Date and End Date must be provided, or both must be empty.",
},
});

return;
}

if (DataIntervalStart && DataIntervalEnd) {
if (DataIntervalStart > DataIntervalEnd) {
setDateValidationError({
body: {
detail: "Data Interval Start Date must be less than or equal to Data Interval End Date.",
},
});

return;
}
}
const logicalDate = dagRunRequestBody.logicalDate ? new Date(dagRunRequestBody.logicalDate) : undefined;

const formattedDataIntervalStart = DataIntervalStart?.toISOString() ?? undefined;
const formattedDataIntervalEnd = DataIntervalEnd?.toISOString() ?? undefined;
// eslint-disable-next-line unicorn/no-null
const formattedLogicalDate = logicalDate?.toISOString() ?? null;

const checkDagRunId = dagRunRequestBody.dagRunId === "" ? undefined : dagRunRequestBody.dagRunId;
const checkNote = dagRunRequestBody.note === "" ? undefined : dagRunRequestBody.note;
Expand All @@ -106,13 +76,11 @@ export const useTrigger = ({ dagId, onSuccessConfirm }: { dagId: string; onSucce
requestBody: {
conf: parsedConfig,
dag_run_id: checkDagRunId,
data_interval_end: formattedDataIntervalEnd,
data_interval_start: formattedDataIntervalStart,
logical_date: null,
logical_date: formattedLogicalDate,
note: checkNote,
},
});
};

return { dateValidationError, error, isPending, triggerDagRun };
return { error, isPending, triggerDagRun };
};

0 comments on commit c490605

Please sign in to comment.