Skip to content

Commit 05fde2e

Browse files
authored
Fix filter loading issues by searching over more rows and caching (#786)
Fixes: HDX-1562 Fixes: HDX-1575
1 parent 41ae915 commit 05fde2e

File tree

3 files changed

+56
-31
lines changed

3 files changed

+56
-31
lines changed

packages/app/src/components/DBSearchPageFilters.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useMemo, useState } from 'react';
2+
import { DEFAULT_MAX_ROWS_TO_READ } from '@hyperdx/common-utils/dist/metadata';
23
import { ChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types';
34
import {
45
Box,
@@ -18,6 +19,7 @@ import {
1819
} from '@mantine/core';
1920
import { IconSearch } from '@tabler/icons-react';
2021

22+
import { useExplainQuery } from '@/hooks/useExplainQuery';
2123
import { useAllFields, useGetKeyValues } from '@/hooks/useMetadata';
2224
import useResizable from '@/hooks/useResizable';
2325
import { FilterStateHook, usePinnedFilters } from '@/searchFilters';
@@ -343,6 +345,9 @@ export const DBSearchPageFilters = ({
343345
);
344346
const { width, startResize } = useResizable(16, 'left');
345347

348+
const { data: countData } = useExplainQuery(chartConfig);
349+
const numRows: number = countData?.[0]?.rows ?? 0;
350+
346351
const { data, isLoading } = useAllFields({
347352
databaseName: chartConfig.from.databaseName,
348353
tableName: chartConfig.from.tableName,
@@ -390,19 +395,33 @@ export const DBSearchPageFilters = ({
390395
useEffect(() => {
391396
if (!isLive) {
392397
setDateRange(chartConfig.dateRange);
398+
setDisableRowLimit(false);
393399
}
394400
}, [chartConfig.dateRange, isLive]);
395401

396402
const showRefreshButton = isLive && dateRange !== chartConfig.dateRange;
397403

404+
const [disableRowLimit, setDisableRowLimit] = useState(false);
405+
const keyLimit = 100;
398406
const {
399407
data: facets,
400408
isLoading: isFacetsLoading,
401409
isFetching: isFacetsFetching,
402410
} = useGetKeyValues({
403411
chartConfigs: { ...chartConfig, dateRange },
412+
limit: keyLimit,
404413
keys: datum,
414+
disableRowLimit,
405415
});
416+
useEffect(() => {
417+
if (
418+
numRows > DEFAULT_MAX_ROWS_TO_READ &&
419+
facets &&
420+
facets.length < keyLimit
421+
) {
422+
setDisableRowLimit(true);
423+
}
424+
}, [numRows, keyLimit, facets]);
406425

407426
const shownFacets = useMemo(() => {
408427
const _facets: { key: string; value: string[] }[] = [];

packages/app/src/hooks/useMetadata.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export function useGetKeyValues(
116116
'useMetadata.useGetKeyValues',
117117
...chartConfigsArr.map(cc => ({ ...cc })),
118118
...keys,
119+
disableRowLimit,
119120
],
120121
queryFn: async () =>
121122
(

packages/common-utils/src/metadata.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { renderChartConfig } from '@/renderChartConfig';
1212
import type { ChartConfig, ChartConfigWithDateRange, TSource } from '@/types';
1313

14-
const DEFAULT_SAMPLE_SIZE = 1e6;
14+
export const DEFAULT_MAX_ROWS_TO_READ = 1e6;
1515

1616
export class MetadataCache {
1717
private cache = new Map<string, any>();
@@ -268,7 +268,7 @@ export class Metadata {
268268
query_params: sql.params,
269269
connectionId,
270270
clickhouse_settings: {
271-
max_rows_to_read: DEFAULT_SAMPLE_SIZE,
271+
max_rows_to_read: DEFAULT_MAX_ROWS_TO_READ,
272272
read_overflow_mode: 'break',
273273
},
274274
})
@@ -341,7 +341,7 @@ export class Metadata {
341341
query_params: sql.params,
342342
connectionId,
343343
clickhouse_settings: {
344-
max_rows_to_read: DEFAULT_SAMPLE_SIZE,
344+
max_rows_to_read: DEFAULT_MAX_ROWS_TO_READ,
345345
read_overflow_mode: 'break',
346346
},
347347
})
@@ -438,36 +438,41 @@ export class Metadata {
438438
limit?: number;
439439
disableRowLimit?: boolean;
440440
}) {
441-
const sql = await renderChartConfig(
442-
{
443-
...chartConfig,
444-
select: keys
445-
.map((k, i) => `groupUniqArray(${limit})(${k}) AS param${i}`)
446-
.join(', '),
441+
return this.cache.getOrFetch(
442+
`${chartConfig.from.databaseName}.${keys.join(',')}.${chartConfig.dateRange.toString()}.${disableRowLimit}.values`,
443+
async () => {
444+
const sql = await renderChartConfig(
445+
{
446+
...chartConfig,
447+
select: keys
448+
.map((k, i) => `groupUniqArray(${limit})(${k}) AS param${i}`)
449+
.join(', '),
450+
},
451+
this,
452+
);
453+
454+
const json = await this.clickhouseClient
455+
.query<'JSON'>({
456+
query: sql.sql,
457+
query_params: sql.params,
458+
connectionId: chartConfig.connection,
459+
clickhouse_settings: !disableRowLimit
460+
? {
461+
max_rows_to_read: DEFAULT_MAX_ROWS_TO_READ,
462+
read_overflow_mode: 'break',
463+
}
464+
: undefined,
465+
})
466+
.then(res => res.json<any>());
467+
468+
// TODO: Fix type issues mentioned in HDX-1548. value is not acually a
469+
// string[], sometimes it's { [key: string]: string; }
470+
return Object.entries(json?.data?.[0]).map(([key, value]) => ({
471+
key: keys[parseInt(key.replace('param', ''))],
472+
value: (value as string[])?.filter(Boolean), // remove nulls
473+
}));
447474
},
448-
this,
449475
);
450-
451-
const json = await this.clickhouseClient
452-
.query<'JSON'>({
453-
query: sql.sql,
454-
query_params: sql.params,
455-
connectionId: chartConfig.connection,
456-
clickhouse_settings: !disableRowLimit
457-
? {
458-
max_rows_to_read: DEFAULT_SAMPLE_SIZE,
459-
read_overflow_mode: 'break',
460-
}
461-
: undefined,
462-
})
463-
.then(res => res.json<any>());
464-
465-
// TODO: Fix type issues mentioned in HDX-1548. value is not acually a
466-
// string[], sometimes it's { [key: string]: string; }
467-
return Object.entries(json.data[0]).map(([key, value]) => ({
468-
key: keys[parseInt(key.replace('param', ''))],
469-
value: (value as string[])?.filter(Boolean), // remove nulls
470-
}));
471476
}
472477
}
473478

0 commit comments

Comments
 (0)