diff --git a/.changeset/five-glasses-live.md b/.changeset/five-glasses-live.md new file mode 100644 index 00000000..c8d5a648 --- /dev/null +++ b/.changeset/five-glasses-live.md @@ -0,0 +1,6 @@ +--- +"@hyperdx/common-utils": patch +"@hyperdx/app": patch +--- + +fix: metric name filtering for some metadata diff --git a/packages/app/src/components/DBEditTimeChartForm.tsx b/packages/app/src/components/DBEditTimeChartForm.tsx index 15660510..86297471 100644 --- a/packages/app/src/components/DBEditTimeChartForm.tsx +++ b/packages/app/src/components/DBEditTimeChartForm.tsx @@ -146,11 +146,12 @@ function ChartSeriesEditor({ ? getMetricTableName(tableSource, metricType) : _tableName; + const metricName = watch(`${namePrefix}metricName`); const { data: attributeKeys } = useFetchMetricResourceAttrs({ databaseName, tableName: tableName || '', metricType, - metricName: watch(`${namePrefix}metricName`), + metricName, tableSource, isSql: aggConditionLanguage === 'sql', }); @@ -192,8 +193,8 @@ function ChartSeriesEditor({ {tableSource?.kind === SourceKind.Metric && ( { setValue(`${namePrefix}metricName`, value); @@ -265,6 +266,10 @@ function ChartSeriesEditor({ databaseName, tableName: tableName ?? '', connectionId: connectionId ?? '', + metricName: + tableSource?.kind === SourceKind.Metric + ? metricName + : undefined, }} control={control} name={`groupBy`} diff --git a/packages/common-utils/src/metadata.ts b/packages/common-utils/src/metadata.ts index e27dca8f..78d6d99d 100644 --- a/packages/common-utils/src/metadata.ts +++ b/packages/common-utils/src/metadata.ts @@ -205,16 +205,19 @@ export class Metadata { column, maxKeys = 1000, connectionId, + metricName, }: { databaseName: string; tableName: string; column: string; maxKeys?: number; connectionId: string; + metricName?: string; }) { - const cachedKeys = this.cache.get( - `${databaseName}.${tableName}.${column}.keys`, - ); + const cacheKey = metricName + ? `${databaseName}.${tableName}.${column}.${metricName}.keys` + : `${databaseName}.${tableName}.${column}.keys`; + const cachedKeys = this.cache.get(cacheKey); if (cachedKeys != null) { return cachedKeys; @@ -239,49 +242,49 @@ export class Metadata { strategy = 'lowCardinalityKeys'; } + const where = metricName + ? chSql`WHERE MetricName=${{ String: metricName }}` + : ''; let sql: ChSql; if (strategy === 'groupUniqArrayArray') { sql = chSql`SELECT groupUniqArrayArray(${{ Int32: maxKeys }})(${{ Identifier: column, }}) as keysArr - FROM ${tableExpr({ database: databaseName, table: tableName })}`; + FROM ${tableExpr({ database: databaseName, table: tableName })} ${where}`; } else { sql = chSql`SELECT DISTINCT lowCardinalityKeys(arrayJoin(${{ Identifier: column, }}.keys)) as key - FROM ${tableExpr({ database: databaseName, table: tableName })} + FROM ${tableExpr({ database: databaseName, table: tableName })} ${where} LIMIT ${{ Int32: maxKeys, }}`; } - return this.cache.getOrFetch( - `${databaseName}.${tableName}.${column}.keys`, - async () => { - const keys = await this.clickhouseClient - .query<'JSON'>({ - query: sql.sql, - query_params: sql.params, - connectionId, - clickhouse_settings: { - max_rows_to_read: DEFAULT_SAMPLE_SIZE, - read_overflow_mode: 'break', - }, - }) - .then(res => res.json>()) - .then(d => { - let output: string[]; - if (strategy === 'groupUniqArrayArray') { - output = d.data[0].keysArr as string[]; - } else { - output = d.data.map(row => row.key) as string[]; - } + return this.cache.getOrFetch(cacheKey, async () => { + const keys = await this.clickhouseClient + .query<'JSON'>({ + query: sql.sql, + query_params: sql.params, + connectionId, + clickhouse_settings: { + max_rows_to_read: DEFAULT_SAMPLE_SIZE, + read_overflow_mode: 'break', + }, + }) + .then(res => res.json>()) + .then(d => { + let output: string[]; + if (strategy === 'groupUniqArrayArray') { + output = d.data[0].keysArr as string[]; + } else { + output = d.data.map(row => row.key) as string[]; + } - return output.filter(r => r); - }); - return keys; - }, - ); + return output.filter(r => r); + }); + return keys; + }); } async getMapValues({ @@ -353,6 +356,7 @@ export class Metadata { databaseName, tableName, connectionId, + metricName, }: TableConnection) { const fields: Field[] = []; const columns = await this.getColumns({ @@ -378,6 +382,7 @@ export class Metadata { tableName, column: column.name, connectionId, + metricName, }); const match = column.type.match(/Map\(.+,\s*(.+)\)/); @@ -476,6 +481,7 @@ export type TableConnection = { databaseName: string; tableName: string; connectionId: string; + metricName?: string; }; export function tcFromChartConfig(config?: ChartConfig): TableConnection {