mirror of
https://github.com/umami-software/umami.git
synced 2026-02-06 05:37:20 +01:00
Updated segment handling.
This commit is contained in:
parent
554c627a58
commit
fba7e12c36
13 changed files with 59 additions and 90 deletions
|
|
@ -87,21 +87,6 @@ function mapFilter(column: string, operator: string, name: string, type: string
|
|||
}
|
||||
}
|
||||
|
||||
function mapCohortFilter(column: string, operator: string, value: string) {
|
||||
switch (operator) {
|
||||
case OPERATORS.equals:
|
||||
return `${column} = '${value}'`;
|
||||
case OPERATORS.notEquals:
|
||||
return `${column} != '${value}'`;
|
||||
case OPERATORS.contains:
|
||||
return `positionCaseInsensitive(${column}, '${value}') > 0`;
|
||||
case OPERATORS.doesNotContain:
|
||||
return `positionCaseInsensitive(${column}, '${value}') = 0`;
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}) {
|
||||
const query = filtersToArray(filters, options).reduce((arr, { name, column, operator }) => {
|
||||
if (column) {
|
||||
|
|
@ -118,40 +103,22 @@ function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}
|
|||
return query.join('\n');
|
||||
}
|
||||
|
||||
function getCohortQuery(websiteId: string, filters: QueryFilters = {}, options: QueryOptions = {}) {
|
||||
const query = filtersToArray(filters, options).reduce(
|
||||
(arr, { name, column, operator, value }) => {
|
||||
if (column) {
|
||||
arr.push(
|
||||
`${arr.length === 0 ? 'where' : 'and'} ${mapCohortFilter(column, operator, value)}`,
|
||||
);
|
||||
|
||||
if (name === 'referrer') {
|
||||
arr.push(`and referrer_domain != hostname`);
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
if (query.length > 0) {
|
||||
// add website and date range filters
|
||||
query.push(`and website_id = '${websiteId}'`);
|
||||
query.push(
|
||||
`and created_at between parseDateTimeBestEffort('${filters.startDate}') and parseDateTimeBestEffort('${filters.endDate}')`,
|
||||
);
|
||||
|
||||
return `join
|
||||
(select distinct session_id
|
||||
from website_event
|
||||
${query.join('\n')}) cohort
|
||||
on cohort.session_id = website_event.session_id
|
||||
`;
|
||||
function getCohortQuery(filters: Record<string, any>, options: QueryOptions = {}) {
|
||||
if (!filters) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '';
|
||||
const filterQuery = getFilterQuery(filters, options);
|
||||
|
||||
return `join (
|
||||
select distinct session_id
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
${filterQuery}
|
||||
) as cohort
|
||||
on cohort.session_id = website_event.session_id
|
||||
`;
|
||||
}
|
||||
|
||||
function getDateQuery(filters: Record<string, any>) {
|
||||
|
|
@ -192,7 +159,7 @@ function parseFilters(filters: Record<string, any>, options?: QueryOptions) {
|
|||
filterQuery: getFilterQuery(filters, options),
|
||||
dateQuery: getDateQuery(filters),
|
||||
queryParams: getQueryParams(filters),
|
||||
cohortQuery: getCohortQuery(filters?.cohort),
|
||||
cohortQuery: getCohortQuery(filters?.cohortFilters, options),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ export function parseParameterValue(param: any) {
|
|||
if (typeof param === 'string') {
|
||||
const [, operator, value] = param.match(/^([a-z]+)\.(.*)/) || [];
|
||||
|
||||
return { operator, value };
|
||||
return { operator: operator || OPERATORS.equals, value: value || param };
|
||||
}
|
||||
return { operator: OPERATORS.equals, value: param };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -241,7 +241,7 @@ async function pagedQuery<T>(model: string, criteria: T, filters?: QueryFilters)
|
|||
return { data, count, page: +page, pageSize: size, orderBy, search };
|
||||
}
|
||||
|
||||
async function pagedRawQuery(
|
||||
async function rawPagedQuery(
|
||||
query: string,
|
||||
filters: QueryFilters,
|
||||
queryParams: Record<string, any>,
|
||||
|
|
@ -360,7 +360,7 @@ export default {
|
|||
getTimestampDiffSQL,
|
||||
getSearchSQL,
|
||||
pagedQuery,
|
||||
pagedRawQuery,
|
||||
pagedRawQuery: rawPagedQuery,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { z } from 'zod/v4';
|
||||
import { FILTER_COLUMNS, DEFAULT_PAGE_SIZE, FILTER_GROUPS } from '@/lib/constants';
|
||||
import { FILTER_COLUMNS, DEFAULT_PAGE_SIZE } from '@/lib/constants';
|
||||
import { badRequest, unauthorized } from '@/lib/response';
|
||||
import { getAllowedUnits, getMinimumUnit, maxDate } from '@/lib/date';
|
||||
import { checkAuth } from '@/lib/auth';
|
||||
|
|
@ -79,16 +79,6 @@ export function getRequestFilters(query: Record<string, any>) {
|
|||
return result;
|
||||
}
|
||||
|
||||
export async function getRequestSegments(websiteId: string, query: Record<string, any>) {
|
||||
for (const key of Object.keys(FILTER_GROUPS)) {
|
||||
const value = query[key];
|
||||
|
||||
if (value !== undefined) {
|
||||
return getWebsiteSegment(websiteId, key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function setWebsiteDate(websiteId: string, data: Record<string, any>) {
|
||||
const website = await fetchWebsite(websiteId);
|
||||
|
||||
|
|
@ -109,7 +99,13 @@ export async function getQueryFilters(
|
|||
if (websiteId) {
|
||||
await setWebsiteDate(websiteId, dateRange);
|
||||
|
||||
Object.assign(filters, await getRequestSegments(websiteId, params));
|
||||
if (params.segment) {
|
||||
Object.assign(filters, (await getWebsiteSegment(websiteId, params.segment))?.parameters);
|
||||
}
|
||||
|
||||
if (params.cohort) {
|
||||
filters.cohortFilters = (await getWebsiteSegment(websiteId, params.cohort))?.parameters;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@ export const filterParams = {
|
|||
hostname: z.string().optional(),
|
||||
language: z.string().optional(),
|
||||
event: z.string().optional(),
|
||||
segment: z.string().optional(),
|
||||
cohort: z.string().optional(),
|
||||
segment: z.string().uuid().optional(),
|
||||
cohort: z.string().uuid().optional(),
|
||||
eventType: z.coerce.number().int().positive().optional(),
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,14 @@ export interface QueryOptions {
|
|||
prefix?: string;
|
||||
}
|
||||
|
||||
export interface QueryFilters extends DateParams, FilterParams, SortParams, PageParams {}
|
||||
export interface QueryFilters
|
||||
extends DateParams,
|
||||
FilterParams,
|
||||
SortParams,
|
||||
PageParams,
|
||||
SegmentParams {
|
||||
cohortFilters?: QueryFilters;
|
||||
}
|
||||
|
||||
export interface DateParams {
|
||||
startDate?: Date;
|
||||
|
|
@ -86,6 +93,11 @@ export interface PageParams {
|
|||
pageSize?: number;
|
||||
}
|
||||
|
||||
export interface SegmentParams {
|
||||
segment?: string;
|
||||
cohort?: string;
|
||||
}
|
||||
|
||||
export interface PageResult<T> {
|
||||
data: T;
|
||||
count: number;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue