Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: Reimplement Timeline Chart and Adjust Server Timezone Handling #704

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@
"@fortawesome/fontawesome-svg-core": "^6.1.2",
"@fortawesome/free-solid-svg-icons": "^6.1.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@mui/icons-material": "^5.8.0",
"@mui/material": "^5.8.1",
"@mui/icons-material": "^6.1.0",
"@mui/material": "^6.1.0",
"@tanstack/react-table": "^8.5.11",
"@types/lodash": "^4.17.7",
"cron-parser": "^4.5.0",
"fontsource-roboto": "^4.0.0",
"mermaid": "^9.1.1",
"moment": "^2.29.3",
"moment-duration-format": "^2.3.2",
"moment-timezone": "^0.5.46",
"monaco-editor": "^0.41.0",
"monaco-loader": "^1.0.0",
"monaco-react": "^1.1.0",
Expand All @@ -80,7 +81,9 @@
"react-dom": "^18.1.0",
"react-monaco-editor": "^0.54.0",
"react-router-dom": "^6.3.0",
"recharts": "^2.1.10",
"swr": "^1.3.0"
"recharts": "^2.13.0",
"swr": "^1.3.0",
"vis-data": "^7.1.9",
"vis-timeline": "^7.7.3"
}
}
250 changes: 148 additions & 102 deletions ui/src/components/molecules/DashboardTimechart.tsx
Original file line number Diff line number Diff line change
@@ -1,127 +1,173 @@
import React, { useEffect, useRef } from 'react';
import { Box } from '@mui/material';
import moment from 'moment';
import React from 'react';
import {
Bar,
BarChart,
Cell,
LabelList,
LabelProps,
ResponsiveContainer,
XAxis,
YAxis,
} from 'recharts';
import moment, { MomentInput } from 'moment-timezone';
import { Timeline, DataSet } from 'vis-timeline/standalone';
import 'vis-timeline/styles/vis-timeline-graph2d.css';
import { statusColorMapping } from '../../consts';
import { DAGStatus } from '../../models';
import { SchedulerStatus } from '../../models';
import { WorkflowListItem } from '../../models/api';

type Props = { data: DAGStatus[] | WorkflowListItem[] };

