-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuseMetricResourceQuery.ts
More file actions
109 lines (99 loc) · 3.03 KB
/
Copy pathuseMetricResourceQuery.ts
File metadata and controls
109 lines (99 loc) · 3.03 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { useCallback, useEffect, useRef, useState } from "react";
import { useInterval } from "./useInterval";
export type MetricResourceRow = Record<string, number | string | null>;
type MetricResourceResponse =
| { success: true; data: { rows: MetricResourceRow[] } }
| { success: false; error: string };
export type MetricResourceTimeRange = {
period: string | null;
from: string | null;
to: string | null;
};
export type MetricResourceQueryOptions = {
organizationId: string;
projectId: string;
environmentId: string;
timeRange: MetricResourceTimeRange;
defaultPeriod: string;
queues?: string[];
fillGaps?: boolean;
refreshIntervalMs?: number;
};
/**
* Client-fetch a TRQL query from the metric resource route (like the dashboard
* widgets): own loading state, interval + on-focus refresh, abort on change/unmount.
*/
export function useMetricResourceQuery(query: string, opts: MetricResourceQueryOptions) {
const [rows, setRows] = useState<MetricResourceRow[] | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [failed, setFailed] = useState(false);
const abortRef = useRef<AbortController | null>(null);
const {
organizationId,
projectId,
environmentId,
defaultPeriod,
fillGaps,
refreshIntervalMs = 60_000,
} = opts;
const { period, from, to } = opts.timeRange;
const queuesKey = opts.queues && opts.queues.length > 0 ? opts.queues.join(",") : undefined;
const load = useCallback(() => {
abortRef.current?.abort();
const controller = new AbortController();
abortRef.current = controller;
setIsLoading(true);
fetch("/resources/metric", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query,
scope: "environment",
period: period ?? (from || to ? null : defaultPeriod),
from,
to,
fillGaps: !!fillGaps,
organizationId,
projectId,
environmentId,
...(queuesKey !== undefined ? { queues: queuesKey.split(",") } : {}),
}),
signal: controller.signal,
})
.then((res) => res.json() as Promise<MetricResourceResponse>)
.then((data) => {
if (controller.signal.aborted) return;
if (data.success) {
setRows(data.data.rows);
setFailed(false);
} else {
setFailed(true);
}
setIsLoading(false);
})
.catch((error) => {
if (error instanceof DOMException && error.name === "AbortError") return;
if (!controller.signal.aborted) {
setFailed(true);
setIsLoading(false);
}
});
}, [
query,
period,
from,
to,
defaultPeriod,
fillGaps,
organizationId,
projectId,
environmentId,
queuesKey,
]);
useEffect(() => {
load();
return () => abortRef.current?.abort();
}, [load]);
useInterval({ interval: refreshIntervalMs, onLoad: false, onFocus: true, callback: load });
return { rows: rows ?? [], isLoading, showLoading: isLoading && !rows, failed };
}