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


##########
airflow/www/static/js/dag/details/taskDuration/index.tsx:
##########
@@ -0,0 +1,244 @@
+/*!
+ * 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.
+ */
+
+/* global moment */
+
+import React from "react";
+
+import useSelection from "src/dag/useSelection";
+import { useGridData } from "src/api";
+import { getDuration, formatDateTime } from "src/datetime_utils";
+import ReactECharts, { ReactEChartsProps } from "src/components/ReactECharts";
+import type { TaskInstance } from "src/types";
+
+class TaskInstanceDuration {
+  instance: TaskInstance;
+
+  dataIntervalStart: string;
+
+  dataIntervalEnd: string;
+
+  executionDate: string;
+
+  runDuration: moment.Duration;
+
+  runDurationUnit: number = 0;
+
+  queuedDuration: moment.Duration;
+
+  queuedDurationUnit: number = 0;
+
+  constructor(
+    instance: TaskInstance,
+    runDuration: moment.Duration,
+    queuedDuration: moment.Duration,
+    dataIntervalStart: string,
+    dataIntervalEnd: string,
+    executionDate: string
+  ) {
+    this.instance = instance;
+    this.runDuration = runDuration;
+    this.queuedDuration = queuedDuration;
+    this.dataIntervalStart = dataIntervalStart;
+    this.dataIntervalEnd = dataIntervalEnd;
+    this.executionDate = executionDate;
+  }
+}
+
+const TaskDuration = () => {
+  const {
+    selected: { taskId },
+    onSelect,
+  } = useSelection();
+
+  const {
+    data: { dagRuns, groups },
+  } = useGridData();
+  let maxDuration = 0;
+  let unit = "seconds";
+  const durations: TaskInstanceDuration[] = [];
+
+  const hasTaskInstances =
+    groups.children &&
+    groups.children &&
+    groups.children.find((child) => child.id === taskId);
+
+  if (!hasTaskInstances) return null;
+
+  dagRuns.forEach((dagRun) => {
+    const instance = hasTaskInstances.instances.find(
+      (_instance) =>
+        _instance.taskId === taskId && _instance.runId === dagRun.runId
+    );
+
+    if (!instance) return null;
+    // @ts-ignore
+    const runDuration = moment.duration(
+      instance?.startDate && instance?.endDate
+        ? getDuration(instance?.startDate, instance?.endDate)
+        : 0
+    );
+
+    // @ts-ignore
+    const queuedDuration = moment.duration(
+      instance?.queuedDttm &&
+        instance?.startDate &&
+        instance?.startDate > instance.queuedDttm
+        ? getDuration(instance?.queuedDttm, instance?.startDate)
+        : 0
+    );
+
+    if (runDuration.asSeconds() > maxDuration) {
+      maxDuration = runDuration.asSeconds();
+    }
+
+    durations.push(
+      new TaskInstanceDuration(
+        instance,
+        runDuration,
+        queuedDuration,
+        dagRun.dataIntervalStart,
+        dagRun.dataIntervalEnd,
+        dagRun.executionDate
+      )
+    );
+
+    return null;
+  });
+
+  if (maxDuration <= 60 * 2) {
+    unit = "seconds";
+  } else if (maxDuration <= 60 * 60 * 2) {
+    unit = "minutes";
+  } else if (maxDuration <= 24 * 60 * 60 * 2) {
+    unit = "hours";
+  } else {
+    unit = "days";
+  }
+
+  /* eslint-disable no-param-reassign */
+  durations.forEach((instance) => {
+    if (unit === "seconds") {
+      instance.runDurationUnit = instance.runDuration.asSeconds();
+      instance.queuedDurationUnit = instance.queuedDuration.asSeconds();
+    } else if (unit === "minutes") {
+      instance.runDurationUnit = instance.runDuration.asMinutes();
+      instance.queuedDurationUnit = instance.queuedDuration.asMinutes();
+    } else if (unit === "hours") {
+      instance.runDurationUnit = instance.runDuration.asHours();
+      instance.queuedDurationUnit = instance.queuedDuration.asHours();
+    } else {
+      instance.runDurationUnit = instance.runDuration.asDays();
+      instance.queuedDurationUnit = instance.queuedDuration.asDays();
+    }
+  });
+
+  // @ts-ignore
+  function formatTooltip(args) {
+    const { data } = args[0];
+    const {
+      instance: { runId, queuedDttm, startDate, endDate, tryNumber },
+      queuedDurationUnit,
+      runDurationUnit,
+    } = data;
+
+    return `
+Run ID : ${runId} <br>
+Try Number : ${tryNumber} <br>
+Queued : ${queuedDttm && formatDateTime(queuedDttm)} <br>
+Started : ${startDate && formatDateTime(startDate)} <br>
+Ended : ${endDate && formatDateTime(endDate)} <br>
+Queued Duration : ${queuedDurationUnit.toFixed(2)} ${unit}<br>
+Run Duration : ${runDurationUnit.toFixed(2)} ${unit}<br>
+Total : ${(queuedDurationUnit + runDurationUnit).toFixed(2)} ${unit}<br>

Review Comment:
   ```suggestion
   Run ID: ${runId} <br>
   Try Number: ${tryNumber} <br>
   Queued: ${queuedDttm && formatDateTime(queuedDttm)} <br>
   Started: ${startDate && formatDateTime(startDate)} <br>
   Ended: ${endDate && formatDateTime(endDate)} <br>
   Queued Duration: ${queuedDurationUnit.toFixed(2)} ${unit}<br>
   Run Duration: ${runDurationUnit.toFixed(2)} ${unit}<br>
   Total: ${(queuedDurationUnit + runDurationUnit).toFixed(2)} ${unit}<br>
   ```



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to