-
Notifications
You must be signed in to change notification settings - Fork 167
/
usePrometheusQueryRange.ts
39 lines (32 loc) · 1.19 KB
/
usePrometheusQueryRange.ts
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
import * as React from 'react';
import axios from 'axios';
import useFetchState, { FetchState, FetchStateCallbackPromise } from '~/utilities/useFetchState';
import {
PrometheusQueryRangeResponse,
PrometheusQueryRangeResponseData,
PrometheusQueryRangeResultValue,
} from '~/types';
export type ResponsePredicate<T = PrometheusQueryRangeResultValue> = (
data: PrometheusQueryRangeResponseData,
) => T[];
const usePrometheusQueryRange = <T = PrometheusQueryRangeResultValue>(
active: boolean,
apiPath: string,
queryLang: string,
span: number,
endInMs: number,
step: number,
responsePredicate: ResponsePredicate<T>,
): FetchState<T[]> => {
const fetchData = React.useCallback<FetchStateCallbackPromise<T[]>>(() => {
const endInS = endInMs / 1000;
const start = endInS - span;
return axios
.post<{ response: PrometheusQueryRangeResponse }>(apiPath, {
query: `query=${queryLang}&start=${start}&end=${endInS}&step=${step}`,
})
.then((response) => responsePredicate(response.data?.response.data));
}, [endInMs, span, apiPath, queryLang, step, responsePredicate]);
return useFetchState<T[]>(fetchData, []);
};
export default usePrometheusQueryRange;