Skip to content

feat(site): add display for workspace timings #14773

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

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5268b1e
Add base components for the chart
BrunoQuaresma Sep 19, 2024
4d509f9
Improve spacing calc
BrunoQuaresma Sep 19, 2024
d48624b
Make bars clickable
BrunoQuaresma Sep 19, 2024
62152ce
Refactor code to allow multiple views
BrunoQuaresma Sep 20, 2024
fd84ed9
Add basic view and breadcrumbs
BrunoQuaresma Sep 23, 2024
f7f09ff
Add resource filtering
BrunoQuaresma Sep 23, 2024
2ffc75a
Find the right tick spacings
BrunoQuaresma Sep 23, 2024
a8372e1
Add colors to the bars
BrunoQuaresma Sep 23, 2024
f7c7488
Do not display Coder resource
BrunoQuaresma Sep 23, 2024
0868185
Add legends
BrunoQuaresma Sep 23, 2024
54d13c8
Handle empty search
BrunoQuaresma Sep 23, 2024
714e37b
Improve coder resource filter and adjust bar hover
BrunoQuaresma Sep 24, 2024
a2dd126
Only scroll the chart
BrunoQuaresma Sep 24, 2024
0b4747e
Add tooltip
BrunoQuaresma Sep 24, 2024
49d3a72
Refactor code and improve legends
BrunoQuaresma Sep 25, 2024
647635d
Adjust columns to fit the space
BrunoQuaresma Sep 25, 2024
6c742aa
Customize scroll
BrunoQuaresma Sep 25, 2024
9a8bb59
Add info tooltip
BrunoQuaresma Sep 25, 2024
4139151
Fix fmt
BrunoQuaresma Sep 25, 2024
bcff9c6
Fix nblock gen
BrunoQuaresma Sep 25, 2024
97b25d9
Fix key
BrunoQuaresma Sep 25, 2024
6d5c344
Debug on chromatic
BrunoQuaresma Sep 25, 2024
c4bd74e
Another debug image
BrunoQuaresma Sep 25, 2024
939ec9a
Try with useEffect
BrunoQuaresma Sep 25, 2024
49c69e0
Fix labels alignment
BrunoQuaresma Sep 26, 2024
61008a3
Increase border radius tooltip
BrunoQuaresma Sep 26, 2024
f969ef2
Add scroll mask
BrunoQuaresma Sep 26, 2024
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
Prev Previous commit
Next Next commit
Add basic view and breadcrumbs
  • Loading branch information
