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

processDiagnosticsState.ts

140 lines4.1 KB

1import { useAtomValue } from "@effect/atom-react";
2import type {
3 ServerProcessDiagnosticsResult,
4 ServerProcessResourceHistoryResult,
5} from "@t3tools/contracts";
6import * as Cause from "effect/Cause";
7import * as Effect from "effect/Effect";
8import * as Option from "effect/Option";
9import { AsyncResult, Atom } from "effect/unstable/reactivity";
10import { useCallback } from "react";
11​
12import { ensureLocalApi } from "../localApi";
13import { appAtomRegistry } from "../rpc/atomRegistry";
14​
15const PROCESS_DIAGNOSTICS_STALE_TIME_MS = 2_000;
16const PROCESS_DIAGNOSTICS_IDLE_TTL_MS = 5 * 60_000;
17const PROCESS_RESOURCE_HISTORY_STALE_TIME_MS = 5_000;
18const PROCESS_RESOURCE_HISTORY_INPUT_SEPARATOR = ":";
19​
20const processDiagnosticsAtom = Atom.make(
21 Effect.promise(() => ensureLocalApi().server.getProcessDiagnostics()),
22).pipe(
23 Atom.swr({
24 staleTime: PROCESS_DIAGNOSTICS_STALE_TIME_MS,
25 revalidateOnMount: true,
26 }),
27 Atom.setIdleTTL(PROCESS_DIAGNOSTICS_IDLE_TTL_MS),
28 Atom.withLabel("process-diagnostics"),
29);
30​
31function formatProcessResourceHistoryKey(input: {
32 readonly windowMs: number;
33 readonly bucketMs: number;
34}): string {
35 return `${input.windowMs}${PROCESS_RESOURCE_HISTORY_INPUT_SEPARATOR}${input.bucketMs}`;
37​
38function parseProcessResourceHistoryKey(key: string): {
39 readonly windowMs: number;
40 readonly bucketMs: number;
41} {
42 const [windowMs = "0", bucketMs = "0"] = key.split(PROCESS_RESOURCE_HISTORY_INPUT_SEPARATOR);
43 return {
44 windowMs: Number(windowMs),
45 bucketMs: Number(bucketMs),
46 };
48​
49const processResourceHistoryAtom = Atom.family((key: string) => {
50 const input = parseProcessResourceHistoryKey(key);
51 return Atom.make(
52 Effect.promise(() => ensureLocalApi().server.getProcessResourceHistory(input)),
53 ).pipe(
54 Atom.swr({
55 staleTime: PROCESS_RESOURCE_HISTORY_STALE_TIME_MS,
56 revalidateOnMount: true,
57 }),
58 Atom.setIdleTTL(PROCESS_DIAGNOSTICS_IDLE_TTL_MS),
59 Atom.withLabel(`process-resource-history:${key}`),
60 );
61});
62​
63export interface ProcessDiagnosticsState {
64 readonly data: ServerProcessDiagnosticsResult | null;
65 readonly error: string | null;
66 readonly isPending: boolean;
67 readonly refresh: () => void;
69​
70export interface ProcessResourceHistoryState {
71 readonly data: ServerProcessResourceHistoryResult | null;
72 readonly error: string | null;
73 readonly isPending: boolean;
74 readonly refresh: () => void;
76​
77function formatProcessDiagnosticsError(error: unknown): string {
78 return error instanceof Error ? error.message : "Failed to load process diagnostics.";
80​
81function readProcessDiagnosticsError(
82 result: AsyncResult.AsyncResult<ServerProcessDiagnosticsResult, unknown>,
83): string | null {
84 if (result._tag !== "Failure") {
85 return null;
86 }
87​
88 const squashed = Cause.squash(result.cause);
89 return formatProcessDiagnosticsError(squashed);
91​
92function readProcessResourceHistoryError(
93 result: AsyncResult.AsyncResult<ServerProcessResourceHistoryResult, unknown>,
94): string | null {
95 if (result._tag !== "Failure") {
96 return null;
97 }
98​
99 const squashed = Cause.squash(result.cause);
100 return formatProcessDiagnosticsError(squashed);
102​
103export function refreshProcessDiagnostics(): void {
104 appAtomRegistry.refresh(processDiagnosticsAtom);
106​
107export function useProcessDiagnostics(): ProcessDiagnosticsState {
108 const result = useAtomValue(processDiagnosticsAtom);
109 const data = Option.getOrNull(AsyncResult.value(result));
110 const refresh = useCallback(() => {
111 refreshProcessDiagnostics();
112 }, []);
113​
114 return {
115 data,
116 error: readProcessDiagnosticsError(result),
117 isPending: result.waiting,
118 refresh,
119 };
121​
122export function useProcessResourceHistory(input: {
123 readonly windowMs: number;
124 readonly bucketMs: number;
125}): ProcessResourceHistoryState {
126 const atom = processResourceHistoryAtom(formatProcessResourceHistoryKey(input));
127 const result = useAtomValue(atom);
128 const data = Option.getOrNull(AsyncResult.value(result));
129​
130 const refresh = useCallback(() => {
131 appAtomRegistry.refresh(atom);
132 }, [atom]);
133​
134 return {
135 data,
136 error: readProcessResourceHistoryError(result),
137 isPending: result.waiting,
138 refresh,
139 };