add logic for booleans, dates, arrays
This commit is contained in:
1 parent
77dda7268e
commit
43fb351ac7
9 files changed
+153
-78
No files matched your search
+25
-22
@@ -4,7 +4,7 @@ import { formatInTimeZone } from 'date-fns-tz';
|
||||
import debug from 'debug';
|
||||
import { DEFAULT_PAGE_SIZE, FILTER_COLUMNS, OPERATORS } from './constants';
|
||||
import { filtersObjectToArray } from './params';
|
||||
import type { EventPropertyFilter, QueryFilters, QueryOptions } from './types';
|
||||
import type { EventPropertyFilter, Operator, QueryFilters, QueryOptions } from './types';
|
||||
|
||||
export const CLICKHOUSE_DATE_FORMATS = {
|
||||
utc: '%Y-%m-%dT%H:%i:%SZ',
|
||||
@@ -18,10 +18,19 @@ export const CLICKHOUSE_DATE_FORMATS = {
|
||||
|
||||
const log = debug('umami:clickhouse');
|
||||
|
||||
const EQUALITY_OPERATORS: Operator[] = [OPERATORS.equals, OPERATORS.notEquals];
|
||||
const REGEX_OPERATORS: Operator[] = [OPERATORS.regex, OPERATORS.notRegex];
|
||||
|
||||
let clickhouse: ClickHouseClient;
|
||||
const enabled = Boolean(process.env.CLICKHOUSE_URL);
|
||||
|
||||
function getClient() {
|
||||
const clickhouseUrl = process.env.CLICKHOUSE_URL;
|
||||
|
||||
if (!clickhouseUrl) {
|
||||
throw new Error('CLICKHOUSE_URL is not set.');
|
||||
}
|
||||
|
||||
const {
|
||||
hostname,
|
||||
port,
|
||||
@@ -29,7 +38,7 @@ function getClient() {
|
||||
protocol,
|
||||
username = 'default',
|
||||
password,
|
||||
} = new URL(process.env.CLICKHOUSE_URL);
|
||||
} = new URL(clickhouseUrl);
|
||||
|
||||
const client = createClient({
|
||||
url: `${protocol}//${hostname}:${port}`,
|
||||
@@ -70,13 +79,7 @@ function getSearchSQL(column: string, param: string = 'search'): string {
|
||||
return `and positionCaseInsensitive(${column}, {${param}:String}) > 0`;
|
||||
}
|
||||
|
||||
function mapFilter(
|
||||
column: string,
|
||||
operator: string,
|
||||
name: string,
|
||||
type: string = 'String',
|
||||
paramName?: string,
|
||||
) {
|
||||
function mapFilter(column: string, operator: Operator, name: string, type: string = 'String', paramName?: string) {
|
||||
const param = paramName ?? name;
|
||||
const value = `{${param}:${type}}`;
|
||||
|
||||
@@ -210,7 +213,7 @@ function getQueryParams(filters: Record<string, any>) {
|
||||
|
||||
const key = paramName ?? name;
|
||||
|
||||
obj[key] = ([OPERATORS.equals, OPERATORS.notEquals] as string[]).includes(operator)
|
||||
obj[key] = EQUALITY_OPERATORS.includes(operator)
|
||||
? Array.isArray(value)
|
||||
? value
|
||||
: [value]
|
||||
@@ -256,30 +259,30 @@ function getEventPropertyFilterQuery(filters: EventPropertyFilter[] = []): {
|
||||
if (isNumeric) {
|
||||
params[valParam] = parseFloat(value) || 0;
|
||||
const opMap: Record<string, string> = {
|
||||
eq: `${col} = {${valParam}:Float64}`,
|
||||
neq: `${col} != {${valParam}:Float64}`,
|
||||
gt: `${col} > {${valParam}:Float64}`,
|
||||
lt: `${col} < {${valParam}:Float64}`,
|
||||
gte: `${col} >= {${valParam}:Float64}`,
|
||||
lte: `${col} <= {${valParam}:Float64}`,
|
||||
[OPERATORS.equals]: `${col} = {${valParam}:Float64}`,
|
||||
[OPERATORS.notEquals]: `${col} != {${valParam}:Float64}`,
|
||||
[OPERATORS.greaterThan]: `${col} > {${valParam}:Float64}`,
|
||||
[OPERATORS.lessThan]: `${col} < {${valParam}:Float64}`,
|
||||
[OPERATORS.greaterThanEquals]: `${col} >= {${valParam}:Float64}`,
|
||||
[OPERATORS.lessThanEquals]: `${col} <= {${valParam}:Float64}`,
|
||||
};
|
||||
condition = opMap[operator] ?? `${col} = {${valParam}:Float64}`;
|
||||
} else if (operator === 'eq' || operator === 'neq') {
|
||||
} else if (EQUALITY_OPERATORS.includes(operator)) {
|
||||
const vals = value.split(',').filter(Boolean);
|
||||
if (!vals.length) return;
|
||||
params[valParam] = vals;
|
||||
condition = mapFilter(
|
||||
col,
|
||||
operator === 'eq' ? OPERATORS.equals : OPERATORS.notEquals,
|
||||
operator === OPERATORS.equals ? OPERATORS.equals : OPERATORS.notEquals,
|
||||
valParam,
|
||||
'String',
|
||||
);
|
||||
} else if (operator === 'regex' || operator === 'notRegex') {
|
||||
} else if (REGEX_OPERATORS.includes(operator)) {
|
||||
if (!value) return;
|
||||
params[valParam] = value;
|
||||
condition = mapFilter(
|
||||
col,
|
||||
operator === 'regex' ? OPERATORS.regex : OPERATORS.notRegex,
|
||||
operator === OPERATORS.regex ? OPERATORS.regex : OPERATORS.notRegex,
|
||||
valParam,
|
||||
'String',
|
||||
);
|
||||
@@ -288,7 +291,7 @@ function getEventPropertyFilterQuery(filters: EventPropertyFilter[] = []): {
|
||||
params[valParam] = value;
|
||||
condition = mapFilter(
|
||||
col,
|
||||
operator === 'c' ? OPERATORS.contains : OPERATORS.doesNotContain,
|
||||
operator === OPERATORS.contains ? OPERATORS.contains : OPERATORS.doesNotContain,
|
||||
valParam,
|
||||
'String',
|
||||
);
|
||||
@@ -379,7 +382,7 @@ async function findFirst(data: any[]) {
|
||||
|
||||
async function connect() {
|
||||
if (enabled && !clickhouse) {
|
||||
clickhouse = process.env.CLICKHOUSE_URL && (globalThis[CLICKHOUSE] || getClient());
|
||||
clickhouse = globalThis[CLICKHOUSE] || getClient();
|
||||
}
|
||||
|
||||
return clickhouse;
|
||||
|
||||
+28
-5
@@ -1,5 +1,22 @@
|
||||
import { FILTER_COLUMNS, OPERATORS } from '@/lib/constants';
|
||||
import type { EventPropertyFilter, Filter, QueryFilters, QueryOptions } from '@/lib/types';
|
||||
import type { EventPropertyFilter, Filter, Operator, QueryFilters, QueryOptions } from '@/lib/types';
|
||||
|
||||
const VALID_OPERATORS: Operator[] = Object.values(OPERATORS);
|
||||
const NUMERIC_EVENT_PROPERTY_OPERATORS: Operator[] = [
|
||||
OPERATORS.greaterThan,
|
||||
OPERATORS.lessThan,
|
||||
OPERATORS.greaterThanEquals,
|
||||
OPERATORS.lessThanEquals,
|
||||
];
|
||||
const EQUALITY_OPERATORS: Operator[] = [OPERATORS.equals, OPERATORS.notEquals];
|
||||
|
||||
function resolveOperator(value?: string): Operator | undefined {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return VALID_OPERATORS.find(operator => operator === value);
|
||||
}
|
||||
|
||||
export function parseFilterValue(param: any) {
|
||||
if (typeof param === 'string') {
|
||||
@@ -9,7 +26,7 @@ export function parseFilterValue(param: any) {
|
||||
|
||||
const [, operator, value] = param.match(regex) || [];
|
||||
|
||||
const resolvedOperator = operator || OPERATORS.equals;
|
||||
const resolvedOperator = resolveOperator(operator) ?? OPERATORS.equals;
|
||||
const resolvedValue = value ?? param;
|
||||
|
||||
if (resolvedOperator === OPERATORS.equals || resolvedOperator === OPERATORS.notEquals) {
|
||||
@@ -97,11 +114,17 @@ export function parseEventPropertyFilters(query: Record<string, any>): EventProp
|
||||
if (dotIndex < 1) return [];
|
||||
const withoutPrefix = key.slice(4); // strip "epf_"
|
||||
const propertyName = withoutPrefix.replace(/\d+$/, ''); // strip trailing index digits
|
||||
const operator = (val as string).slice(0, dotIndex);
|
||||
const rawOperator = (val as string).slice(0, dotIndex);
|
||||
const operator = resolveOperator(rawOperator);
|
||||
if (!operator) {
|
||||
return [];
|
||||
}
|
||||
const value = (val as string).slice(dotIndex + 1);
|
||||
const isNumeric =
|
||||
['gt', 'lt', 'gte', 'lte'].includes(operator) ||
|
||||
(['eq', 'neq'].includes(operator) && value !== '' && !Number.isNaN(Number(value)));
|
||||
NUMERIC_EVENT_PROPERTY_OPERATORS.includes(operator) ||
|
||||
(EQUALITY_OPERATORS.includes(operator) &&
|
||||
value !== '' &&
|
||||
!Number.isNaN(Number(value)));
|
||||
return [{ propertyName, dataType: isNumeric ? 2 : 1, operator, value }];
|
||||
});
|
||||
}
|
||||
|
||||
+32
-14
@@ -8,6 +8,10 @@ import type { EventPropertyFilter, Operator, QueryFilters, QueryOptions } from '
|
||||
|
||||
const log = debug('umami:prisma');
|
||||
|
||||
const EQUALITY_OPERATORS: Operator[] = [OPERATORS.equals, OPERATORS.notEquals];
|
||||
const SEARCH_OPERATORS: Operator[] = [OPERATORS.contains, OPERATORS.doesNotContain];
|
||||
const REGEX_OPERATORS: Operator[] = [OPERATORS.regex, OPERATORS.notRegex];
|
||||
|
||||
const PRISMA = 'prisma';
|
||||
|
||||
const PRISMA_LOG_OPTIONS = {
|
||||
@@ -216,9 +220,9 @@ function getQueryParams(filters: Record<string, any>) {
|
||||
|
||||
const key = paramName ?? name;
|
||||
|
||||
if (([OPERATORS.contains, OPERATORS.doesNotContain] as Operator[]).includes(operator)) {
|
||||
if (SEARCH_OPERATORS.includes(operator)) {
|
||||
obj[key] = `%${value}%`;
|
||||
} else if (([OPERATORS.equals, OPERATORS.notEquals] as Operator[]).includes(operator)) {
|
||||
} else if (EQUALITY_OPERATORS.includes(operator)) {
|
||||
obj[key] = Array.isArray(value) ? value : [value];
|
||||
} else {
|
||||
obj[key] = value;
|
||||
@@ -273,31 +277,34 @@ function getEventPropertyFilterQuery(filters: EventPropertyFilter[] = []): {
|
||||
if (isNumeric) {
|
||||
params[valParam] = parseFloat(value) || 0;
|
||||
const opMap: Record<string, string> = {
|
||||
eq: `${col} = {{${valParam}}}`,
|
||||
neq: `${col} != {{${valParam}}}`,
|
||||
gt: `${col} > {{${valParam}}}`,
|
||||
lt: `${col} < {{${valParam}}}`,
|
||||
gte: `${col} >= {{${valParam}}}`,
|
||||
lte: `${col} <= {{${valParam}}}`,
|
||||
[OPERATORS.equals]: `${col} = {{${valParam}}}`,
|
||||
[OPERATORS.notEquals]: `${col} != {{${valParam}}}`,
|
||||
[OPERATORS.greaterThan]: `${col} > {{${valParam}}}`,
|
||||
[OPERATORS.lessThan]: `${col} < {{${valParam}}}`,
|
||||
[OPERATORS.greaterThanEquals]: `${col} >= {{${valParam}}}`,
|
||||
[OPERATORS.lessThanEquals]: `${col} <= {{${valParam}}}`,
|
||||
};
|
||||
condition = opMap[operator] ?? `${col} = {{${valParam}}}`;
|
||||
} else if (operator === 'eq' || operator === 'neq') {
|
||||
} else if (EQUALITY_OPERATORS.includes(operator)) {
|
||||
const vals = value.split(',').filter(Boolean);
|
||||
if (!vals.length) return;
|
||||
params[valParam] = vals;
|
||||
condition =
|
||||
operator === 'eq'
|
||||
operator === OPERATORS.equals
|
||||
? `${col} = ANY({{${valParam}::text[]}})`
|
||||
: `${col} != ALL({{${valParam}::text[]}})`;
|
||||
} else if (operator === 'regex' || operator === 'notRegex') {
|
||||
} else if (REGEX_OPERATORS.includes(operator)) {
|
||||
if (!value) return;
|
||||
params[valParam] = value;
|
||||
condition = operator === 'regex' ? `${col} ~* {{${valParam}}}` : `${col} !~* {{${valParam}}}`;
|
||||
condition =
|
||||
operator === OPERATORS.regex ? `${col} ~* {{${valParam}}}` : `${col} !~* {{${valParam}}}`;
|
||||
} else {
|
||||
if (!value) return;
|
||||
params[valParam] = `%${value}%`;
|
||||
condition =
|
||||
operator === 'c' ? `${col} ilike {{${valParam}}}` : `${col} not ilike {{${valParam}}}`;
|
||||
operator === OPERATORS.contains
|
||||
? `${col} ilike {{${valParam}}}`
|
||||
: `${col} not ilike {{${valParam}}}`;
|
||||
}
|
||||
|
||||
parts.push(`and website_event.event_id in (
|
||||
@@ -424,7 +431,13 @@ function transaction(input: any, options?: any) {
|
||||
}
|
||||
|
||||
function getSchema() {
|
||||
const connectionUrl = new URL(process.env.DATABASE_URL);
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
|
||||
if (!databaseUrl) {
|
||||
throw new Error('DATABASE_URL is not set.');
|
||||
}
|
||||
|
||||
const connectionUrl = new URL(databaseUrl);
|
||||
|
||||
return connectionUrl.searchParams.get('schema');
|
||||
}
|
||||
@@ -433,6 +446,11 @@ function getClient() {
|
||||
const url = process.env.DATABASE_URL;
|
||||
const replicaUrl = process.env.DATABASE_REPLICA_URL;
|
||||
const logQuery = process.env.LOG_QUERY;
|
||||
|
||||
if (!url) {
|
||||
throw new Error('DATABASE_URL is not set.');
|
||||
}
|
||||
|
||||
const schema = getSchema();
|
||||
|
||||
const baseAdapter = new PrismaPg({ connectionString: url }, { schema });
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ export interface Auth {
|
||||
export interface EventPropertyFilter {
|
||||
propertyName: string;
|
||||
dataType: number;
|
||||
operator: string;
|
||||
operator: Operator;
|
||||
value: string;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user