minyeamer commented on code in PR #71558:
URL: https://github.com/apache/airflow/pull/71558#discussion_r4180947577


##########
airflow-core/src/airflow/ui/src/pages/TimeSchedule/timelineUtils.ts:
##########
@@ -0,0 +1,283 @@
+/*!
+ * 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 { dayjs } from "./dateUtils";
+import type {
+  DayRowLayout,
+  RowSortMode,
+  TimeMarker,
+  TimeScale,
+  TimelineItem,
+  TimelineRow,
+  WeekItemLayout,
+} from "./types";
+
+const getLocalStartTimeSortValue = (startDate: string | null, 
selectedTimezone: string) => {
+  if (startDate === null) {
+    return 0;
+  }
+
+  const localStart = dayjs(startDate).tz(selectedTimezone);
+
+  return (
+    localStart.hour() * 60 * 60 * 1000 +
+    localStart.minute() * 60 * 1000 +
+    localStart.second() * 1000 +
+    localStart.millisecond()
+  );
+};
+
+type BuildTimelineRowsParams = {
+  readonly items: Array<TimelineItem>;
+  readonly rowSortMode: RowSortMode;
+  readonly selectedTimezone: string;
+};
+
+export const buildTimelineRows = ({
+  items,
+  rowSortMode,
+  selectedTimezone,
+}: BuildTimelineRowsParams): Array<TimelineRow> => {
+  const rowsByDagId = new Map<string, Array<TimelineItem>>();
+
+  items.forEach((item) => {
+    const rowItems = rowsByDagId.get(item.dagId);
+
+    if (rowItems) {
+      rowItems.push(item);
+    } else {
+      rowsByDagId.set(item.dagId, [item]);
+    }
+  });
+
+  return Array.from(rowsByDagId, ([dagId, rowItems]) => ({
+    dagId,
+    isTimeScheduled: rowItems.some((item) => item.isTimeScheduled),
+    items: rowItems.sort(
+      (left, right) =>
+        getLocalStartTimeSortValue(left.startDate, selectedTimezone) -
+        getLocalStartTimeSortValue(right.startDate, selectedTimezone),
+    ),
+    label: rowItems[0]?.label ?? dagId,
+  })).sort((left, right) => {
+    if (rowSortMode === "dagIdAscending") {
+      return left.dagId.localeCompare(right.dagId);
+    }
+    if (rowSortMode === "dagIdDescending") {
+      return right.dagId.localeCompare(left.dagId);
+    }
+    if (left.isTimeScheduled !== right.isTimeScheduled) {
+      return Number(right.isTimeScheduled) - Number(left.isTimeScheduled);
+    }
+    const difference =
+      getLocalStartTimeSortValue(left.items[0]?.startDate ?? null, 
selectedTimezone) -
+      getLocalStartTimeSortValue(right.items[0]?.startDate ?? null, 
selectedTimezone);
+
+    return difference || left.dagId.localeCompare(right.dagId);
+  });
+};
+
+export const getPosition = (value: dayjs.Dayjs, dayStart: dayjs.Dayjs) =>
+  Math.max(0, Math.min(100, (value.diff(dayStart) / (24 * 60 * 60 * 1000)) * 
100));
+
+export const formatDurationLabel = (durationMs: number) => {
+  if (durationMs <= 0) {
+    return "";
+  }
+  const seconds = Math.max(1, Math.round(durationMs / 1000));
+
+  if (seconds < 60) {
+    return `${seconds}s`;
+  }
+  const minutes = Math.floor(seconds / 60);
+
+  if (minutes < 60) {
+    return `${minutes}m`;
+  }
+
+  return minutes % 60 > 0 ? `${Math.floor(minutes / 60)}h ${minutes % 60}m` : 
`${Math.floor(minutes / 60)}h`;
+};

Review Comment:
   `getTimelineDurationSeconds` keeps bar labels compact while reusing 
Airflow’s shared `renderDuration` formatter: seconds are retained for 
sub-minute runs (50s) but omitted for longer runs (6m, 1h 2m).
   
   ```typescript
   import { renderDuration } from "src/utils/datetimeUtils";
   ```
   
   ```suggestion
   export const getTimelineDurationSeconds = (durationMs: number) =>
     durationMs >= 60_000 ? Math.floor(durationMs / 60_000) * 60 : durationMs / 
1000;
   
   export const getTimelineBarMinimumWidth = (durationMs: number, locale?: 
string) =>
     STATE_ICON_AND_SPACING_WIDTH_PX +
     (durationMs > 0 ? (renderDuration(getTimelineDurationSeconds(durationMs), 
locale)?.length ?? 0) : 0) *
       DURATION_CHARACTER_WIDTH_PX;
   ```



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