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