type DataFrame = {
name: string;
status: SchedulerStatus;
values: [number, number];
type TimelineItem = {
id: string;
content: string;
start: Date;
end: Date;
group: string;
className: string;
};

function DashboardTimechart({ data: input }: Props) {
const [data, setData] = React.useState<DataFrame[]>([]);
React.useEffect(() => {
const ret: DataFrame[] = [];
const timelineRef = useRef<HTMLDivElement>(null);
const timelineInstance = useRef<Timeline | null>(null);

useEffect(() => {
if (!timelineRef.current) return;

let timezone = getConfig().tz;
if (!timezone) {
timezone = moment.tz.guess();
}

const items: TimelineItem[] = [];
const now = moment();
const startOfDayUnix = moment().startOf('day').unix();
const startOfDay = moment().startOf('day');

input.forEach((wf) => {
const status = wf.Status;
const start = status?.StartedAt;
if (start && start != '-') {
const startUnix = Math.max(moment(start).unix(), startOfDayUnix);
const end = status.FinishedAt;
let to = now.unix();
if (end && end != '-') {
to = moment(end).unix();
}
ret.push({
name: status.Name,
status: status.Status,
values: [startUnix, to],
if (start && start !== '-') {
const startMoment = moment(start);
const end =
status.FinishedAt && status.FinishedAt !== '-'
? moment(status.FinishedAt)
: now;

items.push({
id: status.Name + `_${status.RequestId}`,
content: status.Name,
start: startMoment.tz(timezone).toDate(),
end: end.tz(timezone).toDate(),
group: 'main',
className: `status-${status.Status}`,
});
}
});
const sorted = ret.sort((a, b) => {
return a.values[0] < b.values[0] ? -1 : 1;
});
setData(sorted);

const dataset = new DataSet(items);

if (!timelineInstance.current) {
timelineInstance.current = new Timeline(timelineRef.current, dataset, {
moment: (date: MomentInput) => moment(date).tz(timezone),
start: startOfDay.toDate(),
end: now.endOf('day').toDate(),
orientation: 'top',
stack: true,
showMajorLabels: true,
showMinorLabels: true,
showTooltips: true,
zoomable: false,
verticalScroll: true,
timeAxis: { scale: 'hour', step: 1 },
format: {
minorLabels: {
minute: 'HH:mm',
hour: 'HH:mm',
},
majorLabels: {
hour: 'HH:mm',
day: 'ddd D MMMM',
},
},
height: '100%',
maxHeight: '100%',
margin: { item: { vertical: 10 } },
});
} else {
timelineInstance.current.setItems(dataset);
}

console.log(
{input, items}
)

return () => {
if (timelineInstance.current) {
timelineInstance.current.destroy();
timelineInstance.current = null;
}
};
}, [input]);
const now = moment();
const shouldScroll = data.length >= 40;

return (
<TimelineWrapper shouldScroll={shouldScroll}>
<ResponsiveContainer
width="100%"
minHeight={shouldScroll ? data.length * 12 : undefined}
height={shouldScroll ? undefined : '90%'}
>
<BarChart data={data} layout="vertical">
<XAxis
name="Time"
tickFormatter={(unixTime) => moment.unix(unixTime).format('HH:mm')}
type="number"
dataKey="values"
tickCount={96}
domain={[now.startOf('day').unix(), now.endOf('day').unix()]}
/>
<YAxis dataKey="name" type="category" hide />
<Bar background dataKey="values" fill="lightblue" minPointSize={2}>
{data.map((_, index) => {
const color = statusColorMapping[data[index].status];
return <Cell key={index} fill={color.backgroundColor} />;
})}
<LabelList
dataKey="name"
position="insideLeft"
content={({ x, y, width, height, value }: LabelProps) => {
return (
<text
x={Number(x) + Number(width) + 2}
y={Number(y) + (Number(height) || 0) / 2}
fill="#000"
fontSize={12}
textAnchor="start"
>
{value}
</text>
);
}}
/>
</Bar>
</BarChart>
</ResponsiveContainer>
<TimelineWrapper>
<div
ref={timelineRef}
style={{ width: '100%', height: '100%' }}
/>
<style>
{`
.vis-item .vis-item-overflow {
overflow: visible;
color: black;
}
.vis-panel.vis-top {
position: sticky;
top: 0;
z-index: 1;
background-color: white;
}
.vis-labelset {
position: sticky;
left: 0;
z-index: 2;
background-color: white;
}
.vis-item .vis-item-content {
position: absolute;
left: 100% !important;
padding-left: 5px;
transform: translateY(-50%);
top: 50%;
white-space: nowrap;
}
.vis-item {
overflow: visible !important;
}
`}
</style>
<style>{`
${Object.entries(statusColorMapping)
.map(
([status, color]) => `
.status-${status.toLowerCase()} {
background-color: ${color.backgroundColor};
color: ${color.color};
border-color: ${color.backgroundColor};
}
`
)
.join('\n')}
`}</style>
</TimelineWrapper>
);
}

function TimelineWrapper({
children,
shouldScroll,
}: {
children: React.ReactNode;
shouldScroll: boolean;
}) {
if (shouldScroll) {
return (
<Box
sx={{
width: '100%',
maxWidth: '100%',
height: '90%',
overflow: 'auto',
}}
>
{children}
</Box>
);
}
return <React.Fragment>{children}</React.Fragment>;
function TimelineWrapper({ children }: { children: React.ReactNode }) {
return (
<Box
sx={{
width: '95%',
maxWidth: '95%',
height: '60vh',
overflow: 'auto',
backgroundColor: 'lightgray',
}}
>
{children}
</Box>
);
}

export default DashboardTimechart;
export default DashboardTimechart;
2 changes: 1 addition & 1 deletion ui/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function Dashboard() {
height: '100%',
}}
>
<Title>Timeline</Title>
<Title>{getConfig().tz ? `Timeline in ${getConfig().tz}` : "Timeline"}</Title>
<DashboardTimechart data={data?.DAGs || []} />
</Box>
</Grid>
Expand Down
Loading
Loading