Skip to content

Commit

Permalink
Resolve issues in event data
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscao633 committed Aug 16, 2023
1 parent 38445fc commit c3e261f
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 26 deletions.
1 change: 1 addition & 0 deletions components/pages/event-data/EventDataValueTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function EventDataValueTable({ data = [], event }) {
<GridColumn name="dataType" label={formatMessage(labels.type)}>
{row => DATA_TYPES[row.dataType]}
</GridColumn>
<GridColumn name="fieldValue" label={formatMessage(labels.value)} />
<GridColumn name="total" label={formatMessage(labels.totalRecords)} width="200px">
{({ total }) => total.toLocaleString()}
</GridColumn>
Expand Down
6 changes: 3 additions & 3 deletions components/pages/websites/WebsiteEventData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { EventDataMetricsBar } from 'components/pages/event-data/EventDataMetric
import { useDateRange, useApi, usePageQuery } from 'hooks';
import styles from './WebsiteEventData.module.css';

function useData(websiteId, eventName) {
function useData(websiteId, event) {
const [dateRange] = useDateRange(websiteId);
const { startDate, endDate } = dateRange;
const { get, useQuery } = useApi();
const { data, error, isLoading } = useQuery(
['event-data:events', { websiteId, startDate, endDate, eventName }],
['event-data:events', { websiteId, startDate, endDate, event }],
() =>
get('/event-data/events', {
websiteId,
startAt: +startDate,
endAt: +endDate,
eventName,
event,
}),
{ enabled: !!(websiteId && startDate && endDate) },
);
Expand Down
9 changes: 2 additions & 7 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,8 @@ export interface WebsiteEventMetric {
y: number;
}

export interface WebsiteEventDataStats {
fieldName: string;
dataType: number;
total: number;
}

export interface WebsiteEventDataFields {
export interface WebsiteEventData {
eventName?: string;
fieldName: string;
dataType: number;
fieldValue?: string;
Expand Down
5 changes: 3 additions & 2 deletions pages/api/event-data/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getEventDataEvents } from 'queries';

export interface EventDataFieldsRequestBody {
export interface EventDataEventsRequestQuery {
websiteId: string;
dateRange: {
startDate: string;
endDate: string;
};
event?: string;
}

export default async (
req: NextApiRequestQueryBody<any, EventDataFieldsRequestBody>,
req: NextApiRequestQueryBody<EventDataEventsRequestQuery>,
res: NextApiResponse<any>,
) => {
await useCors(req, res);
Expand Down
4 changes: 2 additions & 2 deletions pages/api/event-data/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getEventDataFields } from 'queries';

export interface EventDataFieldsRequestBody {
export interface EventDataFieldsRequestQuery {
websiteId: string;
dateRange: {
startDate: string;
Expand All @@ -15,7 +15,7 @@ export interface EventDataFieldsRequestBody {
}

export default async (
req: NextApiRequestQueryBody<any, EventDataFieldsRequestBody>,
req: NextApiRequestQueryBody<EventDataFieldsRequestQuery>,
res: NextApiResponse<any>,
) => {
await useCors(req, res);
Expand Down
12 changes: 6 additions & 6 deletions pages/api/event-data/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getEventDataFields } from 'queries';

export interface EventDataRequestBody {
export interface EventDataStatsRequestQuery {
websiteId: string;
dateRange: {
startDate: string;
Expand All @@ -15,7 +15,7 @@ export interface EventDataRequestBody {
}

export default async (
req: NextApiRequestQueryBody<any, EventDataRequestBody>,
req: NextApiRequestQueryBody<EventDataStatsRequestQuery>,
res: NextApiResponse<any>,
) => {
await useCors(req, res);
Expand All @@ -32,18 +32,18 @@ export default async (
const endDate = new Date(+endAt);

const results = await getEventDataFields(websiteId, { startDate, endDate });
const events = new Set();
const fields = new Set();

const data = results.reduce(
(obj, row) => {
events.add(row.fieldName);
fields.add(row.fieldName);
obj.records += Number(row.total);
return obj;
},
{ fields: results.length, records: 0 },
{ events: results.length, records: 0 },
);

return ok(res, { ...data, events: events.size });
return ok(res, { ...data, fields: fields.size });
}

return methodNotAllowed(res);
Expand Down
8 changes: 4 additions & 4 deletions queries/analytics/eventData/getEventDataEvents.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { QueryFilters, WebsiteEventDataFields } from 'lib/types';
import { QueryFilters, WebsiteEventData } from 'lib/types';

export async function getEventDataEvents(
...args: [websiteId: string, filters: QueryFilters]
): Promise<WebsiteEventDataFields[]> {
): Promise<WebsiteEventData[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
Expand All @@ -24,7 +24,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
website_event.event_name as "eventName",
event_data.event_key as "fieldName",
event_data.data_type as "dataType",
event_data.string_value as "value",
event_data.string_value as "fieldValue",
count(*) as "total"
from event_data
inner join website_event
Expand Down Expand Up @@ -71,7 +71,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
event_name as eventName,
event_key as fieldName,
data_type as dataType,
string_value as value,
string_value as fieldValue,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
Expand Down
4 changes: 2 additions & 2 deletions queries/analytics/eventData/getEventDataFields.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { QueryFilters, WebsiteEventDataFields } from 'lib/types';
import { QueryFilters, WebsiteEventData } from 'lib/types';

export async function getEventDataFields(
...args: [websiteId: string, filters: QueryFilters & { field?: string }]
): Promise<WebsiteEventDataFields[]> {
): Promise<WebsiteEventData[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
Expand Down

0 comments on commit c3e261f

Please sign in to comment.