mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 23:27:12 +01:00
Updated filtering logic.
This commit is contained in:
parent
6ee9bb07da
commit
810b0639c8
14 changed files with 97 additions and 83 deletions
|
|
@ -3,9 +3,10 @@ import dateFormat from 'dateformat';
|
|||
import debug from 'debug';
|
||||
import { CLICKHOUSE } from 'lib/db';
|
||||
import { QueryFilters, QueryOptions } from './types';
|
||||
import { FILTER_COLUMNS, OPERATORS } from './constants';
|
||||
import { OPERATORS } from './constants';
|
||||
import { loadWebsite } from './load';
|
||||
import { maxDate } from './date';
|
||||
import { filtersToArray } from './params';
|
||||
|
||||
export const CLICKHOUSE_DATE_FORMATS = {
|
||||
minute: '%Y-%m-%d %H:%M:00',
|
||||
|
|
@ -79,15 +80,11 @@ function mapFilter(column: string, operator: string, name: string, type: string
|
|||
}
|
||||
|
||||
function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {}) {
|
||||
const query = Object.keys(filters).reduce((arr, key) => {
|
||||
const filter = filters[key];
|
||||
const operator = filter?.operator ?? OPERATORS.equals;
|
||||
const column = filter?.column ?? FILTER_COLUMNS[key] ?? options?.columns?.[key];
|
||||
const query = filtersToArray(filters, options).reduce((arr, { name, column, operator }) => {
|
||||
if (column) {
|
||||
arr.push(`and ${mapFilter(column, operator, name)}`);
|
||||
|
||||
if (filter !== undefined && column !== undefined) {
|
||||
arr.push(`and ${mapFilter(column, operator, key)}`);
|
||||
|
||||
if (key === 'referrer') {
|
||||
if (name === 'referrer') {
|
||||
arr.push('and referrer_domain != {websiteDomain:String}');
|
||||
}
|
||||
}
|
||||
|
|
@ -98,11 +95,11 @@ function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {})
|
|||
return query.join('\n');
|
||||
}
|
||||
|
||||
function normalizeFilters(filters = {}) {
|
||||
return Object.keys(filters).reduce((obj, key) => {
|
||||
const value = filters[key];
|
||||
|
||||
obj[key] = value?.value ?? value;
|
||||
function getFilterParams(filters: QueryFilters = {}) {
|
||||
return filtersToArray(filters).reduce((obj, { name, value }) => {
|
||||
if (name && value !== undefined) {
|
||||
obj[name] = value;
|
||||
}
|
||||
|
||||
return obj;
|
||||
}, {});
|
||||
|
|
@ -114,7 +111,7 @@ async function parseFilters(websiteId: string, filters: QueryFilters = {}, optio
|
|||
return {
|
||||
filterQuery: getFilterQuery(filters, options),
|
||||
params: {
|
||||
...normalizeFilters(filters),
|
||||
...getFilterParams(filters),
|
||||
websiteId,
|
||||
startDate: maxDate(filters.startDate, new Date(website?.resetAt)),
|
||||
websiteDomain: website.domain,
|
||||
|
|
|
|||
|
|
@ -1,15 +1,46 @@
|
|||
import { OPERATOR_PREFIXES, OPERATORS } from 'lib/constants';
|
||||
import { FILTER_COLUMNS, OPERATOR_PREFIXES, OPERATORS } from 'lib/constants';
|
||||
import { QueryFilters, QueryOptions } from 'lib/types';
|
||||
|
||||
export function parseParameterValue(param: string) {
|
||||
const [, prefix, value] = param.match(/^(!~|!|~)?(.*)$/);
|
||||
export function parseParameterValue(param: any) {
|
||||
if (typeof param === 'string') {
|
||||
const [, prefix, value] = param.match(/^(!~|!|~)?(.*)$/);
|
||||
|
||||
const operator =
|
||||
Object.keys(OPERATOR_PREFIXES).find(key => OPERATOR_PREFIXES[key] === prefix) ||
|
||||
OPERATORS.equals;
|
||||
const operator =
|
||||
Object.keys(OPERATOR_PREFIXES).find(key => OPERATOR_PREFIXES[key] === prefix) ||
|
||||
OPERATORS.equals;
|
||||
|
||||
return { operator, value };
|
||||
return { operator, value };
|
||||
}
|
||||
return { operator: OPERATORS.equals, value: param };
|
||||
}
|
||||
|
||||
export function operatorEquals(operator: any) {
|
||||
export function isEqualsOperator(operator: any) {
|
||||
return [OPERATORS.equals, OPERATORS.notEquals].includes(operator);
|
||||
}
|
||||
|
||||
export function isSearchOperator(operator: any) {
|
||||
return [OPERATORS.contains, OPERATORS.doesNotContain].includes(operator);
|
||||
}
|
||||
|
||||
export function filtersToArray(filters: QueryFilters = {}, options: QueryOptions = {}) {
|
||||
return Object.keys(filters).reduce((arr, key) => {
|
||||
const filter = filters[key];
|
||||
|
||||
if (filter === undefined || filter === null) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
if (filter?.name && filter?.value !== undefined) {
|
||||
return arr.concat(filter);
|
||||
}
|
||||
|
||||
const { operator, value } = parseParameterValue(filter);
|
||||
|
||||
return arr.concat({
|
||||
name: key,
|
||||
column: options?.columns?.[key] ?? FILTER_COLUMNS[key],
|
||||
operator,
|
||||
value,
|
||||
});
|
||||
}, []);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,11 @@ import { Prisma } from '@prisma/client';
|
|||
import prisma from '@umami/prisma-client';
|
||||
import moment from 'moment-timezone';
|
||||
import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db';
|
||||
import { FILTER_COLUMNS, SESSION_COLUMNS, OPERATORS, DEFAULT_PAGE_SIZE } from './constants';
|
||||
import { SESSION_COLUMNS, OPERATORS, DEFAULT_PAGE_SIZE } from './constants';
|
||||
import { loadWebsite } from './load';
|
||||
import { maxDate } from './date';
|
||||
import { QueryFilters, QueryOptions, SearchFilter } from './types';
|
||||
import { filtersToArray } from './params';
|
||||
|
||||
const MYSQL_DATE_FORMATS = {
|
||||
minute: '%Y-%m-%d %H:%i:00',
|
||||
|
|
@ -112,15 +113,11 @@ function mapFilter(column: string, operator: string, name: string, type: string
|
|||
}
|
||||
|
||||
function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {}): string {
|
||||
const query = Object.keys(filters).reduce((arr, key) => {
|
||||
const filter = filters[key];
|
||||
const operator = filter?.operator ?? OPERATORS.equals;
|
||||
const column = filter?.column ?? FILTER_COLUMNS[key] ?? options?.columns?.[key];
|
||||
const query = filtersToArray(filters, options).reduce((arr, { name, column, operator }) => {
|
||||
if (column) {
|
||||
arr.push(`and ${mapFilter(column, operator, name)}`);
|
||||
|
||||
if (filter !== undefined && column !== undefined) {
|
||||
arr.push(`and ${mapFilter(column, operator, key)}`);
|
||||
|
||||
if (key === 'referrer') {
|
||||
if (name === 'referrer') {
|
||||
arr.push(
|
||||
'and (website_event.referrer_domain != {{websiteDomain}} or website_event.referrer_domain is null)',
|
||||
);
|
||||
|
|
@ -133,12 +130,9 @@ function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {}):
|
|||
return query.join('\n');
|
||||
}
|
||||
|
||||
function normalizeFilters(filters = {}) {
|
||||
return Object.keys(filters).reduce((obj, key) => {
|
||||
const filter = filters[key];
|
||||
const value = filter?.value ?? filter;
|
||||
|
||||
obj[key] = [OPERATORS.contains, OPERATORS.doesNotContain].includes(filter?.operator)
|
||||
function getFilterParams(filters: QueryFilters = {}) {
|
||||
return filtersToArray(filters).reduce((obj, { name, operator, value }) => {
|
||||
obj[name] = [OPERATORS.contains, OPERATORS.doesNotContain].includes(operator)
|
||||
? `%${value}%`
|
||||
: value;
|
||||
|
||||
|
|
@ -152,15 +146,16 @@ async function parseFilters(
|
|||
options: QueryOptions = {},
|
||||
) {
|
||||
const website = await loadWebsite(websiteId);
|
||||
const joinSession = Object.keys(filters).find(key => SESSION_COLUMNS.includes(key));
|
||||
|
||||
return {
|
||||
joinSession:
|
||||
options?.joinSession || Object.keys(filters).find(key => SESSION_COLUMNS.includes(key))
|
||||
options?.joinSession || joinSession
|
||||
? `inner join session on website_event.session_id = session.session_id`
|
||||
: '',
|
||||
filterQuery: getFilterQuery(filters, options),
|
||||
params: {
|
||||
...normalizeFilters(filters),
|
||||
...getFilterParams(filters),
|
||||
websiteId,
|
||||
startDate: maxDate(filters.startDate, website?.resetAt),
|
||||
websiteDomain: website.domain,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { NextApiRequest } from 'next';
|
||||
import { getAllowedUnits, getMinimumUnit } from './date';
|
||||
import { getWebsiteDateRange } from '../queries';
|
||||
import { FILTER_COLUMNS, OPERATORS, OPERATOR_PREFIXES } from 'lib/constants';
|
||||
import { FILTER_COLUMNS } from 'lib/constants';
|
||||
|
||||
export async function parseDateRangeQuery(req: NextApiRequest) {
|
||||
export async function getRequestDateRange(req: NextApiRequest) {
|
||||
const { websiteId, startAt, endAt, unit } = req.query;
|
||||
|
||||
// All-time
|
||||
|
|
@ -31,27 +31,14 @@ export async function parseDateRangeQuery(req: NextApiRequest) {
|
|||
};
|
||||
}
|
||||
|
||||
export function getQueryFilters(req: NextApiRequest) {
|
||||
export function getRequestFilters(req: NextApiRequest) {
|
||||
return Object.keys(FILTER_COLUMNS).reduce((obj, key) => {
|
||||
const value = req.query[key];
|
||||
|
||||
if (value) {
|
||||
if (value !== undefined) {
|
||||
obj[key] = value;
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
const [, prefix, paramValue] = value.match(/^(!~|!|~)?(.*)$/);
|
||||
|
||||
if (prefix && paramValue) {
|
||||
obj[key] = {
|
||||
name: key,
|
||||
column: FILTER_COLUMNS[key],
|
||||
operator: OPERATOR_PREFIXES[prefix] || OPERATORS.equals,
|
||||
value: paramValue,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}, {});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue