aboutsummaryrefslogtreecommitdiff
path: root/src/queries/sql/events/getEventData.ts
blob: f12c95ca04e2d3f0a33b3ca74d9935108943c5ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { EventData } from '@/generated/prisma/client';
import clickhouse from '@/lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';

const FUNCTION_NAME = 'getEventData';

export async function getEventData(
  ...args: [websiteId: string, eventId: string]
): Promise<EventData[]> {
  return runQuery({
    [PRISMA]: () => relationalQuery(...args),
    [CLICKHOUSE]: () => clickhouseQuery(...args),
  });
}

async function relationalQuery(websiteId: string, eventId: string) {
  const { rawQuery } = prisma;

  return rawQuery(
    `
    select event_data.website_id as "websiteId",
       event_data.website_event_id as "eventId",
       website_event.event_name as "eventName",
       event_data.data_key as "dataKey",
       event_data.string_value as "stringValue",
       event_data.number_value as "numberValue",
       event_data.date_value as "dateValue",
       event_data.data_type as "dataType",
       event_data.created_at as "createdAt"
    from event_data
    join website_event on website_event.event_id = event_data.website_event_id
      and website_event.website_id = {{websiteId::uuid}}
    where event_data.website_id = {{websiteId::uuid}}
      and event_data.website_event_id = {{eventId::uuid}}
    `,
    { websiteId, eventId },
    FUNCTION_NAME,
  );
}

async function clickhouseQuery(websiteId: string, eventId: string): Promise<EventData[]> {
  const { rawQuery } = clickhouse;

  return rawQuery(
    `
      select website_id as websiteId,
        event_id as eventId,
        event_name as eventName,
        data_key as dataKey,
        string_value as stringValue,
        number_value as numberValue,
        date_value as dateValue,
        data_type as dataType,
        created_at as createdAt
      from event_data
      where website_id = {websiteId:UUID} 
        and event_id = {eventId:UUID}
    `,
    { websiteId, eventId },
    FUNCTION_NAME,
  );
}