Metrics components refactoring. New event data page.

This commit is contained in:
Mike Cao 2023-07-10 04:35:19 -07:00
parent 4e6d24e932
commit c865f43b11
47 changed files with 756 additions and 672 deletions

View file

@ -13,11 +13,9 @@ export async function getReportById(reportId: string): Promise<Report> {
});
}
export async function getReports(userId: string): Promise<Report[]> {
export async function getReports(where: Prisma.ReportWhereUniqueInput): Promise<Report[]> {
return prisma.client.report.findMany({
where: {
userId,
},
where,
});
}

View file

@ -3,26 +3,10 @@ import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { WebsiteEventDataMetric } from 'lib/types';
import { loadWebsite } from 'lib/query';
import { DEFAULT_CREATED_AT } from 'lib/constants';
export interface EventDataCriteria {
fields: [{ name: string; type: string; value: string }];
filters: [
{
name: string;
type: string;
value: [string, string];
},
];
groups: [
{
name: string;
type: string;
},
];
}
import prisma from '../../../lib/prisma';
export async function getEventData(
...args: [websiteId: string, startDate: Date, endDate: Date, criteria: EventDataCriteria]
...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]
): Promise<WebsiteEventDataMetric[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -30,76 +14,79 @@ export async function getEventData(
});
}
async function relationalQuery() {
return null;
async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
const { toUuid, rawQuery } = prisma;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
if (field) {
return rawQuery(
`select event_key as field,
count(*) as total
from event_data
where website_id = $1${toUuid()}
and event_key = $2
and created_at >= $3
and created_at between $4 and $5
group by event_key
order by 2 desc, 1 asc
limit 1000
`,
[websiteId, field, resetDate, startDate, endDate] as any,
);
}
return rawQuery(
`select
event_key as field,
count(*) as total
from event_data
where website_id = $1${toUuid()}
and created_at >= $2
and created_at between $3 and $4
group by event_key
order by 2 desc, 1 asc
limit 1000
`,
[websiteId, resetDate, startDate, endDate] as any,
);
}
async function clickhouseQuery(
websiteId: string,
startDate: Date,
endDate: Date,
criteria: EventDataCriteria,
) {
const { fields, filters } = criteria;
async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
const uniqueFields = fields.reduce((obj, { name, type, value }) => {
const prefix = type === 'array' ? 'string' : type;
if (!obj[name]) {
obj[name] = {
columns: [
'event_key as field',
`count(*) as total`,
value === 'unique' ? `${prefix}_value as value` : null,
].filter(n => n),
groups: ['event_key', value === 'unique' ? `${prefix}_value` : null].filter(n => n),
};
}
return obj;
}, {});
const queries = Object.keys(uniqueFields).reduce((arr, key) => {
const field = uniqueFields[key];
const params = { websiteId, name: key };
return arr.concat(
rawQuery(
`select
${field.columns.join(',')}
if (field) {
return rawQuery(
`select
event_key as field,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and event_key = {name:String}
and event_key = {field:String}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
group by ${field.groups.join(',')}
limit 100
group by event_key
order by 2 desc, 1 asc
limit 1000
`,
params,
),
{ websiteId, field },
);
}, []);
}
const results = (await Promise.all(queries)).flatMap(n => n);
const columns = results.reduce((arr, row) => {
const keys = Object.keys(row);
for (const key of keys) {
if (!arr.includes(key)) {
arr.push(key);
}
}
return arr;
}, []);
return results.reduce((arr, row) => {
return arr.concat(
columns.reduce((obj, key) => {
obj[key] = row[key];
return obj;
}, {}),
);
}, []);
return rawQuery(
`select
event_key as field,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
group by event_key
order by 2 desc, 1 asc
limit 1000
`,
{ websiteId },
);
}