@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 102 lines 3.66 KB
Raw
1 import type { ResourceTelemetryProcess, ResourceTelemetrySourceStatus } from "@t3tools/contracts";
2 import { msg } from "gt-react";
3
4 import { m, type MessageResolver } from "../../i18n/messages";
5
6 export function processIdentityKey(process: Pick<ResourceTelemetryProcess, "identity">): string {
7 return `${process.identity.pid}:${process.identity.startTimeMs}`;
8 }
9
10 export function visibleResourceTelemetryProcesses(
11 processes: ReadonlyArray<ResourceTelemetryProcess>,
12 collapsed: ReadonlySet<string>,
13 ): ReadonlyArray<ResourceTelemetryProcess> {
14 const childrenByParent = new Map<number, ResourceTelemetryProcess[]>();
15 for (const process of processes) {
16 const children = childrenByParent.get(process.ppid) ?? [];
17 children.push(process);
18 childrenByParent.set(process.ppid, children);
19 }
20
21 const hidden = new Set<string>();
22 const hideDescendants = (pid: number): void => {
23 for (const child of childrenByParent.get(pid) ?? []) {
24 // Depth 0 rows are their own roots, e.g. the server launched by Electron main.
25 if (child.depth === 0) continue;
26 const key = processIdentityKey(child);
27 if (hidden.has(key)) continue;
28 hidden.add(key);
29 hideDescendants(child.identity.pid);
30 }
31 };
32 for (const process of processes) {
33 if (collapsed.has(processIdentityKey(process))) {
34 hideDescendants(process.identity.pid);
35 }
36 }
37 return processes.filter((process) => !hidden.has(processIdentityKey(process)));
38 }
39
40 export function shouldShowResourceMonitorRetry(input: {
41 readonly nativeStatus: ResourceTelemetrySourceStatus | null;
42 readonly error: string | null;
43 }): boolean {
44 return (
45 (input.nativeStatus === null && input.error !== null) ||
46 input.nativeStatus === "degraded" ||
47 input.nativeStatus === "unavailable" ||
48 input.nativeStatus === "stopped"
49 );
50 }
51
52 export function resourceHistoryBarHeight(input: {
53 readonly value: number;
54 readonly max: number;
55 readonly minimumVisiblePercent: number;
56 }): number {
57 if (input.value <= 0) return 0;
58 return Math.max(input.minimumVisiblePercent, (input.value / Math.max(1, input.max)) * 100);
59 }
60
61 export function resourceHistoryCpuScaleMax(
62 buckets: ReadonlyArray<{ readonly avgCpuPercent: number }>,
63 ): number {
64 return Math.max(1, ...buckets.map((bucket) => bucket.avgCpuPercent));
65 }
66
67 export function formatCpuTime(seconds: number, resolve: MessageResolver = m): string {
68 const format = (value: number) => value.toFixed(value >= 10 ? 1 : 2).replace(/\.0+$/, "");
69 // Pick the unit after rounding so 59.97 seconds never renders as "60s".
70 const shortSeconds = format(seconds);
71 if (Number(shortSeconds) < 60) {
72 return resolve(msg("{value}s", { value: shortSeconds, $context: "CPU time in seconds" }));
73 }
74 const shortMinutes = format(seconds / 60);
75 if (Number(shortMinutes) < 60) {
76 return resolve(msg("{value}m", { value: shortMinutes, $context: "CPU time in minutes" }));
77 }
78 return resolve(
79 msg("{value}h", {
80 value: (seconds / 3_600).toFixed(2).replace(/\.0+$/, ""),
81 $context: "CPU time in hours",
82 }),
83 );
84 }
85
86 export function formatSamplingStatus(intervalMs: number, resolve: MessageResolver = m): string {
87 // The server reports 0 while the native monitor is idle and samples only on request.
88 if (intervalMs <= 0) return resolve(msg("Sampling on demand"));
89 if (intervalMs < 1_000) {
90 return resolve(msg("Sampling every {ms} ms", { ms: Math.round(intervalMs) }));
91 }
92 const seconds = intervalMs / 1_000;
93 return resolve(
94 msg(
95 "{count, plural, one {Sampling every {seconds} second} other {Sampling every {seconds} seconds}}",
96 {
97 count: seconds,
98 seconds: seconds.toLocaleString(undefined, { maximumFractionDigits: 1 }),
99 },
100 ),
101 );
102 }