All files server/types/retention.ts

55.55% Statements 10/18
27.77% Branches 5/18
80% Functions 4/5
58.82% Lines 10/17

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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                                                                                                                                                                                                                                              90x                 180x                                           90x       90x 90x     90x                 90x       90x 90x     90x    
/**
 * Retention analysis types for the semantic layer
 * Supports cohort-based retention tracking with classic and rolling retention types
 */
 
import type { Filter } from './query'
 
/**
 * Binding key mapping for multi-cube retention
 * Maps the user/entity identifier across cohort and activity cubes
 */
export interface RetentionBindingKeyMapping {
  cube: string
  dimension: string
}
 
/**
 * Time dimension mapping for multi-cube retention
 * Maps the timestamp field across different cubes
 */
export interface RetentionTimeDimensionMapping {
  cube: string
  dimension: string
}
 
/**
 * Date range for cohort analysis
 */
export interface RetentionDateRange {
  /** Start date (inclusive), ISO 8601 format (YYYY-MM-DD) */
  start: string
  /** End date (inclusive), ISO 8601 format (YYYY-MM-DD) */
  end: string
}
 
/**
 * Retention query configuration (Simplified Mixpanel-style format)
 *
 * Key simplifications from previous version:
 * - Single timeDimension for both cohort entry and activity
 * - Single granularity for viewing periods (no separate cohort/period granularity)
 * - Single cohort (date range defines the cohort) with optional breakdown
 */
export interface RetentionQueryConfig {
  /**
   * Single timestamp dimension for the analysis.
   * String for single-cube (e.g., 'Events.timestamp'),
   * Object for multi-cube with explicit cube reference.
   */
  timeDimension: string | RetentionTimeDimensionMapping
 
  /**
   * Binding key - dimension that links users across events.
   * This is typically a user 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 | RetentionBindingKeyMapping[]
 
  /**
   * Date range for cohort analysis (REQUIRED).
   * Users who first performed the cohort action within this range are included.
   */
  dateRange: RetentionDateRange
 
  /**
   * Granularity for viewing retention periods.
   * Determines how period_number is calculated (day/week/month).
   */
  granularity: 'day' | 'week' | 'month'
 
  /**
   * Number of periods to calculate (e.g., 12 for 12 weeks).
   * Period 0 is always the cohort entry period.
   */
  periods: number
 
  /**
   * Retention type:
   * - 'classic': User returned exactly in period N (bounded)
   * - 'rolling': User returned in period N or any later period (unbounded)
   */
  retentionType: 'classic' | 'rolling'
 
  /**
   * Optional filters on cohort entry events.
   * Applied when identifying which users enter the cohort.
   */
  cohortFilters?: Filter | Filter[]
 
  /**
   * Optional filters on return activity events.
   * Applied when checking for user activity in each period.
   */
  activityFilters?: Filter | Filter[]
 
  /**
   * Optional breakdown dimensions for segmenting the cohort.
   * When provided, retention is calculated per unique combination of breakdown values.
   * e.g., ["Events.country", "Events.plan"] to see retention by country and plan.
   */
  breakdownDimensions?: string[]
}
 
/**
 * Single retention data point in the flat result format.
 * Results are returned as a flat array; client transforms to matrix if needed.
 *
 * Simplified format: no cohortPeriod since we use a single cohort model.
 * When breakdownDimensions are specified, results include breakdownValues.
 */
export interface RetentionResultRow {
  /** Period number (0 = cohort entry, 1 = first retention period, etc.) */
  period: number
 
  /** Number of users in the cohort (or segment when breakdown is used) */
  cohortSize: number
 
  /** Number of users retained in this period */
  retainedUsers: number
 
  /** Retention rate as decimal (0-1), e.g., 0.45 for 45% */
  retentionRate: number
 
  /**
   * Breakdown values when breakdownDimensions are specified.
   * Keyed by dimension name (e.g., { "Events.country": "US", "Events.plan": "pro" })
   */
  breakdownValues?: Record<string, string | null>
}
 
/**
 * Retention capabilities per database engine
 */
export interface RetentionCapabilities {
  /** Whether database supports DATE_TRUNC natively */
  supportsDateTrunc: boolean
 
  /** Whether database supports DATE_DIFF with unit specification */
  supportsDateDiff: boolean
 
  /** Whether database supports generate_series for period generation */
  supportsGenerateSeries: boolean
}
 
/**
 * Type guard for multi-cube binding key
 */
export function isRetentionMultiCubeBindingKey(
  bindingKey: string | RetentionBindingKeyMapping[]
): bindingKey is RetentionBindingKeyMapping[] {
  return Array.isArray(bindingKey)
}
 
/**
 * Type guard for multi-cube time dimension (object form)
 */
export function isRetentionMultiCubeTimeDimension(
  timeDimension: string | RetentionTimeDimensionMapping
): timeDimension is RetentionTimeDimensionMapping {
  return typeof timeDimension === 'object' && timeDimension !== null && 'cube' in timeDimension
}
 
/**
 * Type guard for retention query
 */
export function isRetentionQuery(query: unknown): query is { retention: RetentionQueryConfig } {
  if (!query || typeof query !== 'object') return false
  const q = query as Record<string, unknown>
  return (
    q.retention !== undefined &&
    typeof q.retention === 'object' &&
    q.retention !== null
  )
}
 
/**
 * Extract cube name from a time dimension specification
 */
export function extractCubeFromTimeDimension(
  timeDimension: string | RetentionTimeDimensionMapping
): string {
  Iif (isRetentionMultiCubeTimeDimension(timeDimension)) {
    return timeDimension.cube
  }
  // String format: 'CubeName.dimensionName'
  const dotIndex = timeDimension.indexOf('.')
  Iif (dotIndex === -1) {
    throw new Error(`Invalid time dimension format: ${timeDimension}. Expected 'CubeName.dimensionName'`)
  }
  return timeDimension.substring(0, dotIndex)
}
 
/**
 * Extract dimension name from a time dimension specification
 */
export function extractDimensionFromTimeDimension(
  timeDimension: string | RetentionTimeDimensionMapping
): string {
  Iif (isRetentionMultiCubeTimeDimension(timeDimension)) {
    return timeDimension.dimension
  }
  // String format: 'CubeName.dimensionName'
  const dotIndex = timeDimension.indexOf('.')
  Iif (dotIndex === -1) {
    throw new Error(`Invalid time dimension format: ${timeDimension}. Expected 'CubeName.dimensionName'`)
  }
  return timeDimension.substring(dotIndex + 1)
}