mirror of
https://github.com/umami-software/umami.git
synced 2026-02-07 14:17:13 +01:00
Updated session and events queries. Added sessions page.
This commit is contained in:
parent
082a751ffe
commit
db36c37d32
39 changed files with 376 additions and 180 deletions
|
|
@ -2,8 +2,8 @@ import { ClickHouseClient, createClient } from '@clickhouse/client';
|
|||
import dateFormat from 'dateformat';
|
||||
import debug from 'debug';
|
||||
import { CLICKHOUSE } from 'lib/db';
|
||||
import { QueryFilters, QueryOptions } from './types';
|
||||
import { OPERATORS } from './constants';
|
||||
import { PageParams, QueryFilters, QueryOptions } from './types';
|
||||
import { DEFAULT_PAGE_SIZE, OPERATORS } from './constants';
|
||||
import { fetchWebsite } from './load';
|
||||
import { maxDate } from './date';
|
||||
import { filtersToArray } from './params';
|
||||
|
|
@ -47,11 +47,11 @@ function getClient() {
|
|||
return client;
|
||||
}
|
||||
|
||||
function getDateStringQuery(data: any, unit: string | number) {
|
||||
function getDateStringSQL(data: any, unit: string | number) {
|
||||
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
|
||||
}
|
||||
|
||||
function getDateQuery(field: string, unit: string, timezone?: string) {
|
||||
function getDateSQL(field: string, unit: string, timezone?: string) {
|
||||
if (timezone) {
|
||||
return `date_trunc('${unit}', ${field}, '${timezone}')`;
|
||||
}
|
||||
|
|
@ -95,6 +95,20 @@ function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {})
|
|||
return query.join('\n');
|
||||
}
|
||||
|
||||
function getDateQuery(filters: QueryFilters = {}) {
|
||||
const { startDate, endDate } = filters;
|
||||
|
||||
if (startDate) {
|
||||
if (endDate) {
|
||||
return `and created_at between {startDate:DateTime64} and {endDate:DateTime64}`;
|
||||
} else {
|
||||
return `and created_at >= {startDate:DateTime64}`;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function getFilterParams(filters: QueryFilters = {}) {
|
||||
return filtersToArray(filters).reduce((obj, { name, value }) => {
|
||||
if (name && value !== undefined) {
|
||||
|
|
@ -110,6 +124,7 @@ async function parseFilters(websiteId: string, filters: QueryFilters = {}, optio
|
|||
|
||||
return {
|
||||
filterQuery: getFilterQuery(filters, options),
|
||||
dateQuery: getDateQuery(filters),
|
||||
params: {
|
||||
...getFilterParams(filters),
|
||||
websiteId,
|
||||
|
|
@ -119,6 +134,32 @@ async function parseFilters(websiteId: string, filters: QueryFilters = {}, optio
|
|||
};
|
||||
}
|
||||
|
||||
async function pagedQuery(
|
||||
query: string,
|
||||
queryParams: { [key: string]: any },
|
||||
pageParams: PageParams = {},
|
||||
) {
|
||||
const { page = 1, pageSize, orderBy, sortDescending = false } = pageParams;
|
||||
const size = +pageSize || DEFAULT_PAGE_SIZE;
|
||||
const offset = +size * (page - 1);
|
||||
const direction = sortDescending ? 'desc' : 'asc';
|
||||
|
||||
const statements = [
|
||||
orderBy && `order by ${orderBy} ${direction}`,
|
||||
+size > 0 && `limit ${+size} offset ${offset}`,
|
||||
]
|
||||
.filter(n => n)
|
||||
.join('\n');
|
||||
|
||||
const count = await rawQuery(`select count(*) as num from (${query}) t`, queryParams).then(
|
||||
res => res[0].num,
|
||||
);
|
||||
|
||||
const data = await rawQuery(`${query}${statements}`, queryParams);
|
||||
|
||||
return { data, count, page: +page, pageSize: size, orderBy };
|
||||
}
|
||||
|
||||
async function rawQuery<T = unknown>(
|
||||
query: string,
|
||||
params: Record<string, unknown> = {},
|
||||
|
|
@ -170,11 +211,12 @@ export default {
|
|||
client: clickhouse,
|
||||
log,
|
||||
connect,
|
||||
getDateStringQuery,
|
||||
getDateQuery,
|
||||
getDateStringSQL,
|
||||
getDateSQL,
|
||||
getDateFormat,
|
||||
getFilterQuery,
|
||||
parseFilters,
|
||||
pagedQuery,
|
||||
findUnique,
|
||||
findFirst,
|
||||
rawQuery,
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ function getCastColumnQuery(field: string, type: string): string {
|
|||
}
|
||||
}
|
||||
|
||||
function getDateQuery(field: string, unit: string, timezone?: string): string {
|
||||
function getDateSQL(field: string, unit: string, timezone?: string): string {
|
||||
const db = getDatabaseType();
|
||||
|
||||
if (db === POSTGRESQL) {
|
||||
|
|
@ -81,7 +81,19 @@ function getDateQuery(field: string, unit: string, timezone?: string): string {
|
|||
}
|
||||
}
|
||||
|
||||
function getTimestampDiffQuery(field1: string, field2: string): string {
|
||||
export function getTimestampSQL(field: string) {
|
||||
const db = getDatabaseType();
|
||||
|
||||
if (db === POSTGRESQL) {
|
||||
return `floor(extract(epoch from ${field}))`;
|
||||
}
|
||||
|
||||
if (db === MYSQL) {
|
||||
return `UNIX_TIMESTAMP(${field})`;
|
||||
}
|
||||
}
|
||||
|
||||
function getTimestampDiffSQL(field1: string, field2: string): string {
|
||||
const db = getDatabaseType();
|
||||
|
||||
if (db === POSTGRESQL) {
|
||||
|
|
@ -93,7 +105,7 @@ function getTimestampDiffQuery(field1: string, field2: string): string {
|
|||
}
|
||||
}
|
||||
|
||||
function getSearchQuery(column: string): string {
|
||||
function getSearchSQL(column: string): string {
|
||||
const db = getDatabaseType();
|
||||
const like = db === POSTGRESQL ? 'ilike' : 'like';
|
||||
|
||||
|
|
@ -137,6 +149,20 @@ function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {}):
|
|||
return query.join('\n');
|
||||
}
|
||||
|
||||
function getDateQuery(filters: QueryFilters = {}) {
|
||||
const { startDate, endDate } = filters;
|
||||
|
||||
if (startDate) {
|
||||
if (endDate) {
|
||||
return `and website_event.created_at between {{startDate}} and {{endDate}}`;
|
||||
} else {
|
||||
return `and website_event.created_at >= {{startDate}}`;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function getFilterParams(filters: QueryFilters = {}) {
|
||||
return filtersToArray(filters).reduce((obj, { name, operator, value }) => {
|
||||
obj[name] = [OPERATORS.contains, OPERATORS.doesNotContain].includes(operator)
|
||||
|
|
@ -161,6 +187,7 @@ async function parseFilters(
|
|||
? `inner join session on website_event.session_id = session.session_id`
|
||||
: '',
|
||||
filterQuery: getFilterQuery(filters, options),
|
||||
dateQuery: getDateQuery(filters),
|
||||
params: {
|
||||
...getFilterParams(filters),
|
||||
websiteId,
|
||||
|
|
@ -191,8 +218,8 @@ async function rawQuery(sql: string, data: object): Promise<any> {
|
|||
return prisma.rawQuery(query, params);
|
||||
}
|
||||
|
||||
async function pagedQuery<T>(model: string, criteria: T, filters: PageParams) {
|
||||
const { page = 1, pageSize, orderBy, sortDescending = false } = filters || {};
|
||||
async function pagedQuery<T>(model: string, criteria: T, pageParams: PageParams) {
|
||||
const { page = 1, pageSize, orderBy, sortDescending = false } = pageParams || {};
|
||||
const size = +pageSize || DEFAULT_PAGE_SIZE;
|
||||
|
||||
const data = await prisma.client[model].findMany({
|
||||
|
|
@ -256,11 +283,11 @@ export default {
|
|||
getAddIntervalQuery,
|
||||
getCastColumnQuery,
|
||||
getDayDiffQuery,
|
||||
getDateQuery,
|
||||
getDateSQL,
|
||||
getFilterQuery,
|
||||
getSearchParameters,
|
||||
getTimestampDiffQuery,
|
||||
getSearchQuery,
|
||||
getTimestampDiffSQL,
|
||||
getSearchSQL,
|
||||
getQueryMode,
|
||||
pagedQuery,
|
||||
parseFilters,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue