Add Reset / Delete functionality.

This commit is contained in:
Brian Cao 2022-11-09 22:46:50 -08:00
parent 4f098a34c1
commit 30743feb0e
6 changed files with 26 additions and 29 deletions

View File

@ -16,13 +16,7 @@ export async function deleteUser(userId) {
return client
.$transaction([
client.pageview.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.eventData.deleteMany({
where: { event: { websiteId: { in: websiteIds } } },
}),
client.event.deleteMany({
client.websiteEvent.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.session.deleteMany({

View File

@ -5,13 +5,7 @@ export async function deleteWebsite(id) {
const { client, transaction } = prisma;
return transaction([
client.pageview.deleteMany({
where: { websiteId: id },
}),
client.eventData.deleteMany({
where: { event: { websiteId: id } },
}),
client.event.deleteMany({
client.websiteEvent.deleteMany({
where: { websiteId: id },
}),
client.session.deleteMany({

View File

@ -1,20 +1,25 @@
import prisma from 'lib/prisma';
import { getWebsite } from 'queries';
import cache from 'lib/cache';
export async function resetWebsite(id) {
const { client, transaction } = prisma;
const { revId } = await getWebsite({ id });
return transaction([
client.pageview.deleteMany({
where: { websiteId: id },
}),
client.eventData.deleteMany({
where: { event: { websiteId: id } },
}),
client.event.deleteMany({
client.websiteEvent.deleteMany({
where: { websiteId: id },
}),
client.session.deleteMany({
where: { websiteId: id },
}),
]);
client.website.update({ where: { id }, data: { revId: revId + 1 } }),
]).then(async data => {
if (cache.enabled) {
await cache.storeWebsite(data[2]);
}
return data;
});
}

View File

@ -66,7 +66,8 @@ async function clickhouseQuery(
count(${count !== '*' ? 'distinct session_id' : count}) y
from event
where event_name = ''
and website_id= $1
and website_id = $1
and rev_id = $2
and ${getBetweenDates('created_at', start_at, end_at)}
${pageviewQuery}
${sessionQuery}

View File

@ -33,11 +33,11 @@ async function clickhouseQuery(websites, start_at) {
url
from event
where event_name = ''
and ${
websites && websites.length > 0
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
: '0 = 0'
}
and ${
websites && websites.length > 0
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
: '0 = 0'
}
and created_at >= ${clickhouse.getDateFormat(start_at)}`,
);
}

View File

@ -1,6 +1,7 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import cache from 'lib/cache';
export async function getWebsiteStats(...args) {
return runQuery({
@ -45,7 +46,8 @@ async function relationalQuery(websiteId, { start_at, end_at, filters = {} }) {
async function clickhouseQuery(websiteId, { start_at, end_at, filters = {} }) {
const { rawQuery, getDateQuery, getBetweenDates, parseFilters } = clickhouse;
const params = [websiteId];
const website = await cache.fetchWebsite(websiteId);
const params = [websiteId, website?.revId || 0];
const { pageviewQuery, sessionQuery } = parseFilters(null, filters, params);
return rawQuery(
@ -63,6 +65,7 @@ async function clickhouseQuery(websiteId, { start_at, end_at, filters = {} }) {
from event
where event_name = ''
and website_id = $1
and rev_id = $2
and ${getBetweenDates('created_at', start_at, end_at)}
${pageviewQuery}
${sessionQuery}