Skip to content

Commit 88589ef

Browse files
fix: fix build timeline scale for longer builds (coder#17514)
Fix coder#15374
1 parent 71dbd0c commit 88589ef

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

site/src/modules/workspaces/WorkspaceTiming/Chart/utils.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,20 @@ export const calcDuration = (range: TimeRange): number => {
2929
// data in 200ms intervals. However, if the total time is 1 minute, we should
3030
// display the data in 5 seconds intervals. To achieve this, we define the
3131
// dimensions object that contains the time intervals for the chart.
32-
const scales = [5_000, 500, 100];
32+
const second = 1_000;
33+
const minute = 60 * second;
34+
const hour = 60 * minute;
35+
const day = 24 * hour;
36+
const scales = [
37+
day,
38+
hour,
39+
5 * minute,
40+
minute,
41+
10 * second,
42+
5 * second,
43+
500,
44+
100,
45+
];
3346

3447
const pickScale = (totalTime: number): number => {
3548
for (const s of scales) {
@@ -48,7 +61,29 @@ export const makeTicks = (time: number) => {
4861
};
4962

5063
export const formatTime = (time: number): string => {
51-
return `${time.toLocaleString()}ms`;
64+
const seconds = Math.floor(time / 1000);
65+
const minutes = Math.floor(seconds / 60);
66+
const hours = Math.floor(minutes / 60);
67+
const days = Math.floor(hours / 24);
68+
69+
const parts: string[] = [];
70+
if (days > 0) {
71+
parts.push(`${days}d`);
72+
}
73+
if (hours > 0) {
74+
parts.push(`${hours % 24}h`);
75+
}
76+
if (minutes > 0) {
77+
parts.push(`${minutes % 60}m`);
78+
}
79+
if (seconds > 0) {
80+
parts.push(`${seconds % 60}s`);
81+
}
82+
if (time % 1000 > 0) {
83+
parts.push(`${time % 1000}ms`);
84+
}
85+
86+
return parts.join(" ");
5287
};
5388

5489
export const calcOffset = (range: TimeRange, baseRange: TimeRange): number => {

site/src/modules/workspaces/WorkspaceTiming/WorkspaceTimings.stories.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,29 @@ export const LoadingWhenAgentScriptTimingsAreEmpty: Story = {
126126
agentScriptTimings: undefined,
127127
},
128128
};
129+
130+
export const LongTimeRange = {
131+
args: {
132+
provisionerTimings: [
133+
{
134+
...WorkspaceTimingsResponse.provisioner_timings[0],
135+
started_at: "2021-09-01T00:00:00Z",
136+
ended_at: "2021-09-01T00:10:00Z",
137+
},
138+
],
139+
agentConnectionTimings: [
140+
{
141+
...WorkspaceTimingsResponse.agent_connection_timings[0],
142+
started_at: "2021-09-01T00:10:00Z",
143+
ended_at: "2021-09-01T00:35:00Z",
144+
},
145+
],
146+
agentScriptTimings: [
147+
{
148+
...WorkspaceTimingsResponse.agent_script_timings[0],
149+
started_at: "2021-09-01T00:35:00Z",
150+
ended_at: "2021-09-01T01:00:00Z",
151+
},
152+
],
153+
},
154+
};

site/src/modules/workspaces/WorkspaceTiming/WorkspaceTimings.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import type {
1212
import sortBy from "lodash/sortBy";
1313
import uniqBy from "lodash/uniqBy";
1414
import { type FC, useState } from "react";
15-
import { type TimeRange, calcDuration, mergeTimeRanges } from "./Chart/utils";
15+
import {
16+
type TimeRange,
17+
calcDuration,
18+
formatTime,
19+
mergeTimeRanges,
20+
} from "./Chart/utils";
1621
import { ResourcesChart, isCoderResource } from "./ResourcesChart";
1722
import { ScriptsChart } from "./ScriptsChart";
1823
import {
@@ -85,7 +90,7 @@ export const WorkspaceTimings: FC<WorkspaceTimingsProps> = ({
8590
const displayProvisioningTime = () => {
8691
const totalRange = mergeTimeRanges(timings.map(toTimeRange));
8792
const totalDuration = calcDuration(totalRange);
88-
return humanizeDuration(totalDuration);
93+
return formatTime(totalDuration);
8994
};
9095

9196
return (

0 commit comments

Comments
 (0)