segments implementation and migration update. update getRequestFilters to include filter groups.

This commit is contained in:
Francis Cao 2025-06-13 07:34:54 -07:00
parent 1ccc8a1a86
commit f61421b742
16 changed files with 188 additions and 23 deletions

View file

@ -47,7 +47,7 @@ export const SESSION_COLUMNS = [
'host',
];
export const GROUP_FILTERS = ['segment', 'cohort'];
export const FILTER_GROUPS = ['segment', 'cohort'];
export const FILTER_COLUMNS = {
url: 'url_path',
@ -67,6 +67,7 @@ export const FILTER_COLUMNS = {
event: 'event_name',
tag: 'tag',
segment: 'segment',
cohort: 'cohort',
};
export const COLLECTION_TYPE = {

View file

@ -1,9 +1,9 @@
import { z, ZodSchema } from 'zod';
import { FILTER_COLUMNS } from '@/lib/constants';
import { FILTER_COLUMNS, FILTER_GROUPS } from '@/lib/constants';
import { badRequest, unauthorized } from '@/lib/response';
import { getAllowedUnits, getMinimumUnit } from '@/lib/date';
import { checkAuth } from '@/lib/auth';
import { getWebsiteDateRange } from '@/queries';
import { getWebsiteSegment, getWebsiteDateRange } from '@/queries';
export async function getJsonBody(request: Request) {
try {
@ -85,14 +85,21 @@ export async function getRequestDateRange(query: Record<string, any>) {
};
}
export function getRequestFilters(query: Record<string, any>) {
return Object.keys(FILTER_COLUMNS).reduce((obj, key) => {
export async function getRequestFilters(query: Record<string, any>, websiteId?: string) {
const result: Record<string, any> = {};
for (const key of Object.keys(FILTER_COLUMNS)) {
const value = query[key];
if (value !== undefined) {
obj[key] = value;
if (FILTER_GROUPS.includes(key)) {
const segment = await getWebsiteSegment(websiteId, value);
// merge filters into result
Object.assign(result, segment.filters);
} else {
result[key] = value;
}
}
}
return obj;
}, {});
return result;
}

View file

@ -17,6 +17,8 @@ export const filterParams = {
host: z.string().optional(),
language: z.string().optional(),
event: z.string().optional(),
segment: z.string().optional(),
cohort: z.string().optional(),
};
export const pagingParams = {