All files / server/types funnel.ts

0% Statements 0/3
0% Branches 0/2
0% Functions 0/3
0% Lines 0/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                                                                                                                                                                                                                                                                                                                                 
/**
 * Funnel analysis types for the semantic layer
 * Supports query-time funnel definition with temporal ordering and conversion metrics
 */
 
import type { Filter } from './query'
 
/**
 * Single-cube step definition - all steps use the same cube
 */
export interface FunnelStepSingleCube {
  /** Step display name */
  name: string
 
  /** Filter conditions for this step */
  filter?: Filter | Filter[]
 
  /**
   * Time window constraint - max duration from previous step.
   * Format: ISO 8601 duration (e.g., "P7D" for 7 days, "PT1H" for 1 hour)
   */
  timeToConvert?: string
}
 
/**
 * Multi-cube step definition - steps can use different cubes
 */
export interface FunnelStepMultiCube extends FunnelStepSingleCube {
  /** Which cube this step uses */
  cube: string
}
 
export type FunnelStep = FunnelStepSingleCube | FunnelStepMultiCube
 
/**
 * Binding key mapping for multi-cube funnels
 * Maps the user/entity identifier across different cubes
 */
export interface FunnelBindingKeyMapping {
  cube: string
  dimension: string
}
 
/**
 * Time dimension mapping for multi-cube funnels
 * Maps the timestamp field across different cubes
 */
export interface FunnelTimeDimensionMapping {
  cube: string
  dimension: string
}
 
/**
 * Funnel query configuration
 */
export interface FunnelQueryConfig {
  /**
   * Binding key - dimension that links entities across steps.
   * This is typically a user ID, session ID, or other entity identifier.
   * String for single-cube (e.g., 'Events.userId'),
   * Array for multi-cube with different column names per cube.
   */
  bindingKey: string | FunnelBindingKeyMapping[]
 
  /**
   * Time dimension for temporal ordering.
   * Used to determine event order and calculate time-to-convert.
   * String for single-cube (e.g., 'Events.timestamp'),
   * Array for multi-cube with different timestamp columns per cube.
   */
  timeDimension: string | FunnelTimeDimensionMapping[]
 
  /** Ordered array of funnel steps (minimum 2) */
  steps: FunnelStep[]
 
  /** Include time-to-convert metrics in results (default: false) */
  includeTimeMetrics?: boolean
 
  /**
   * Global time window - all steps must complete within this duration
   * from the first step. Format: ISO 8601 duration.
   */
  globalTimeWindow?: string
}
 
/**
 * Result row for funnel query
 */
export interface FunnelResultRow {
  /** Step name */
  step: string
 
  /** Step position (0-indexed) */
  stepIndex: number
 
  /** Count of entities that reached this step */
  count: number
 
  /**
   * Step-to-step conversion rate (count / previous step count).
   * null for first step.
   */
  conversionRate: number | null
 
  /**
   * Cumulative conversion rate from first step (count / first step count).
   */
  cumulativeConversionRate: number
 
  /** Average time to reach this step from previous step (seconds) */
  avgSecondsToConvert?: number | null
 
  /** Median time to reach this step from previous step (seconds) */
  medianSecondsToConvert?: number | null
 
  /** 90th percentile time to reach this step (seconds) */
  p90SecondsToConvert?: number | null
 
  /** Minimum time to reach this step (seconds) */
  minSecondsToConvert?: number | null
 
  /** Maximum time to reach this step (seconds) */
  maxSecondsToConvert?: number | null
}
 
/**
 * Funnel capabilities per database engine
 */
export interface FunnelCapabilities {
  /** Whether database supports PERCENTILE_CONT */
  supportsPercentile: boolean
 
  /** Whether database supports INTERVAL arithmetic directly */
  supportsIntervalArithmetic: boolean
}
 
/**
 * Type guard for multi-cube step
 */
export function isMultiCubeStep(step: FunnelStep): step is FunnelStepMultiCube {
  return 'cube' in step && typeof step.cube === 'string'
}
 
/**
 * Type guard for multi-cube binding key
 */
export function isMultiCubeBindingKey(
  bindingKey: string | FunnelBindingKeyMapping[]
): bindingKey is FunnelBindingKeyMapping[] {
  return Array.isArray(bindingKey)
}
 
/**
 * Type guard for multi-cube time dimension
 */
export function isMultiCubeTimeDimension(
  timeDimension: string | FunnelTimeDimensionMapping[]
): timeDimension is FunnelTimeDimensionMapping[] {
  return Array.isArray(timeDimension)
}