10000 feat(site): add workspace timings by BrunoQuaresma · Pull Request #15068 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(site): add workspace timings #15068

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

Merged
merged 54 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 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
f55033a
Merge branch 'main' of https://github.com/coder/coder into bq/workspa…
BrunoQuaresma Oct 14, 2024
00616d3
Include agent scripts
BrunoQuaresma Oct 14, 2024
8cc7eef
Add failed icon
BrunoQuaresma Oct 14, 2024
6b95860
Use use effect
BrunoQuaresma Oct 14, 2024
5d08ff0
Apply @chifro feedback
BrunoQuaresma Oct 17, 2024
ea3676a
Add legends to script chart
BrunoQuaresma Oct 17, 2024
1c74c96
Fix fmt
BrunoQuaresma Oct 17, 2024
57df89b
Apply Christin suggestions
BrunoQuaresma Oct 18, 2024
48a61d8
Move style constants to css variables
BrunoQuaresma Oct 18, 2024
0726f39
Fix and rename bar blocks
BrunoQuaresma Oct 18, 2024
a4752c3
Remove unecessary cursor
BrunoQuaresma Oct 18, 2024
b481edd
Add tooltip for scripts
BrunoQuaresma Oct 18, 2024
90b54b4
Fix fmt
BrunoQuaresma Oct 18, 2024
266d82d
Add provisioning time collapse
BrunoQuaresma Oct 18, 2024
a4a1ba8
Merge branch 'main' of https://github.com/coder/coder into bq/workspa…
BrunoQuaresma Oct 18, 2024
4eb24f7
Add timings to the workspace UI
BrunoQuaresma Oct 18, 2024
79f370c
Fix fmt
BrunoQuaresma Oct 18, 2024
a1ee002
Merge branch 'main' of https://github.com/coder/coder into bq/workspa…
BrunoQuaresma Oct 21, 2024
75ea1e5
Fix code verbiage
BrunoQuaresma Oct 21, 2024
edb885c
Fetch timings from build and not workspace
BrunoQuaresma Oct 21, 2024
f8bb22c
Don't could coder resources for blocks
BrunoQuaresma Oct 21, 2024
1ff245c
Fix timings getting fetched before build getting done
BrunoQuaresma Oct 21, 2024
541ccba
Fix comment spacing
BrunoQuaresma Oct 21, 2024
b3f0351
Revert scales on def
BrunoQuaresma Oct 21, 2024
65ca087
Fix columnd calcl
BrunoQuaresma Oct 21, 2024
f288169
Fix fmt
BrunoQuaresma Oct 22, 2024
08874a3
Add extra tests
BrunoQuaresma Oct 23, 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
Adjust columns to fit the space
  • Loading branch information
BrunoQuaresma committed Sep 25, 2024
commit 647635d7c0aa257eba4951e63005cb60358b0990
29 changes: 20 additions & 9 deletions site/src/modules/workspaces/WorkspaceTiming/Chart/Bar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Interpolation, Theme } from "@emotion/react";
import { type ButtonHTMLAttributes, forwardRef, type HTMLProps } from "react";
import { CSSVars } from "./constants";

export type BarColors = {
stroke: string;
Expand All @@ -8,9 +9,10 @@ export type BarColors = {

type BaseBarProps<T> = Omit<T, "size" | "color"> & {
/**
* The width of the bar component.
* Scale used to determine the width based on the given value.
*/
size: number;
scale: number;
value: number;
/**
* The X position of the bar component.
*/
Expand All @@ -25,36 +27,45 @@ type BaseBarProps<T> = Omit<T, "size" | "color"> & {
type BarProps = BaseBarProps<HTMLProps<HTMLDivElement>>;

export const Bar = forwardRef<HTMLDivElement, BarProps>(
({ colors, size, children, offset, ...htmlProps }, ref) => {
({ colors, scale, value, offset, ...htmlProps }, ref) => {
return (
<div css={barCss({ colors, size, offset })} {...htmlProps} ref={ref} />
<div
css={barCss({ colors, scale, value, offset })}
{...htmlProps}
ref={ref}
/>
);
},
);

type ClickableBarProps = BaseBarProps<ButtonHTMLAttributes<HTMLButtonElement>>;

export const ClickableBar = forwardRef<HTMLButtonElement, ClickableBarProps>(
({ colors, size, offset, ...htmlProps }, ref) => {
({ colors, scale, value, offset, ...htmlProps }, ref) => {
return (
<button
type="button"
css={[...barCss({ colors, size, offset }), styles.clickable]}
css={[...barCss({ colors, scale, value, offset }), styles.clickable]}
{...htmlProps}
ref={ref}
/>
);
},
);

export const barCss = ({ size, colors, offset }: BaseBarProps<unknown>) => {
export const barCss = ({
scale,
value,
colors,
offset,
}: BaseBarProps<unknown>) => {
return [
styles.bar,
{
width: size,
width: `calc((var(${CSSVars.xAxisWidth}) * ${value}) / ${scale})`,
backgroundColor: colors?.fill,
borderColor: colors?.stroke,
marginLeft: offset,
marginLeft: `calc((var(${CSSVars.xAxisWidth}) * ${offset}) / ${scale})`,
},
];
};
Expand Down
26 changes: 20 additions & 6 deletions site/src/modules/workspaces/WorkspaceTiming/Chart/BarBlocks.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Interpolation, Theme } from "@emotion/react";
import { MoreHorizOutlined } from "@mui/icons-material";
import type { FC } from "react";
import { useLayoutEffect, useRef, useState, type FC } from "react";

const sidePadding = 8;
const spaceBetweenBlocks = 4;
Expand All @@ -9,21 +9,35 @@ const blockSize = 20;

type BarBlocksProps = {
count: number;
barSize: number;
};

export const BarBlocks: FC<BarBlocksProps> = ({ count, barSize }) => {
export const BarBlocks: FC<BarBlocksProps> = ({ count }) => {
const [parentWidth, setParentWidth] = useState<number>();
const blocksRef = useRef<HTMLDivElement>(null);

useLayoutEffect(() => {
if (!blocksRef.current || parentWidth) {
return;
}
const parentEl = blocksRef.current.parentElement;
if (!parentEl) {
throw new Error("BarBlocks must be a child of a Bar");
}
setParentWidth(parentEl.clientWidth);
}, [parentWidth]);

const totalSpaceBetweenBlocks = (count - 1) * spaceBetweenBlocks;
const freeSize = barSize - sidePadding * 2;
const freeSize = parentWidth ? parentWidth - sidePadding * 2 : 0;
const necessarySize = blockSize * count + totalSpaceBetweenBlocks;
const hasSpacing = necessarySize <= freeSize;
const nOfPossibleBlocks = Math.floor(
(freeSize - moreIconSize - totalSpaceBetweenBlocks) / blockSize,
(freeSize - moreIconSize) / (blockSize + spaceBetweenBlocks),
);
const nOfBlocks = hasSpacing ? count : nOfPossibleBlocks;
console.log("POSSIBLE ->", count, nOfPossibleBlocks);

return (
<div css={styles.blocks}>
<div ref={blocksRef} css={styles.blocks}>
{Array.from({ length: nOfBlocks }).map((_, i) => (
// biome-ignore lint/suspicious/noArrayIndexKey: we are using the index as a key here because the blocks are not expected to be reordered
<div key={i} css={styles.block} style={{ minWidth: blockSize }} />
Expand Down
8 changes: 4 additions & 4 deletions site/src/modules/workspaces/WorkspaceTiming/Chart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import React from "react";
import type { BarColors } from "./Bar";
import { YAxisSidePadding, YAxisWidth } from "./YAxis";

export const ChartContent: FC<HTMLProps<HTMLDivElement>> = (props) => {
return <div css={styles.content} {...props} />;
};

export const Chart = (props: HTMLProps<HTMLDivElement>) => {
return <div css={styles.chart} {...props} />;
};

export const ChartContent: FC<HTMLProps<HTMLDivElement>> = (props) => {
return <div css={styles.content} {...props} />;
};

export const ChartToolbar = (props: HTMLProps<HTMLDivElement>) => {
return <div css={styles.toolbar} {...props} />;
};
Expand Down
54 changes: 37 additions & 17 deletions site/src/modules/workspaces/WorkspaceTiming/Chart/XAxis.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import type { FC, HTMLProps } from "react";
import {
useLayoutEffect,
useRef,
useState,
type FC,
type HTMLProps,
} from "react";
import type { Interpolation, Theme } from "@emotion/react";
import { YAxisCaptionHeight } from "./YAxis";
import { formatTime } from "./utils";
import { XAxisLabelsHeight, XAxisRowsGap } from "./constants";
import { CSSVars, XAxisLabelsHeight, XAxisRowsGap } from "./constants";

export const XAxisWidth = 130;
export const XAxisMinWidth = 130;
export const XAxisSidePadding = 16;

type XAxisProps = HTMLProps<HTMLDivElement> & {
Expand All @@ -13,13 +19,28 @@ type XAxisProps = HTMLProps<HTMLDivElement> & {
};

export const XAxis: FC<XAxisProps> = ({ ticks, scale, ...htmlProps }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this component is related, but I found that if I:

  1. Stop a running workspace, wait for it to stop
  2. Click "Workspaces"
  3. Go back into my now-stopped workspace

Then the timings run off edge of the component and I am not able to see them. It seems to fix itself if I navigate away and come back again. Weirdly, the same issue does not appear to happen if I start a stopped workspace then navigate away and back again, it only happens when I stop my workspace.

screenshot

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing is if I resize my screen then the timings and legend get cut off, idk if it is worth adding resizing logic but maybe we can just throw in a horizontal scrollbar.

const rootRef = useRef<HTMLDivElement>(null);

// The X axis should occupy all available space. If there is extra space,
// increase the column width accordingly. Use a CSS variable to propagate the
// value to the child components.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important, but this comment has a bunch of space at the beginning 😄

useLayoutEffect(() => {
const rootEl = rootRef.current;
if (!rootEl) {
return;
}
// We always add one extra column to the grid to ensure that the last column
// is fully visible.
const avgWidth = rootEl.clientWidth / (ticks.length + 1);
avgWidth > XAxisMinWidth ? avgWidth : XAxisMinWidth;
rootEl.style.setProperty(CSSVars.xAxisWidth, `${avgWidth}px`);
}, [ticks]);

return (
<div css={styles.root} {...htmlProps}>
<div css={styles.root} {...htmlProps} ref={rootRef}>
<XAxisLabels>
{ticks.map((tick) => (
<XAxisLabel key={tick} width={XAxisWidth}>
{formatTime(tick, scale)}
</XAxisLabel>
<XAxisLabel key={tick}>{formatTime(tick, scale)}</XAxisLabel>
))}
</XAxisLabels>
{htmlProps.children}
Expand All @@ -32,11 +53,7 @@ export const XAxisLabels: FC<HTMLProps<HTMLUListElement>> = (props) => {
return <ul css={styles.labels} {...props} />;
};

type XAxisLabelProps = HTMLProps<HTMLLIElement> & {
width: number;
};

export const XAxisLabel: FC<XAxisLabelProps> = ({ width, ...htmlProps }) => {
export const XAxisLabel: FC<HTMLProps<HTMLLIElement>> = (props) => {
return (
<li
css={[
Expand All @@ -47,13 +64,13 @@ export const XAxisLabel: FC<XAxisLabelProps> = ({ width, ...htmlProps }) => {
// 2. Shift the label to the left by half of the column width.
// Note: This adjustment is not applied to the first element,
// as the 0 label/value is not displayed in the chart.
width: width * 2,
width: `calc(var(${CSSVars.xAxisWidth}) * 2)`,
"&:not(:first-child)": {
marginLeft: -width,
marginLeft: `calc(-1 * var(${CSSVars.xAxisWidth}))`,
},
},
]}
{...htmlProps}
{...props}
/>
);
};
Expand Down Expand Up @@ -108,7 +125,10 @@ export const XGrid: FC<XGridProps> = ({ columns, ...htmlProps }) => {
return (
<div css={styles.grid} role="presentation" {...htmlProps}>
{[...Array(columns).keys()].map((key) => (
<div key={key} css={[styles.column, { width: XAxisWidth }]} />
<div
key={key}
css={[styles.column, { width: `var(${CSSVars.xAxisWidth})` }]}
/>
))}
</div>
);
Expand Down Expand Up @@ -138,7 +158,7 @@ const styles = {
alignItems: "center",
borderBottom: `1px solid ${theme.palette.divider}`,
height: XAxisLabelsHeight,
padding: `0px ${XAxisSidePadding}px`,
padding: 0,
minWidth: "100%",
flexShrink: 0,
position: "sticky",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Constants that are used across the Chart components
export const XAxisLabelsHeight = 40;
export const XAxisRowsGap = 20;
export const CSSVars = {
xAxisWidth: "--x-axis-width",
};
27 changes: 3 additions & 24 deletions site/src/modules/workspaces/WorkspaceTiming/Chart/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,9 @@ export const formatTime = (time: number, scale: number): string => {
})}s`;
};

// Helper function to convert the tick spacing into pixel size. This is used
// for setting the bar width and offset.
export const calcSize = (
time: number,
scale: number,
columnWidth: number,
): number => {
return (columnWidth * time) / scale;
};

export const calcBarSizeAndOffset = (
export const calcOffset = (
timing: BaseTiming,
generalTiming: BaseTiming,
scale: number,
columnWidth: number,
) => {
const offset = calcSize(
timing.startedAt.getTime() - generalTiming.startedAt.getTime(),
scale,
columnWidth,
);
const size = calcSize(calcDuration(timing), scale, columnWidth);
return {
size,
offset,
};
): number => {
return timing.startedAt.getTime() - generalTiming.startedAt.getTime();
};
13 changes: 5 additions & 8 deletions site/src/modules/workspaces/WorkspaceTiming/ResourcesChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
XAxisRow,
XAxisRows,
XAxisSections,
XAxisWidth,
XAxisMinWidth,
} from "./Chart/XAxis";
import { useState, type FC } from "react";
import {
Expand All @@ -15,8 +15,8 @@ import {
} from "./Chart/YAxis";
import { Bar } from "./Chart/Bar";
import {
calcBarSizeAndOffset,
calcDuration,
calcOffset,
combineTimings,
formatTime,
makeTicks,
Expand Down Expand Up @@ -137,12 +137,9 @@ export const ResourcesChart: FC<ResourcesChartProps> = ({
<XAxisRow key={t.name} yAxisLabelId={t.name}>
<ResourceTooltip timing={t}>
<Bar
{...calcBarSizeAndOffset(
t,
generalTiming,
scale,
XAxisWidth,
)}
value={calcDuration(t)}
offset={calcOffset(t, generalTiming)}
scale={scale}
colors={legendsByAction[t.action].colors}
/>
</ResourceTooltip>
Expand Down
24 changes: 10 additions & 14 deletions site/src/modules/workspaces/WorkspaceTiming/StagesChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
XAxisRow,
XAxisRows,
XAxisSections,
XAxisWidth,
XAxisMinWidth,
} from "./Chart/XAxis";
import type { FC } from "react";
import {
Expand All @@ -15,8 +15,8 @@ import {
} from "./Chart/YAxis";
import { Bar, ClickableBar } from "./Chart/Bar";
import {
calcBarSizeAndOffset,
calcDuration,
calcOffset,
combineTimings,
formatTime,
makeTicks,
Expand Down Expand Up @@ -112,29 +112,25 @@ export const StagesChart: FC<StagesChartProps> = ({
return (
<XAxisRows key={category}>
{timingsInCategory.map((t) => {
const barSizeAndOffset = calcBarSizeAndOffset(
t,
generalTiming,
scale,
XAxisWidth,
);
const value = calcDuration(t);
const offset = calcOffset(t, generalTiming);

return (
<XAxisRow key={t.name} yAxisLabelId={t.name}>
{/** We only want to expand stages with more than one resource */}
{t.resources > 1 ? (
<ClickableBar
{...barSizeAndOffset}
scale={scale}
value={value}
offset={offset}
onClick={() => {
onSelectStage(t, category);
}}
>
<BarBlocks
count={t.resources}
barSize={barSizeAndOffset.size}
/>
<BarBlocks count={t.resources} />
</ClickableBar>
) : (
<Bar {...barSizeAndOffset} />
<Bar scale={scale} value={value} offset={offset} />
)}
{formatTime(calcDuration(t), scale)}
</XAxisRow>
Expand Down
0