Add Search Api/Components.

This commit is contained in:
Brian Cao 2023-08-10 13:26:33 -07:00
parent 45888fabe6
commit dcf8b2edaa
37 changed files with 1069 additions and 287 deletions

View file

@ -30,6 +30,22 @@ export const FILTER_RANGE = 'filter-range';
export const FILTER_REFERRERS = 'filter-referrers';
export const FILTER_PAGES = 'filter-pages';
export const USER_FILTER_TYPES = {
all: 'All',
username: 'Username',
} as const;
export const WEBSITE_FILTER_TYPES = { all: 'All', name: 'Name', domain: 'Domain' } as const;
export const TEAM_FILTER_TYPES = { all: 'All', name: 'Name', 'user:username': 'Owner' } as const;
export const REPORT_FILTER_TYPES = {
all: 'All',
name: 'Name',
description: 'Description',
type: 'Type',
'user:username': 'Username',
'website:name': 'Website Name',
'website:domain': 'Website Domain',
} as const;
export const EVENT_COLUMNS = ['url', 'referrer', 'title', 'query', 'event'];
export const SESSION_COLUMNS = [

View file

@ -4,7 +4,7 @@ import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db';
import { FILTER_COLUMNS, SESSION_COLUMNS } from './constants';
import { loadWebsite } from './load';
import { maxDate } from './date';
import { QueryFilters, QueryOptions } from './types';
import { QueryFilters, QueryOptions, SearchFilter } from './types';
const MYSQL_DATE_FORMATS = {
minute: '%Y-%m-%d %H:%i:00',
@ -128,6 +128,37 @@ async function rawQuery(sql: string, data: object): Promise<any> {
return prisma.rawQuery(query, params);
}
function getPageFilters(filters: SearchFilter<any>): [
{
orderBy: {
[x: string]: string;
}[];
take: number;
skip: number;
},
{
pageSize: number;
page: number;
orderBy: string;
},
] {
const { pageSize = 10, page = 1, orderBy } = filters;
return [
{
...(pageSize > 0 && { take: pageSize, skip: pageSize * (page - 1) }),
...(orderBy && {
orderBy: [
{
[orderBy]: 'asc',
},
],
}),
},
{ pageSize, page: +page, orderBy },
];
}
export default {
...prisma,
getAddMinutesQuery,
@ -135,5 +166,6 @@ export default {
getTimestampIntervalQuery,
getFilterQuery,
parseFilters,
getPageFilters,
rawQuery,
};

View file

@ -1,17 +1,62 @@
import { NextApiRequest } from 'next';
import { COLLECTION_TYPE, DATA_TYPE, EVENT_TYPE, KAFKA_TOPIC, ROLES } from './constants';
import {
COLLECTION_TYPE,
DATA_TYPE,
EVENT_TYPE,
KAFKA_TOPIC,
REPORT_FILTER_TYPES,
ROLES,
TEAM_FILTER_TYPES,
USER_FILTER_TYPES,
WEBSITE_FILTER_TYPES,
} from './constants';
type ObjectValues<T> = T[keyof T];
export type CollectionType = ObjectValues<typeof COLLECTION_TYPE>;
export type Role = ObjectValues<typeof ROLES>;
export type EventType = ObjectValues<typeof EVENT_TYPE>;
export type DynamicDataType = ObjectValues<typeof DATA_TYPE>;
export type KafkaTopic = ObjectValues<typeof KAFKA_TOPIC>;
export type ReportSearchFilterType = ObjectValues<typeof REPORT_FILTER_TYPES>;
export type UserSearchFilterType = ObjectValues<typeof USER_FILTER_TYPES>;
export type WebsiteSearchFilterType = ObjectValues<typeof WEBSITE_FILTER_TYPES>;
export type TeamSearchFilterType = ObjectValues<typeof TEAM_FILTER_TYPES>;
export interface WebsiteSearchFilter extends SearchFilter<WebsiteSearchFilterType> {
userId?: string;
teamId?: string;
includeTeams?: boolean;
}
export interface UserSearchFilter extends SearchFilter<UserSearchFilterType> {
teamId?: string;
}
export interface TeamSearchFilter extends SearchFilter<TeamSearchFilterType> {
userId?: string;
}
export interface ReportSearchFilter extends SearchFilter<ReportSearchFilterType> {
userId?: string;
websiteId?: string;
}
export interface SearchFilter<T> {
filter?: string;
filterType?: T;
pageSize?: number;
page?: number;
orderBy?: string;
}
export interface FilterResult<T> {
data: T;
count: number;
pageSize: number;
page: number;
orderBy?: string;
}
export interface DynamicData {
[key: string]: number | string | DynamicData | number[] | string[] | DynamicData[];