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


##########
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`;
+};
+
+const STATE_ICON_AND_SPACING_WIDTH_PX = 30;
+const DURATION_CHARACTER_WIDTH_PX = 10;
+const TIMELINE_BAR_BREATHING_ROOM_PX = 4;
+
+export const getTimelineBarMinimumWidth = (durationMs: number) =>
+  STATE_ICON_AND_SPACING_WIDTH_PX +
+  formatDurationLabel(durationMs).length * DURATION_CHARACTER_WIDTH_PX +
+  TIMELINE_BAR_BREATHING_ROOM_PX;
+
+export const getVisualDurationWidth = (durationMs: number) =>
+  durationMs <= 0
+    ? getTimelineBarMinimumWidth(durationMs)
+    : `max(${getTimelineBarMinimumWidth(durationMs)}px, ${(durationMs / 60_000 
/ (24 * 60)) * 100}%)`;
+
+export const getTimelineBarLeft = (startPosition: number, width: number | 
string) =>
+  `min(${startPosition}%, calc(100% - ${typeof width === "number" ? 
`${width}px` : width}))`;
+
+type BuildDayRowLayoutsParams = {
+  readonly rows: Array<TimelineRow>;
+  readonly selectedTimezone: string;
+  readonly timelineWidth: number;
+};
+
+export const buildDayRowLayouts = ({
+  rows,
+  selectedTimezone,
+  timelineWidth,
+}: BuildDayRowLayoutsParams): Array<DayRowLayout> => {
+  let top = 0;
+
+  return rows.map((row) => {
+    const laneEnds: Array<number> = [];
+    const items = row.items
+      .filter((item) => !item.isPlaceholder && item.startDate !== null)
+      .sort(
+        (left, right) =>
+          getLocalStartTimeSortValue(left.startDate, selectedTimezone) -
+          getLocalStartTimeSortValue(right.startDate, selectedTimezone),
+      )
+      .map((item) => {
+        const start = dayjs(item.startDate).tz(selectedTimezone);
+        const startX = (getPosition(start, start.startOf("day")) / 100) * 
timelineWidth;
+        const width = Math.max(
+          getTimelineBarMinimumWidth(item.durationMs),
+          (item.durationMs / (24 * 60 * 60 * 1000)) * timelineWidth,
+        );
+        let lane = laneEnds.findIndex((laneEnd) => laneEnd <= startX);
+
+        if (lane === -1) {
+          lane = laneEnds.length;
+          laneEnds.push(startX + width);
+        } else {
+          laneEnds[lane] = startX + width;
+        }
+
+        return { item, lane };
+      });
+    const height = Math.max(48, laneEnds.length * 20 + 16);
+    const layout = { height, items, row, top };
+
+    top += height;
+
+    return layout;
+  });
+};
+
+type BuildWeekItemLayoutsParams = {
+  readonly contentHeight: number;
+  readonly items: Array<TimelineItem>;
+  readonly selectedTimezone: string;
+};
+
+export const buildWeekItemLayouts = ({
+  contentHeight,
+  items,
+  selectedTimezone,
+}: BuildWeekItemLayoutsParams): Array<WeekItemLayout> => {
+  const positionedItems = items
+    .filter((item) => item.startDate !== null)
+    .map((item) => {
+      const start = dayjs(item.startDate).tz(selectedTimezone);
+      const height = Math.min(
+        contentHeight,
+        Math.max(20, (item.durationMs / (24 * 60 * 60 * 1000)) * 
contentHeight),

Review Comment:
   ```typescript
   // airflow-core/src/airflow/ui/src/pages/TimeSchedule/constants.ts
   
   export const DAY_MINUTES = 24 * 60;
   export const DAY_DURATION_MS = DAY_MINUTES * 60_000;
   ```
   
   ```suggestion
           Math.max(20, (item.duration_ms / DAY_DURATION_MS) * contentHeight),
   ```



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