bbovenzi commented on code in PR #49519: URL: https://github.com/apache/airflow/pull/49519#discussion_r2054911817
########## airflow-core/src/airflow/ui/src/pages/Dashboard/Stats/StatsCard.tsx: ########## @@ -0,0 +1,112 @@ +/*! + * 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, HStack, Skeleton, Text } from "@chakra-ui/react"; +import { FiChevronRight } from "react-icons/fi"; +import { Link as RouterLink } from "react-router-dom"; + +import type { TaskInstanceState } from "openapi/requests/types.gen"; +import { StateBadge } from "src/components/StateBadge"; +import { capitalize } from "src/utils"; + +export const StatsCard = ({ + colorScheme, + count, + icon, + isLoading = false, + label, + link, + onClick, + state, +}: { + readonly colorScheme: string; + readonly count: number; + readonly icon?: React.ReactNode; + readonly isLoading?: boolean; + readonly label: string; + readonly link?: string; + readonly onClick?: () => void; + readonly state?: TaskInstanceState | null; +}) => { + const content = ( + <Box textAlign="left" width="100%"> + {isLoading ? ( + <Skeleton> + <Flex + alignItems="center" + bg="bg.surface" + borderColor={`${colorScheme}.border`} + borderRadius="lg" + borderWidth={1} + height="60px" + overflow="hidden" + px={2} + py={1} + /> + </Skeleton> + ) : ( + <Flex + _hover={{ + boxShadow: "sm", + transform: "translateY(-0.5px)", + transition: "all 0.1s", + }} + alignItems="center" + bg="bg.surface" + borderColor={`${colorScheme}.border`} + borderRadius="lg" + borderWidth={1} + cursor="pointer" + height="60px" + overflow="hidden" + px={2} + py={1} + width="100%" + > + <StateBadge colorPalette={colorScheme} fontSize="md" mr={2} state={state} variant="solid"> + {icon} + {count} + </StateBadge> + <Box flex={1}> + <HStack alignItems="center" gap={2}> + <Text + fontSize="sm" + fontWeight="bold" + maxWidth="100%" + overflow="hidden" + textOverflow="ellipsis" + whiteSpace="nowrap" + > + {capitalize(label)} + </Text> + </HStack> + </Box> + <Box color="gray.emphasized" px={1}> + <FiChevronRight size={16} /> + </Box> + </Flex> + )} + </Box> + ); + + if (onClick) { + return <Box onClick={onClick}>{content}</Box>; + } + + return <RouterLink to={link ?? "#"}>{content}</RouterLink>; Review Comment: ```suggestion if (isLoading) { return <Skeleton borderRadius="lg" height="42px" width="175px" />; } const content = ( <HStack alignItems="center" borderRadius="lg" borderWidth={1} color="fg.emphasized" cursor="pointer" p={2} > <StateBadge colorPalette={colorScheme} mr={2} state={state}> {icon} {count} </StateBadge> <Text color="fg" fontSize="sm" fontWeight="bold"> {capitalize(label)} </Text> <FiChevronRight size={16} /> </HStack> ); if (onClick) { return ( <Box as="button" onClick={onClick}> {content} </Box> ); } return <RouterLink to={link ?? "#"}>{content}</RouterLink>; ``` We have a lot of unnecessary component nesting and styling. This suggestion should clean this up a lot ########## airflow-core/src/airflow/ui/src/pages/Dashboard/Stats/Stats.tsx: ########## @@ -16,33 +16,87 @@ * specific language governing permissions and limitations * under the License. */ -import { Box, Flex, Heading, HStack } from "@chakra-ui/react"; -import { FiClipboard, FiZap } from "react-icons/fi"; +import { Box, Flex, Heading, SimpleGrid } from "@chakra-ui/react"; +import { FiClipboard } from "react-icons/fi"; +import { MdOutlineTask } from "react-icons/md"; -import { StateIcon } from "src/components/StateIcon"; +import { useDagServiceGetDags } from "openapi/queries"; import { DAGImportErrors } from "./DAGImportErrors"; -import { DagFilterButton } from "./DagFilterButton"; - -export const Stats = () => ( - <Box> - <Flex color="fg.muted" my={2}> - <FiClipboard /> - <Heading ml={1} size="xs"> - Links - </Heading> - </Flex> - <HStack> - <DagFilterButton colorPalette="failed" filter="failed" link="dags?last_dag_run_state=failed"> - <StateIcon state="failed" /> - </DagFilterButton> - <DAGImportErrors /> - <DagFilterButton colorPalette="running" filter="running" link="dags?last_dag_run_state=running"> - <StateIcon state="running" /> - </DagFilterButton> - <DagFilterButton colorPalette="blue" filter="active" link="dags?paused=false"> - <FiZap /> - </DagFilterButton> - </HStack> - </Box> -); +import { StatsCard } from "./StatsCard"; + +export const Stats = () => { + const { data: activeDagsData, isLoading: isActiveDagsLoading } = useDagServiceGetDags({ + paused: false, + }); + + const { data: failedDagsData, isLoading: isFailedDagsLoading } = useDagServiceGetDags({ + lastDagRunState: "failed", + }); + + const { data: queuedDagsData, isLoading: isQueuedDagsLoading } = useDagServiceGetDags({ + lastDagRunState: "queued", + }); + + const { data: runningDagsData, isLoading: isRunningDagsLoading } = useDagServiceGetDags({ + lastDagRunState: "running", + }); + + const activeDagsCount = activeDagsData?.total_entries ?? 0; + const failedDagsCount = failedDagsData?.total_entries ?? 0; + const queuedDagsCount = queuedDagsData?.total_entries ?? 0; + const runningDagsCount = runningDagsData?.total_entries ?? 0; + + return ( + <Box> + <Flex alignItems="center" color="fg.muted" my={2}> + <FiClipboard /> + <Heading ml={1} size="xs"> + Stats + </Heading> + </Flex> + + <SimpleGrid columns={{ base: 1, lg: 5, md: 3 }} gap={4}> + <StatsCard + colorScheme="failed" + count={failedDagsCount} + isLoading={isFailedDagsLoading} + label="Failed dags" + link="dags?last_dag_run_state=failed" + state="failed" + /> + + <DAGImportErrors /> + + {queuedDagsCount > 0 ? ( + <StatsCard + colorScheme="queued" + count={queuedDagsCount} + isLoading={isQueuedDagsLoading} + label="Queued dags" + link="dags?last_dag_run_state=queued" + state="queued" + /> + ) : undefined} + + <StatsCard + colorScheme="running" + count={runningDagsCount} + isLoading={isRunningDagsLoading} + label="Running dags" + link="dags?last_dag_run_state=running" + state="running" + /> + + <StatsCard + colorScheme="blue" + count={activeDagsCount} + icon={<MdOutlineTask />} Review Comment: ```suggestion icon={<FiZap />} ``` ########## airflow-core/src/airflow/ui/src/pages/Dashboard/Stats/Stats.tsx: ########## @@ -16,33 +16,87 @@ * specific language governing permissions and limitations * under the License. */ -import { Box, Flex, Heading, HStack } from "@chakra-ui/react"; -import { FiClipboard, FiZap } from "react-icons/fi"; +import { Box, Flex, Heading, SimpleGrid } from "@chakra-ui/react"; +import { FiClipboard } from "react-icons/fi"; +import { MdOutlineTask } from "react-icons/md"; -import { StateIcon } from "src/components/StateIcon"; +import { useDagServiceGetDags } from "openapi/queries"; import { DAGImportErrors } from "./DAGImportErrors"; -import { DagFilterButton } from "./DagFilterButton"; - -export const Stats = () => ( - <Box> - <Flex color="fg.muted" my={2}> - <FiClipboard /> - <Heading ml={1} size="xs"> - Links - </Heading> - </Flex> - <HStack> - <DagFilterButton colorPalette="failed" filter="failed" link="dags?last_dag_run_state=failed"> - <StateIcon state="failed" /> - </DagFilterButton> - <DAGImportErrors /> - <DagFilterButton colorPalette="running" filter="running" link="dags?last_dag_run_state=running"> - <StateIcon state="running" /> - </DagFilterButton> - <DagFilterButton colorPalette="blue" filter="active" link="dags?paused=false"> - <FiZap /> - </DagFilterButton> - </HStack> - </Box> -); +import { StatsCard } from "./StatsCard"; + +export const Stats = () => { + const { data: activeDagsData, isLoading: isActiveDagsLoading } = useDagServiceGetDags({ + paused: false, + }); + + const { data: failedDagsData, isLoading: isFailedDagsLoading } = useDagServiceGetDags({ + lastDagRunState: "failed", + }); + + const { data: queuedDagsData, isLoading: isQueuedDagsLoading } = useDagServiceGetDags({ + lastDagRunState: "queued", + }); + + const { data: runningDagsData, isLoading: isRunningDagsLoading } = useDagServiceGetDags({ + lastDagRunState: "running", + }); + + const activeDagsCount = activeDagsData?.total_entries ?? 0; + const failedDagsCount = failedDagsData?.total_entries ?? 0; + const queuedDagsCount = queuedDagsData?.total_entries ?? 0; + const runningDagsCount = runningDagsData?.total_entries ?? 0; + + return ( + <Box> + <Flex alignItems="center" color="fg.muted" my={2}> + <FiClipboard /> + <Heading ml={1} size="xs"> + Stats + </Heading> + </Flex> + + <SimpleGrid columns={{ base: 1, lg: 5, md: 3 }} gap={4}> Review Comment: <img width="673" alt="Screenshot 2025-04-22 at 4 52 34 PM" src="https://github.com/user-attachments/assets/3ee53f43-5810-4fce-9004-8411731296c6" /> I think a Hstack is better here. Otherwise we get an odd gap if a statcard isn't there or we're between breakpoints. <img width="513" alt="Screenshot 2025-04-22 at 4 52 12 PM" src="https://github.com/user-attachments/assets/ec511324-4476-43f1-bd00-6332b876c977" /> -- 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]
