Repository navigation
Expand file tree
/
Copy pathresourceTelemetryState.ts
More file actions
77 lines (72 loc) · 2.7 KB
/
Copy pathresourceTelemetryState.ts
File metadata and controls
77 lines (72 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { AuthDiagnosticsReadScope } from "@t3tools/contracts";
import { AuthEnvironmentMaintainScope } from "@t3tools/contracts";
import type {
EnvironmentId,
ResourceTelemetryHistoryInput,
ResourceTelemetrySnapshot,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import { usePrimaryEnvironment } from "../state/environments";
import { useEnvironmentQuery } from "../state/query";
import { serverEnvironment } from "../state/server";
import { readEnvironmentScope, useEnvironmentScope } from "../state/session";
import { useAtomCommand } from "../state/use-atom-command";
export interface ResourceTelemetryState {
readonly data: ResourceTelemetrySnapshot | null;
readonly error: string | null;
readonly isPending: boolean;
readonly refresh: () => void;
readonly retry: () => Promise<ResourceTelemetrySnapshot>;
}
export function useResourceTelemetry(
targetEnvironmentId?: EnvironmentId | null,
): ResourceTelemetryState {
const primaryEnvironment = usePrimaryEnvironment();
const environmentId =
targetEnvironmentId === undefined
? (primaryEnvironment?.environmentId ?? null)
: targetEnvironmentId;
const canReadDiagnostics = useEnvironmentScope(environmentId, AuthDiagnosticsReadScope);
const query = useEnvironmentQuery(
environmentId === null || !canReadDiagnostics
? null
: serverEnvironment.resourceTelemetry({ environmentId, input: {} }),
);
const retryCommand = useAtomCommand(serverEnvironment.retryResourceTelemetry, {
reportFailure: false,
});
const retry = async () => {
if (environmentId === null) {
throw new Error("No environment is selected.");
}
if (
!readEnvironmentScope(environmentId, AuthEnvironmentMaintainScope) ||
!readEnvironmentScope(environmentId, AuthDiagnosticsReadScope)
) {
throw new Error("This connection cannot restart the resource monitor.");
}
const result = await retryCommand({ environmentId, input: {} });
if (result._tag === "Failure") {
throw Cause.squash(result.cause);
}
query.refresh();
return result.value.snapshot;
};
return { ...query, retry };
}
export function useResourceTelemetryHistory(
input: ResourceTelemetryHistoryInput,
targetEnvironmentId?: EnvironmentId | null,
) {
const primaryEnvironment = usePrimaryEnvironment();
const environmentId =
targetEnvironmentId === undefined
? (primaryEnvironment?.environmentId ?? null)
: targetEnvironmentId;
const canReadDiagnostics = useEnvironmentScope(environmentId, AuthDiagnosticsReadScope);
return useEnvironmentQuery(
environmentId === null || !canReadDiagnostics
? null
: serverEnvironment.resourceTelemetryHistory({ environmentId, input }),
);
}