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

@ -68,6 +68,8 @@ export function MetricsTable({
country, country,
region, region,
city, city,
limit,
search,
}, },
{ retryDelay: delay || DEFAULT_ANIMATION_DURATION, onDataLoad }, { retryDelay: delay || DEFAULT_ANIMATION_DURATION, onDataLoad },
); );
@ -86,20 +88,8 @@ export function MetricsTable({
} }
} }
if (search) {
items = items.filter(({ x, ...data }) => {
const value = formatValue(x, type, data);
return value?.toLowerCase().includes(search.toLowerCase());
});
}
items = percentFilter(items); items = percentFilter(items);
if (limit) {
items = items.slice(0, limit - 1);
}
return items; return items;
} }
return []; return [];
@ -114,6 +104,7 @@ export function MetricsTable({
className={styles.search} className={styles.search}
value={search} value={search}
onSearch={setSearch} onSearch={setSearch}
delay={300}
autoFocus={true} autoFocus={true}
/> />
)} )}

View File

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

View File

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

View File

@ -26,6 +26,8 @@ export interface WebsiteMetricsRequestQuery {
language?: string; language?: string;
event?: string; event?: string;
limit?: number; limit?: number;
offset?: number;
search?: string;
} }
const schema = { const schema = {
@ -47,6 +49,8 @@ const schema = {
language: yup.string(), language: yup.string(),
event: yup.string(), event: yup.string(),
limit: yup.number(), limit: yup.number(),
offset: yup.number(),
search: yup.string(),
}), }),
}; };
@ -74,6 +78,8 @@ export default async (
language, language,
event, event,
limit, limit,
offset,
search,
} = req.query; } = req.query;
if (req.method === 'GET') { if (req.method === 'GET') {
@ -98,12 +104,19 @@ export default async (
city, city,
language, language,
event, event,
search,
}; };
const column = FILTER_COLUMNS[type] || type; const column = FILTER_COLUMNS[type] || type;
if (SESSION_COLUMNS.includes(type)) { if (SESSION_COLUMNS.includes(type)) {
const data = await getSessionMetrics(websiteId, column, filters, limit); const data = await getSessionMetrics(
websiteId,
column,
{ ...filters, search },
limit,
offset,
);
if (type === 'language') { if (type === 'language') {
const combined = {}; const combined = {};
@ -125,7 +138,13 @@ export default async (
} }
if (EVENT_COLUMNS.includes(type)) { if (EVENT_COLUMNS.includes(type)) {
const data = await getPageviewMetrics(websiteId, column, filters, limit); const data = await getPageviewMetrics(
websiteId,
column,
{ ...filters, search },
limit,
offset,
);
return ok(res, data); return ok(res, data);
} }

View File

@ -1,11 +1,17 @@
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse'; import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { EVENT_TYPE, SESSION_COLUMNS } from 'lib/constants'; import { EVENT_TYPE, SESSION_COLUMNS, OPERATORS } from 'lib/constants';
import { QueryFilters } from 'lib/types'; import { QueryFilters } from 'lib/types';
export async function getPageviewMetrics( export async function getPageviewMetrics(
...args: [websiteId: string, columns: string, filters: QueryFilters, limit?: number] ...args: [
websiteId: string,
column: string,
filters: QueryFilters,
limit?: number,
offset?: number,
]
) { ) {
return runQuery({ return runQuery({
[PRISMA]: () => relationalQuery(...args), [PRISMA]: () => relationalQuery(...args),
@ -18,6 +24,7 @@ async function relationalQuery(
column: string, column: string,
filters: QueryFilters, filters: QueryFilters,
limit: number = 500, limit: number = 500,
offset: number = 0,
) { ) {
const { rawQuery, parseFilters } = prisma; const { rawQuery, parseFilters } = prisma;
const { filterQuery, joinSession, params } = await parseFilters( const { filterQuery, joinSession, params } = await parseFilters(
@ -48,6 +55,7 @@ async function relationalQuery(
group by 1 group by 1
order by 2 desc order by 2 desc
limit ${limit} limit ${limit}
offset ${offset}
`, `,
params, params,
); );
@ -58,10 +66,19 @@ async function clickhouseQuery(
column: string, column: string,
filters: QueryFilters, filters: QueryFilters,
limit: number = 500, limit: number = 500,
offset: number = 0,
): Promise<{ x: string; y: number }[]> { ): Promise<{ x: string; y: number }[]> {
const { rawQuery, parseFilters } = clickhouse; const { rawQuery, parseFilters } = clickhouse;
const { filterQuery, params } = await parseFilters(websiteId, { const { filterQuery, params } = await parseFilters(websiteId, {
...filters, ...filters,
...(filters.search && {
[column]: {
value: filters.search,
filter: OPERATORS.contains,
column,
name: column,
},
}),
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView, eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
}); });
@ -82,6 +99,7 @@ async function clickhouseQuery(
group by x group by x
order by y desc order by y desc
limit ${limit} limit ${limit}
offset ${offset}
`, `,
params, params,
).then(a => { ).then(a => {

View File

@ -5,7 +5,13 @@ import { EVENT_TYPE, SESSION_COLUMNS } from 'lib/constants';
import { QueryFilters } from 'lib/types'; import { QueryFilters } from 'lib/types';
export async function getSessionMetrics( export async function getSessionMetrics(
...args: [websiteId: string, column: string, filters: QueryFilters, limit?: number] ...args: [
websiteId: string,
column: string,
filters: QueryFilters,
limit?: number,
offset?: number,
]
) { ) {
return runQuery({ return runQuery({
[PRISMA]: () => relationalQuery(...args), [PRISMA]: () => relationalQuery(...args),
@ -18,6 +24,7 @@ async function relationalQuery(
column: string, column: string,
filters: QueryFilters, filters: QueryFilters,
limit: number = 500, limit: number = 500,
offset: number = 0,
) { ) {
const { parseFilters, rawQuery } = prisma; const { parseFilters, rawQuery } = prisma;
const { filterQuery, joinSession, params } = await parseFilters( const { filterQuery, joinSession, params } = await parseFilters(
@ -47,7 +54,9 @@ async function relationalQuery(
group by 1 group by 1
${includeCountry ? ', 3' : ''} ${includeCountry ? ', 3' : ''}
order by 2 desc order by 2 desc
limit ${limit}`, limit ${limit}
offset ${offset}
`,
params, params,
); );
} }
@ -57,6 +66,7 @@ async function clickhouseQuery(
column: string, column: string,
filters: QueryFilters, filters: QueryFilters,
limit: number = 500, limit: number = 500,
offset: number = 0,
): Promise<{ x: string; y: number }[]> { ): Promise<{ x: string; y: number }[]> {
const { parseFilters, rawQuery } = clickhouse; const { parseFilters, rawQuery } = clickhouse;
const { filterQuery, params } = await parseFilters(websiteId, { const { filterQuery, params } = await parseFilters(websiteId, {
@ -80,6 +90,7 @@ async function clickhouseQuery(
${includeCountry ? ', country' : ''} ${includeCountry ? ', country' : ''}
order by y desc order by y desc
limit ${limit} limit ${limit}
offset ${offset}
`, `,
params, params,
).then(a => { ).then(a => {