2025-10-30 15:16:33 +00:00
|
|
|
import { renderChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig';
|
2026-03-05 20:30:58 +00:00
|
|
|
import { isBuilderChartConfig } from '@hyperdx/common-utils/dist/guards';
|
2026-01-15 09:21:27 +00:00
|
|
|
import { ChartConfigWithOptDateRange } from '@hyperdx/common-utils/dist/types';
|
2025-01-21 18:44:14 +00:00
|
|
|
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
|
|
|
|
|
|
2025-08-30 02:42:05 +00:00
|
|
|
import { useClickhouseClient } from '@/clickhouse';
|
2026-01-21 16:07:30 +00:00
|
|
|
import { useSource } from '@/source';
|
2026-01-13 12:55:12 +00:00
|
|
|
|
|
|
|
|
import { useMetadataWithSettings } from './useMetadata';
|
2024-12-23 22:39:16 +00:00
|
|
|
|
|
|
|
|
export function useExplainQuery(
|
2026-01-15 09:21:27 +00:00
|
|
|
_config: ChartConfigWithOptDateRange,
|
2024-12-23 22:39:16 +00:00
|
|
|
options?: Omit<UseQueryOptions<any>, 'queryKey' | 'queryFn'>,
|
|
|
|
|
) {
|
2025-06-05 16:05:28 +00:00
|
|
|
const config = {
|
|
|
|
|
..._config,
|
|
|
|
|
with: undefined,
|
|
|
|
|
};
|
2025-08-30 02:42:05 +00:00
|
|
|
const clickhouseClient = useClickhouseClient();
|
2026-01-21 16:07:30 +00:00
|
|
|
|
2026-01-13 12:55:12 +00:00
|
|
|
const metadata = useMetadataWithSettings();
|
|
|
|
|
|
2026-01-21 16:07:30 +00:00
|
|
|
const { data: source, isLoading: isSourceLoading } = useSource({
|
2026-03-05 20:30:58 +00:00
|
|
|
id: isBuilderChartConfig(config) ? config.source : undefined,
|
2026-01-21 16:07:30 +00:00
|
|
|
});
|
|
|
|
|
|
2026-01-15 09:21:27 +00:00
|
|
|
return useQuery({
|
2024-12-23 22:39:16 +00:00
|
|
|
queryKey: ['explain', config],
|
|
|
|
|
queryFn: async ({ signal }) => {
|
2026-01-21 16:07:30 +00:00
|
|
|
const query = await renderChartConfig(
|
|
|
|
|
config,
|
|
|
|
|
metadata,
|
|
|
|
|
source?.querySettings,
|
|
|
|
|
);
|
2025-01-21 18:44:14 +00:00
|
|
|
const response = await clickhouseClient.query<'JSONEachRow'>({
|
2024-12-23 22:39:16 +00:00
|
|
|
query: `EXPLAIN ESTIMATE ${query.sql}`,
|
|
|
|
|
query_params: query.params,
|
|
|
|
|
format: 'JSONEachRow',
|
|
|
|
|
abort_signal: signal,
|
|
|
|
|
connectionId: config.connection,
|
|
|
|
|
});
|
|
|
|
|
return response.json();
|
|
|
|
|
},
|
|
|
|
|
retry: false,
|
|
|
|
|
staleTime: 1000 * 60,
|
2026-01-21 16:07:30 +00:00
|
|
|
enabled: !isSourceLoading,
|
2024-12-23 22:39:16 +00:00
|
|
|
...options,
|
|
|
|
|
});
|
|
|
|
|
}
|