mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
translate dateRange and cohort prefix for filtering
This commit is contained in:
parent
65024d4bf7
commit
b2e03390da
8 changed files with 68 additions and 60 deletions
|
|
@ -15,7 +15,7 @@ export function CohortAddButton({ websiteId }: { websiteId: string }) {
|
||||||
<Text>{formatMessage(labels.cohort)}</Text>
|
<Text>{formatMessage(labels.cohort)}</Text>
|
||||||
</Button>
|
</Button>
|
||||||
<Modal>
|
<Modal>
|
||||||
<Dialog title={formatMessage(labels.cohort)} style={{ width: 800 }}>
|
<Dialog title={formatMessage(labels.cohort)} style={{ width: 800, maxHeight: '90vh' }}>
|
||||||
{({ close }) => {
|
{({ close }) => {
|
||||||
return <CohortEditForm websiteId={websiteId} onClose={close} />;
|
return <CohortEditForm websiteId={websiteId} onClose={close} />;
|
||||||
}}
|
}}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ export function CohortEditButton({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ActionButton title={formatMessage(labels.edit)} icon={<Edit />}>
|
<ActionButton title={formatMessage(labels.edit)} icon={<Edit />}>
|
||||||
<Dialog title={formatMessage(labels.cohort)} style={{ width: 800 }}>
|
<Dialog title={formatMessage(labels.cohort)} style={{ width: 800, maxHeight: '90vh' }}>
|
||||||
{({ close }) => {
|
{({ close }) => {
|
||||||
return (
|
return (
|
||||||
<CohortEditForm
|
<CohortEditForm
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ export function SegmentAddButton({ websiteId }: { websiteId: string }) {
|
||||||
<Text>{formatMessage(labels.segment)}</Text>
|
<Text>{formatMessage(labels.segment)}</Text>
|
||||||
</Button>
|
</Button>
|
||||||
<Modal>
|
<Modal>
|
||||||
<Dialog title={formatMessage(labels.segment)} style={{ width: 800 }}>
|
<Dialog title={formatMessage(labels.segment)} style={{ width: 800, maxHeight: '90vh' }}>
|
||||||
{({ close }) => {
|
{({ close }) => {
|
||||||
return <SegmentEditForm websiteId={websiteId} onClose={close} />;
|
return <SegmentEditForm websiteId={websiteId} onClose={close} />;
|
||||||
}}
|
}}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ export function SegmentEditButton({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ActionButton title={formatMessage(labels.edit)} icon={<Edit />}>
|
<ActionButton title={formatMessage(labels.edit)} icon={<Edit />}>
|
||||||
<Dialog title={formatMessage(labels.segment)} style={{ width: 800 }}>
|
<Dialog title={formatMessage(labels.segment)} style={{ width: 800, maxHeight: '90vh' }}>
|
||||||
{({ close }) => {
|
{({ close }) => {
|
||||||
return (
|
return (
|
||||||
<SegmentEditForm
|
<SegmentEditForm
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { ClickHouseClient, createClient } from '@clickhouse/client';
|
||||||
import { formatInTimeZone } from 'date-fns-tz';
|
import { formatInTimeZone } from 'date-fns-tz';
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
import { CLICKHOUSE } from '@/lib/db';
|
import { CLICKHOUSE } from '@/lib/db';
|
||||||
import { DEFAULT_PAGE_SIZE, OPERATORS } from './constants';
|
import { DEFAULT_PAGE_SIZE, FILTER_COLUMNS, OPERATORS } from './constants';
|
||||||
import { filtersObjectToArray } from './params';
|
import { filtersObjectToArray } from './params';
|
||||||
import { QueryFilters, QueryOptions } from './types';
|
import { QueryFilters, QueryOptions } from './types';
|
||||||
|
|
||||||
|
|
@ -89,6 +89,12 @@ function mapFilter(column: string, operator: string, name: string, type: string
|
||||||
|
|
||||||
function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}) {
|
function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}) {
|
||||||
const query = filtersObjectToArray(filters, options).reduce((arr, { name, column, operator }) => {
|
const query = filtersObjectToArray(filters, options).reduce((arr, { name, column, operator }) => {
|
||||||
|
const isCohort = options?.isCohort;
|
||||||
|
|
||||||
|
if (isCohort) {
|
||||||
|
column = FILTER_COLUMNS[name.slice('cohort_'.length)];
|
||||||
|
}
|
||||||
|
|
||||||
if (column) {
|
if (column) {
|
||||||
if (name === 'eventType') {
|
if (name === 'eventType') {
|
||||||
arr.push(`and ${mapFilter(column, operator, name, 'UInt32')}`);
|
arr.push(`and ${mapFilter(column, operator, name, 'UInt32')}`);
|
||||||
|
|
@ -107,18 +113,18 @@ function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}
|
||||||
return query.join('\n');
|
return query.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCohortQuery(filters: Record<string, any>, options: QueryOptions = {}) {
|
function getCohortQuery(filters: Record<string, any>) {
|
||||||
if (!filters) {
|
if (!filters || Object.keys(filters).length === 0) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const filterQuery = getFilterQuery(filters, options);
|
const filterQuery = getFilterQuery(filters, { isCohort: true });
|
||||||
|
|
||||||
return `join (
|
return `join (
|
||||||
select distinct session_id
|
select distinct session_id
|
||||||
from website_event
|
from website_event
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {cohort_startDate:DateTime64} and {cohort_endDate:DateTime64}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
) as cohort
|
) as cohort
|
||||||
on cohort.session_id = website_event.session_id
|
on cohort.session_id = website_event.session_id
|
||||||
|
|
@ -159,11 +165,15 @@ function getQueryParams(filters: Record<string, any>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseFilters(filters: Record<string, any>, options?: QueryOptions) {
|
function parseFilters(filters: Record<string, any>, options?: QueryOptions) {
|
||||||
|
const cohortFilters = Object.fromEntries(
|
||||||
|
Object.entries(filters).filter(([key]) => key.startsWith('cohort_')),
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
filterQuery: getFilterQuery(filters, options),
|
filterQuery: getFilterQuery(filters, options),
|
||||||
dateQuery: getDateQuery(filters),
|
dateQuery: getDateQuery(filters),
|
||||||
queryParams: getQueryParams(filters),
|
queryParams: getQueryParams(filters),
|
||||||
cohortQuery: getCohortQuery(filters?.cohortFilters, options),
|
cohortQuery: getCohortQuery(cohortFilters),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import debug from 'debug';
|
||||||
import { PrismaClient } from '@/generated/prisma/client';
|
import { PrismaClient } from '@/generated/prisma/client';
|
||||||
import { PrismaPg } from '@prisma/adapter-pg';
|
import { PrismaPg } from '@prisma/adapter-pg';
|
||||||
import { readReplicas } from '@prisma/extension-read-replicas';
|
import { readReplicas } from '@prisma/extension-read-replicas';
|
||||||
import { SESSION_COLUMNS, OPERATORS, DEFAULT_PAGE_SIZE } from './constants';
|
import { SESSION_COLUMNS, OPERATORS, DEFAULT_PAGE_SIZE, FILTER_COLUMNS } from './constants';
|
||||||
import { QueryOptions, QueryFilters } from './types';
|
import { QueryOptions, QueryFilters } from './types';
|
||||||
import { filtersObjectToArray } from './params';
|
import { filtersObjectToArray } from './params';
|
||||||
|
|
||||||
|
|
@ -79,24 +79,15 @@ 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 `${column} ilike '${value}'`;
|
|
||||||
case OPERATORS.doesNotContain:
|
|
||||||
return `${column} not ilike '${value}'`;
|
|
||||||
default:
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}): string {
|
function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}): string {
|
||||||
const query = filtersObjectToArray(filters, options).reduce(
|
const query = filtersObjectToArray(filters, options).reduce(
|
||||||
(arr, { name, column, operator, prefix = '' }) => {
|
(arr, { name, column, operator, prefix = '' }) => {
|
||||||
|
const isCohort = options?.isCohort;
|
||||||
|
|
||||||
|
if (isCohort) {
|
||||||
|
column = FILTER_COLUMNS[name.slice('cohort_'.length)];
|
||||||
|
}
|
||||||
|
|
||||||
if (column) {
|
if (column) {
|
||||||
arr.push(`and ${mapFilter(`${prefix}${column}`, operator, name)}`);
|
arr.push(`and ${mapFilter(`${prefix}${column}`, operator, name)}`);
|
||||||
|
|
||||||
|
|
@ -115,43 +106,25 @@ function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}
|
||||||
return query.join('\n');
|
return query.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCohortQuery(websiteId: string, filters: QueryFilters = {}, options: QueryOptions = {}) {
|
function getCohortQuery(filters: QueryFilters = {}) {
|
||||||
const query = filtersObjectToArray(filters, options).reduce(
|
if (!filters || Object.keys(filters).length === 0) {
|
||||||
(arr, { name, column, operator, value }) => {
|
return '';
|
||||||
if (column) {
|
|
||||||
arr.push(
|
|
||||||
`${arr.length === 0 ? 'where' : 'and'} ${mapCohortFilter(column, operator, value)}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (name === 'referrer') {
|
|
||||||
arr.push(`and referrer_domain != hostname`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return arr;
|
const filterQuery = getFilterQuery(filters, { isCohort: true });
|
||||||
},
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (query.length > 0) {
|
|
||||||
// add website and date range filters
|
|
||||||
query.push(`and website_event.website_id = '${websiteId}'`);
|
|
||||||
query.push(
|
|
||||||
`and website_event.created_at between '${filters.startDate}'::timestamptz and '${filters.endDate}'::timestamptz`,
|
|
||||||
);
|
|
||||||
|
|
||||||
return `join
|
return `join
|
||||||
(select distinct website_event.session_id
|
(select distinct website_event.session_id
|
||||||
from website_event
|
from website_event
|
||||||
join session on session.session_id = website_event.session_id
|
join session on session.session_id = website_event.session_id
|
||||||
${query.join('\n')}) cohort
|
where website_event.website_id = {{websiteId}}
|
||||||
|
and website_event.created_at between {{cohort_startDate}} and {{cohort_endDate}}
|
||||||
|
${filterQuery}
|
||||||
|
) cohort
|
||||||
on cohort.session_id = website_event.session_id
|
on cohort.session_id = website_event.session_id
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDateQuery(filters: Record<string, any>) {
|
function getDateQuery(filters: Record<string, any>) {
|
||||||
const { startDate, endDate } = filters;
|
const { startDate, endDate } = filters;
|
||||||
|
|
||||||
|
|
@ -184,6 +157,10 @@ function parseFilters(filters: Record<string, any>, options?: QueryOptions) {
|
||||||
['referrer', ...SESSION_COLUMNS].includes(key),
|
['referrer', ...SESSION_COLUMNS].includes(key),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const cohortFilters = Object.fromEntries(
|
||||||
|
Object.entries(filters).filter(([key]) => key.startsWith('cohort_')),
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
joinSessionQuery:
|
joinSessionQuery:
|
||||||
options?.joinSession || joinSession
|
options?.joinSession || joinSession
|
||||||
|
|
@ -192,7 +169,7 @@ function parseFilters(filters: Record<string, any>, options?: QueryOptions) {
|
||||||
dateQuery: getDateQuery(filters),
|
dateQuery: getDateQuery(filters),
|
||||||
filterQuery: getFilterQuery(filters, options),
|
filterQuery: getFilterQuery(filters, options),
|
||||||
queryParams: getQueryParams(filters),
|
queryParams: getQueryParams(filters),
|
||||||
cohortQuery: getCohortQuery(filters?.cohort),
|
cohortQuery: getCohortQuery(cohortFilters),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
import { z } from 'zod/v4';
|
|
||||||
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';
|
import { checkAuth } from '@/lib/auth';
|
||||||
|
import { DEFAULT_PAGE_SIZE, FILTER_COLUMNS } from '@/lib/constants';
|
||||||
|
import { getAllowedUnits, getMinimumUnit, maxDate, parseDateRange } from '@/lib/date';
|
||||||
import { fetchWebsite } from '@/lib/load';
|
import { fetchWebsite } from '@/lib/load';
|
||||||
|
import { badRequest, unauthorized } from '@/lib/response';
|
||||||
import { QueryFilters } from '@/lib/types';
|
import { QueryFilters } from '@/lib/types';
|
||||||
import { getWebsiteSegment } from '@/queries';
|
import { getWebsiteSegment } from '@/queries';
|
||||||
|
import { z } from 'zod/v4';
|
||||||
|
|
||||||
export async function parseRequest(
|
export async function parseRequest(
|
||||||
request: Request,
|
request: Request,
|
||||||
|
|
@ -104,7 +104,27 @@ export async function getQueryFilters(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.cohort) {
|
if (params.cohort) {
|
||||||
filters.cohortFilters = (await getWebsiteSegment(websiteId, params.cohort))?.parameters;
|
const cohortFilters = (await getWebsiteSegment(websiteId, params.cohort))
|
||||||
|
?.parameters as Record<string, any>;
|
||||||
|
|
||||||
|
// convert dateRange to startDate and endDate
|
||||||
|
if (cohortFilters.dateRange) {
|
||||||
|
const { startDate, endDate } = parseDateRange(cohortFilters.dateRange);
|
||||||
|
cohortFilters.startDate = startDate;
|
||||||
|
cohortFilters.endDate = endDate;
|
||||||
|
delete cohortFilters.dateRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(
|
||||||
|
filters,
|
||||||
|
Object.fromEntries(
|
||||||
|
Object.entries(cohortFilters || {}).map(([key, value]) =>
|
||||||
|
key === 'startDate' || key === 'endDate'
|
||||||
|
? [`cohort_${key}`, new Date(value)]
|
||||||
|
: [`cohort_${key}`, value],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ export interface QueryOptions {
|
||||||
columns?: Record<string, string>;
|
columns?: Record<string, string>;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
prefix?: string;
|
prefix?: string;
|
||||||
|
isCohort?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueryFilters
|
export interface QueryFilters
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue