Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat: add metadata filter for all UI tables using clickhouse (#4022)

Max Deichmannedf0dc8399f0
diff --git a/packages/shared/src/server/queries/clickhouse-filter/clickhouse-filter.ts b/packages/shared/src/server/queries/clickhouse-filter/clickhouse-filter.ts
index 8c8558979b..99db33e553 100644
--- a/packages/shared/src/server/queries/clickhouse-filter/clickhouse-filter.ts
+++ b/packages/shared/src/server/queries/clickhouse-filter/clickhouse-filter.ts
@@ -169,6 +169,66 @@ export class StringOptionsFilter implements Filter {
}
}
+// stringObject filter is used when we want to filter on a key value pair in a clickhouse map.
+// As we use the MAP form clickhouse, we can only filter efficiently on the first level of a json obj.
+export class StringObjectFilter implements Filter {
+ public clickhouseTable: string;
+ protected field: string;
+ protected key: string;
+ protected value: string;
+ protected operator: (typeof filterOperators)["stringObject"][number];
+ protected tablePrefix?: string;
+
+ constructor(opts: {
+ clickhouseTable: string;
+ field: string;
+ operator: (typeof filterOperators)["stringObject"][number];
+ key: string;
+ value: string;
+ tablePrefix?: string;
+ }) {
+ this.clickhouseTable = opts.clickhouseTable;
+ this.field = opts.field;
+ this.value = opts.value;
+ this.operator = opts.operator;
+ this.tablePrefix = opts.tablePrefix;
+ this.key = opts.key;
+ }
+
+ apply(): ClickhouseFilter {
+ const varKeyName = `stringObjectKeyFilter${randomCharacters()}`;
+ const varValueName = `stringObjectValueFilter${randomCharacters()}`;
+ const column = `${this.tablePrefix ? this.tablePrefix + "." : ""}${this.field}`;
+
+ // const query: `${column}['{varKeyName: String}'] ${this.operator} {${varValueName}: String}`,
+ let query: string;
+ switch (this.operator) {
+ case "=":
+ query = `${column}[{${varKeyName}: String}] = {${varValueName}: String}`;
+ break;
+ case "contains":
+ query = `position(${column}[{${varKeyName}: String}], {${varValueName}: String}) > 0`;
+ break;
+ case "does not contain":
+ query = `position(${column}[{${varKeyName}: String}], {${varValueName}: String}) = 0`;
+ break;
+ case "starts with":
+ query = `startsWith(${column}[{${varKeyName}: String}], {${varValueName}: String})`;
+ break;
+ case "ends with":
+ query = `endsWith(${column}[{${varKeyName}: String}], {${varValueName}: String})`;
+ break;
+ default:
+ throw new Error(`Unsupported operator: ${this.operator}`);
+ }
+
+ return {
+ query,
+ params: { [varKeyName]: this.key, [varValueName]: this.value },
+ };
+ }
+}
+
// this is used when we want to filter multiple values on a clickhouse column which is also an array
export class ArrayOptionsFilter implements Filter {
public clickhouseTable: string;
diff --git a/packages/shared/src/server/queries/clickhouse-filter/factory.ts b/packages/shared/src/server/queries/clickhouse-filter/factory.ts
index 758c4a66ef..c509d3214a 100644
--- a/packages/shared/src/server/queries/clickhouse-filter/factory.ts
+++ b/packages/shared/src/server/queries/clickhouse-filter/factory.ts
@@ -13,6 +13,7 @@ import {
ArrayOptionsFilter,
BooleanFilter,
NumberObjectFilter,
+ StringObjectFilter,
} from "./clickhouse-filter";
export class QueryBuilderError extends Error {
@@ -90,11 +91,19 @@ export const createFilterFromFilterState = (
value: frontEndFilter.value,
tablePrefix: column.queryPrefix,
});
+ case "stringObject":
+ return new StringObjectFilter({
+ clickhouseTable: column.clickhouseTableName,
+ field: column.clickhouseSelect,
+ operator: frontEndFilter.operator,
+ key: frontEndFilter.key,
+ value: frontEndFilter.value,
+ tablePrefix: column.queryPrefix,
+ });
default:
- throw new QueryBuilderError(
- `Invalid filter type: ${frontEndFilter.type}`,
- );
+ logger.error(`Invalid filter type: ${JSON.stringify(frontEndFilter)}`);
+ throw new QueryBuilderError(`Invalid filter type`);
}
});
};
diff --git a/packages/shared/src/server/repositories/clickhouse.ts b/packages/shared/src/server/repositories/clickhouse.ts
index 21bc26db89..d446d22a62 100644
--- a/packages/shared/src/server/repositories/clickhouse.ts
+++ b/packages/shared/src/server/repositories/clickhouse.ts
@@ -33,16 +33,15 @@ export async function queryClickhouse<T>(opts: {
// https://opentelemetry.io/docs/specs/semconv/database/database-spans/
span.setAttribute("ch.query.text", opts.query);
- // same logic as for prisma. we want to see queries in development
- if (env.NODE_ENV === "development") {
- logger.info(`clickhouse:query ${opts.query}`);
- }
-
const res = await clickhouseClient.query({
query: opts.query,
format: "JSONEachRow",
query_params: opts.params,
});
+ // same logic as for prisma. we want to see queries in development
+ if (env.NODE_ENV === "development") {
+ logger.info(`clickhouse:query ${res.query_id} ${opts.query}`);
+ }
span.setAttribute("ch.queryId", res.query_id);
diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.ts
index cdd3221f20..6984c03500 100644
--- a/packages/shared/src/server/repositories/observations.ts
+++ b/packages/shared/src/server/repositories/observations.ts
@@ -499,6 +499,7 @@ const getObservationsTableInternal = async <T>(
...appliedObservationsFilter.params,
},
});
+
return res;
};