umami/src/queries/analytics/getValues.ts

53 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-08-08 22:29:59 +00:00
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
2023-10-13 16:31:53 +00:00
export async function getValues(
...args: [websiteId: string, column: string, startDate: Date, endDate: Date]
) {
2023-08-08 22:29:59 +00:00
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
2023-10-13 16:31:53 +00:00
async function relationalQuery(websiteId: string, column: string, startDate: Date, endDate: Date) {
2023-08-08 22:29:59 +00:00
const { rawQuery } = prisma;
return rawQuery(
`
select distinct ${column} as "value"
from website_event
inner join session
on session.session_id = website_event.session_id
where website_event.website_id = {{websiteId::uuid}}
2023-10-13 17:01:23 +00:00
and website_event.created_at between {{startDate}} and {{endDate}}
2024-02-14 23:13:53 +00:00
limit 500
2023-08-08 22:29:59 +00:00
`,
2023-10-13 16:31:53 +00:00
{
websiteId,
startDate,
endDate,
},
2023-08-08 22:29:59 +00:00
);
}
2023-10-13 16:31:53 +00:00
async function clickhouseQuery(websiteId: string, column: string, startDate: Date, endDate: Date) {
2023-08-08 22:29:59 +00:00
const { rawQuery } = clickhouse;
return rawQuery(
`
select distinct ${column} as value
from website_event
where website_id = {websiteId:UUID}
2023-10-13 16:31:53 +00:00
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
2024-02-14 23:13:53 +00:00
limit 500
2023-08-08 22:29:59 +00:00
`,
2023-10-13 16:31:53 +00:00
{
websiteId,
startDate,
endDate,
},
2023-08-08 22:29:59 +00:00
);
}