aboutsummaryrefslogtreecommitdiff
path: root/scripts/seed/generators/sessions.ts
blob: 1370511ff9443e1f8384f103f9307aca072d74ef (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
import { uuid } from '../utils.js';
import { getRandomDevice } from '../distributions/devices.js';
import { getRandomGeo, getRandomLanguage } from '../distributions/geographic.js';
import { generateTimestampForDay } from '../distributions/temporal.js';

export interface SessionData {
  id: string;
  websiteId: string;
  browser: string;
  os: string;
  device: string;
  screen: string;
  language: string;
  country: string;
  region: string;
  city: string;
  createdAt: Date;
}

export function createSession(websiteId: string, day: Date): SessionData {
  const deviceInfo = getRandomDevice();
  const geo = getRandomGeo();
  const language = getRandomLanguage();
  const createdAt = generateTimestampForDay(day);

  return {
    id: uuid(),
    websiteId,
    browser: deviceInfo.browser,
    os: deviceInfo.os,
    device: deviceInfo.device,
    screen: deviceInfo.screen,
    language,
    country: geo.country,
    region: geo.region,
    city: geo.city,
    createdAt,
  };
}

export function createSessions(websiteId: string, day: Date, count: number): SessionData[] {
  const sessions: SessionData[] = [];

  for (let i = 0; i < count; i++) {
    sessions.push(createSession(websiteId, day));
  }

  // Sort by createdAt to maintain chronological order
  sessions.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());

  return sessions;
}