Skip to content

Commit

Permalink
add date param to values
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscao633 committed Oct 13, 2023
1 parent c18daf4 commit 88aa341
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/app/(main)/reports/[id]/FilterSelectForm.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { useState } from 'react';
import FieldSelectForm from './FieldSelectForm';
import FieldFilterForm from './FieldFilterForm';
import { useApi } from 'components/hooks';
import { useApi, useDateRange } from 'components/hooks';
import { Loading } from 'react-basics';

function useValues(websiteId, type) {
const { get, useQuery } = useApi();
const [dateRange] = useDateRange(websiteId);
const { startDate, endDate } = dateRange;
const { data, error, isLoading } = useQuery(
['websites:values', websiteId, type],
() =>
get(`/websites/${websiteId}/values`, {
type,
startAt: +startDate,
endAt: +endDate,
}),
{ enabled: !!(websiteId && type) },
);
Expand Down
8 changes: 7 additions & 1 deletion src/pages/api/websites/[id]/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { EVENT_COLUMNS, FILTER_COLUMNS, SESSION_COLUMNS } from 'lib/constants';
import { getValues } from 'queries';
import { parseDateRangeQuery } from 'lib/query';

export interface ValuesRequestQuery {
id: string;
startAt: number;
endAt: number;
}

import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
}),
};

Expand All @@ -23,6 +28,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
await useValidate(schema, req, res);

const { id: websiteId, type } = req.query;
const { startDate, endDate } = await parseDateRangeQuery(req);

if (req.method === 'GET') {
if (!SESSION_COLUMNS.includes(type as string) && !EVENT_COLUMNS.includes(type as string)) {
Expand All @@ -33,7 +39,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
return unauthorized(res);
}

const values = await getValues(websiteId, FILTER_COLUMNS[type as string]);
const values = await getValues(websiteId, FILTER_COLUMNS[type as string], startDate, endDate);

return ok(
res,
Expand Down
22 changes: 17 additions & 5 deletions src/queries/analytics/getValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';

export async function getValues(...args: [websiteId: string, column: string]) {
export async function getValues(
...args: [websiteId: string, column: string, startDate: Date, endDate: Date]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}

async function relationalQuery(websiteId: string, column: string) {
async function relationalQuery(websiteId: string, column: string, startDate: Date, endDate: Date) {
const { rawQuery } = prisma;

return rawQuery(
Expand All @@ -19,20 +21,30 @@ async function relationalQuery(websiteId: string, column: string) {
inner join session
on session.session_id = website_event.session_id
where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
`,
{ websiteId },
{
websiteId,
startDate,
endDate,
},
);
}

async function clickhouseQuery(websiteId: string, column: string) {
async function clickhouseQuery(websiteId: string, column: string, startDate: Date, endDate: Date) {
const { rawQuery } = clickhouse;

return rawQuery(
`
select distinct ${column} as value
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
`,
{ websiteId },
{
websiteId,
startDate,
endDate,
},
);
}

0 comments on commit 88aa341

Please sign in to comment.