Add collect limits.

This commit is contained in:
Brian Cao 2022-12-31 13:42:03 -08:00
parent f42cab8d83
commit e487da72c3
13 changed files with 217 additions and 61 deletions

View file

@ -1,16 +1,45 @@
import { Prisma, Website } from '@prisma/client';
import { Prisma, Team, Website } from '@prisma/client';
import cache from 'lib/cache';
import prisma from 'lib/prisma';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<
Website & {
team?: Team;
}
> {
return prisma.client.website.findUnique({
where,
include: {
team: true,
},
});
}
export async function getWebsites(): Promise<Website[]> {
export async function getWebsites(
where: Prisma.WebsiteFindManyArgs,
showDeleted = false,
): Promise<Website[]> {
return prisma.client.website.findMany({
where: { ...where, deletedAt: showDeleted ? { not: null } : null },
orderBy: {
name: 'asc',
},
});
}
export async function getAllWebsitesByUser(userId): Promise<Website[]> {
return prisma.client.website.findMany({
where: {
OR: [
{ userId },
{
team: {
userId,
},
},
],
},
orderBy: {
name: 'asc',
},

View file

@ -1,7 +1,7 @@
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
import { Prisma } from '@prisma/client';
import { Prisma, Session } from '@prisma/client';
export async function getSession(args: { id: string }) {
return runQuery({
@ -10,7 +10,7 @@ export async function getSession(args: { id: string }) {
});
}
async function relationalQuery(where: Prisma.SessionWhereUniqueInput) {
async function relationalQuery(where: Prisma.SessionWhereUniqueInput): Promise<Session> {
return prisma.client.session.findUnique({
where,
});

View file

@ -0,0 +1,38 @@
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
export async function getViewTotals(...args: [websites: string[], startDate: Date]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websites: string[], startDate: Date) {
return prisma.client.websiteEvent.count({
where: {
websiteId: {
in: websites,
},
createdAt: {
gte: startDate,
},
},
});
}
async function clickhouseQuery(websiteIds: string[], startDate: Date) {
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat, findFirst } = clickhouse;
return rawQuery(
`
select
count(*) as views
from event
where
website_id in (${getCommaSeparatedStringFormat(websiteIds)})
and created_at >= ${getDateFormat(startDate)}
`,
).then(data => findFirst(data));
}

View file

@ -18,7 +18,7 @@ async function relationalQuery(
) {
const { startDate, endDate, filters = {} } = data;
const { getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
const params = [startDate, endDate];
const params: any = [startDate, endDate];
const { filterQuery, joinSession } = parseFilters(filters, params);
return rawQuery(

View file

@ -16,4 +16,5 @@ export * from './analytics/session/getSessionMetrics';
export * from './analytics/session/getSessions';
export * from './analytics/stats/getActiveVisitors';
export * from './analytics/stats/getRealtimeData';
export * from './analytics/stats/getViewTotals';
export * from './analytics/stats/getWebsiteStats';