Allow filter search for country and region.

This commit is contained in:
Mike Cao 2024-11-27 23:43:28 -08:00
parent 2ed7628997
commit a18d1a923c
5 changed files with 55 additions and 17 deletions

View File

@ -28,12 +28,15 @@ export function useWebsiteValues({
const getSearch = (type: string, value: string) => { const getSearch = (type: string, value: string) => {
if (value) { if (value) {
const values = names[type]; const values = names[type];
return Object.keys(values).reduce((code: string, key: string) => { return Object.keys(values)
if (!code && values[key].toLowerCase().includes(value.toLowerCase())) { .reduce((arr: string[], key: string) => {
code = key; if (values[key].toLowerCase().includes(value.toLowerCase())) {
} return arr.concat(key);
return code; }
}, ''); return arr;
}, [])
.slice(0, 5)
.join(',');
} }
}; };

View File

@ -68,6 +68,10 @@ function getDateSQL(field: string, unit: string, timezone?: string) {
return `toDateTime(date_trunc('${unit}', ${field}))`; return `toDateTime(date_trunc('${unit}', ${field}))`;
} }
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') { function mapFilter(column: string, operator: string, name: string, type: string = 'String') {
const value = `{${name}:${type}}`; const value = `{${name}:${type}}`;
@ -229,6 +233,7 @@ export default {
connect, connect,
getDateStringSQL, getDateStringSQL,
getDateSQL, getDateSQL,
getSearchSQL,
getFilterQuery, getFilterQuery,
getUTCString, getUTCString,
parseFilters, parseFilters,

View File

@ -119,11 +119,11 @@ function getTimestampDiffSQL(field1: string, field2: string): string {
} }
} }
function getSearchSQL(column: string): string { function getSearchSQL(column: string, param: string = 'search'): string {
const db = getDatabaseType(); const db = getDatabaseType();
const like = db === POSTGRESQL ? 'ilike' : 'like'; const like = db === POSTGRESQL ? 'ilike' : 'like';
return `and ${column} ${like} {{search}}`; return `and ${column} ${like} {{${param}}`;
} }
function mapFilter(column: string, operator: string, name: string, type: string = '') { function mapFilter(column: string, operator: string, name: string, type: string = '') {

View File

@ -49,13 +49,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
return unauthorized(res); return unauthorized(res);
} }
const values = await getValues( const values = await getValues(websiteId, FILTER_COLUMNS[type], startDate, endDate, search);
websiteId,
FILTER_COLUMNS[type as string],
startDate,
endDate,
search,
);
return ok( return ok(
res, res,

View File

@ -19,10 +19,25 @@ async function relationalQuery(
search: string, search: string,
) { ) {
const { rawQuery, getSearchSQL } = prisma; const { rawQuery, getSearchSQL } = prisma;
const params = {};
let searchQuery = ''; let searchQuery = '';
if (search) { if (search) {
searchQuery = getSearchSQL(column); if (decodeURIComponent(search).includes(',')) {
searchQuery = `AND (${decodeURIComponent(search)
.split(',')
.slice(0, 5)
.map((value: string, index: number) => {
const key = `search${index}`;
params[key] = value;
return getSearchSQL(column, key).replace('and ', '');
})
.join(' OR ')})`;
} else {
searchQuery = getSearchSQL(column);
}
} }
return rawQuery( return rawQuery(
@ -43,6 +58,7 @@ async function relationalQuery(
startDate, startDate,
endDate, endDate,
search: `%${search}%`, search: `%${search}%`,
...params,
}, },
); );
} }
@ -54,13 +70,32 @@ async function clickhouseQuery(
endDate: Date, endDate: Date,
search: string, search: string,
) { ) {
const { rawQuery } = clickhouse; const { rawQuery, getSearchSQL } = clickhouse;
const params = {};
let searchQuery = ''; let searchQuery = '';
if (search) { if (search) {
searchQuery = `and positionCaseInsensitive(${column}, {search:String}) > 0`; searchQuery = `and positionCaseInsensitive(${column}, {search:String}) > 0`;
} }
if (search) {
if (decodeURIComponent(search).includes(',')) {
searchQuery = `AND (${decodeURIComponent(search)
.split(',')
.slice(0, 5)
.map((value: string, index: number) => {
const key = `search${index}`;
params[key] = value;
return getSearchSQL(column, key).replace('and ', '');
})
.join(' OR ')})`;
} else {
searchQuery = getSearchSQL(column);
}
}
return rawQuery( return rawQuery(
` `
select ${column} as value, count(*) select ${column} as value, count(*)
@ -77,6 +112,7 @@ async function clickhouseQuery(
startDate, startDate,
endDate, endDate,
search, search,
...params,
}, },
); );
} }