Fixed event data queries.

This commit is contained in:
Mike Cao 2023-08-04 15:21:14 -07:00
parent 0fd57aa7cf
commit e11766ca1c
6 changed files with 44 additions and 38 deletions

View File

@ -13,15 +13,18 @@ export function EventDataTable({ data = [] }) {
return (
<GridTable data={data}>
<GridColumn name="event" label={formatMessage(labels.event)}>
<GridColumn name="eventName" label={formatMessage(labels.event)}>
{row => (
<Link href={resolveUrl({ event: row.event })} shallow={true}>
{row.event}
<Link href={resolveUrl({ eventName: row.event })} shallow={true}>
{row.eventName}
</Link>
)}
</GridColumn>
<GridColumn name="field" label={formatMessage(labels.field)}>
{row => row.field}
<GridColumn name="fieldName" label={formatMessage(labels.field)}>
{row => row.fieldName}
</GridColumn>
<GridColumn name="dataType" label={formatMessage(labels.type)}>
{row => row.dataType}
</GridColumn>
<GridColumn name="total" label={formatMessage(labels.totalRecords)}>
{({ total }) => total.toLocaleString()}

View File

@ -48,12 +48,12 @@ export const FILTER_COLUMNS = {
referrer: 'referrer_domain',
title: 'page_title',
query: 'url_query',
event: 'event_name',
region: 'subdivision1',
type: 'event_type',
eventType: 'event_type',
eventName: 'event_name',
};
export const IGNORED_FILTERS = ['startDate', 'endDate', 'timezone', 'unit', 'eventType'];
export const IGNORED_FILTERS = ['startDate', 'endDate', 'timezone', 'unit'];
export const COLLECTION_TYPE = {
event: 'event',

View File

@ -153,4 +153,5 @@ export interface QueryFilters {
export interface QueryOptions {
joinSession?: boolean;
ignoreFilters?: string[];
}

View File

@ -21,15 +21,19 @@ export default async (
await useAuth(req, res);
if (req.method === 'GET') {
const { websiteId, startAt, endAt, field, event } = req.query;
const { websiteId, startAt, endAt, eventName } = req.query;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getEventDataEvents(websiteId, new Date(+startAt), new Date(+endAt), {
field,
event,
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const data = await getEventDataEvents(websiteId, {
startDate,
endDate,
eventName,
});
return ok(res, data);

View File

@ -28,7 +28,10 @@ export default async (
return unauthorized(res);
}
const results = await getEventDataFields(websiteId, new Date(+startAt), new Date(+endAt));
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const results = await getEventDataFields(websiteId, { startDate, endDate });
const data = results.reduce(
(obj, row) => {

View File

@ -4,7 +4,7 @@ import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { QueryFilters, WebsiteEventDataFields } from 'lib/types';
export async function getEventDataEvents(
...args: [websiteId: string, filters: QueryFilters & { field?: string; event?: string }]
...args: [websiteId: string, filters: QueryFilters]
): Promise<WebsiteEventDataFields[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -12,20 +12,18 @@ export async function getEventDataEvents(
});
}
async function relationalQuery(
websiteId: string,
filters: QueryFilters & { field?: string; event?: string },
) {
async function relationalQuery(websiteId: string, filters: QueryFilters) {
const { rawQuery, parseFilters } = prisma;
const { eventName } = filters;
const { params } = await parseFilters(websiteId, filters);
if (event) {
if (eventName) {
return rawQuery(
`
select
website_event.event_name as event,
event_data.event_key as field,
event_data.data_type as type,
website_event.event_name as eventName,
event_data.event_key as fieldName,
event_data.data_type as dataType,
event_data.string_value as value,
count(*) as total
from event_data
@ -33,7 +31,7 @@ async function relationalQuery(
on website_event.event_id = event_data.website_event_id
where event_data.website_id = {{websiteId::uuid}}
and event_data.created_at between {{startDate}} and {{endDate}}
and websit_event.event_name = {{event}}
and websit_event.event_name = {{eventName}}
group by website_event.event_name, event_data.event_key, event_data.data_type, event_data.string_value
order by 1 asc, 2 asc, 3 asc, 4 desc
`,
@ -44,9 +42,9 @@ async function relationalQuery(
return rawQuery(
`
select
website_event.event_name as event,
event_data.event_key as field,
event_data.data_type as type,
website_event.event_name as eventName,
event_data.event_key as fieldName,
event_data.data_type as dataType,
count(*) as total
from event_data
inner join website_event
@ -61,21 +59,18 @@ async function relationalQuery(
);
}
async function clickhouseQuery(
websiteId: string,
filters: QueryFilters & { field?: string; event?: string },
) {
async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
const { rawQuery, parseFilters } = clickhouse;
const { event } = filters;
const { eventName } = filters;
const { params } = await parseFilters(websiteId, filters);
if (event) {
if (eventName) {
return rawQuery(
`
select
event_name as event,
event_key as field,
data_type as type,
event_name as eventName,
event_key as fieldName,
data_type as dataType,
string_value as value,
count(*) as total
from event_data
@ -93,9 +88,9 @@ async function clickhouseQuery(
return rawQuery(
`
select
event_name as event,
event_key as field,
data_type as type,
event_name as eventName,
event_key as fieldName,
data_type as dataType,
count(*) as total
from event_data
where website_id = {websiteId:UUID}