mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Initial Typescript models.
This commit is contained in:
parent
04e9f06e93
commit
0aaba8cbd1
74 changed files with 1144 additions and 768 deletions
|
|
@ -2,11 +2,12 @@ import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export async function createSession(...args) {
|
||||
export async function createSession(args: Prisma.SessionCreateInput) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[PRISMA]: () => relationalQuery(args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||
}).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeSession(data);
|
||||
|
|
@ -16,11 +17,21 @@ export async function createSession(...args) {
|
|||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(data) {
|
||||
async function relationalQuery(data: Prisma.SessionCreateInput) {
|
||||
return prisma.client.session.create({ data });
|
||||
}
|
||||
|
||||
async function clickhouseQuery(data) {
|
||||
async function clickhouseQuery(data: {
|
||||
id: string;
|
||||
websiteId: string;
|
||||
hostname?: string;
|
||||
browser?: string;
|
||||
os?: string;
|
||||
device?: string;
|
||||
screen?: string;
|
||||
language?: string;
|
||||
country?: string;
|
||||
}) {
|
||||
const { id, websiteId, hostname, browser, os, device, screen, language, country } = data;
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
|
|
@ -1,21 +1,22 @@
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export async function getSession(...args) {
|
||||
export async function getSession(args: { id: string }) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[PRISMA]: () => relationalQuery(args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(where) {
|
||||
async function relationalQuery(where: Prisma.SessionWhereUniqueInput) {
|
||||
return prisma.client.session.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery({ id: sessionId }) {
|
||||
async function clickhouseQuery({ id: sessionId }: { id: string }) {
|
||||
const { rawQuery, findFirst } = clickhouse;
|
||||
const params = [sessionId];
|
||||
|
||||
|
|
@ -3,17 +3,26 @@ import clickhouse from 'lib/clickhouse';
|
|||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getSessionMetrics(...args) {
|
||||
export async function getSessionMetrics(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; field: string; filters: object },
|
||||
]
|
||||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId, { startDate, endDate, field, filters = {} }) {
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; field: string; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, field, filters = {} } = data;
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(null, filters, params);
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${field} x, count(*) y
|
||||
|
|
@ -26,8 +35,7 @@ async function relationalQuery(websiteId, { startDate, endDate, field, filters =
|
|||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
${filterQuery}
|
||||
)
|
||||
group by 1
|
||||
order by 2 desc`,
|
||||
|
|
@ -35,11 +43,15 @@ async function relationalQuery(websiteId, { startDate, endDate, field, filters =
|
|||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId, { startDate, endDate, field, filters = {} }) {
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; field: string; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, field, filters = {} } = data;
|
||||
const { parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { pageviewQuery, sessionQuery } = parseFilters(null, filters, params);
|
||||
const { filterQuery } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${field} x, count(*) y
|
||||
|
|
@ -48,8 +60,7 @@ async function clickhouseQuery(websiteId, { startDate, endDate, field, filters =
|
|||
and rev_id = $2
|
||||
and event_name = ''
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
${filterQuery}
|
||||
group by x
|
||||
order by y desc`,
|
||||
params,
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, PRISMA, CLICKHOUSE } from 'lib/db';
|
||||
|
||||
export async function getSessions(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websites, start_at) {
|
||||
return prisma.client.session.findMany({
|
||||
where: {
|
||||
...(websites && websites.length > 0
|
||||
? {
|
||||
websiteId: {
|
||||
in: websites,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
createdAt: {
|
||||
gte: start_at,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, start_at) {
|
||||
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select distinct
|
||||
session_id,
|
||||
website_id,
|
||||
created_at,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
device,
|
||||
screen,
|
||||
language,
|
||||
country
|
||||
from event
|
||||
where ${
|
||||
websites && websites.length > 0
|
||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||
: '0 = 0'
|
||||
}
|
||||
and created_at >= ${getDateFormat(start_at)}`,
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue