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


##########
airflow-core/src/airflow/ui/src/pages/Dag/Overview/Overview.tsx:
##########
@@ -66,6 +68,22 @@ export const Overview = () => {
 
   const failedTaskCount = failedTasks?.total_entries ?? 0;
 
+  // Recent completed instances feed the per-task median; the endpoint caps the
+  // page at the configured n_page_limit, so this is the recent window, not 
all runs.
+  const { data: slowestTasksData, isLoading: isLoadingSlowestTasks } = 
useTaskInstanceServiceGetTaskInstances(
+    {
+      dagId: dagId ?? "",
+      dagRunId: "~",
+      limit: 100,
+      orderBy: ["-run_after"],
+      runAfterGte: startDate,
+      runAfterLte: endDate,
+      state: ["success", "failed"],
+    },
+  );
+
+  const slowestTasks = aggregateSlowestTasks(slowestTasksData?.task_instances 
?? [], 10);

Review Comment:
   Until we have a custom /ui endpoint to aggregate median task duration. Then 
this chart should just say "Slowest task instances and we shouldn't try to 
aggregate them by task_id"



##########
airflow-core/src/airflow/ui/src/pages/Dag/Overview/Overview.tsx:
##########
@@ -66,6 +68,22 @@ export const Overview = () => {
 
   const failedTaskCount = failedTasks?.total_entries ?? 0;
 
+  // Recent completed instances feed the per-task median; the endpoint caps the
+  // page at the configured n_page_limit, so this is the recent window, not 
all runs.
+  const { data: slowestTasksData, isLoading: isLoadingSlowestTasks } = 
useTaskInstanceServiceGetTaskInstances(
+    {
+      dagId: dagId ?? "",
+      dagRunId: "~",
+      limit: 100,
+      orderBy: ["-run_after"],

Review Comment:
   Let's sort by duration. That's what we want anyway. 
   
   Later on we can build a custom /ui endpoint to aggregate the median duration 
per task.



##########
airflow-core/src/airflow/ui/src/components/SlowestTasksChart.tsx:
##########
@@ -0,0 +1,151 @@
+/*!
+ * 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, Center, Heading, Text, useToken } from "@chakra-ui/react";
+import { Chart as ChartJS, CategoryScale, LinearScale, BarElement, Tooltip, 
type Plugin } from "chart.js";
+import { Bar } from "react-chartjs-2";
+import { useTranslation } from "react-i18next";
+
+import { getComputedCSSVariableValue } from "src/theme";
+import { getDurationTickStep, renderCompactDuration } from 
"src/utils/datetimeUtils";
+import type { TaskDurationSummary } from "src/utils/slowestTasks";
+
+ChartJS.register(CategoryScale, LinearScale, BarElement, Tooltip);
+
+const CHART_HEIGHT = "340px";
+
+export const SlowestTasksChart = ({ tasks }: { readonly tasks: 
Array<TaskDurationSummary> }) => {
+  const { t: translate } = useTranslation(["components", "common"]);
+  const [labelColorToken] = useToken("colors", ["fg.muted"]);
+
+  const states = tasks.map((task) => task.latestState).filter(Boolean);
+  const stateColorTokens = useToken(
+    "colors",
+    states.map((state) => `${state}.solid`),
+  );
+
+  if (tasks.length === 0) {
+    return (
+      <Box data-testid="slowest-tasks-chart" height={CHART_HEIGHT}>
+        <Heading pb={1} size="sm" textAlign="center">
+          {translate("slowestTasks.title")}
+        </Heading>
+        <Center color="fg.muted" fontSize="sm" height="100%">
+          {translate("slowestTasks.empty")}
+        </Center>
+      </Box>
+    );
+  }
+
+  const stateColorMap: Record<string, string> = {};
+
+  states.forEach((state, index) => {
+    if (state) {
+      stateColorMap[state] = 
getComputedCSSVariableValue(stateColorTokens[index] ?? "oklch(0.5 0 0)");
+    }
+  });
+
+  const barEndLabels: Plugin<"bar"> = {
+    afterDatasetsDraw: (chart) => {
+      const { ctx } = chart;
+      const meta = chart.getDatasetMeta(0);
+
+      ctx.save();
+      ctx.font = "11px system-ui, sans-serif";
+      ctx.fillStyle = getComputedCSSVariableValue(labelColorToken ?? 
"oklch(0.5 0 0)");
+      ctx.textBaseline = "middle";
+      meta.data.forEach((bar, index) => {
+        const task = tasks[index];
+
+        if (task !== undefined) {
+          ctx.fillText(renderCompactDuration(task.medianDuration), bar.x + 6, 
bar.y);
+        }
+      });
+      ctx.restore();
+    },
+    id: "slowestTasksBarEndLabels",
+  };
+
+  const maxMedian = Math.max(...tasks.map((task) => task.medianDuration), 0);
+
+  return (
+    <Box data-testid="slowest-tasks-chart">
+      <Heading pb={1} size="sm" textAlign="center">
+        {translate("slowestTasks.title")}
+      </Heading>
+      <Text color="fg.muted" fontSize="xs" pb={2} textAlign="center">
+        {translate("slowestTasks.subtitle")}
+      </Text>
+      <Box height={CHART_HEIGHT}>
+        <Bar
+          data={{
+            datasets: [
+              {
+                backgroundColor: tasks.map(
+                  (task) =>
+                    (task.latestState ? stateColorMap[task.latestState] : 
undefined) ?? "oklch(0.5 0 0)",

Review Comment:
   We use `getComputedCSSVariableValue` for fallbacks not the raw oklch



##########
airflow-core/src/airflow/ui/src/pages/Dag/Overview/Overview.tsx:
##########
@@ -66,6 +68,22 @@ export const Overview = () => {
 
   const failedTaskCount = failedTasks?.total_entries ?? 0;
 
+  // Recent completed instances feed the per-task median; the endpoint caps the
+  // page at the configured n_page_limit, so this is the recent window, not 
all runs.
+  const { data: slowestTasksData, isLoading: isLoadingSlowestTasks } = 
useTaskInstanceServiceGetTaskInstances(
+    {
+      dagId: dagId ?? "",
+      dagRunId: "~",
+      limit: 100,
+      orderBy: ["-run_after"],
+      runAfterGte: startDate,
+      runAfterLte: endDate,
+      state: ["success", "failed"],
+    },

Review Comment:
   Let's also add a `{ enabled: Boolean(dagId) }` check on here.



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