-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Init Dag Overview page with time range selector and failed tasks
- Loading branch information
Showing
9 changed files
with
342 additions
and
749 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ export const StateCircle = ({ | |
bg={stateColor[state]} | ||
borderRadius="50%" | ||
h={2} | ||
maxW={2} | ||
minW={2} | ||
w={2} | ||
/> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/*! | ||
* 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 { Box, useToken } from "@chakra-ui/react"; | ||
import { | ||
Chart as ChartJS, | ||
CategoryScale, | ||
LinearScale, | ||
PointElement, | ||
LineElement, | ||
Filler, | ||
Tooltip, | ||
type ChartOptions, | ||
} from "chart.js"; | ||
import dayjs from "dayjs"; | ||
import { useMemo, useRef, useEffect } from "react"; | ||
import { Line } from "react-chartjs-2"; | ||
|
||
import { useColorMode } from "src/context/colorMode"; | ||
|
||
ChartJS.register( | ||
CategoryScale, | ||
LinearScale, | ||
PointElement, | ||
LineElement, | ||
Filler, | ||
Tooltip, | ||
); | ||
|
||
type Event = { timestamp: string }; | ||
|
||
const aggregateEventsIntoIntervals = ( | ||
events: Array<Event>, | ||
startDate: string, | ||
endDate: string, | ||
) => { | ||
const totalMinutes = dayjs(endDate).diff(startDate, "minutes"); | ||
const intervalSize = Math.floor(totalMinutes / 10); | ||
const intervals = Array.from({ length: 10 }).fill(0) as Array<number>; | ||
|
||
events.forEach((event) => { | ||
const minutesSinceStart = dayjs(event.timestamp).diff(startDate, "minutes"); | ||
const intervalIndex = Math.min( | ||
Math.floor(minutesSinceStart / intervalSize), | ||
9, | ||
); | ||
|
||
if (intervals[intervalIndex] !== undefined) { | ||
intervals[intervalIndex] += 1; | ||
} | ||
}); | ||
|
||
return intervals; | ||
}; | ||
|
||
const options = { | ||
layout: { | ||
padding: { | ||
bottom: 2, | ||
top: 2, | ||
}, | ||
}, | ||
maintainAspectRatio: false, | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
tooltip: { | ||
enabled: false, | ||
}, | ||
}, | ||
responsive: true, | ||
scales: { | ||
x: { | ||
display: false, | ||
grid: { | ||
display: false, | ||
}, | ||
}, | ||
y: { | ||
display: false, | ||
grid: { | ||
display: false, | ||
}, | ||
}, | ||
}, | ||
} satisfies ChartOptions; | ||
|
||
type Props = { | ||
readonly endDate: string; | ||
readonly events: Array<Event>; | ||
readonly startDate: string; | ||
}; | ||
|
||
export const Chart = ({ endDate, events, startDate }: Props) => { | ||
const { colorMode } = useColorMode(); | ||
const chartRef = useRef<ChartJS<"line">>(); | ||
|
||
// Get raw color values instead of CSS variables | ||
const [bgLight, bgDark, lineLight, lineDark] = useToken("colors", [ | ||
"red.100", | ||
"red.800", | ||
"red.500", | ||
"red.400", | ||
]); | ||
|
||
const backgroundColor = colorMode === "light" ? bgLight : bgDark; | ||
const lineColor = colorMode === "light" ? lineLight : lineDark; | ||
|
||
const intervalData = useMemo( | ||
() => aggregateEventsIntoIntervals(events, startDate, endDate), | ||
[events, startDate, endDate], | ||
); | ||
|
||
// Cleanup chart instance on unmount | ||
useEffect( | ||
() => () => { | ||
if (chartRef.current) { | ||
chartRef.current.destroy(); | ||
} | ||
}, | ||
[], | ||
); | ||
|
||
const data = { | ||
datasets: [ | ||
{ | ||
backgroundColor, | ||
borderColor: lineColor, | ||
borderWidth: 2, | ||
data: intervalData, | ||
fill: true, | ||
pointRadius: 0, | ||
tension: 0.4, | ||
}, | ||
], | ||
labels: Array.from({ length: 10 }).fill(""), | ||
}; | ||
|
||
return ( | ||
<Box h="25px" w="200px"> | ||
<Line data={data} options={options} ref={chartRef} /> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/*! | ||
* 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 { Box, HStack, Badge, Text, Skeleton } from "@chakra-ui/react"; | ||
import dayjs from "dayjs"; | ||
import { useState } from "react"; | ||
import { Link, useLocation, useParams } from "react-router-dom"; | ||
|
||
import { useTaskInstanceServiceGetTaskInstances } from "openapi/queries"; | ||
import TimeRangeSelector from "src/components/TimeRangeSelector"; | ||
import { pluralize } from "src/utils"; | ||
import { stateColor } from "src/utils/stateColor"; | ||
|
||
import { Chart } from "./Chart"; | ||
|
||
const defaultHour = "8"; | ||
|
||
export const Overview = () => { | ||
const { dagId } = useParams(); | ||
|
||
const now = dayjs(); | ||
const [startDate, setStartDate] = useState( | ||
now.subtract(Number(defaultHour), "hour").toISOString(), | ||
); | ||
const [endDate, setEndDate] = useState(now.toISOString()); | ||
|
||
const { data: failedTasks, isLoading } = | ||
useTaskInstanceServiceGetTaskInstances({ | ||
dagId: dagId ?? "", | ||
dagRunId: "~", | ||
logicalDateGte: startDate, | ||
logicalDateLte: endDate, | ||
state: ["failed"], | ||
}); | ||
|
||
const location = useLocation(); | ||
|
||
// TODO actually link to task instances list | ||
return ( | ||
<Box m={4}> | ||
<Box my={2}> | ||
<TimeRangeSelector | ||
defaultValue={defaultHour} | ||
endDate={endDate} | ||
setEndDate={setEndDate} | ||
setStartDate={setStartDate} | ||
startDate={startDate} | ||
/> | ||
</Box> | ||
{failedTasks?.total_entries !== undefined && | ||
failedTasks.total_entries > 0 ? ( | ||
// TODO: make sure url params pass correctly | ||
<Link to={`${location.pathname}/tasks?state=failed`}> | ||
<HStack borderRadius={4} borderWidth={1} p={3} width="max-content"> | ||
<Badge | ||
borderRadius="50%" | ||
colorPalette={stateColor.failed} | ||
variant="solid" | ||
> | ||
{failedTasks.total_entries} | ||
</Badge> | ||
<Text fontSize="sm" fontWeight="bold"> | ||
Failed{" "} | ||
{pluralize("Task", failedTasks.total_entries, undefined, true)} | ||
</Text> | ||
<Chart | ||
endDate={endDate} | ||
events={failedTasks.task_instances.map((ti) => ({ | ||
timestamp: ti.start_date ?? ti.logical_date, | ||
}))} | ||
startDate={startDate} | ||
/> | ||
</HStack> | ||
</Link> | ||
) : undefined} | ||
{isLoading ? ( | ||
<Skeleton borderRadius={4} height="45px" width="350px" /> | ||
) : undefined} | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/*! | ||
* 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. | ||
*/ | ||
|
||
export * from "./Overview"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.