Enable search for metrics.

This commit is contained in:
Mike Cao 2024-03-05 00:45:55 -08:00
parent bbd7c4b6ea
commit b0bfd0b5ab
7 changed files with 74 additions and 30 deletions

View file

@ -61,12 +61,14 @@ function getDateFormat(date: Date) {
return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`;
}
function mapFilter(column: string, operator: string, name: string, type = 'String') {
switch (operator) {
function mapFilter(column: string, filter: string, name: string, type: string = 'String') {
switch (filter) {
case OPERATORS.equals:
return `${column} = {${name}:${type}}`;
case OPERATORS.notEquals:
return `${column} != {${name}:${type}}`;
case OPERATORS.contains:
return `positionCaseInsensitive(${column}, {${name}:${type}}) > 0`;
default:
return '';
}
@ -75,11 +77,11 @@ function mapFilter(column: string, operator: string, name: string, type = 'Strin
function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {}) {
const query = Object.keys(filters).reduce((arr, name) => {
const value = filters[name];
const operator = value?.filter ?? OPERATORS.equals;
const column = FILTER_COLUMNS[name] ?? options?.columns?.[name];
const filter = value?.filter ?? OPERATORS.equals;
const column = value?.column ?? FILTER_COLUMNS[name] ?? options?.columns?.[name];
if (value !== undefined && column) {
arr.push(`and ${mapFilter(column, operator, name)}`);
if (value !== undefined && column !== undefined) {
arr.push(`and ${mapFilter(column, filter, name)}`);
if (name === 'referrer') {
arr.push('and referrer_domain != {websiteDomain:String}');

View file

@ -92,12 +92,14 @@ function getTimestampDiffQuery(field1: string, field2: string): string {
}
}
function mapFilter(column: string, operator: string, name: string, type = 'varchar') {
switch (operator) {
function mapFilter(column: string, filter: string, name: string, type = 'varchar') {
switch (filter) {
case OPERATORS.equals:
return `${column} = {{${name}::${type}}}`;
case OPERATORS.notEquals:
return `${column} != {{${name}::${type}}}`;
case OPERATORS.contains:
return `${column} like {{${name}::${type}}}`;
default:
return '';
}
@ -106,11 +108,11 @@ function mapFilter(column: string, operator: string, name: string, type = 'varch
function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {}): string {
const query = Object.keys(filters).reduce((arr, name) => {
const value = filters[name];
const operator = value?.filter ?? OPERATORS.equals;
const column = FILTER_COLUMNS[name] ?? options?.columns?.[name];
const filter = value?.filter ?? OPERATORS.equals;
const column = value?.column ?? FILTER_COLUMNS[name] ?? options?.columns?.[name];
if (value !== undefined && column) {
arr.push(`and ${mapFilter(column, operator, name)}`);
if (value !== undefined && column !== undefined) {
arr.push(`and ${mapFilter(column, filter, name)}`);
if (name === 'referrer') {
arr.push(

View file

@ -213,6 +213,7 @@ export interface QueryFilters {
city?: string;
language?: string;
event?: string;
search?: string;
}
export interface QueryOptions {