bbovenzi commented on code in PR #68038:
URL: https://github.com/apache/airflow/pull/68038#discussion_r3382481640


##########
airflow-core/src/airflow/ui/src/pages/Dashboard/Deadlines/DashboardDeadlines.tsx:
##########


Review Comment:
   Let's split this up into one component per file



##########
airflow-core/src/airflow/ui/public/i18n/locales/en/dashboard.json:
##########
@@ -38,6 +51,7 @@
   "stats": {
     "activeDags": "Active Dags",
     "failedDags": "Failed Dags",
+    "missedDeadlines": "Missed Deadlines",

Review Comment:
   We already have this translation key. Let's use nesting to reference our 
existing key https://github.com/apache/airflow/issues/68252



##########
airflow-core/src/airflow/ui/src/pages/Dashboard/useDashboardDeadlines.ts:
##########
@@ -0,0 +1,74 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { useQuery } from "@tanstack/react-query";
+import dayjs from "dayjs";
+
+import { DeadlinesService } from "openapi/requests/services.gen";
+
+export const DASHBOARD_DEADLINE_LIMIT = 5;
+
+type DashboardDeadlinesQueryOptions = {
+  readonly refetchInterval: number | false;
+};
+
+export const getDeadlineWindow = () => {

Review Comment:
   Do we need this if we have a date selector in the history tab?
   
   We should then move the date selector above the deadline stats cards.



##########
airflow-core/src/airflow/ui/src/pages/Dashboard/useDashboardDeadlines.ts:
##########
@@ -0,0 +1,74 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { useQuery } from "@tanstack/react-query";
+import dayjs from "dayjs";
+
+import { DeadlinesService } from "openapi/requests/services.gen";
+
+export const DASHBOARD_DEADLINE_LIMIT = 5;
+
+type DashboardDeadlinesQueryOptions = {
+  readonly refetchInterval: number | false;
+};
+
+export const getDeadlineWindow = () => {
+  const currentMinute = dayjs().startOf("minute");
+
+  return {
+    lastDay: currentMinute.subtract(24, "hour").toISOString(),
+    now: currentMinute.toISOString(),
+  };
+};
+
+export const useDashboardPendingDeadlines = ({ refetchInterval }: 
DashboardDeadlinesQueryOptions) =>

Review Comment:
   No, let's use the existing hooks. We should almost never call any api 
Service directly. At most we wrap around the existing get deadlines hook.



##########
airflow-core/src/airflow/ui/src/pages/Dashboard/Deadlines/DashboardDeadlines.tsx:
##########
@@ -0,0 +1,178 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { Box, Flex, Heading, HStack, Link, Separator, Text, VStack } from 
"@chakra-ui/react";
+import { useTranslation } from "react-i18next";
+import { FiClock } from "react-icons/fi";
+import { Link as RouterLink } from "react-router-dom";
+
+import type { DeadlineResponse } from "openapi/requests/types.gen";
+import { ErrorAlert } from "src/components/ErrorAlert";
+import Time from "src/components/Time";
+import { TruncatedText } from "src/components/TruncatedText";
+import { SearchParamsKeys } from "src/constants/searchParams";
+import { useAutoRefresh } from "src/utils";
+
+import {
+  getDeadlineWindow,
+  useDashboardMissedDeadlines,
+  useDashboardPendingDeadlines,
+} from "../useDashboardDeadlines";
+
+const buildDeadlinesPath = (params: Record<string, string>) => {
+  const searchParams = new URLSearchParams(params);
+
+  return `/deadlines?${searchParams.toString()}`;
+};
+
+type DeadlineItemProps = {
+  readonly deadline: DeadlineResponse;
+};
+
+const DeadlineItem = ({ deadline }: DeadlineItemProps) => (
+  <HStack justify="space-between" px={3} py={2} width="100%">
+    <VStack align="start" gap={0} minWidth={0} overflow="hidden">
+      <Link asChild color="fg.info" fontSize="sm" fontWeight="medium">
+        <RouterLink to={`/dags/${deadline.dag_id}`}>
+          <TruncatedText text={deadline.dag_id} />
+        </RouterLink>
+      </Link>
+      <Link asChild color="fg.muted" fontSize="xs">
+        <RouterLink 
to={`/dags/${deadline.dag_id}/runs/${deadline.dag_run_id}`}>
+          <TruncatedText text={deadline.dag_run_id} />
+        </RouterLink>
+      </Link>
+    </VStack>
+    <Box color="fg.muted" flexShrink={0} fontSize="xs">
+      <Time datetime={deadline.deadline_time} />
+    </Box>
+  </HStack>
+);
+
+type DeadlineSectionProps = {
+  readonly deadlines: Array<DeadlineResponse>;
+  readonly emptyLabel: string;
+  readonly showMoreLabel: string;
+  readonly showMoreTo: string;
+  readonly subtitle?: string;
+  readonly title: string;
+  readonly totalEntries: number;
+};
+
+const DeadlineSection = ({
+  deadlines,
+  emptyLabel,
+  showMoreLabel,
+  showMoreTo,
+  subtitle,
+  title,
+  totalEntries,
+}: DeadlineSectionProps) => {
+  const hasMoreDeadlines = totalEntries > deadlines.length;
+
+  return (
+    <Box flex={1} minWidth={0}>
+      <Box borderRadius="md" borderWidth="1px" overflow="hidden">
+        <HStack justify="space-between" px={3} py={2}>
+          <Heading color="fg.muted" size="xs">
+            {title}
+          </Heading>
+          {subtitle === undefined ? undefined : (
+            <Text color="fg.muted" fontSize="xs">
+              {subtitle}
+            </Text>
+          )}
+        </HStack>
+        <Separator />
+        {deadlines.length === 0 ? (
+          <Text color="fg.muted" fontSize="sm" px={3} py={3} 
textAlign="center">
+            {emptyLabel}
+          </Text>
+        ) : (
+          <VStack align="stretch" gap={0} separator={<Separator />}>
+            {deadlines.map((deadline) => (
+              <DeadlineItem deadline={deadline} key={deadline.id} />
+            ))}
+          </VStack>
+        )}
+        {hasMoreDeadlines ? (
+          <>
+            <Separator />
+            <Flex justify="center" px={3} py={2}>
+              <Link asChild color="fg.info" fontSize="xs" fontWeight="medium">
+                <RouterLink to={showMoreTo}>{showMoreLabel}</RouterLink>
+              </Link>
+            </Flex>
+          </>
+        ) : undefined}
+      </Box>
+    </Box>
+  );
+};
+
+export const DashboardDeadlines = () => {
+  const { t: translate } = useTranslation("dashboard");
+  const deadlineWindow = getDeadlineWindow();
+  const refetchInterval = useAutoRefresh({ checkPendingRuns: true });
+
+  const { data: pendingData, error: pendingError } = 
useDashboardPendingDeadlines({ refetchInterval });
+  const { data: missedData, error: missedError } = 
useDashboardMissedDeadlines({ refetchInterval });
+
+  const pendingDeadlines = pendingData?.deadlines ?? [];
+  const missedDeadlines = missedData?.deadlines ?? [];
+  const pendingDeadlinesPath = buildDeadlinesPath({
+    [SearchParamsKeys.DEADLINE_TIME_GTE]: deadlineWindow.now,
+    [SearchParamsKeys.MISSED]: "false",
+  });
+  const missedDeadlinesPath = buildDeadlinesPath({
+    [SearchParamsKeys.DEADLINE_TIME_GTE]: deadlineWindow.lastDay,
+    [SearchParamsKeys.DEADLINE_TIME_LTE]: deadlineWindow.now,
+    [SearchParamsKeys.MISSED]: "true",
+  });
+
+  return (

Review Comment:
   Arguably if there are no deadline alerts to speak of we probably don't want 
to even bother rendering any of these cards.



##########
airflow-core/src/airflow/ui/src/pages/Dashboard/Deadlines/DashboardDeadlines.tsx:
##########
@@ -0,0 +1,178 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { Box, Flex, Heading, HStack, Link, Separator, Text, VStack } from 
"@chakra-ui/react";
+import { useTranslation } from "react-i18next";
+import { FiClock } from "react-icons/fi";
+import { Link as RouterLink } from "react-router-dom";
+
+import type { DeadlineResponse } from "openapi/requests/types.gen";
+import { ErrorAlert } from "src/components/ErrorAlert";
+import Time from "src/components/Time";
+import { TruncatedText } from "src/components/TruncatedText";
+import { SearchParamsKeys } from "src/constants/searchParams";
+import { useAutoRefresh } from "src/utils";
+
+import {
+  getDeadlineWindow,
+  useDashboardMissedDeadlines,
+  useDashboardPendingDeadlines,
+} from "../useDashboardDeadlines";
+
+const buildDeadlinesPath = (params: Record<string, string>) => {
+  const searchParams = new URLSearchParams(params);
+
+  return `/deadlines?${searchParams.toString()}`;
+};
+
+type DeadlineItemProps = {
+  readonly deadline: DeadlineResponse;
+};
+
+const DeadlineItem = ({ deadline }: DeadlineItemProps) => (
+  <HStack justify="space-between" px={3} py={2} width="100%">
+    <VStack align="start" gap={0} minWidth={0} overflow="hidden">
+      <Link asChild color="fg.info" fontSize="sm" fontWeight="medium">
+        <RouterLink to={`/dags/${deadline.dag_id}`}>
+          <TruncatedText text={deadline.dag_id} />
+        </RouterLink>
+      </Link>
+      <Link asChild color="fg.muted" fontSize="xs">
+        <RouterLink 
to={`/dags/${deadline.dag_id}/runs/${deadline.dag_run_id}`}>
+          <TruncatedText text={deadline.dag_run_id} />
+        </RouterLink>
+      </Link>
+    </VStack>
+    <Box color="fg.muted" flexShrink={0} fontSize="xs">
+      <Time datetime={deadline.deadline_time} />
+    </Box>
+  </HStack>
+);
+
+type DeadlineSectionProps = {
+  readonly deadlines: Array<DeadlineResponse>;
+  readonly emptyLabel: string;
+  readonly showMoreLabel: string;
+  readonly showMoreTo: string;
+  readonly subtitle?: string;
+  readonly title: string;
+  readonly totalEntries: number;
+};
+
+const DeadlineSection = ({
+  deadlines,
+  emptyLabel,
+  showMoreLabel,
+  showMoreTo,
+  subtitle,
+  title,
+  totalEntries,
+}: DeadlineSectionProps) => {
+  const hasMoreDeadlines = totalEntries > deadlines.length;
+
+  return (
+    <Box flex={1} minWidth={0}>
+      <Box borderRadius="md" borderWidth="1px" overflow="hidden">
+        <HStack justify="space-between" px={3} py={2}>
+          <Heading color="fg.muted" size="xs">
+            {title}
+          </Heading>
+          {subtitle === undefined ? undefined : (
+            <Text color="fg.muted" fontSize="xs">
+              {subtitle}
+            </Text>
+          )}
+        </HStack>
+        <Separator />
+        {deadlines.length === 0 ? (
+          <Text color="fg.muted" fontSize="sm" px={3} py={3} 
textAlign="center">
+            {emptyLabel}
+          </Text>
+        ) : (
+          <VStack align="stretch" gap={0} separator={<Separator />}>
+            {deadlines.map((deadline) => (
+              <DeadlineItem deadline={deadline} key={deadline.id} />
+            ))}
+          </VStack>
+        )}
+        {hasMoreDeadlines ? (
+          <>
+            <Separator />
+            <Flex justify="center" px={3} py={2}>
+              <Link asChild color="fg.info" fontSize="xs" fontWeight="medium">
+                <RouterLink to={showMoreTo}>{showMoreLabel}</RouterLink>
+              </Link>
+            </Flex>
+          </>
+        ) : undefined}
+      </Box>
+    </Box>
+  );
+};
+
+export const DashboardDeadlines = () => {
+  const { t: translate } = useTranslation("dashboard");
+  const deadlineWindow = getDeadlineWindow();
+  const refetchInterval = useAutoRefresh({ checkPendingRuns: true });
+
+  const { data: pendingData, error: pendingError } = 
useDashboardPendingDeadlines({ refetchInterval });
+  const { data: missedData, error: missedError } = 
useDashboardMissedDeadlines({ refetchInterval });
+
+  const pendingDeadlines = pendingData?.deadlines ?? [];
+  const missedDeadlines = missedData?.deadlines ?? [];
+  const pendingDeadlinesPath = buildDeadlinesPath({
+    [SearchParamsKeys.DEADLINE_TIME_GTE]: deadlineWindow.now,
+    [SearchParamsKeys.MISSED]: "false",
+  });
+  const missedDeadlinesPath = buildDeadlinesPath({
+    [SearchParamsKeys.DEADLINE_TIME_GTE]: deadlineWindow.lastDay,
+    [SearchParamsKeys.DEADLINE_TIME_LTE]: deadlineWindow.now,
+    [SearchParamsKeys.MISSED]: "true",
+  });
+
+  return (

Review Comment:
   Let's return undefined if there are no deadlines.



-- 
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]

Reply via email to