Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadDetailsMachineLoad.tsx

114 lines4.7 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 type EnvironmentId,
4 type HostPressureLevel,
5 worstHostPressureLevel,
6} from "@t3tools/contracts";
7import { useGT, plural, part } from "gt-react";
8import { useState } from "react";
9​
10import { formatBytes } from "../../lib/formatBytes";
11import { useServerConfigs } from "../../state/entities";
12import { environmentPresentations } from "../../state/presentation";
13import { serverEnvironment } from "../../state/server";
14import { ThreadDetailsSection } from "./ThreadDetailsSection";
15import { UsageRow, type UsageTone } from "./ThreadDetailsUsage";
16​
17const toneOf = (level: HostPressureLevel | undefined): UsageTone =>
18 level === "severe" ? "error" : level === "elevated" ? "warning" : null;
19​
20const percent = (fraction: number) => Math.round(Math.max(0, Math.min(1, fraction)) * 100);
21​
22/** CPU, memory and disk on the machine the thread runs on, colored by whether work there stalls. */
23export function ThreadDetailsMachineLoad({
24 environmentId,
25}: {
26 readonly environmentId: EnvironmentId;
27}) {
28 const gt = useGT();
29 const result = useAtomValue(serverEnvironment.liveHostResources({ environmentId, input: {} }));
30 const pressure = useServerConfigs().get(environmentId)?.hostPressure;
31 const connected = useAtomValue(
32 environmentPresentations.presentationsAtom,
33 (presentations) => presentations.get(environmentId)?.connection.phase === "connected",
34 );
35 // A reconnecting query keeps its last sample, also briefly after it reconnects. Live gauges
36 // stay hidden until a sample newer than the one held when the connection dropped arrives.
37 const sample = result._tag === "Success" ? result.value : null;
38 const [staleSample, setStaleSample] = useState<typeof sample>(null);
39 if (!connected && sample !== null && sample !== staleSample) setStaleSample(sample);
40 if (!connected || sample === null || sample === staleSample) return null;
41 const snapshot = sample;
42 const usedMemory = snapshot.totalMemoryBytes - snapshot.availableMemoryBytes;
43 const diskFree = snapshot.diskFreeBytes ?? null;
44 const diskTotal = snapshot.diskTotalBytes ?? null;
45 // CPU and storage stalls both slow work down, so the CPU row carries the worse of the two.
46 const cpuTone =
47 pressure === undefined
48 ? null
49 : toneOf(worstHostPressureLevel({ ...pressure, memory: "none", disk: "none" }));
50 return (
51 <ThreadDetailsSection
52 headingId="thread-details-machine-load-heading"
53 title={gt`Machine load`}
54 showHeading={false}
55 >
56 <div className="grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs">
57 <UsageRow
58 label={gt({ context: "machine load row label" })`CPU`}
59 fill={snapshot.cpuUtilization === null ? null : percent(snapshot.cpuUtilization)}
60 tone={cpuTone}
61 muted
62 value={
63 snapshot.cpuUtilization === null
64 ? gt`Unavailable`
65 : `${percent(snapshot.cpuUtilization)}%`
66 }
67 tooltip={
68 <span className="text-foreground">
69 {cpuTone === null
70 ? gt`${plural(
71 { count: snapshot.cpuCount },
72 {
73 one: part`${{ count: snapshot.cpuCount }} CPU`,
74 other: part`${{ count: snapshot.cpuCount }} CPUs`,
75 },
76 )}`
77 : gt`Work waits for a free CPU or for the disk, so agents run slowly.`}
78 </span>
79 }
80 />
81 <UsageRow
82 label={gt({ context: "machine load row label" })`Memory`}
83 fill={
84 snapshot.totalMemoryBytes > 0 ? percent(usedMemory / snapshot.totalMemoryBytes) : null
85 }
86 tone={toneOf(pressure?.memory)}
87 muted
88 value={`${formatBytes(usedMemory)} / ${formatBytes(snapshot.totalMemoryBytes)}`}
89 tooltip={
90 <span className="text-foreground">
91 {pressure?.memory === undefined || pressure.memory === "none"
92 ? gt`${{ available: formatBytes(snapshot.availableMemoryBytes) }} available`
93 : gt`Agents keep waiting while the system frees up memory.`}
94 </span>
95 }
96 />
97 {diskFree !== null && diskTotal !== null && diskTotal > 0 ? (
98 <UsageRow
99 label={gt({ context: "machine load row label" })`Disk`}
100 fill={percent((diskTotal - diskFree) / diskTotal)}
101 tone={toneOf(pressure?.disk)}
102 muted
103 value={gt`${{ free: formatBytes(diskFree) }} free`}
104 tooltip={
105 <span className="text-foreground">
106 {gt`Where T3 Code keeps its data and worktrees`}
107 </span>
108 }
109 />
110 ) : null}
111 </div>
112 </ThreadDetailsSection>
113 );