Skip to content

Commit

Permalink
Allow filter search for country and region.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Nov 28, 2024
1 parent 2ed7628 commit a18d1a9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
15 changes: 9 additions & 6 deletions src/components/hooks/queries/useWebsiteValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ export function useWebsiteValues({
const getSearch = (type: string, value: string) => {
if (value) {
const values = names[type];
return Object.keys(values).reduce((code: string, key: string) => {
if (!code && values[key].toLowerCase().includes(value.toLowerCase())) {
code = key;
}
return code;
}, '');
return Object.keys(values)
.reduce((arr: string[], key: string) => {
if (values[key].toLowerCase().includes(value.toLowerCase())) {
return arr.concat(key);
}
return arr;
}, [])
.slice(0, 5)
.join(',');
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/lib/clickhouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ function getDateSQL(field: string, unit: string, timezone?: string) {
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') {
const value = `{${name}:${type}}`;

Expand Down Expand Up @@ -229,6 +233,7 @@ export default {
connect,
getDateStringSQL,
getDateSQL,
getSearchSQL,
getFilterQuery,
getUTCString,
parseFilters,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 = '') {
Expand Down
8 changes: 1 addition & 7 deletions src/pages/api/websites/[websiteId]/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
return unauthorized(res);
}

const values = await getValues(
websiteId,
FILTER_COLUMNS[type as string],
startDate,
endDate,
search,
);
const values = await getValues(websiteId, FILTER_COLUMNS[type], startDate, endDate, search);

return ok(
res,
Expand Down
40 changes: 38 additions & 2 deletions src/queries/analytics/getValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ async function relationalQuery(
search: string,
) {
const { rawQuery, getSearchSQL } = prisma;
const params = {};
let searchQuery = '';

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(
Expand All @@ -43,6 +58,7 @@ async function relationalQuery(
startDate,
endDate,
search: `%${search}%`,
...params,
},
);
}
Expand All @@ -54,13 +70,32 @@ async function clickhouseQuery(
endDate: Date,
search: string,
) {
const { rawQuery } = clickhouse;
const { rawQuery, getSearchSQL } = clickhouse;
const params = {};
let searchQuery = '';

if (search) {
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(
`
select ${column} as value, count(*)
Expand All @@ -77,6 +112,7 @@ async function clickhouseQuery(
startDate,
endDate,
search,
...params,
},
);
}

0 comments on commit a18d1a9

Please sign in to comment.