Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

2.3 KB
Raw Blame
import { useQuery, useQueryClient, UseQueryResult } from 'react-query';
import { isAxiosError } from 'axios';
import { queryRangeV5 } from 'api/generated/services/querier';
import type {
	Querybuildertypesv5QueryRangeRequestDTO,
	QueryRangeV5200,
} from 'api/generated/services/sigNoz.schemas';
import { MAX_QUERY_RETRIES } from 'constants/reactQuery';

export interface UseGetQueryRangeV5Args {
	requestPayload: Querybuildertypesv5QueryRangeRequestDTO;
	queryKey: unknown[];
	enabled: boolean;
	/** Retain prior data across a key change (list paging) so the table + pager stay mounted. */
	keepPreviousData?: boolean;
	/** Unused-entry TTL; callers drop to 0 under auto-refresh to bound cache growth (V1 parity). */
	cacheTime?: number;
}

/**
 * Don't retry deterministic 4xx (bad query, auth) — they fail identically (V1 parity).
 * The retry callback gets the raw AxiosError this path rejects with (not yet normalized to
 * APIError — that happens later at the display boundary), so inspect it at the axios level.
 */
export function retryUnlessClientError(
	failureCount: number,
	error: Error,
): boolean {
	if (isAxiosError(error)) {
		if (error.code === 'ERR_CANCELED') {
			return false;
		}
		const status = error.response?.status;
		if (status && status >= 400 && status < 500) {
			return false;
		}
	}
	return failureCount < MAX_QUERY_RETRIES;
}

/**
 * Pure-V5 query-range fetch: posts the generated request DTO and returns the raw response.
 * Wrapped in `useQuery` (not the generated `useQueryRangeV5` mutation) for caching, `enabled`
 * gating, and refetch.
 */
export function useGetQueryRangeV5({
	requestPayload,
	queryKey,
	enabled,
	keepPreviousData,
	cacheTime,
}: UseGetQueryRangeV5Args): UseQueryResult<QueryRangeV5200, Error> {
	const queryClient = useQueryClient();
	// An errored key has no data, so it is always stale; keep it enabled so
	// re-enabling doesn't re-run it.
	const hasErrored = queryClient.getQueryState(queryKey)?.status === 'error';

	return useQuery<QueryRangeV5200, Error>({
		queryKey,
		queryFn: ({ signal }) => queryRangeV5(requestPayload, signal),
		enabled: enabled || hasErrored,
		retry: retryUnlessClientError,
		keepPreviousData,
		cacheTime,
		// A resolved window is immutable per key, so a panel scrolled back into view
		// serves cache instead of refetching; a key change or manual refetch still runs.
		staleTime: Infinity,
	});
}