Skip to content

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

2.9 KB
Raw Blame
// The chart module's isomorphic surface: limits, zod schemas and the column
// allow-lists shared by more than one file here.

import { z } from 'zod';
import { zChartEvent } from '../report/report.constants';

/**
 * Window the REST retention routes (`/insights/:projectId/retention`,
 * `/engagement`) read when the caller names none, wider than engagement's
 * 60-day churn bucket. Not applied to callers of the service that pass no
 * dates: those still read all time.
 */
export const RETENTION_SERIES_DEFAULT_RANGE = '3m';

/** User-flow depth: how many events one sankey path may span. */
export const DEFAULT_SANKEY_STEPS = 5;

/**
 * Longest window `chart.sankey` will answer, in days.
 *
 * Both of its statements scan every event in the range and hold one
 * `groupArray` per session, so cost is linear in the window and `steps`
 * bounds the answer rather than the work (~60ms and ~31MiB per day,
 * measured on the local production copy). The range picker's `12m` and
 * `lastYear` options would extrapolate to ~22s and ~11.8GiB per statement,
 * twice per call — past what a node can answer at all.
 *
 * 93 is the widest span the picker's `3m` option can produce - the 92-day
 * June-August quarter plus the half-open end-of-day boundary
 * `getDatesFromRange` adds - so every option up to `3m` is served and `6m`,
 * `12m`, `lastYear` and a long `yearToDate` or `custom` are refused. Refused,
 * not silently shortened: see `assertSankeyWindowIsAnswerable` in
 * sankey.service.ts, applied by `getSankeyChart`.
 */
export const MAX_SANKEY_WINDOW_DAYS = 93;
const MIN_SANKEY_STEPS = 2;
const MAX_SANKEY_STEPS = 10;

export const zGetSankeyInput = z.object({
  projectId: z.string(),
  startDate: z.string(),
  endDate: z.string(),
  steps: z
    .number()
    .min(MIN_SANKEY_STEPS)
    .max(MAX_SANKEY_STEPS)
    .default(DEFAULT_SANKEY_STEPS),
  mode: z.enum(['between', 'after', 'before']),
  startEvent: zChartEvent,
  endEvent: zChartEvent.optional(),
  exclude: z.array(z.string()).default([]),
  include: z.array(z.string()).optional(),
});

export type IGetSankeyInput = z.infer<typeof zGetSankeyInput> & {
  timezone: string;
};

/**
 * Top-level `profiles` columns a `profile.<field>` reference may resolve to.
 * Both halves of the chart path read this one list: the SELECT side
 * (`getProfilePropertySelect`, src/field-resolution.ts) and the filter side
 * (`profileColumnSql`, src/table-filter-where.ts). Anything outside it is
 * not a column name and must never reach the SQL text.
 */
export const PROFILE_SELECT_COLUMNS = [
  'id',
  'first_name',
  'last_name',
  'email',
  'avatar',
  'created_at',
  'last_seen_at',
];

/**
 * Profile columns the funnel's and conversion's `profiles` join may expose
 * beyond the `properties` Map. Narrower than PROFILE_SELECT_COLUMNS: those two
 * statements join per event row, so `id` and `avatar` are never selected.
 */
export const JOINABLE_PROFILE_COLUMNS = [
  'email',
  'first_name',
  'last_name',
  'created_at',
  'last_seen_at',
];