mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-24 02:06:19 +01:00
Re-add realtime data
This commit is contained in:
parent
0aaba8cbd1
commit
f3726e5abf
@ -11,7 +11,6 @@ model User {
|
|||||||
id String @id @unique @map("user_id") @db.Uuid
|
id String @id @unique @map("user_id") @db.Uuid
|
||||||
username String @unique @db.VarChar(255)
|
username String @unique @db.VarChar(255)
|
||||||
password String @db.VarChar(60)
|
password String @db.VarChar(60)
|
||||||
isAdmin Boolean @default(false) @map("is_admin")
|
|
||||||
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
|
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
|
||||||
|
|
||||||
groupRole GroupRole[]
|
groupRole GroupRole[]
|
||||||
@ -20,7 +19,6 @@ model User {
|
|||||||
teamWebsite TeamWebsite[]
|
teamWebsite TeamWebsite[]
|
||||||
teamUser TeamUser[]
|
teamUser TeamUser[]
|
||||||
userWebsite UserWebsite[]
|
userWebsite UserWebsite[]
|
||||||
website Website[]
|
|
||||||
|
|
||||||
@@map("user")
|
@@map("user")
|
||||||
}
|
}
|
||||||
@ -44,7 +42,6 @@ model Session {
|
|||||||
|
|
||||||
model Website {
|
model Website {
|
||||||
id String @id @unique @map("website_id") @db.Uuid
|
id String @id @unique @map("website_id") @db.Uuid
|
||||||
userId String @map("user_id") @db.Uuid
|
|
||||||
name String @db.VarChar(100)
|
name String @db.VarChar(100)
|
||||||
domain String? @db.VarChar(500)
|
domain String? @db.VarChar(500)
|
||||||
shareId String? @unique @map("share_id") @db.VarChar(64)
|
shareId String? @unique @map("share_id") @db.VarChar(64)
|
||||||
@ -52,11 +49,9 @@ model Website {
|
|||||||
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
|
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
|
||||||
isDeleted Boolean @default(false) @map("is_deleted")
|
isDeleted Boolean @default(false) @map("is_deleted")
|
||||||
|
|
||||||
user User @relation(fields: [userId], references: [id])
|
|
||||||
teamWebsite TeamWebsite[]
|
teamWebsite TeamWebsite[]
|
||||||
userWebsite UserWebsite[]
|
userWebsite UserWebsite[]
|
||||||
|
|
||||||
@@index([userId])
|
|
||||||
@@index([createdAt])
|
@@index([createdAt])
|
||||||
@@index([shareId])
|
@@index([shareId])
|
||||||
@@map("website")
|
@@map("website")
|
||||||
@ -117,7 +112,7 @@ model GroupUser {
|
|||||||
isDeleted Boolean @default(false) @map("is_deleted")
|
isDeleted Boolean @default(false) @map("is_deleted")
|
||||||
|
|
||||||
group Group @relation(fields: [groupId], references: [id])
|
group Group @relation(fields: [groupId], references: [id])
|
||||||
User User @relation(fields: [userId], references: [id])
|
user User @relation(fields: [userId], references: [id])
|
||||||
|
|
||||||
@@map("group_user")
|
@@map("group_user")
|
||||||
}
|
}
|
||||||
|
45
queries/analytics/event/getEvents.js
Normal file
45
queries/analytics/event/getEvents.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import prisma from 'lib/prisma';
|
||||||
|
import clickhouse from 'lib/clickhouse';
|
||||||
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||||
|
|
||||||
|
export function getEvents(...args) {
|
||||||
|
return runQuery({
|
||||||
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function relationalQuery(websites, start_at) {
|
||||||
|
return prisma.client.event.findMany({
|
||||||
|
where: {
|
||||||
|
websiteId: {
|
||||||
|
in: websites,
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
gte: start_at,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function clickhouseQuery(websites, start_at) {
|
||||||
|
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat } = clickhouse;
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
event_id,
|
||||||
|
website_id,
|
||||||
|
session_id,
|
||||||
|
created_at,
|
||||||
|
url,
|
||||||
|
event_name
|
||||||
|
from event
|
||||||
|
where event_name != ''
|
||||||
|
and ${
|
||||||
|
websites && websites.length > 0
|
||||||
|
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||||
|
: '0 = 0'
|
||||||
|
}
|
||||||
|
and created_at >= ${getDateFormat(start_at)}`,
|
||||||
|
);
|
||||||
|
}
|
43
queries/analytics/pageview/getPageviews.js
Normal file
43
queries/analytics/pageview/getPageviews.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import prisma from 'lib/prisma';
|
||||||
|
import clickhouse from 'lib/clickhouse';
|
||||||
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||||
|
|
||||||
|
export async function getPageviews(...args) {
|
||||||
|
return runQuery({
|
||||||
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function relationalQuery(websites, start_at) {
|
||||||
|
return prisma.client.pageview.findMany({
|
||||||
|
where: {
|
||||||
|
websiteId: {
|
||||||
|
in: websites,
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
gte: start_at,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(websites, start_at) {
|
||||||
|
const { rawQuery, getCommaSeparatedStringFormat } = clickhouse;
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
website_id,
|
||||||
|
session_id,
|
||||||
|
created_at,
|
||||||
|
url
|
||||||
|
from event
|
||||||
|
where event_name = ''
|
||||||
|
and ${
|
||||||
|
websites && websites.length > 0
|
||||||
|
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||||
|
: '0 = 0'
|
||||||
|
}
|
||||||
|
and created_at >= ${clickhouse.getDateFormat(start_at)}`,
|
||||||
|
);
|
||||||
|
}
|
52
queries/analytics/session/getSessions.js
Normal file
52
queries/analytics/session/getSessions.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
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)}`,
|
||||||
|
);
|
||||||
|
}
|
@ -1,3 +1,30 @@
|
|||||||
export async function getRealtimeData() {
|
import { getPageviews } from '../pageview/getPageviews';
|
||||||
throw new Error('Not Implemented');
|
import { getSessions } from '../session/getSessions';
|
||||||
|
import { getEvents } from '../event/getEvents';
|
||||||
|
|
||||||
|
export async function getRealtimeData(websites, time) {
|
||||||
|
const [pageviews, sessions, events] = await Promise.all([
|
||||||
|
getPageviews(websites, time),
|
||||||
|
getSessions(websites, time),
|
||||||
|
getEvents(websites, time),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
pageviews: pageviews.map(({ id, ...props }) => ({
|
||||||
|
__id: `p${id}`,
|
||||||
|
pageviewId: id,
|
||||||
|
...props,
|
||||||
|
})),
|
||||||
|
sessions: sessions.map(({ id, ...props }) => ({
|
||||||
|
__id: `s${id}`,
|
||||||
|
sessionId: id,
|
||||||
|
...props,
|
||||||
|
})),
|
||||||
|
events: events.map(({ id, ...props }) => ({
|
||||||
|
__id: `e${id}`,
|
||||||
|
eventId: id,
|
||||||
|
...props,
|
||||||
|
})),
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user