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 YYYY") };
};
+export const calculateDataBounds = (
+ data: Array<CalendarTimeRangeResponse>,
+ viewMode: CalendarColorMode,
+ granularity: CalendarGranularity,
+): { maxCount: number; minCount: number } => {
+ if (data.length === 0) {
+ return { maxCount: 0, minCount: 0 };
+ }
+
+ const counts: Array<number> = [];
+ 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<CalendarTimeRangeResponse>,
+ 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]