BrunoQuaresma committed Sep 23, 2024
commit fd84ed94c7cf3d3ff83593d7671cce845893cc97
1 change: 1 addition & 0 deletions site/src/modules/workspaces/WorkspaceTiming/Chart/Bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const styles = {
height: 32,
display: "flex",
padding: 0,
minWidth: 8,

"&:not(:disabled)": {
cursor: "pointer",
Expand Down
100 changes: 58 additions & 42 deletions site/src/modules/workspaces/WorkspaceTiming/Chart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ import {
barsSpacing,
columnWidth,
contentSidePadding,
intervalDimension,
XAxisHeight,
} from "./constants";
import { Bar } from "./Bar";

// When displaying the chart we must consider the time intervals to display the
// data. For example, if the total time is 10 seconds, we should display the
// data in 200ms intervals. However, if the total time is 1 minute, we should
// display the data in 5 seconds intervals. To achieve this, we define the
// dimensions object that contains the time intervals for the chart.
const dimensions = {
small: 500,
default: 5_000,
};

export type ChartProps = {
data: DataSection[];
onBarClick: (label: string, section: string) => void;
Expand Down Expand Up @@ -54,8 +63,33 @@ export type Timing = Duration & {
};

export const Chart: FC<ChartProps> = ({ data, onBarClick }) => {
const totalDuration = calcTotalDuration(data.flatMap((d) => d.timings));
const intervals = createIntervals(totalDuration, intervalDimension);
const totalDuration = duration(data.flatMap((d) => d.timings));
const totalTime = durationTime(totalDuration);
// Use smaller dimensions for the chart if the total time is less than 10
// seconds; otherwise, use default intervals.
const dimension = totalTime < 10_000 ? dimensions.small : dimensions.default;

// XAxis intervals
const intervalsCount = Math.ceil(totalTime / dimension);
const intervals = Array.from(
{ length: intervalsCount },
(_, i) => i * dimension + dimension,
);

// Helper function to convert time into pixel size, used for setting bar width
// and offset
const calcSize = (time: number): number => {
return (columnWidth * time) / dimension;
};

const formatTime = (time: number): string => {
if (dimension === dimensions.small) {
return `${time.toLocaleString()}ms`;
}
return `${(time / 1_000).toLocaleString(undefined, {
maximumFractionDigits: 2,
})}s`;
};

return (
<div css={styles.chart}>
Expand All @@ -65,7 +99,10 @@ export const Chart: FC<ChartProps> = ({ data, onBarClick }) => {
<YAxisCaption>{section.name}</YAxisCaption>
<YAxisLabels>
{section.timings.map((t) => (
<YAxisLabel key={t.label} id={`${t.label}-label`}>
<YAxisLabel
key={t.label}
id={`${encodeURIComponent(t.label)}-label`}
>
{t.label}
</YAxisLabel>
))}
Expand All @@ -75,28 +112,28 @@ export const Chart: FC<ChartProps> = ({ data, onBarClick }) => {
</YAxis>

<div css={styles.main}>
<XAxis labels={intervals.map(formatAsTimer)} />
<XAxis labels={intervals.map(formatTime)} />
<div css={styles.content}>
{data.map((section) => {
return (
<div key={section.name} css={styles.bars}>
{section.timings.map((t) => {
// The time this timing started relative to the initial timing
const offset = diffInSeconds(
t.startedAt,
totalDuration.startedAt,
);
const size = secondsToPixel(durationToSeconds(t));
const offset =
t.startedAt.getTime() - totalDuration.startedAt.getTime();
const size = calcSize(durationTime(t));
return (
<Bar
key={t.label}
x={secondsToPixel(offset)}
x={calcSize(offset)}
width={size}
afterLabel={`${durationToSeconds(t).toFixed(2)}s`}
afterLabel={formatTime(durationTime(t))}
aria-labelledby={`${t.label}-label`}
ref={applyBarHeightToLabel}
disabled={t.count <= 1}
onClick={() => {
if (t.count <= 1) {
return;
}
onBarClick(t.label, section.name);
}}
>
Expand Down Expand Up @@ -130,41 +167,22 @@ const applyBarHeightToLabel = (bar: HTMLDivElement | null) => {
// #coder_metadata.container_info[0]) will fail because it is not a valid
// selector. To handle this, we need to query by the id attribute and escape
// it with quotes.
const label = document.querySelector<HTMLSpanElement>(`[id="${labelId}"]`);
const label = document.querySelector<HTMLSpanElement>(
`[id="${encodeURIComponent(labelId)}"]`,
);
if (!label) {
return;
}
label.style.height = `${bar.clientHeight}px`;
};

// Format a number in seconds to 00:00:00 format
const formatAsTimer = (seconds: number): string => {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = seconds % 60;

return `${hours.toString().padStart(2, "0")}:${minutes
.toString()
.padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
};

const durationToSeconds = (duration: Duration): number => {
return (duration.endedAt.getTime() - duration.startedAt.getTime()) / 1000;
};

// Create the intervals to be used in the XAxis
const createIntervals = (duration: Duration, range: number): number[] => {
const intervals = Math.ceil(durationToSeconds(duration) / range);
return Array.from({ length: intervals }, (_, i) => i * range + range);
};

const secondsToPixel = (seconds: number): number => {
return (columnWidth * seconds) / intervalDimension;
const durationTime = (duration: Duration): number => {
return duration.endedAt.getTime() - duration.startedAt.getTime();
};

// Combine multiple durations into a single duration by using the initial start
// time and the final end time.
export const calcTotalDuration = (durations: readonly Duration[]): Duration => {
export const duration = (durations: readonly Duration[]): Duration => {
const sortedDurations = durations
.slice()
.sort((a, b) => a.startedAt.getTime() - b.startedAt.getTime());
Expand All @@ -177,10 +195,6 @@ export const calcTotalDuration = (durations: readonly Duration[]): Duration => {
return { startedAt: start, endedAt: end };
};

const diffInSeconds = (b: Date, a: Date): number => {
return (b.getTime() - a.getTime()) / 1000;
};

const styles = {
chart: {
display: "flex",
Expand Down Expand Up @@ -216,6 +230,8 @@ const styles = {
flexDirection: "column",
flex: 1,
borderLeft: `1px solid ${theme.palette.divider}`,
height: "fit-content",
minHeight: "100%",
}),
content: {
flex: 1,
Expand Down
27 changes: 19 additions & 8 deletions site/src/modules/workspaces/WorkspaceTiming/Chart/YAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { barsSpacing, XAxisHeight } from "./constants";
// Predicting the caption height is necessary to add appropriate spacing to the
// grouped bars, ensuring alignment with the sidebar labels.
export const YAxisCaptionHeight = 20;
export const YAxisWidth = 200;
export const YAxisSidePadding = 16;

export const YAxis: FC<HTMLProps<HTMLDivElement>> = (props) => {
return <div css={styles.root} {...props} />;
Expand All @@ -23,14 +25,18 @@ export const YAxisLabels: FC<HTMLProps<HTMLUListElement>> = (props) => {
};

export const YAxisLabel: FC<HTMLProps<HTMLLIElement>> = (props) => {
return <li {...props} css={styles.label} />;
return (
<li {...props} css={styles.label}>
<span>{props.children}</span>
</li>
);
};

const styles = {
root: {
width: 200,
width: YAxisWidth,
flexShrink: 0,
padding: 16,
padding: YAxisSidePadding,
paddingTop: XAxisHeight,
},
caption: (theme) => ({
Expand All @@ -51,10 +57,15 @@ const styles = {
textAlign: "right",
},
label: {
display: "block",
maxWidth: "100%",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
display: "flex",
alignItems: "center",

"& > *": {
display: "block",
width: "100%",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
},
},
} satisfies Record<string, Interpolation<Theme>>;
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ export const contentSidePadding = 4;
*/
export const columnWidth = 130;

/**
* Time interval used to calculate the XAxis dimension.
*/
export const intervalDimension = 5;
Loading