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
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { ProcessSignalActions } from "./ProcessSignalActions"; |
| 3 | import { AuthEnvironmentMaintainScope } from "@t3tools/contracts"; |
| 4 | import { RefreshIcon } from "~/components/ui/refresh-icon"; |
| 5 | import { |
| 6 | ActivityIcon, |
| 7 | AlertTriangleIcon, |
| 8 | BatteryIcon, |
| 9 | CpuIcon, |
| 10 | DatabaseIcon, |
| 11 | GaugeIcon, |
| 12 | HardDriveIcon, |
| 13 | MemoryStickIcon, |
| 14 | ChevronDownIcon, |
| 15 | ChevronRightIcon, |
| 16 | } from "lucide-react"; |
| 17 | import type { |
| 18 | BackgroundBooleanState, |
| 19 | EnvironmentId, |
| 20 | ResourceAttributionEntry, |
| 21 | ResourceTelemetryAggregate, |
| 22 | ResourceTelemetryHistoryBucket, |
| 23 | ResourceTelemetryIoSemantics, |
| 24 | ResourceTelemetryProcess, |
| 25 | ResourceTelemetryProcessCategory, |
| 26 | ResourceTelemetryProcessSummary, |
| 27 | ResourceTelemetrySourceHealth, |
| 28 | ResourceTelemetrySourceStatus, |
| 29 | ServerProcessSignal, |
| 30 | } from "@t3tools/contracts"; |
| 31 | import * as DateTime from "effect/DateTime"; |
| 32 | import * as Option from "effect/Option"; |
| 33 | import { RegistryContext } from "@effect/atom-react"; |
| 34 | import { useContext, useRef, useState, type ReactNode } from "react"; |
| 35 | import { |
| 36 | isAtomCommandInterrupted, |
| 37 | squashAtomCommandFailure, |
| 38 | } from "@t3tools/client-runtime/state/runtime"; |
| 39 | import { msg, T, useGT, useMessages, Var } from "gt-react"; |
| 40 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 41 | |
| 42 | import { |
| 43 | useResourceTelemetry, |
| 44 | useResourceTelemetryHistory, |
| 45 | } from "../../lib/resourceTelemetryState"; |
| 46 | import { cn } from "../../lib/utils"; |
| 47 | import { formatBytes } from "../../lib/formatBytes"; |
| 48 | import { ensureLocalApi } from "../../localApi"; |
| 49 | import { serverEnvironment } from "../../state/server"; |
| 50 | import { readEnvironmentScope, useEnvironmentScope } from "../../state/session"; |
| 51 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 52 | import { useRelativeTimeState } from "../../hooks/useRelativeTimeState"; |
| 53 | import { Button } from "../ui/button"; |
| 54 | import { ScrollArea } from "../ui/scroll-area"; |
| 55 | import { Toggle, ToggleGroup } from "../ui/toggle-group"; |
| 56 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 57 | import { toastManager } from "../ui/toast"; |
| 58 | import { |
| 59 | formatCpuTime, |
| 60 | formatSamplingStatus, |
| 61 | processIdentityKey, |
| 62 | resourceHistoryBarHeight, |
| 63 | resourceHistoryCpuScaleMax, |
| 64 | shouldShowResourceMonitorRetry, |
| 65 | visibleResourceTelemetryProcesses, |
| 66 | } from "./ResourceTelemetryDiagnostics.logic"; |
| 67 | import { SettingsSection } from "./settingsLayout"; |
| 68 | |
| 69 | const HISTORY_WINDOWS = [ |
| 70 | { label: "5m", windowMs: 5 * 60_000, bucketMs: 15_000 }, |
| 71 | { label: "15m", windowMs: 15 * 60_000, bucketMs: 30_000 }, |
| 72 | { label: "30m", windowMs: 30 * 60_000, bucketMs: 60_000 }, |
| 73 | { label: "1h", windowMs: 60 * 60_000, bucketMs: 2 * 60_000 }, |
| 74 | ] as const; |
| 75 | |
| 76 | function formatRate(value: number): string { |
| 77 | return `${formatBytes(value)}/s`; |
| 78 | } |
| 79 | |
| 80 | function formatDurationMicros(value: number): string { |
| 81 | if (value < 1_000) return `${Math.round(value)} µs`; |
| 82 | if (value < 1_000_000) return `${(value / 1_000).toFixed(2).replace(/\.0+$/, "")} ms`; |
| 83 | return `${(value / 1_000_000).toFixed(2).replace(/\.0+$/, "")} s`; |
| 84 | } |
| 85 | |
| 86 | function formatProcessName(process: Pick<ResourceTelemetryProcess, "command" | "name">): string { |
| 87 | if (process.name.trim()) return process.name; |
| 88 | const firstToken = process.command.trim().split(/\s+/)[0] ?? process.command; |
| 89 | const normalized = firstToken.replace(/^['"]|['"]$/g, ""); |
| 90 | return normalized.split(/[\\/]/).findLast((segment) => segment.length > 0) ?? normalized; |
| 91 | } |
| 92 | |
| 93 | function categoryLabel( |
| 94 | category: ResourceTelemetryProcessCategory, |
| 95 | resolve: MessageResolver = m, |
| 96 | ): string { |
| 97 | switch (category) { |
| 98 | case "server": |
| 99 | return resolve(msg("Server")); |
| 100 | case "server-child": |
| 101 | return resolve(msg("Backend child")); |
| 102 | case "provider-root": |
| 103 | return resolve(msg("Provider")); |
| 104 | case "terminal-root": |
| 105 | return resolve(msg("Terminal")); |
| 106 | case "electron-main": |
| 107 | return resolve(msg("Electron main")); |
| 108 | case "electron-renderer": |
| 109 | return resolve(msg("Renderer")); |
| 110 | case "electron-gpu": |
| 111 | return "GPU"; |
| 112 | case "electron-utility": |
| 113 | return resolve(msg("Electron utility")); |
| 114 | case "resource-monitor": |
| 115 | return resolve(msg("Monitor")); |
| 116 | case "unknown-t3": |
| 117 | return resolve(msg("T3 process")); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | function categoryDotClass(category: ResourceTelemetryProcessCategory): string { |
| 122 | if (category === "resource-monitor") return "bg-warning"; |
| 123 | if (category.startsWith("electron-")) return "bg-info"; |
| 124 | if (category === "server") return "bg-violet-500"; |
| 125 | return "bg-success"; |
| 126 | } |
| 127 | |
| 128 | function ioSemanticsLabel( |
| 129 | semantics: ResourceTelemetryIoSemantics, |
| 130 | resolve: MessageResolver = m, |
| 131 | ): string { |
| 132 | switch (semantics) { |
| 133 | case "storage": |
| 134 | return resolve(msg("Storage bytes")); |
| 135 | case "logical": |
| 136 | return resolve(msg("Logical bytes")); |
| 137 | case "all-io": |
| 138 | return resolve(msg("All I/O bytes")); |
| 139 | case "unavailable": |
| 140 | return resolve(msg("Unavailable")); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | function booleanStateLabel( |
| 145 | value: BackgroundBooleanState, |
| 146 | labels: { readonly true: string; readonly false: string }, |
| 147 | resolve: MessageResolver = m, |
| 148 | ): string { |
| 149 | if (value === "true") return labels.true; |
| 150 | if (value === "false") return labels.false; |
| 151 | return resolve(msg("Unknown")); |
| 152 | } |
| 153 | |
| 154 | function sourceStatusTone(status: ResourceTelemetrySourceStatus): "default" | "warning" | "danger" { |
| 155 | if (status === "healthy") return "default"; |
| 156 | if (status === "starting" || status === "degraded") return "warning"; |
| 157 | return "danger"; |
| 158 | } |
| 159 | |
| 160 | function SourceStatusBadge({ |
| 161 | label, |
| 162 | status, |
| 163 | presentation, |
| 164 | }: { |
| 165 | label: string; |
| 166 | status: ResourceTelemetrySourceStatus; |
| 167 | presentation?: |
| 168 | | { |
| 169 | readonly label: string; |
| 170 | readonly tone: "neutral"; |
| 171 | } |
| 172 | | undefined; |
| 173 | }) { |
| 174 | const tone = presentation?.tone ?? sourceStatusTone(status); |
| 175 | return ( |
| 176 | <span |
| 177 | className={cn( |
| 178 | "inline-flex items-center gap-1.5 rounded-full border px-2.5 py-1 text-3xs font-semibold uppercase tracking-widest", |
| 179 | tone === "neutral" && "border-border/70 bg-muted/45 text-muted-foreground", |
| 180 | tone === "default" && "border-success/25 bg-success/10 text-success-foreground", |
| 181 | tone === "warning" && "border-warning/30 bg-warning/10 text-warning-foreground", |
| 182 | tone === "danger" && "border-destructive/30 bg-destructive/10 text-destructive", |
| 183 | )} |
| 184 | > |
| 185 | <span |
| 186 | className={cn( |
| 187 | "size-1.5 rounded-full", |
| 188 | tone === "neutral" && "bg-muted-foreground/55", |
| 189 | tone === "default" && "bg-success", |
| 190 | tone === "warning" && "bg-warning", |
| 191 | tone === "danger" && "bg-destructive", |
| 192 | )} |
| 193 | /> |
| 194 | {label} {presentation?.label ?? status} |
| 195 | </span> |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | function LastSampleLabel({ sampledAt }: { sampledAt: DateTime.Utc | null }) { |
| 200 | const relative = useRelativeTimeState(sampledAt ? DateTime.formatIso(sampledAt) : null); |
| 201 | if (relative.status !== "relative") { |
| 202 | return ( |
| 203 | <span className="text-2xs text-muted-foreground/55"> |
| 204 | <T>Waiting for sample</T> |
| 205 | </span> |
| 206 | ); |
| 207 | } |
| 208 | return ( |
| 209 | <span className="text-2xs text-muted-foreground/60"> |
| 210 | {relative.suffix ? ( |
| 211 | <T context="the time is an elapsed time such as 5m"> |
| 212 | Updated{" "} |
| 213 | <span className="font-mono tabular-nums"> |
| 214 | <Var>{relative.value}</Var> |
| 215 | </span>{" "} |
| 216 | ago |
| 217 | </T> |
| 218 | ) : ( |
| 219 | <T> |
| 220 | Updated{" "} |
| 221 | <span className="font-mono tabular-nums"> |
| 222 | <Var>{relative.value}</Var> |
| 223 | </span> |
| 224 | </T> |
| 225 | )} |
| 226 | </span> |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | function IconStat({ |
| 231 | icon, |
| 232 | label, |
| 233 | value, |
| 234 | detail, |
| 235 | tone = "default", |
| 236 | }: { |
| 237 | icon: ReactNode; |
| 238 | label: string; |
| 239 | value: string; |
| 240 | detail?: string | undefined; |
| 241 | tone?: "default" | "warning" | "danger"; |
| 242 | }) { |
| 243 | return ( |
| 244 | <div className="group min-w-0 px-4 py-4 sm:px-5"> |
| 245 | <div className="flex items-center gap-2 text-3xs font-semibold uppercase tracking-widest text-muted-foreground/70"> |
| 246 | <span className="text-muted-foreground/55 transition-colors group-hover:text-foreground/65"> |
| 247 | {icon} |
| 248 | </span> |
| 249 | <span className="truncate">{label}</span> |
| 250 | </div> |
| 251 | <div |
| 252 | className={cn( |
| 253 | "mt-2.5 truncate font-mono text-2xl font-semibold tracking-tighter tabular-nums text-foreground", |
| 254 | tone === "warning" && "text-warning-foreground", |
| 255 | tone === "danger" && "text-destructive", |
| 256 | )} |
| 257 | > |
| 258 | {value} |
| 259 | </div> |
| 260 | {detail ? ( |
| 261 | <div className="mt-1.5 truncate text-3xs text-muted-foreground/60">{detail}</div> |
| 262 | ) : null} |
| 263 | </div> |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | function AggregateCard({ |
| 268 | label, |
| 269 | accentClass, |
| 270 | aggregate, |
| 271 | }: { |
| 272 | label: string; |
| 273 | accentClass: string; |
| 274 | aggregate: ResourceTelemetryAggregate; |
| 275 | }) { |
| 276 | const gt = useGT(); |
| 277 | return ( |
| 278 | <div className="relative overflow-hidden border-t border-border/60 px-4 py-4 first:border-t-0 md:border-t-0 md:border-l md:first:border-l-0 sm:px-5"> |
| 279 | <span className={cn("absolute inset-x-5 top-0 h-0.5 rounded-full opacity-75", accentClass)} /> |
| 280 | <div className="flex items-center justify-between gap-3"> |
| 281 | <div className="text-3xs font-semibold uppercase tracking-widest text-muted-foreground/75"> |
| 282 | {label} |
| 283 | </div> |
| 284 | <div className="rounded-md bg-muted/55 px-1.5 py-0.5 font-mono text-3xs tabular-nums text-muted-foreground/70"> |
| 285 | {aggregate.processCount} {aggregate.processCount === 1 ? "process" : "processes"} |
| 286 | </div> |
| 287 | </div> |
| 288 | <div className="mt-3.5 grid grid-cols-2 gap-x-4 gap-y-2.5"> |
| 289 | <MetricPair label={gt("CPU")} value={`${aggregate.currentCpuPercent.toFixed(1)}%`} /> |
| 290 | <MetricPair label={gt("Memory")} value={formatBytes(aggregate.currentRssBytes)} /> |
| 291 | <MetricPair label={gt("Read")} value={formatRate(aggregate.ioReadBytesPerSecond)} /> |
| 292 | <MetricPair label={gt("Write")} value={formatRate(aggregate.ioWriteBytesPerSecond)} /> |
| 293 | </div> |
| 294 | </div> |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | function MetricPair({ label, value }: { label: string; value: string }) { |
| 299 | return ( |
| 300 | <div className="min-w-0"> |
| 301 | <div className="text-3xs font-semibold uppercase tracking-widest text-muted-foreground/45"> |
| 302 | {label} |
| 303 | </div> |
| 304 | <div className="truncate font-mono text-xs font-medium tabular-nums text-foreground/90"> |
| 305 | {value} |
| 306 | </div> |
| 307 | </div> |
| 308 | ); |
| 309 | } |
| 310 | |
| 311 | function HealthSource({ label, health }: { label: string; health: ResourceTelemetrySourceHealth }) { |
| 312 | const gt = useGT(); |
| 313 | const expectedInBrowser = |
| 314 | health.status === "unavailable" && |
| 315 | Option.exists(health.lastError, (error) => error.includes("'web' mode")); |
| 316 | return ( |
| 317 | <div className="flex items-start justify-between gap-4 border-t border-border/50 py-3 first:border-t-0"> |
| 318 | <div className="min-w-0"> |
| 319 | <div className="text-sm font-medium text-foreground">{label}</div> |
| 320 | <div className="mt-1 text-2xs leading-relaxed text-muted-foreground/65"> |
| 321 | {expectedInBrowser |
| 322 | ? gt("Available when this page runs inside the desktop app.") |
| 323 | : Option.match(health.lastError, { |
| 324 | onNone: () => gt("No reported errors"), |
| 325 | onSome: (error) => error, |
| 326 | })} |
| 327 | </div> |
| 328 | </div> |
| 329 | <SourceStatusBadge |
| 330 | label="" |
| 331 | status={health.status} |
| 332 | presentation={ |
| 333 | expectedInBrowser |
| 334 | ? { |
| 335 | label: gt("Desktop only"), |
| 336 | tone: "neutral", |
| 337 | } |
| 338 | : undefined |
| 339 | } |
| 340 | /> |
| 341 | </div> |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | function DetailRow({ |
| 346 | label, |
| 347 | value, |
| 348 | valueClassName, |
| 349 | }: { |
| 350 | label: string; |
| 351 | value: ReactNode; |
| 352 | valueClassName?: string | undefined; |
| 353 | }) { |
| 354 | return ( |
| 355 | <div className="flex items-center justify-between gap-4 border-t border-border/50 py-2.5 first:border-t-0"> |
| 356 | <span className="text-2xs text-muted-foreground/75">{label}</span> |
| 357 | <span |
| 358 | className={cn( |
| 359 | "min-w-0 truncate text-right font-mono text-2xs tabular-nums text-foreground/85", |
| 360 | valueClassName, |
| 361 | )} |
| 362 | > |
| 363 | {value} |
| 364 | </span> |
| 365 | </div> |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | function HistoryWindowSelector({ |
| 370 | selectedWindowMs, |
| 371 | onSelect, |
| 372 | }: { |
| 373 | selectedWindowMs: number; |
| 374 | onSelect: (windowMs: number) => void; |
| 375 | }) { |
| 376 | const gt = useGT(); |
| 377 | return ( |
| 378 | <ToggleGroup |
| 379 | aria-label={gt("Resource history period")} |
| 380 | variant="segmented" |
| 381 | value={[String(selectedWindowMs)]} |
| 382 | onValueChange={(next) => { |
| 383 | const selected = HISTORY_WINDOWS.find((option) => String(option.windowMs) === next[0]); |
| 384 | if (selected) onSelect(selected.windowMs); |
| 385 | }} |
| 386 | > |
| 387 | {HISTORY_WINDOWS.map((option) => ( |
| 388 | <Toggle key={option.windowMs} value={String(option.windowMs)}> |
| 389 | {option.label} |
| 390 | </Toggle> |
| 391 | ))} |
| 392 | </ToggleGroup> |
| 393 | ); |
| 394 | } |
| 395 | |
| 396 | function ResourceHistoryChart({ |
| 397 | buckets, |
| 398 | }: { |
| 399 | buckets: ReadonlyArray<ResourceTelemetryHistoryBucket>; |
| 400 | }) { |
| 401 | const gt = useGT(); |
| 402 | const maxCpu = resourceHistoryCpuScaleMax(buckets); |
| 403 | const maxIo = Math.max(1, ...buckets.map((bucket) => bucket.ioReadBytes + bucket.ioWriteBytes)); |
| 404 | |
| 405 | return ( |
| 406 | <div className="border-t border-border/60 px-4 py-4 sm:px-5"> |
| 407 | <div className="mb-3 flex flex-wrap items-center gap-x-4 gap-y-1 text-3xs text-muted-foreground/65"> |
| 408 | <span className="inline-flex items-center gap-1.5"> |
| 409 | <span className="h-1.5 w-3 rounded-full bg-foreground/70" /> <T>CPU average</T> |
| 410 | </span> |
| 411 | <span className="inline-flex items-center gap-1.5"> |
| 412 | <span className="h-1.5 w-3 rounded-full bg-info/70" /> <T>I/O reads</T> |
| 413 | </span> |
| 414 | <span className="inline-flex items-center gap-1.5"> |
| 415 | <span className="h-1.5 w-3 rounded-full bg-warning/80" /> <T>I/O writes</T> |
| 416 | </span> |
| 417 | </div> |
| 418 | <div className="flex h-32 items-end gap-1 overflow-hidden rounded-lg border border-border/40 bg-muted/8 px-2 pt-3 pb-2"> |
| 419 | {buckets.map((bucket) => { |
| 420 | const cpuHeight = resourceHistoryBarHeight({ |
| 421 | value: bucket.avgCpuPercent, |
| 422 | max: maxCpu, |
| 423 | minimumVisiblePercent: 2, |
| 424 | }); |
| 425 | const readHeight = resourceHistoryBarHeight({ |
| 426 | value: bucket.ioReadBytes, |
| 427 | max: maxIo, |
| 428 | minimumVisiblePercent: 1, |
| 429 | }); |
| 430 | const writeHeight = resourceHistoryBarHeight({ |
| 431 | value: bucket.ioWriteBytes, |
| 432 | max: maxIo, |
| 433 | minimumVisiblePercent: 1, |
| 434 | }); |
| 435 | return ( |
| 436 | <Tooltip key={DateTime.formatIso(bucket.startedAt)}> |
| 437 | <TooltipTrigger |
| 438 | render={ |
| 439 | <div className="grid h-full min-w-1 flex-1 grid-cols-3 items-end gap-px"> |
| 440 | <span |
| 441 | className="block rounded-t-sm bg-foreground/65" |
| 442 | style={{ height: `${cpuHeight}%` }} |
| 443 | /> |
| 444 | <span |
| 445 | className="block rounded-t-sm bg-info/70" |
| 446 | style={{ height: `${readHeight}%` }} |
| 447 | /> |
| 448 | <span |
| 449 | className="block rounded-t-sm bg-warning/80" |
| 450 | style={{ height: `${writeHeight}%` }} |
| 451 | /> |
| 452 | </div> |
| 453 | } |
| 454 | /> |
| 455 | <TooltipPopup side="top" className="text-left"> |
| 456 | <div className="space-y-0.5"> |
| 457 | <div> |
| 458 | {gt("CPU avg {percent}%", { percent: bucket.avgCpuPercent.toFixed(1) })} |
| 459 | </div> |
| 460 | <div> |
| 461 | {gt("CPU peak {percent}%", { percent: bucket.maxCpuPercent.toFixed(1) })} |
| 462 | </div> |
| 463 | <div>{gt("Read {bytes}", { bytes: formatBytes(bucket.ioReadBytes) })}</div> |
| 464 | <div>{gt("Write {bytes}", { bytes: formatBytes(bucket.ioWriteBytes) })}</div> |
| 465 | </div> |
| 466 | </TooltipPopup> |
| 467 | </Tooltip> |
| 468 | ); |
| 469 | })} |
| 470 | </div> |
| 471 | </div> |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | function ProcessTreeName({ |
| 476 | process, |
| 477 | collapsed, |
| 478 | onToggle, |
| 479 | }: { |
| 480 | process: ResourceTelemetryProcess; |
| 481 | collapsed: boolean; |
| 482 | onToggle: (process: ResourceTelemetryProcess) => void; |
| 483 | }) { |
| 484 | const gt = useGT(); |
| 485 | const name = formatProcessName(process); |
| 486 | const hasChildren = process.childPids.length > 0; |
| 487 | return ( |
| 488 | <div |
| 489 | className="grid min-w-0 grid-cols-[1.25rem_0.375rem_minmax(0,1fr)] items-center gap-2" |
| 490 | style={{ paddingLeft: `${Math.min(process.depth, 7) * 10}px` }} |
| 491 | > |
| 492 | {hasChildren ? ( |
| 493 | <Button |
| 494 | size="icon-micro" |
| 495 | variant="ghost-muted" |
| 496 | onClick={() => onToggle(process)} |
| 497 | aria-label={collapsed ? gt("Expand {name}", { name }) : gt("Collapse {name}", { name })} |
| 498 | > |
| 499 | {collapsed ? ( |
| 500 | <ChevronRightIcon className="size-3.5" /> |
| 501 | ) : ( |
| 502 | <ChevronDownIcon className="size-3.5" /> |
| 503 | )} |
| 504 | </Button> |
| 505 | ) : ( |
| 506 | <span className="size-5" aria-hidden /> |
| 507 | )} |
| 508 | <span className={cn("size-1.5 rounded-full", categoryDotClass(process.category))} /> |
| 509 | <Tooltip> |
| 510 | <TooltipTrigger |
| 511 | render={<span className="min-w-0 truncate font-medium text-foreground">{name}</span>} |
| 512 | /> |
| 513 | <TooltipPopup side="top" variant="code"> |
| 514 | {process.command || process.name} |
| 515 | </TooltipPopup> |
| 516 | </Tooltip> |
| 517 | </div> |
| 518 | ); |
| 519 | } |
| 520 | |
| 521 | function canSignalProcess(process: ResourceTelemetryProcess): boolean { |
| 522 | return ( |
| 523 | process.category === "server-child" || |
| 524 | process.category === "provider-root" || |
| 525 | process.category === "terminal-root" |
| 526 | ); |
| 527 | } |
| 528 | |
| 529 | function ProcessActions({ |
| 530 | process, |
| 531 | canMaintainEnvironment, |
| 532 | signalingKeys, |
| 533 | onSignal, |
| 534 | }: { |
| 535 | process: ResourceTelemetryProcess; |
| 536 | canMaintainEnvironment: boolean; |
| 537 | signalingKeys: ReadonlySet<string>; |
| 538 | onSignal: (process: ResourceTelemetryProcess, signal: ServerProcessSignal) => void; |
| 539 | }) { |
| 540 | if (!canSignalProcess(process)) { |
| 541 | return <span className="text-3xs text-muted-foreground/35">-</span>; |
| 542 | } |
| 543 | const isSignaling = signalingKeys.has(processIdentityKey(process)); |
| 544 | return ( |
| 545 | <ProcessSignalActions |
| 546 | disabled={!canMaintainEnvironment || isSignaling} |
| 547 | onSignal={(signal) => onSignal(process, signal)} |
| 548 | /> |
| 549 | ); |
| 550 | } |
| 551 | |
| 552 | function ProcessTable({ |
| 553 | processes, |
| 554 | canMaintainEnvironment, |
| 555 | signalingKeys, |
| 556 | onSignal, |
| 557 | }: { |
| 558 | processes: ReadonlyArray<ResourceTelemetryProcess>; |
| 559 | canMaintainEnvironment: boolean; |
| 560 | signalingKeys: ReadonlySet<string>; |
| 561 | onSignal: (process: ResourceTelemetryProcess, signal: ServerProcessSignal) => void; |
| 562 | }) { |
| 563 | const messages = useMessages(); |
| 564 | const [collapsed, setCollapsed] = useState<ReadonlySet<string>>(() => new Set()); |
| 565 | const visible = visibleResourceTelemetryProcesses(processes, collapsed); |
| 566 | const toggle = (process: ResourceTelemetryProcess) => { |
| 567 | const identityKey = processIdentityKey(process); |
| 568 | setCollapsed((current) => { |
| 569 | const next = new Set(current); |
| 570 | if (next.has(identityKey)) { |
| 571 | next.delete(identityKey); |
| 572 | } else { |
| 573 | next.add(identityKey); |
| 574 | } |
| 575 | return next; |
| 576 | }); |
| 577 | }; |
| 578 | |
| 579 | return ( |
| 580 | <div className="border-t border-border/60"> |
| 581 | <ScrollArea |
| 582 | chainVerticalScroll |
| 583 | scrollFade |
| 584 | hideScrollbars |
| 585 | className="max-h-[min(68vh,48rem)] w-full max-w-full" |
| 586 | > |
| 587 | <table className="w-full min-w-[1320px] table-fixed text-left text-xs"> |
| 588 | <colgroup> |
| 589 | <col className="w-[20%]" /> |
| 590 | <col className="w-[10%]" /> |
| 591 | <col className="w-[7%]" /> |
| 592 | <col className="w-[8%]" /> |
| 593 | <col className="w-[9%]" /> |
| 594 | <col className="w-[9%]" /> |
| 595 | <col className="w-[9%]" /> |
| 596 | <col className="w-[10%]" /> |
| 597 | <col className="w-[8%]" /> |
| 598 | <col className="w-[6%]" /> |
| 599 | <col className="w-[4%]" /> |
| 600 | </colgroup> |
| 601 | <thead className="sticky top-0 z-10 border-b border-border/60 bg-card text-3xs uppercase tracking-widest text-muted-foreground/65"> |
| 602 | <tr> |
| 603 | <th className="px-4 py-2 font-semibold sm:pl-5"> |
| 604 | <T>Process</T> |
| 605 | </th> |
| 606 | <th className="px-3 py-2 font-semibold"> |
| 607 | <T>Category</T> |
| 608 | </th> |
| 609 | <th className="px-3 py-2 text-right font-semibold"> |
| 610 | <T>CPU</T> |
| 611 | </th> |
| 612 | <th className="px-3 py-2 text-right font-semibold"> |
| 613 | <T>CPU Time</T> |
| 614 | </th> |
| 615 | <th className="px-3 py-2 text-right font-semibold"> |
| 616 | <T>Memory</T> |
| 617 | </th> |
| 618 | <th className="px-3 py-2 text-right font-semibold"> |
| 619 | <T>Read/s</T> |
| 620 | </th> |
| 621 | <th className="px-3 py-2 text-right font-semibold"> |
| 622 | <T>Write/s</T> |
| 623 | </th> |
| 624 | <th className="px-3 py-2 text-right font-semibold"> |
| 625 | <T>Read Total</T> |
| 626 | </th> |
| 627 | <th className="px-3 py-2 text-right font-semibold"> |
| 628 | <T>Write Total</T> |
| 629 | </th> |
| 630 | <th className="px-3 py-2 text-right font-semibold"> |
| 631 | <T>PID</T> |
| 632 | </th> |
| 633 | <th className="px-2 py-2 text-right font-semibold sm:pr-4"> |
| 634 | <T>Kill</T> |
| 635 | </th> |
| 636 | </tr> |
| 637 | </thead> |
| 638 | <tbody className="divide-y divide-border/50"> |
| 639 | {visible.length === 0 ? ( |
| 640 | <tr> |
| 641 | <td colSpan={11} className="px-4 py-5 text-xs text-muted-foreground sm:px-5"> |
| 642 | <T>Waiting for the native process monitor.</T> |
| 643 | </td> |
| 644 | </tr> |
| 645 | ) : null} |
| 646 | {visible.map((process) => ( |
| 647 | <tr key={processIdentityKey(process)} className="hover:bg-muted/20"> |
| 648 | <td className="px-4 py-2 sm:pl-5"> |
| 649 | <ProcessTreeName |
| 650 | process={process} |
| 651 | collapsed={collapsed.has(processIdentityKey(process))} |
| 652 | onToggle={toggle} |
| 653 | /> |
| 654 | </td> |
| 655 | <td className="truncate px-3 py-2 text-2xs text-muted-foreground"> |
| 656 | {categoryLabel(process.category, messages)} |
| 657 | </td> |
| 658 | <td className="px-3 py-2 text-right font-mono tabular-nums"> |
| 659 | {process.cpuPercent.toFixed(1)}% |
| 660 | </td> |
| 661 | <td className="px-3 py-2 text-right font-mono tabular-nums"> |
| 662 | {formatCpuTime(process.cpuTimeMs / 1_000, messages)} |
| 663 | </td> |
| 664 | <td className="px-3 py-2 text-right font-mono tabular-nums"> |
| 665 | {formatBytes(process.residentBytes)} |
| 666 | </td> |
| 667 | <td className="px-3 py-2 text-right font-mono tabular-nums text-info-foreground"> |
| 668 | {formatRate(process.ioReadBytesPerSecond)} |
| 669 | </td> |
| 670 | <td className="px-3 py-2 text-right font-mono tabular-nums text-warning-foreground"> |
| 671 | {formatRate(process.ioWriteBytesPerSecond)} |
| 672 | </td> |
| 673 | <td className="px-3 py-2 text-right font-mono tabular-nums text-muted-foreground"> |
| 674 | {formatBytes(process.ioReadBytes)} |
| 675 | </td> |
| 676 | <td className="px-3 py-2 text-right font-mono tabular-nums text-muted-foreground"> |
| 677 | <Tooltip> |
| 678 | <TooltipTrigger render={<span>{formatBytes(process.ioWriteBytes)}</span>} /> |
| 679 | <TooltipPopup side="top"> |
| 680 | {ioSemanticsLabel(process.ioSemantics, messages)} |
| 681 | </TooltipPopup> |
| 682 | </Tooltip> |
| 683 | </td> |
| 684 | <td className="px-3 py-2 text-right font-mono tabular-nums text-muted-foreground"> |
| 685 | {process.identity.pid} |
| 686 | </td> |
| 687 | <td className="px-2 py-2 text-right sm:pr-4"> |
| 688 | <ProcessActions |
| 689 | process={process} |
| 690 | canMaintainEnvironment={canMaintainEnvironment} |
| 691 | signalingKeys={signalingKeys} |
| 692 | onSignal={onSignal} |
| 693 | /> |
| 694 | </td> |
| 695 | </tr> |
| 696 | ))} |
| 697 | </tbody> |
| 698 | </table> |
| 699 | </ScrollArea> |
| 700 | </div> |
| 701 | ); |
| 702 | } |
| 703 | |
| 704 | function HistoryProcessTable({ |
| 705 | processes, |
| 706 | }: { |
| 707 | processes: ReadonlyArray<ResourceTelemetryProcessSummary>; |
| 708 | }) { |
| 709 | const messages = useMessages(); |
| 710 | return ( |
| 711 | <div className="border-t border-border/60"> |
| 712 | <ScrollArea |
| 713 | chainVerticalScroll |
| 714 | scrollFade |
| 715 | hideScrollbars |
| 716 | className="max-h-[28rem] w-full max-w-full" |
| 717 | > |
| 718 | <table className="w-full min-w-[1020px] table-fixed text-left text-xs"> |
| 719 | <colgroup> |
| 720 | <col className="w-[24%]" /> |
| 721 | <col className="w-[11%]" /> |
| 722 | <col className="w-[10%]" /> |
| 723 | <col className="w-[10%]" /> |
| 724 | <col className="w-[11%]" /> |
| 725 | <col className="w-[11%]" /> |
| 726 | <col className="w-[11%]" /> |
| 727 | <col className="w-[7%]" /> |
| 728 | <col className="w-[5%]" /> |
| 729 | </colgroup> |
| 730 | <thead className="sticky top-0 z-10 border-b border-border/60 bg-card text-3xs uppercase tracking-widest text-muted-foreground/65"> |
| 731 | <tr> |
| 732 | <th className="px-4 py-2 font-semibold sm:pl-5"> |
| 733 | <T>Process</T> |
| 734 | </th> |
| 735 | <th className="px-3 py-2 font-semibold"> |
| 736 | <T>Category</T> |
| 737 | </th> |
| 738 | <th className="px-3 py-2 text-right font-semibold"> |
| 739 | <T>CPU Time</T> |
| 740 | </th> |
| 741 | <th className="px-3 py-2 text-right font-semibold"> |
| 742 | <T>Peak CPU</T> |
| 743 | </th> |
| 744 | <th className="px-3 py-2 text-right font-semibold"> |
| 745 | <T>Peak Mem</T> |
| 746 | </th> |
| 747 | <th className="px-3 py-2 text-right font-semibold"> |
| 748 | <T>Read</T> |
| 749 | </th> |
| 750 | <th className="px-3 py-2 text-right font-semibold"> |
| 751 | <T>Write</T> |
| 752 | </th> |
| 753 | <th className="px-3 py-2 text-right font-semibold"> |
| 754 | <T>Samples</T> |
| 755 | </th> |
| 756 | <th className="px-3 py-2 text-right font-semibold sm:pr-5"> |
| 757 | <T>PID</T> |
| 758 | </th> |
| 759 | </tr> |
| 760 | </thead> |
| 761 | <tbody className="divide-y divide-border/50"> |
| 762 | {processes.length === 0 ? ( |
| 763 | <tr> |
| 764 | <td colSpan={9} className="px-4 py-5 text-xs text-muted-foreground sm:px-5"> |
| 765 | <T>No retained process samples in this window.</T> |
| 766 | </td> |
| 767 | </tr> |
| 768 | ) : null} |
| 769 | {processes.map((process) => ( |
| 770 | <tr key={processIdentityKey(process)} className="hover:bg-muted/20"> |
| 771 | <td className="px-4 py-2 sm:pl-5"> |
| 772 | <Tooltip> |
| 773 | <TooltipTrigger |
| 774 | render={ |
| 775 | <span className="block truncate font-medium text-foreground"> |
| 776 | {process.name || process.command} |
| 777 | </span> |
| 778 | } |
| 779 | /> |
| 780 | <TooltipPopup side="top" variant="code"> |
| 781 | {process.command || process.name} |
| 782 | </TooltipPopup> |
| 783 | </Tooltip> |
| 784 | </td> |
| 785 | <td className="truncate px-3 py-2 text-2xs text-muted-foreground"> |
| 786 | {categoryLabel(process.category, messages)} |
| 787 | </td> |
| 788 | <td className="px-3 py-2 text-right font-mono tabular-nums"> |
| 789 | {formatCpuTime(process.cpuTimeMs / 1_000, messages)} |
| 790 | </td> |
| 791 | <td className="px-3 py-2 text-right font-mono tabular-nums"> |
| 792 | {process.maxCpuPercent.toFixed(1)}% |
| 793 | </td> |
| 794 | <td className="px-3 py-2 text-right font-mono tabular-nums"> |
| 795 | {formatBytes(process.peakRssBytes)} |
| 796 | </td> |
| 797 | <td className="px-3 py-2 text-right font-mono tabular-nums text-info-foreground"> |
| 798 | {formatBytes(process.ioReadBytes)} |
| 799 | </td> |
| 800 | <td className="px-3 py-2 text-right font-mono tabular-nums text-warning-foreground"> |
| 801 | {formatBytes(process.ioWriteBytes)} |
| 802 | </td> |
| 803 | <td className="px-3 py-2 text-right font-mono tabular-nums text-muted-foreground"> |
| 804 | {process.sampleCount} |
| 805 | </td> |
| 806 | <td className="px-3 py-2 text-right font-mono tabular-nums text-muted-foreground sm:pr-5"> |
| 807 | {process.identity.pid} |
| 808 | </td> |
| 809 | </tr> |
| 810 | ))} |
| 811 | </tbody> |
| 812 | </table> |
| 813 | </ScrollArea> |
| 814 | </div> |
| 815 | ); |
| 816 | } |
| 817 | |
| 818 | function AttributionTable({ entries }: { entries: ReadonlyArray<ResourceAttributionEntry> }) { |
| 819 | return ( |
| 820 | <div className="overflow-x-auto border-t border-border/60"> |
| 821 | <table className="w-full min-w-[720px] table-fixed text-left text-xs"> |
| 822 | <colgroup> |
| 823 | <col className="w-[22%]" /> |
| 824 | <col className="w-[28%]" /> |
| 825 | <col className="w-[14%]" /> |
| 826 | <col className="w-[14%]" /> |
| 827 | <col className="w-[10%]" /> |
| 828 | <col className="w-[12%]" /> |
| 829 | </colgroup> |
| 830 | <thead className="border-b border-border/60 text-3xs uppercase tracking-widest text-muted-foreground/65"> |
| 831 | <tr> |
| 832 | <th className="px-4 py-2 font-semibold sm:pl-5"> |
| 833 | <T>Component</T> |
| 834 | </th> |
| 835 | <th className="px-3 py-2 font-semibold"> |
| 836 | <T>Operation</T> |
| 837 | </th> |
| 838 | <th className="px-3 py-2 text-right font-semibold"> |
| 839 | <T>Logical Read</T> |
| 840 | </th> |
| 841 | <th className="px-3 py-2 text-right font-semibold"> |
| 842 | <T>Logical Write</T> |
| 843 | </th> |
| 844 | <th className="px-3 py-2 text-right font-semibold"> |
| 845 | <T>Count</T> |
| 846 | </th> |
| 847 | <th className="px-3 py-2 text-right font-semibold sm:pr-5"> |
| 848 | <T>Time</T> |
| 849 | </th> |
| 850 | </tr> |
| 851 | </thead> |
| 852 | <tbody className="divide-y divide-border/50"> |
| 853 | {entries.length === 0 ? ( |
| 854 | <tr> |
| 855 | <td colSpan={6} className="px-4 py-5 text-xs text-muted-foreground sm:px-5"> |
| 856 | <T>No instrumented application I/O has been recorded yet.</T> |
| 857 | </td> |
| 858 | </tr> |
| 859 | ) : null} |
| 860 | {entries.map((entry) => ( |
| 861 | <tr key={`${entry.component}:${entry.operation}`} className="hover:bg-muted/20"> |
| 862 | <td className="truncate px-4 py-2 font-medium text-foreground sm:pl-5"> |
| 863 | {entry.component} |
| 864 | </td> |
| 865 | <td className="truncate px-3 py-2 text-muted-foreground">{entry.operation}</td> |
| 866 | <td className="px-3 py-2 text-right font-mono tabular-nums text-info-foreground"> |
| 867 | {formatBytes(entry.logicalReadBytes)} |
| 868 | </td> |
| 869 | <td className="px-3 py-2 text-right font-mono tabular-nums text-warning-foreground"> |
| 870 | {formatBytes(entry.logicalWriteBytes)} |
| 871 | </td> |
| 872 | <td className="px-3 py-2 text-right font-mono tabular-nums">{entry.count}</td> |
| 873 | <td className="px-3 py-2 text-right font-mono tabular-nums text-muted-foreground sm:pr-5"> |
| 874 | {(entry.durationMs / 1_000).toFixed(2).replace(/\.0+$/, "")}s |
| 875 | </td> |
| 876 | </tr> |
| 877 | ))} |
| 878 | </tbody> |
| 879 | </table> |
| 880 | </div> |
| 881 | ); |
| 882 | } |
| 883 | |
| 884 | export function ResourceTelemetryDiagnostics({ |
| 885 | environmentId, |
| 886 | }: { |
| 887 | environmentId: EnvironmentId | null; |
| 888 | }) { |
| 889 | // Pending signals and retries belong to one environment, so a switch starts fresh. |
| 890 | return ( |
| 891 | <ResourceTelemetryDiagnosticsBody key={environmentId ?? ""} environmentId={environmentId} /> |
| 892 | ); |
| 893 | } |
| 894 | |
| 895 | function ResourceTelemetryDiagnosticsBody({ |
| 896 | environmentId, |
| 897 | }: { |
| 898 | environmentId: EnvironmentId | null; |
| 899 | }) { |
| 900 | const messages = useMessages(); |
| 901 | const gt = useGT(); |
| 902 | const registry = useContext(RegistryContext); |
| 903 | const [windowMs, setWindowMs] = useState(15 * 60_000); |
| 904 | const selectedWindow = |
| 905 | HISTORY_WINDOWS.find((option) => option.windowMs === windowMs) ?? HISTORY_WINDOWS[1]; |
| 906 | const canMaintainEnvironment = useEnvironmentScope(environmentId, AuthEnvironmentMaintainScope); |
| 907 | const telemetry = useResourceTelemetry(environmentId); |
| 908 | const retryTelemetry = telemetry.retry; |
| 909 | const history = useResourceTelemetryHistory( |
| 910 | { |
| 911 | windowMs: selectedWindow.windowMs, |
| 912 | bucketMs: selectedWindow.bucketMs, |
| 913 | }, |
| 914 | environmentId, |
| 915 | ); |
| 916 | const signalServerProcess = useAtomCommand(serverEnvironment.signalProcess, { |
| 917 | reportFailure: false, |
| 918 | }); |
| 919 | const [signalingKeys, setSignalingKeys] = useState<ReadonlySet<string>>(() => new Set()); |
| 920 | const signalingKeysRef = useRef<ReadonlySet<string>>(new Set()); |
| 921 | const [isRetrying, setIsRetrying] = useState(false); |
| 922 | const snapshot = telemetry.data; |
| 923 | const allT3 = snapshot?.groups.allT3; |
| 924 | |
| 925 | const signalProcess = async (process: ResourceTelemetryProcess, signal: ServerProcessSignal) => { |
| 926 | const targetEnvironmentId = environmentId; |
| 927 | if ( |
| 928 | targetEnvironmentId === null || |
| 929 | !readEnvironmentScope(targetEnvironmentId, AuthEnvironmentMaintainScope) |
| 930 | ) |
| 931 | return; |
| 932 | const identityKey = processIdentityKey(process); |
| 933 | if (signalingKeysRef.current.has(identityKey)) return; |
| 934 | const nextSignalingKeys = new Set(signalingKeysRef.current).add(identityKey); |
| 935 | signalingKeysRef.current = nextSignalingKeys; |
| 936 | setSignalingKeys(nextSignalingKeys); |
| 937 | const clearSignaling = () => { |
| 938 | const next = new Set(signalingKeysRef.current); |
| 939 | next.delete(identityKey); |
| 940 | signalingKeysRef.current = next; |
| 941 | setSignalingKeys(next); |
| 942 | }; |
| 943 | |
| 944 | if (signal === "SIGKILL") { |
| 945 | let confirmed = false; |
| 946 | try { |
| 947 | confirmed = await ensureLocalApi().dialogs.confirm( |
| 948 | gt("Send SIGKILL to process {pid}? This cannot be handled by the process.", { |
| 949 | pid: process.identity.pid, |
| 950 | }), |
| 951 | { variant: "destructive" }, |
| 952 | ); |
| 953 | } catch (error) { |
| 954 | clearSignaling(); |
| 955 | toastManager.add({ |
| 956 | type: "error", |
| 957 | title: gt("Could not confirm signal"), |
| 958 | description: |
| 959 | error instanceof Error |
| 960 | ? (thrownErrorMessage(error, m) ?? error.message) |
| 961 | : gt("Failed to send {signal}.", { signal }), |
| 962 | }); |
| 963 | return; |
| 964 | } |
| 965 | if (!confirmed) { |
| 966 | clearSignaling(); |
| 967 | return; |
| 968 | } |
| 969 | } |
| 970 | if (!readEnvironmentScope(targetEnvironmentId, AuthEnvironmentMaintainScope)) { |
| 971 | clearSignaling(); |
| 972 | return; |
| 973 | } |
| 974 | void signalServerProcess({ |
| 975 | environmentId: targetEnvironmentId, |
| 976 | input: { |
| 977 | pid: process.identity.pid, |
| 978 | startTimeMs: process.identity.startTimeMs, |
| 979 | signal, |
| 980 | }, |
| 981 | }) |
| 982 | .then((result) => { |
| 983 | if (result._tag === "Failure") { |
| 984 | if (isAtomCommandInterrupted(result)) return; |
| 985 | throw squashAtomCommandFailure(result); |
| 986 | } |
| 987 | if (result.value.signaled) return; |
| 988 | toastManager.add({ |
| 989 | type: "error", |
| 990 | title: gt("Could not send {signal}", { signal }), |
| 991 | description: Option.getOrElse(result.value.message, () => |
| 992 | gt("Failed to send {signal} to process {pid}.", { |
| 993 | signal, |
| 994 | pid: process.identity.pid, |
| 995 | }), |
| 996 | ), |
| 997 | }); |
| 998 | }) |
| 999 | .catch((error: unknown) => { |
| 1000 | toastManager.add({ |
| 1001 | type: "error", |
| 1002 | title: gt("Could not send {signal}", { signal }), |
| 1003 | description: |
| 1004 | error instanceof Error |
| 1005 | ? (thrownErrorMessage(error, m) ?? error.message) |
| 1006 | : gt("Failed to send {signal}.", { signal }), |
| 1007 | }); |
| 1008 | }) |
| 1009 | .finally(() => { |
| 1010 | clearSignaling(); |
| 1011 | }); |
| 1012 | }; |
| 1013 | |
| 1014 | const retryCollector = () => { |
| 1015 | if ( |
| 1016 | environmentId === null || |
| 1017 | !readEnvironmentScope(environmentId, AuthEnvironmentMaintainScope) |
| 1018 | ) |
| 1019 | return; |
| 1020 | setIsRetrying(true); |
| 1021 | void retryTelemetry() |
| 1022 | .then(() => { |
| 1023 | // The user can pick another window while the retry runs; refreshing every window |
| 1024 | // re-reads the one on screen and only invalidates the rest. |
| 1025 | for (const option of HISTORY_WINDOWS) { |
| 1026 | registry.refresh( |
| 1027 | serverEnvironment.resourceTelemetryHistory({ |
| 1028 | environmentId, |
| 1029 | input: { windowMs: option.windowMs, bucketMs: option.bucketMs }, |
| 1030 | }), |
| 1031 | ); |
| 1032 | } |
| 1033 | }) |
| 1034 | .catch((error: unknown) => { |
| 1035 | toastManager.add({ |
| 1036 | type: "error", |
| 1037 | title: gt("Could not restart resource monitor"), |
| 1038 | description: |
| 1039 | error instanceof Error |
| 1040 | ? (thrownErrorMessage(error, m) ?? error.message) |
| 1041 | : gt("The resource monitor retry failed."), |
| 1042 | }); |
| 1043 | }) |
| 1044 | .finally(() => { |
| 1045 | setIsRetrying(false); |
| 1046 | }); |
| 1047 | }; |
| 1048 | |
| 1049 | const speedLimit = snapshot ? Option.getOrNull(snapshot.speedLimitPercent) : null; |
| 1050 | const collectorNeedsRetry = shouldShowResourceMonitorRetry({ |
| 1051 | nativeStatus: snapshot?.health.native.status ?? null, |
| 1052 | error: telemetry.error, |
| 1053 | }); |
| 1054 | const hasHostPowerSignal = |
| 1055 | snapshot !== null && |
| 1056 | (snapshot.power.onBattery !== "unknown" || |
| 1057 | snapshot.power.lowPowerMode !== "unknown" || |
| 1058 | snapshot.power.idle !== "unknown" || |
| 1059 | snapshot.power.locked !== "unknown" || |
| 1060 | snapshot.power.thermalState !== "unknown"); |
| 1061 | |
| 1062 | return ( |
| 1063 | <> |
| 1064 | <SettingsSection |
| 1065 | title={gt("Resource monitor")} |
| 1066 | icon={<ActivityIcon className="size-4 text-muted-foreground" />} |
| 1067 | headerAction={ |
| 1068 | <div className="flex items-center gap-2"> |
| 1069 | {snapshot ? ( |
| 1070 | <SourceStatusBadge label={gt("Native")} status={snapshot.health.native.status} /> |
| 1071 | ) : null} |
| 1072 | <LastSampleLabel sampledAt={snapshot?.readAt ?? null} /> |
| 1073 | <Tooltip> |
| 1074 | <TooltipTrigger |
| 1075 | render={ |
| 1076 | <Button |
| 1077 | size="icon-micro" |
| 1078 | variant="ghost" |
| 1079 | disabled={telemetry.isPending} |
| 1080 | onClick={telemetry.refresh} |
| 1081 | aria-label={gt("Refresh resource telemetry")} |
| 1082 | > |
| 1083 | <RefreshIcon size="xs" refreshing={telemetry.isPending} /> |
| 1084 | </Button> |
| 1085 | } |
| 1086 | /> |
| 1087 | <TooltipPopup side="top"> |
| 1088 | <T>Refresh telemetry snapshot</T> |
| 1089 | </TooltipPopup> |
| 1090 | </Tooltip> |
| 1091 | </div> |
| 1092 | } |
| 1093 | > |
| 1094 | <div className="overflow-hidden rounded-[inherit]"> |
| 1095 | <div className="flex flex-col gap-3 border-b border-border/60 bg-linear-to-r from-muted/45 via-muted/20 to-transparent px-4 py-4 sm:flex-row sm:items-center sm:justify-between sm:px-5"> |
| 1096 | <div> |
| 1097 | <div className="text-3xs font-semibold uppercase tracking-widest text-muted-foreground/70"> |
| 1098 | <T>T3 system footprint</T> |
| 1099 | </div> |
| 1100 | <p className="mt-1 max-w-xl text-xs leading-relaxed text-muted-foreground"> |
| 1101 | <T> |
| 1102 | Live native counters for the server, providers, terminals, desktop processes, and |
| 1103 | the monitor itself. |
| 1104 | </T> |
| 1105 | </p> |
| 1106 | </div> |
| 1107 | <div className="flex items-center gap-2 text-3xs text-muted-foreground/65"> |
| 1108 | <span |
| 1109 | className={cn( |
| 1110 | "size-1.5 rounded-full", |
| 1111 | snapshot && snapshot.sampleIntervalMs > 0 |
| 1112 | ? "bg-success" |
| 1113 | : "bg-muted-foreground/55", |
| 1114 | )} |
| 1115 | /> |
| 1116 | {snapshot |
| 1117 | ? formatSamplingStatus(snapshot.sampleIntervalMs, messages) |
| 1118 | : gt("Sampling every ...")} |
| 1119 | </div> |
| 1120 | </div> |
| 1121 | <div className="grid grid-cols-2 divide-x divide-y divide-border/55 md:grid-cols-3"> |
| 1122 | <IconStat |
| 1123 | icon={<CpuIcon className="size-3.5" />} |
| 1124 | label={gt("Current CPU")} |
| 1125 | value={allT3 ? `${allT3.currentCpuPercent.toFixed(1)}%` : "..."} |
| 1126 | detail={ |
| 1127 | allT3 |
| 1128 | ? gt("{time} observed CPU time", { |
| 1129 | time: formatCpuTime(allT3.cpuTimeMs / 1_000, messages), |
| 1130 | }) |
| 1131 | : undefined |
| 1132 | } |
| 1133 | /> |
| 1134 | <IconStat |
| 1135 | icon={<MemoryStickIcon className="size-3.5" />} |
| 1136 | label={gt("Resident memory")} |
| 1137 | value={allT3 ? formatBytes(allT3.currentRssBytes) : "..."} |
| 1138 | detail={ |
| 1139 | allT3 |
| 1140 | ? gt("{bytes} combined process peaks", { bytes: formatBytes(allT3.peakRssBytes) }) |
| 1141 | : undefined |
| 1142 | } |
| 1143 | /> |
| 1144 | <IconStat |
| 1145 | icon={<ActivityIcon className="size-3.5" />} |
| 1146 | label={gt("Process count")} |
| 1147 | value={allT3 ? String(allT3.processCount) : "..."} |
| 1148 | detail={ |
| 1149 | allT3 |
| 1150 | ? gt("{processStarts} starts · {processExits} exits", { |
| 1151 | processStarts: allT3.processStarts, |
| 1152 | processExits: allT3.processExits, |
| 1153 | }) |
| 1154 | : undefined |
| 1155 | } |
| 1156 | /> |
| 1157 | <IconStat |
| 1158 | icon={<HardDriveIcon className="size-3.5" />} |
| 1159 | label={gt("Read throughput")} |
| 1160 | value={allT3 ? formatRate(allT3.ioReadBytesPerSecond) : "..."} |
| 1161 | detail={allT3 ? `${formatBytes(allT3.ioReadBytes)} observed` : undefined} |
| 1162 | /> |
| 1163 | <IconStat |
| 1164 | icon={<DatabaseIcon className="size-3.5" />} |
| 1165 | label={gt("Write throughput")} |
| 1166 | value={allT3 ? formatRate(allT3.ioWriteBytesPerSecond) : "..."} |
| 1167 | detail={allT3 ? `${formatBytes(allT3.ioWriteBytes)} observed` : undefined} |
| 1168 | tone={ |
| 1169 | allT3 && allT3.ioWriteBytesPerSecond >= 10 * 1_024 * 1_024 |
| 1170 | ? "danger" |
| 1171 | : allT3 && allT3.ioWriteBytesPerSecond >= 1_024 * 1_024 |
| 1172 | ? "warning" |
| 1173 | : "default" |
| 1174 | } |
| 1175 | /> |
| 1176 | <IconStat |
| 1177 | icon={<GaugeIcon className="size-3.5" />} |
| 1178 | label={gt("CPU speed limit")} |
| 1179 | value={ |
| 1180 | snapshot |
| 1181 | ? speedLimit === null |
| 1182 | ? gt("Unknown") |
| 1183 | : `${speedLimit.toFixed(0)}%` |
| 1184 | : "..." |
| 1185 | } |
| 1186 | detail={ |
| 1187 | snapshot |
| 1188 | ? gt("{thermalState} thermal state", { |
| 1189 | thermalState: snapshot.power.thermalState, |
| 1190 | }) |
| 1191 | : undefined |
| 1192 | } |
| 1193 | tone={speedLimit !== null && speedLimit < 80 ? "warning" : "default"} |
| 1194 | /> |
| 1195 | </div> |
| 1196 | {telemetry.error ? ( |
| 1197 | <div className="flex items-start gap-2 border-t border-destructive/20 bg-destructive/5 px-4 py-3 text-xs text-destructive sm:px-5"> |
| 1198 | <AlertTriangleIcon className="size-3.5 h-lh shrink-0" /> |
| 1199 | <span>{telemetry.error}</span> |
| 1200 | </div> |
| 1201 | ) : null} |
| 1202 | {snapshot ? ( |
| 1203 | <div className="grid border-t border-border/60 bg-muted/10 md:grid-cols-3"> |
| 1204 | <AggregateCard |
| 1205 | label={gt("Backend + agents")} |
| 1206 | accentClass="bg-success/80" |
| 1207 | aggregate={snapshot.groups.backend} |
| 1208 | /> |
| 1209 | <AggregateCard |
| 1210 | label={gt("Desktop")} |
| 1211 | accentClass="bg-info/80" |
| 1212 | aggregate={snapshot.groups.electron} |
| 1213 | /> |
| 1214 | <AggregateCard |
| 1215 | label={gt("Monitor overhead")} |
| 1216 | accentClass="bg-warning/80" |
| 1217 | aggregate={snapshot.groups.monitor} |
| 1218 | /> |
| 1219 | </div> |
| 1220 | ) : null} |
| 1221 | </div> |
| 1222 | </SettingsSection> |
| 1223 | |
| 1224 | <SettingsSection |
| 1225 | title={gt("Host & collection")} |
| 1226 | icon={<GaugeIcon className="size-4 text-muted-foreground" />} |
| 1227 | headerAction={ |
| 1228 | collectorNeedsRetry ? ( |
| 1229 | <Button size="xs" variant="outline" disabled={isRetrying} onClick={retryCollector}> |
| 1230 | <RefreshIcon size="xs" refreshing={isRetrying} /> |
| 1231 | <T>Retry monitor</T> |
| 1232 | </Button> |
| 1233 | ) : null |
| 1234 | } |
| 1235 | > |
| 1236 | <div className="grid overflow-hidden rounded-[inherit] md:grid-cols-2 md:divide-x md:divide-border/60"> |
| 1237 | <div className="px-4 py-4 sm:px-5"> |
| 1238 | <div className="mb-3 flex items-center gap-2 text-3xs font-semibold uppercase tracking-widest text-muted-foreground/70"> |
| 1239 | <span className="flex size-6 items-center justify-center rounded-md bg-muted/60"> |
| 1240 | <BatteryIcon className="size-3.5" /> |
| 1241 | </span> |
| 1242 | <T>Host state</T> |
| 1243 | </div> |
| 1244 | {hasHostPowerSignal && snapshot ? ( |
| 1245 | <> |
| 1246 | <DetailRow |
| 1247 | label={gt("Power source")} |
| 1248 | value={booleanStateLabel( |
| 1249 | snapshot.power.onBattery, |
| 1250 | { true: gt("Battery"), false: gt("External power") }, |
| 1251 | messages, |
| 1252 | )} |
| 1253 | /> |
| 1254 | <DetailRow |
| 1255 | label={gt("Low power mode")} |
| 1256 | value={booleanStateLabel( |
| 1257 | snapshot.power.lowPowerMode, |
| 1258 | { true: gt("Enabled"), false: gt("Disabled") }, |
| 1259 | messages, |
| 1260 | )} |
| 1261 | /> |
| 1262 | <DetailRow |
| 1263 | label={gt("Idle")} |
| 1264 | value={`${booleanStateLabel( |
| 1265 | snapshot.power.idle, |
| 1266 | { |
| 1267 | true: gt("Idle", { $context: "host power state" }), |
| 1268 | false: gt("Active", { $context: "host power state" }), |
| 1269 | }, |
| 1270 | messages, |
| 1271 | )}${ |
| 1272 | snapshot.power.idleSeconds === null |
| 1273 | ? "" |
| 1274 | : ` · ${Math.round(snapshot.power.idleSeconds)}s` |
| 1275 | }`} |
| 1276 | /> |
| 1277 | <DetailRow |
| 1278 | label={gt("Session")} |
| 1279 | value={ |
| 1280 | snapshot.power.suspended |
| 1281 | ? gt("Suspended") |
| 1282 | : booleanStateLabel( |
| 1283 | snapshot.power.locked, |
| 1284 | { true: gt("Locked"), false: gt("Unlocked") }, |
| 1285 | messages, |
| 1286 | ) |
| 1287 | } |
| 1288 | /> |
| 1289 | <DetailRow |
| 1290 | label={gt("Thermal")} |
| 1291 | value={snapshot.power.thermalState} |
| 1292 | valueClassName={ |
| 1293 | snapshot.power.thermalState === "serious" || |
| 1294 | snapshot.power.thermalState === "critical" |
| 1295 | ? "text-destructive" |
| 1296 | : undefined |
| 1297 | } |
| 1298 | /> |
| 1299 | </> |
| 1300 | ) : ( |
| 1301 | <div className="rounded-xl border border-dashed border-border/70 bg-muted/20 px-4 py-5"> |
| 1302 | <div className="text-sm font-medium text-foreground"> |
| 1303 | <T>Desktop host signals not connected</T> |
| 1304 | </div> |
| 1305 | <p className="mt-1.5 max-w-sm text-2xs leading-relaxed text-muted-foreground/70"> |
| 1306 | <T> |
| 1307 | Power, idle, lock, and thermal state are supplied by the desktop host. Process |
| 1308 | telemetry remains fully active in this browser session. |
| 1309 | </T> |
| 1310 | </p> |
| 1311 | </div> |
| 1312 | )} |
| 1313 | </div> |
| 1314 | <div className="border-t border-border/60 px-4 py-4 md:border-t-0 sm:px-5"> |
| 1315 | <div className="mb-3 flex items-center gap-2 text-3xs font-semibold uppercase tracking-widest text-muted-foreground/70"> |
| 1316 | <span className="flex size-6 items-center justify-center rounded-md bg-muted/60"> |
| 1317 | <GaugeIcon className="size-3.5" /> |
| 1318 | </span> |
| 1319 | <T>Collection health</T> |
| 1320 | </div> |
| 1321 | {snapshot ? ( |
| 1322 | <> |
| 1323 | <HealthSource |
| 1324 | label={gt("Native process monitor")} |
| 1325 | health={snapshot.health.native} |
| 1326 | /> |
| 1327 | <HealthSource |
| 1328 | label={gt("Electron main process")} |
| 1329 | health={snapshot.health.desktop} |
| 1330 | /> |
| 1331 | <DetailRow |
| 1332 | label={gt("Collection time")} |
| 1333 | value={formatDurationMicros(snapshot.health.collectionDurationMicros)} |
| 1334 | /> |
| 1335 | <DetailRow |
| 1336 | label={gt("Process scan")} |
| 1337 | value={`${snapshot.health.retainedProcessCount}/${snapshot.health.scannedProcessCount} retained`} |
| 1338 | /> |
| 1339 | <DetailRow |
| 1340 | label={gt("Inaccessible")} |
| 1341 | value={String(snapshot.health.inaccessibleProcessCount)} |
| 1342 | valueClassName={ |
| 1343 | snapshot.health.inaccessibleProcessCount > 0 |
| 1344 | ? "text-warning-foreground" |
| 1345 | : undefined |
| 1346 | } |
| 1347 | /> |
| 1348 | <DetailRow |
| 1349 | label={gt("Sidecar")} |
| 1350 | value={Option.match(snapshot.health.sidecarVersion, { |
| 1351 | onNone: () => gt("Unavailable"), |
| 1352 | onSome: (version) => |
| 1353 | `${version}${Option.match(snapshot.health.sidecarPid, { |
| 1354 | onNone: () => "", |
| 1355 | onSome: (pid) => ` · PID ${pid}`, |
| 1356 | })}`, |
| 1357 | })} |
| 1358 | /> |
| 1359 | <DetailRow label={gt("Restarts")} value={String(snapshot.health.restartCount)} /> |
| 1360 | </> |
| 1361 | ) : ( |
| 1362 | <div className="py-4 text-xs text-muted-foreground"> |
| 1363 | <T>Waiting for collector health.</T> |
| 1364 | </div> |
| 1365 | )} |
| 1366 | </div> |
| 1367 | </div> |
| 1368 | </SettingsSection> |
| 1369 | |
| 1370 | <SettingsSection |
| 1371 | title={gt("Resource timeline")} |
| 1372 | icon={<HardDriveIcon className="size-4 text-muted-foreground" />} |
| 1373 | headerAction={ |
| 1374 | <div className="flex items-center gap-2"> |
| 1375 | <HistoryWindowSelector selectedWindowMs={windowMs} onSelect={setWindowMs} /> |
| 1376 | <Button |
| 1377 | size="icon-micro" |
| 1378 | variant="ghost" |
| 1379 | disabled={history.isPending} |
| 1380 | onClick={history.refresh} |
| 1381 | aria-label={gt("Refresh resource history")} |
| 1382 | > |
| 1383 | <RefreshIcon size="xs" refreshing={history.isPending} /> |
| 1384 | </Button> |
| 1385 | </div> |
| 1386 | } |
| 1387 | > |
| 1388 | <div className="overflow-hidden rounded-[inherit]"> |
| 1389 | {history.error ? ( |
| 1390 | <div className="flex items-start gap-2 border-b border-destructive/20 bg-destructive/5 px-4 py-3 text-xs text-destructive sm:px-5"> |
| 1391 | <AlertTriangleIcon className="size-3.5 h-lh shrink-0" /> |
| 1392 | <span>{history.error}</span> |
| 1393 | </div> |
| 1394 | ) : null} |
| 1395 | <ResourceHistoryChart buckets={history.data?.buckets ?? []} /> |
| 1396 | <HistoryProcessTable processes={history.data?.topProcesses ?? []} /> |
| 1397 | </div> |
| 1398 | </SettingsSection> |
| 1399 | |
| 1400 | <SettingsSection |
| 1401 | title={gt("Live process tree")} |
| 1402 | icon={<CpuIcon className="size-4 text-muted-foreground" />} |
| 1403 | headerAction={ |
| 1404 | snapshot ? ( |
| 1405 | <span className="text-3xs text-muted-foreground/55"> |
| 1406 | <T> |
| 1407 | Identity: <span className="font-mono">PID + start time</span> |
| 1408 | </T> |
| 1409 | </span> |
| 1410 | ) : null |
| 1411 | } |
| 1412 | > |
| 1413 | <div className="overflow-hidden rounded-[inherit]"> |
| 1414 | <ProcessTable |
| 1415 | processes={snapshot?.processes ?? []} |
| 1416 | canMaintainEnvironment={canMaintainEnvironment} |
| 1417 | signalingKeys={signalingKeys} |
| 1418 | onSignal={signalProcess} |
| 1419 | /> |
| 1420 | </div> |
| 1421 | </SettingsSection> |
| 1422 | |
| 1423 | <SettingsSection |
| 1424 | title={gt("Instrumented application I/O")} |
| 1425 | icon={<DatabaseIcon className="size-4 text-muted-foreground" />} |
| 1426 | headerAction={ |
| 1427 | <span className="text-3xs text-muted-foreground/55"> |
| 1428 | <T>Logical bytes by operation</T> |
| 1429 | </span> |
| 1430 | } |
| 1431 | > |
| 1432 | <div className="overflow-hidden rounded-[inherit]"> |
| 1433 | <div className="bg-muted/15 px-4 py-3 text-2xs leading-relaxed text-muted-foreground sm:px-5"> |
| 1434 | <T> |
| 1435 | Native counters identify which process is reading or writing. These application-level |
| 1436 | counters identify known T3 operations so process spikes can be correlated with |
| 1437 | specific persistence and logging paths. |
| 1438 | </T> |
| 1439 | </div> |
| 1440 | <AttributionTable entries={snapshot?.attribution.entries ?? []} /> |
| 1441 | </div> |
| 1442 | </SettingsSection> |
| 1443 | </> |
| 1444 | ); |
| 1445 | } |