Re: [PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-03 Thread via GitHub


bbovenzi merged PR #55155:
URL: https://github.com/apache/airflow/pull/55155


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



[PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-03 Thread via GitHub


RoyLee1224 opened a new pull request, #55155:
URL: https://github.com/apache/airflow/pull/55155

   Closes #54951
   
   ## Summary
   Replaced the hardcoded color scale in the Calendar view with a dynamic 
system. The new scale is generated based on the true min/max of the data.
   
   ## Key Changes:
   
   - **Dynamic Legend**: The legend now intelligently adjusts its labels and 
display type (gradient vs. single value) to match the data distribution.
   
   - **Fixes Low-Variance Display**: Correctly handles cases where run counts 
are uniform (e.g., all days have 5 runs). The view now shows an appropriate 
medium-intensity color and a single-value legend, instead of a misleading 
maximum color.
   
   ## Screenshots:
   
   
https://github.com/user-attachments/assets/3033c51f-bab4-4f89-bcdb-0eca8dc66bf9
   
   
   
   
   
   
   
   
   
   
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party 
License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a 
newsfragment file, named `{pr_number}.significant.rst` or 
`{issue_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
   


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



Re: [PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-03 Thread via GitHub


RoyLee1224 commented on code in PR #55155:
URL: https://github.com/apache/airflow/pull/55155#discussion_r2316793467


##
airflow-core/src/airflow/ui/src/pages/Dag/Calendar/calendarUtils.ts:
##
@@ -219,6 +150,173 @@ export const generateHourlyCalendarData = (
   return { days: monthData, month: monthStart.format("MMM ") };
 };
 
+export const calculateDataBounds = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): { maxCount: number; minCount: number } => {
+  if (data.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  const counts: Array = [];
+  const mapCreator = granularity === "daily" ? createDailyDataMap : 
createHourlyDataMap;
+  const dataMap = mapCreator(data);
+
+  dataMap.forEach((runs) => {
+const runCounts = calculateRunCounts(runs);
+const targetCount = viewMode === "total" ? runCounts.total : 
runCounts.failed;
+
+if (targetCount > 0) {
+  counts.push(targetCount);
+}
+  });
+
+  if (counts.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  return {
+maxCount: Math.max(...counts),
+minCount: Math.min(...counts),
+  };
+};
+
+export const createCalendarScale = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): CalendarScale => {
+  const { maxCount, minCount } = calculateDataBounds(data, viewMode, 
granularity);
+
+  // Handle empty data case
+  if (maxCount === 0) {
+const emptyColor = { _dark: "gray.700", _light: "gray.100" };
+
+return {
+  getColor: () => emptyColor,
+  legendItems: [{ color: emptyColor, label: "0" }],
+  type: "empty",
+};
+  }
+
+  // Handle single value case
+  if (minCount === maxCount) {
+const singleColor =
+  viewMode === "total"
+? { _dark: "green.500", _light: "green.400" }
+: { _dark: "red.500", _light: "red.400" };
+
+return {
+  getColor: (counts: RunCounts) => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+const targetCount = viewMode === "total" ? counts.total : 
counts.failed;
+
+return targetCount === 0 ? { _dark: "gray.700", _light: "gray.100" } : 
singleColor;

Review Comment:
   You're right, that makes perfect sense. I'll go ahead and refactor this to 
use constants. Thanks for the feedback!



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



Re: [PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-03 Thread via GitHub


RoyLee1224 commented on code in PR #55155:
URL: https://github.com/apache/airflow/pull/55155#discussion_r2316793467


##
airflow-core/src/airflow/ui/src/pages/Dag/Calendar/calendarUtils.ts:
##
@@ -219,6 +150,173 @@ export const generateHourlyCalendarData = (
   return { days: monthData, month: monthStart.format("MMM ") };
 };
 
+export const calculateDataBounds = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): { maxCount: number; minCount: number } => {
+  if (data.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  const counts: Array = [];
+  const mapCreator = granularity === "daily" ? createDailyDataMap : 
createHourlyDataMap;
+  const dataMap = mapCreator(data);
+
+  dataMap.forEach((runs) => {
+const runCounts = calculateRunCounts(runs);
+const targetCount = viewMode === "total" ? runCounts.total : 
runCounts.failed;
+
+if (targetCount > 0) {
+  counts.push(targetCount);
+}
+  });
+
+  if (counts.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  return {
+maxCount: Math.max(...counts),
+minCount: Math.min(...counts),
+  };
+};
+
+export const createCalendarScale = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): CalendarScale => {
+  const { maxCount, minCount } = calculateDataBounds(data, viewMode, 
granularity);
+
+  // Handle empty data case
+  if (maxCount === 0) {
+const emptyColor = { _dark: "gray.700", _light: "gray.100" };
+
+return {
+  getColor: () => emptyColor,
+  legendItems: [{ color: emptyColor, label: "0" }],
+  type: "empty",
+};
+  }
+
+  // Handle single value case
+  if (minCount === maxCount) {
+const singleColor =
+  viewMode === "total"
+? { _dark: "green.500", _light: "green.400" }
+: { _dark: "red.500", _light: "red.400" };
+
+return {
+  getColor: (counts: RunCounts) => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+const targetCount = viewMode === "total" ? counts.total : 
counts.failed;
+
+return targetCount === 0 ? { _dark: "gray.700", _light: "gray.100" } : 
singleColor;

Review Comment:
   You're right, that makes perfect sense! I'll go ahead and refactor this to 
use constants. Thanks for the feedback!



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



Re: [PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-02 Thread via GitHub


RoyLee1224 commented on code in PR #55155:
URL: https://github.com/apache/airflow/pull/55155#discussion_r2316755103


##
airflow-core/src/airflow/ui/src/pages/Dag/Calendar/calendarUtils.ts:
##
@@ -219,6 +150,173 @@ export const generateHourlyCalendarData = (
   return { days: monthData, month: monthStart.format("MMM ") };
 };
 
+export const calculateDataBounds = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): { maxCount: number; minCount: number } => {
+  if (data.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  const counts: Array = [];
+  const mapCreator = granularity === "daily" ? createDailyDataMap : 
createHourlyDataMap;
+  const dataMap = mapCreator(data);
+
+  dataMap.forEach((runs) => {
+const runCounts = calculateRunCounts(runs);
+const targetCount = viewMode === "total" ? runCounts.total : 
runCounts.failed;
+
+if (targetCount > 0) {
+  counts.push(targetCount);
+}
+  });
+
+  if (counts.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  return {
+maxCount: Math.max(...counts),
+minCount: Math.min(...counts),
+  };
+};
+
+export const createCalendarScale = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): CalendarScale => {
+  const { maxCount, minCount } = calculateDataBounds(data, viewMode, 
granularity);
+
+  // Handle empty data case
+  if (maxCount === 0) {
+const emptyColor = { _dark: "gray.700", _light: "gray.100" };
+
+return {
+  getColor: () => emptyColor,
+  legendItems: [{ color: emptyColor, label: "0" }],
+  type: "empty",
+};
+  }
+
+  // Handle single value case
+  if (minCount === maxCount) {
+const singleColor =
+  viewMode === "total"
+? { _dark: "green.500", _light: "green.400" }
+: { _dark: "red.500", _light: "red.400" };
+
+return {
+  getColor: (counts: RunCounts) => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+const targetCount = viewMode === "total" ? counts.total : 
counts.failed;
+
+return targetCount === 0 ? { _dark: "gray.700", _light: "gray.100" } : 
singleColor;
+  },
+  legendItems: [
+{ color: { _dark: "gray.700", _light: "gray.100" }, label: "0" },
+{ color: singleColor, label: maxCount.toString() },
+  ],
+  type: "single_value",
+};
+  }
+
+  // Handle gradient case - create dynamic thresholds
+  const range = maxCount - minCount;
+  const colorScheme =
+viewMode === "total"
+  ? [
+  { _dark: "gray.700", _light: "gray.100" }, // 0
+  { _dark: "green.300", _light: "green.200" },
+  { _dark: "green.500", _light: "green.400" },
+  { _dark: "green.700", _light: "green.600" },
+  { _dark: "green.900", _light: "green.800" },
+]
+  : [
+  { _dark: "gray.700", _light: "gray.100" }, // 0
+  { _dark: "red.300", _light: "red.200" },
+  { _dark: "red.500", _light: "red.400" },
+  { _dark: "red.700", _light: "red.600" },
+  { _dark: "red.900", _light: "red.800" },
+];
+
+  const thresholds = [
+0,
+Math.max(1, Math.ceil(minCount + range * 0.25)),
+Math.max(2, Math.ceil(minCount + range * 0.5)),
+Math.max(3, Math.ceil(minCount + range * 0.75)),
+maxCount,
+  ];
+
+  const uniqueThresholds = [...new Set(thresholds)].sort((first, second) => 
first - second);
+
+  const getColor = (counts: RunCounts): string | { _dark: string; _light: 
string } => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+
+const targetCount = viewMode === "total" ? counts.total : counts.failed;
+
+if (targetCount === 0) {
+  return colorScheme[0] ?? { _dark: "gray.700", _light: "gray.100" };
+}
+
+for (let index = uniqueThresholds.length - 1; index >= 1; index -= 1) {
+  const threshold = uniqueThresholds[index];

Review Comment:
   Since this loop needs to run in reverse and exit early with a `return`, I 
guess the classic `for` loop is the best fit 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]



Re: [PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-02 Thread via GitHub


RoyLee1224 commented on code in PR #55155:
URL: https://github.com/apache/airflow/pull/55155#discussion_r2316758066


##
airflow-core/src/airflow/ui/src/pages/Dag/Calendar/calendarUtils.ts:
##
@@ -219,6 +150,173 @@ export const generateHourlyCalendarData = (
   return { days: monthData, month: monthStart.format("MMM ") };
 };
 
+export const calculateDataBounds = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): { maxCount: number; minCount: number } => {
+  if (data.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  const counts: Array = [];
+  const mapCreator = granularity === "daily" ? createDailyDataMap : 
createHourlyDataMap;
+  const dataMap = mapCreator(data);
+
+  dataMap.forEach((runs) => {
+const runCounts = calculateRunCounts(runs);
+const targetCount = viewMode === "total" ? runCounts.total : 
runCounts.failed;
+
+if (targetCount > 0) {
+  counts.push(targetCount);
+}
+  });
+
+  if (counts.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  return {
+maxCount: Math.max(...counts),
+minCount: Math.min(...counts),
+  };
+};
+
+export const createCalendarScale = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): CalendarScale => {
+  const { maxCount, minCount } = calculateDataBounds(data, viewMode, 
granularity);
+
+  // Handle empty data case
+  if (maxCount === 0) {
+const emptyColor = { _dark: "gray.700", _light: "gray.100" };
+
+return {
+  getColor: () => emptyColor,
+  legendItems: [{ color: emptyColor, label: "0" }],
+  type: "empty",
+};
+  }
+
+  // Handle single value case
+  if (minCount === maxCount) {
+const singleColor =
+  viewMode === "total"
+? { _dark: "green.500", _light: "green.400" }
+: { _dark: "red.500", _light: "red.400" };
+
+return {
+  getColor: (counts: RunCounts) => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+const targetCount = viewMode === "total" ? counts.total : 
counts.failed;
+
+return targetCount === 0 ? { _dark: "gray.700", _light: "gray.100" } : 
singleColor;
+  },
+  legendItems: [
+{ color: { _dark: "gray.700", _light: "gray.100" }, label: "0" },
+{ color: singleColor, label: maxCount.toString() },
+  ],
+  type: "single_value",
+};
+  }
+
+  // Handle gradient case - create dynamic thresholds
+  const range = maxCount - minCount;
+  const colorScheme =
+viewMode === "total"
+  ? [
+  { _dark: "gray.700", _light: "gray.100" }, // 0
+  { _dark: "green.300", _light: "green.200" },
+  { _dark: "green.500", _light: "green.400" },
+  { _dark: "green.700", _light: "green.600" },
+  { _dark: "green.900", _light: "green.800" },
+]
+  : [
+  { _dark: "gray.700", _light: "gray.100" }, // 0
+  { _dark: "red.300", _light: "red.200" },
+  { _dark: "red.500", _light: "red.400" },
+  { _dark: "red.700", _light: "red.600" },
+  { _dark: "red.900", _light: "red.800" },
+];
+
+  const thresholds = [
+0,
+Math.max(1, Math.ceil(minCount + range * 0.25)),
+Math.max(2, Math.ceil(minCount + range * 0.5)),
+Math.max(3, Math.ceil(minCount + range * 0.75)),
+maxCount,
+  ];
+
+  const uniqueThresholds = [...new Set(thresholds)].sort((first, second) => 
first - second);
+
+  const getColor = (counts: RunCounts): string | { _dark: string; _light: 
string } => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+
+const targetCount = viewMode === "total" ? counts.total : counts.failed;
+
+if (targetCount === 0) {
+  return colorScheme[0] ?? { _dark: "gray.700", _light: "gray.100" };
+}
+
+for (let index = uniqueThresholds.length - 1; index >= 1; index -= 1) {
+  const threshold = uniqueThresholds[index];

Review Comment:
   Is there a different pattern you'd prefer for this situation? I'd be happy 
to change it.



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



Re: [PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-02 Thread via GitHub


RoyLee1224 commented on PR #55155:
URL: https://github.com/apache/airflow/pull/55155#issuecomment-3246342726

   Hi @pierrejeambrun, I've pushed the updates addressing your comments. The 
code should be cleaner now. Please let me know what you think. Thanks!
   


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



Re: [PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-02 Thread via GitHub


RoyLee1224 commented on code in PR #55155:
URL: https://github.com/apache/airflow/pull/55155#discussion_r2316758066


##
airflow-core/src/airflow/ui/src/pages/Dag/Calendar/calendarUtils.ts:
##
@@ -219,6 +150,173 @@ export const generateHourlyCalendarData = (
   return { days: monthData, month: monthStart.format("MMM ") };
 };
 
+export const calculateDataBounds = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): { maxCount: number; minCount: number } => {
+  if (data.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  const counts: Array = [];
+  const mapCreator = granularity === "daily" ? createDailyDataMap : 
createHourlyDataMap;
+  const dataMap = mapCreator(data);
+
+  dataMap.forEach((runs) => {
+const runCounts = calculateRunCounts(runs);
+const targetCount = viewMode === "total" ? runCounts.total : 
runCounts.failed;
+
+if (targetCount > 0) {
+  counts.push(targetCount);
+}
+  });
+
+  if (counts.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  return {
+maxCount: Math.max(...counts),
+minCount: Math.min(...counts),
+  };
+};
+
+export const createCalendarScale = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): CalendarScale => {
+  const { maxCount, minCount } = calculateDataBounds(data, viewMode, 
granularity);
+
+  // Handle empty data case
+  if (maxCount === 0) {
+const emptyColor = { _dark: "gray.700", _light: "gray.100" };
+
+return {
+  getColor: () => emptyColor,
+  legendItems: [{ color: emptyColor, label: "0" }],
+  type: "empty",
+};
+  }
+
+  // Handle single value case
+  if (minCount === maxCount) {
+const singleColor =
+  viewMode === "total"
+? { _dark: "green.500", _light: "green.400" }
+: { _dark: "red.500", _light: "red.400" };
+
+return {
+  getColor: (counts: RunCounts) => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+const targetCount = viewMode === "total" ? counts.total : 
counts.failed;
+
+return targetCount === 0 ? { _dark: "gray.700", _light: "gray.100" } : 
singleColor;
+  },
+  legendItems: [
+{ color: { _dark: "gray.700", _light: "gray.100" }, label: "0" },
+{ color: singleColor, label: maxCount.toString() },
+  ],
+  type: "single_value",
+};
+  }
+
+  // Handle gradient case - create dynamic thresholds
+  const range = maxCount - minCount;
+  const colorScheme =
+viewMode === "total"
+  ? [
+  { _dark: "gray.700", _light: "gray.100" }, // 0
+  { _dark: "green.300", _light: "green.200" },
+  { _dark: "green.500", _light: "green.400" },
+  { _dark: "green.700", _light: "green.600" },
+  { _dark: "green.900", _light: "green.800" },
+]
+  : [
+  { _dark: "gray.700", _light: "gray.100" }, // 0
+  { _dark: "red.300", _light: "red.200" },
+  { _dark: "red.500", _light: "red.400" },
+  { _dark: "red.700", _light: "red.600" },
+  { _dark: "red.900", _light: "red.800" },
+];
+
+  const thresholds = [
+0,
+Math.max(1, Math.ceil(minCount + range * 0.25)),
+Math.max(2, Math.ceil(minCount + range * 0.5)),
+Math.max(3, Math.ceil(minCount + range * 0.75)),
+maxCount,
+  ];
+
+  const uniqueThresholds = [...new Set(thresholds)].sort((first, second) => 
first - second);
+
+  const getColor = (counts: RunCounts): string | { _dark: string; _light: 
string } => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+
+const targetCount = viewMode === "total" ? counts.total : counts.failed;
+
+if (targetCount === 0) {
+  return colorScheme[0] ?? { _dark: "gray.700", _light: "gray.100" };
+}
+
+for (let index = uniqueThresholds.length - 1; index >= 1; index -= 1) {
+  const threshold = uniqueThresholds[index];

Review Comment:
   Or is there a cleaner way you'd recommend?



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



Re: [PR] feat(ui): implement dynamic legend system for calendar [airflow]

2025-09-02 Thread via GitHub


pierrejeambrun commented on code in PR #55155:
URL: https://github.com/apache/airflow/pull/55155#discussion_r2316294688


##
airflow-core/src/airflow/ui/src/pages/Dag/Calendar/calendarUtils.ts:
##
@@ -219,6 +150,173 @@ export const generateHourlyCalendarData = (
   return { days: monthData, month: monthStart.format("MMM ") };
 };
 
+export const calculateDataBounds = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): { maxCount: number; minCount: number } => {
+  if (data.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  const counts: Array = [];
+  const mapCreator = granularity === "daily" ? createDailyDataMap : 
createHourlyDataMap;
+  const dataMap = mapCreator(data);
+
+  dataMap.forEach((runs) => {
+const runCounts = calculateRunCounts(runs);
+const targetCount = viewMode === "total" ? runCounts.total : 
runCounts.failed;
+
+if (targetCount > 0) {
+  counts.push(targetCount);
+}
+  });
+
+  if (counts.length === 0) {
+return { maxCount: 0, minCount: 0 };
+  }
+
+  return {
+maxCount: Math.max(...counts),
+minCount: Math.min(...counts),
+  };
+};
+
+export const createCalendarScale = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): CalendarScale => {
+  const { maxCount, minCount } = calculateDataBounds(data, viewMode, 
granularity);
+
+  // Handle empty data case
+  if (maxCount === 0) {
+const emptyColor = { _dark: "gray.700", _light: "gray.100" };
+
+return {
+  getColor: () => emptyColor,
+  legendItems: [{ color: emptyColor, label: "0" }],
+  type: "empty",
+};
+  }
+
+  // Handle single value case
+  if (minCount === maxCount) {
+const singleColor =
+  viewMode === "total"
+? { _dark: "green.500", _light: "green.400" }
+: { _dark: "red.500", _light: "red.400" };
+
+return {
+  getColor: (counts: RunCounts) => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+const targetCount = viewMode === "total" ? counts.total : 
counts.failed;
+
+return targetCount === 0 ? { _dark: "gray.700", _light: "gray.100" } : 
singleColor;
+  },
+  legendItems: [
+{ color: { _dark: "gray.700", _light: "gray.100" }, label: "0" },
+{ color: singleColor, label: maxCount.toString() },
+  ],
+  type: "single_value",
+};
+  }
+
+  // Handle gradient case - create dynamic thresholds
+  const range = maxCount - minCount;
+  const colorScheme =
+viewMode === "total"
+  ? [
+  { _dark: "gray.700", _light: "gray.100" }, // 0
+  { _dark: "green.300", _light: "green.200" },
+  { _dark: "green.500", _light: "green.400" },
+  { _dark: "green.700", _light: "green.600" },
+  { _dark: "green.900", _light: "green.800" },
+]
+  : [
+  { _dark: "gray.700", _light: "gray.100" }, // 0
+  { _dark: "red.300", _light: "red.200" },
+  { _dark: "red.500", _light: "red.400" },
+  { _dark: "red.700", _light: "red.600" },
+  { _dark: "red.900", _light: "red.800" },
+];
+
+  const thresholds = [
+0,
+Math.max(1, Math.ceil(minCount + range * 0.25)),
+Math.max(2, Math.ceil(minCount + range * 0.5)),
+Math.max(3, Math.ceil(minCount + range * 0.75)),
+maxCount,
+  ];
+
+  const uniqueThresholds = [...new Set(thresholds)].sort((first, second) => 
first - second);
+
+  const getColor = (counts: RunCounts): string | { _dark: string; _light: 
string } => {
+if (counts.planned > 0) {
+  return { _dark: "scheduled.600", _light: "scheduled.200" };
+}
+
+const targetCount = viewMode === "total" ? counts.total : counts.failed;
+
+if (targetCount === 0) {
+  return colorScheme[0] ?? { _dark: "gray.700", _light: "gray.100" };
+}
+
+for (let index = uniqueThresholds.length - 1; index >= 1; index -= 1) {
+  const threshold = uniqueThresholds[index];
+
+  if (threshold !== undefined && targetCount >= threshold) {
+return (
+  colorScheme[Math.min(index, colorScheme.length - 1)] ?? { _dark: 
"gray.700", _light: "gray.100" }
+);
+  }
+}
+
+return colorScheme[1] ?? { _dark: "gray.700", _light: "gray.100" };
+  };
+
+  const legendItems: Array = [];
+
+  for (let index = 0; index < uniqueThresholds.length; index += 1) {
+const threshold = uniqueThresholds[index];

Review Comment:
   nit: forEach



##
airflow-core/src/airflow/ui/src/pages/Dag/Calendar/calendarUtils.ts:
##
@@ -219,6 +150,173 @@ export const generateHourlyCalendarData = (
   return { days: monthData, month: monthStart.format("MMM ") };
 };
 
+export const calculateDataBounds = (
+  data: Array,
+  viewMode: CalendarColorMode,
+  granularity: CalendarGranularity,
+): { maxCount: number; minCount: number } => {
+  if (data.length === 0) {
+return { maxCoun