aboutsummaryrefslogtreecommitdiff
path: root/src/queries/sql/sessions/getSessionData.ts
blob: 8f1e493326d0e80acb887d1b54fcb0130c7d83a6 (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
import clickhouse from '@/lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';

const FUNCTION_NAME = 'getSessionData';

export async function getSessionData(...args: [websiteId: string, sessionId: string]) {
  return runQuery({
    [PRISMA]: () => relationalQuery(...args),
    [CLICKHOUSE]: () => clickhouseQuery(...args),
  });
}

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

  return rawQuery(
    `
    select
        website_id as "websiteId",
        session_id as "sessionId",
        data_key as "dataKey",
        data_type as "dataType",
        replace(string_value, '.0000', '') as "stringValue",
        number_value as "numberValue",
        date_value as "dateValue",
        created_at as "createdAt"
    from session_data
    where website_id = {{websiteId::uuid}}
      and session_id = {{sessionId::uuid}}
    order by data_key asc
    `,
    { websiteId, sessionId },
    FUNCTION_NAME,
  );
}

async function clickhouseQuery(websiteId: string, sessionId: string) {
  const { rawQuery } = clickhouse;

  return rawQuery(
    `
    select
        website_id as websiteId,
        session_id as sessionId,
        data_key as dataKey,
        data_type as dataType,
        replace(string_value, '.0000', '')  as stringValue,
        number_value as numberValue,
        date_value as dateValue,
        created_at as createdAt
    from session_data final
    where website_id = {websiteId:UUID}
    and session_id = {sessionId:UUID}
    order by data_key asc
    `,
    { websiteId, sessionId },
    FUNCTION_NAME,
  );
}