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

ResourceTelemetryDiagnostics.logic.ts

82 lines3.1 KB

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