Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

4.7 KB
Raw Blame
import { z } from 'zod';
import { chQuery } from '../../ch-query';
import type { Ctx } from '../../context';
import { defineJob } from '../../jobs/define';
import type { Logger } from '../../logger';
import {
  createEvent,
  type IClickhouseEvent,
  transformEvent,
  transformSessionToEvent,
} from '../event/event.service';
import { sessionRuntimeFrom } from './src/runtime';
import {
  createSessionEnd,
  type SessionEndDeps,
  type SessionEndJobData,
  type SessionEndJobWire,
} from './src/session-end';
import { reapIdleSessions } from './src/session-reaper';
import { vacuumStaleSessions } from './src/session-vacuum';
import { updateEventsCount } from './src/usage';

const MINUTE_MS = 60 * 1000;
const SESSION_REAPER_INTERVAL_MS = 5 * MINUTE_MS;
/** Daily 04:00 UTC — backstop for cleanup leaks. */
const SESSION_VACUUM_CRON = '0 4 * * *';
const FLUSH_SESSIONS_INTERVAL_MS = 10_000;
const FLUSH_REPLAY_INTERVAL_MS = 10_000;

// Wire-shape check only: tightening it would reject payloads currently accepted.
//
// The schema's INPUT is what the producer holds (`SessionEndJobData`, a real `Date`);
// its OUTPUT is what a handler gets after BullMQ's JSON round-trip (`SessionEndJobWire`,
// a string). The transform is a type seam only.
const sessionEndWireShape = z.object({
  event: z.object({ projectId: z.string(), deviceId: z.string() }),
  snapshot: z.object({
    id: z.string(),
    project_id: z.string(),
    device_id: z.string(),
    ended_at: z.string(),
  }),
});

const sessionEndPayload = z
  .custom<SessionEndJobData>(
    (value) => sessionEndWireShape.safeParse(value).success
  )
  .transform((value) => value as unknown as SessionEndJobWire);

/**
 * The session-end job's dependencies, bound to the run's own ctx. Two lookups stay
 * dynamic to keep `notification.service.ts` out of jobs.registry.ts's eager import graph.
 */
async function sessionEndDeps(
  ctx: Ctx,
  logger: Logger
): Promise<SessionEndDeps> {
  const [notifications, { checkNotificationRulesForSessionEnd }] =
    await Promise.all([
      import('../notification/notification.service'),
      import('../notification/src/notification-dispatch'),
    ]);

  return {
    ...sessionRuntimeFrom(ctx),
    logger,
    createEvent: (payload) => createEvent(ctx, payload),
    transformEvent,
    transformSessionToEvent,
    getEvents: async (query) =>
      (await chQuery<IClickhouseEvent>(ctx, query)).map(transformEvent),
    profileBackfill: ctx.buffers.profileBackfill,
    notifications: {
      getRules: (projectId) =>
        notifications.getNotificationRulesByProjectId(ctx, projectId),
      hasFunnelRules: notifications.getHasFunnelRules,
      checkFunnelRules: (events) =>
        checkNotificationRulesForSessionEnd(ctx, events),
    },
  };
}

/** The `sessions` queue's own job. */
export const sessionQueueJobs = {
  session: defineJob({
    payload: sessionEndPayload,
    handler: async ({ payload: data, ctx }) => {
      // Child bindings bypass log redaction, so only ids: the event carries
      // the visitor's IP and properties.
      const logger = ctx.logger.child({
        projectId: data.event.projectId,
        sessionId: data.snapshot.id,
      });

      await createSessionEnd(data, await sessionEndDeps(ctx, logger));

      try {
        await updateEventsCount(ctx, data.event.projectId);
      } catch (error) {
        logger.error({ err: error }, 'Failed to update events count');
      }
    },
  }),
};

/** This module's fragment of the `cron` queue's jobs. */
export const sessionCronJobs = {
  flushSessions: defineJob({
    payload: z.null(),
    cron: { every: FLUSH_SESSIONS_INTERVAL_MS },
    handler: async ({ ctx }) => {
      await ctx.buffers.session.tryFlush({ trigger: 'cron' });
    },
  }),
  // Session replay chunks are ingested onto this module's session id, so the
  // replay buffer (Redis → ClickHouse `session_replay_chunks`) flushes here
  // too, next to the sessions it belongs to.
  flushReplay: defineJob({
    payload: z.null(),
    cron: { every: FLUSH_REPLAY_INTERVAL_MS },
    handler: async ({ ctx }) => {
      await ctx.buffers.replay.tryFlush({ trigger: 'cron' });
    },
  }),
  sessionReaper: defineJob({
    payload: z.null(),
    cron: { every: SESSION_REAPER_INTERVAL_MS },
    handler: async ({ ctx }) => {
      await reapIdleSessions({
        ...sessionRuntimeFrom(ctx),
        logger: ctx.logger.child({ job: 'session-reaper' }),
        enqueueSessionEnd: (input) =>
          ctx.services.session.enqueueSessionEnd(input),
      });
    },
  }),
  sessionVacuum: defineJob({
    payload: z.null(),
    cron: { pattern: SESSION_VACUUM_CRON },
    handler: async ({ ctx }) => {
      await vacuumStaleSessions({
        ...sessionRuntimeFrom(ctx),
        logger: ctx.logger.child({ job: 'session-vacuum' }),
      });
    },
  }),
};