Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

7.2 KB
Raw Blame
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import {
  IsString,
  IsOptional,
  IsEnum,
  IsArray,
  ValidateNested,
  IsBoolean,
  IsInt,
  Min,
  Max,
  MaxLength,
  IsUUID,
  IsNotEmpty,
  Matches,
  ArrayMaxSize,
} from 'class-validator'
import { Type } from 'class-transformer'
import {
  ExperimentStatus,
  ExposureTrigger,
  MultipleVariantHandling,
  FeatureFlagMode,
} from '../entity/experiment.entity'
import { PID_REGEX } from '../../common/constants'

const KEY_REGEX = /^[a-zA-Z0-9_-]+$/

class ExperimentVariantDto {
  @ApiProperty()
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  name: string

  @ApiProperty()
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  @Matches(KEY_REGEX, {
    message:
      'Variant key must contain only alphanumeric characters, underscores, and hyphens',
  })
  key: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(300)
  description?: string

  @ApiProperty()
  @IsInt()
  @Min(0)
  @Max(100)
  rolloutPercentage: number

  @ApiProperty()
  @IsBoolean()
  isControl: boolean
}

export class CreateExperimentDto {
  @ApiProperty({
    description: 'The project ID',
    example: 'aHbCdEfGhIjK',
  })
  @IsNotEmpty()
  @Matches(PID_REGEX, { message: 'The provided Project ID (pid) is incorrect' })
  pid: string

  @ApiProperty()
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  name: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(500)
  description?: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(500)
  hypothesis?: string

  @ApiPropertyOptional({ enum: ExposureTrigger })
  @IsOptional()
  @IsEnum(ExposureTrigger)
  exposureTrigger?: ExposureTrigger

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(200)
  customEventName?: string

  @ApiPropertyOptional({ enum: MultipleVariantHandling })
  @IsOptional()
  @IsEnum(MultipleVariantHandling)
  multipleVariantHandling?: MultipleVariantHandling

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  filterInternalUsers?: boolean

  @ApiPropertyOptional({ enum: FeatureFlagMode })
  @IsOptional()
  @IsEnum(FeatureFlagMode)
  featureFlagMode?: FeatureFlagMode

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(100)
  @Matches(KEY_REGEX, {
    message:
      'Feature flag key must contain only alphanumeric characters, underscores, and hyphens',
  })
  featureFlagKey?: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID()
  existingFeatureFlagId?: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID()
  goalId?: string

  @ApiProperty({ type: [ExperimentVariantDto] })
  @IsArray()
  @ArrayMaxSize(20, {
    message: 'An experiment cannot have more than 20 variants',
  })
  @ValidateNested({ each: true })
  @Type(() => ExperimentVariantDto)
  variants: ExperimentVariantDto[]
}

export class UpdateExperimentDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  name?: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(500)
  description?: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(500)
  hypothesis?: string

  @ApiPropertyOptional({ enum: ExposureTrigger })
  @IsOptional()
  @IsEnum(ExposureTrigger)
  exposureTrigger?: ExposureTrigger

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(200)
  customEventName?: string

  @ApiPropertyOptional({ enum: MultipleVariantHandling })
  @IsOptional()
  @IsEnum(MultipleVariantHandling)
  multipleVariantHandling?: MultipleVariantHandling

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  filterInternalUsers?: boolean

  @ApiPropertyOptional({ enum: FeatureFlagMode })
  @IsOptional()
  @IsEnum(FeatureFlagMode)
  featureFlagMode?: FeatureFlagMode

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(100)
  @Matches(KEY_REGEX, {
    message:
      'Feature flag key must contain only alphanumeric characters, underscores, and hyphens',
  })
  featureFlagKey?: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID()
  existingFeatureFlagId?: string

  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID()
  goalId?: string

  @ApiPropertyOptional({ type: [ExperimentVariantDto] })
  @IsOptional()
  @IsArray()
  @ArrayMaxSize(20, {
    message: 'An experiment cannot have more than 20 variants',
  })
  @ValidateNested({ each: true })
  @Type(() => ExperimentVariantDto)
  variants?: ExperimentVariantDto[]
}

export class ExperimentDto {
  @ApiProperty()
  id: string

  @ApiProperty()
  name: string

  @ApiProperty()
  description: string | null

  @ApiProperty()
  hypothesis: string | null

  @ApiProperty({ enum: ExperimentStatus })
  status: ExperimentStatus

  @ApiProperty({ enum: ExposureTrigger })
  exposureTrigger: ExposureTrigger

  @ApiProperty()
  customEventName: string | null

  @ApiProperty({ enum: MultipleVariantHandling })
  multipleVariantHandling: MultipleVariantHandling

  @ApiProperty()
  filterInternalUsers: boolean

  @ApiProperty({ enum: FeatureFlagMode })
  featureFlagMode: FeatureFlagMode

  @ApiProperty()
  featureFlagKey: string | null

  @ApiProperty()
  startedAt: Date | null

  @ApiProperty()
  endedAt: Date | null

  @ApiProperty()
  pid: string

  @ApiProperty()
  goalId: string | null

  @ApiProperty()
  featureFlagId: string | null

  @ApiProperty({ type: [ExperimentVariantDto] })
  variants: ExperimentVariantDto[]

  @ApiProperty()
  created: Date
}

export class VariantResultDto {
  @ApiProperty()
  key: string

  @ApiProperty()
  name: string

  @ApiProperty()
  isControl: boolean

  @ApiProperty()
  exposures: number

  @ApiProperty()
  conversions: number

  @ApiProperty()
  conversionRate: number

  @ApiProperty()
  probabilityOfBeingBest: number

  @ApiProperty()
  improvement: number
}

class ExperimentChartDataDto {
  @ApiProperty({ type: [String], description: 'X-axis timestamps' })
  x: string[]

  @ApiProperty({
    type: 'object',
    additionalProperties: { type: 'array', items: { type: 'number' } },
    description:
      'Win probability for each variant over time. Keys are variant keys.',
  })
  winProbability: Record<string, number[]>
}

class ExperimentResultWindowDto {
  @ApiProperty()
  mode: 'selected' | 'active_overlap' | 'final'

  @ApiProperty()
  from: string

  @ApiProperty()
  to: string

  @ApiProperty()
  selectedFrom: string

  @ApiProperty()
  selectedTo: string

  @ApiPropertyOptional()
  activeFrom?: string

  @ApiPropertyOptional()
  activeTo?: string
}

export class ExperimentResultsDto {
  @ApiProperty()
  experimentId: string

  @ApiProperty()
  status: ExperimentStatus

  @ApiProperty({ type: [VariantResultDto] })
  variants: VariantResultDto[]

  @ApiProperty()
  totalExposures: number

  @ApiProperty()
  totalConversions: number

  @ApiProperty()
  hasWinner: boolean

  @ApiProperty()
  winnerKey: string | null

  @ApiProperty()
  confidenceLevel: number

  @ApiPropertyOptional({ type: ExperimentChartDataDto })
  chart?: ExperimentChartDataDto

  @ApiPropertyOptional({
    type: [String],
    description: 'Allowed time buckets when period is "all"',
  })
  timeBucket?: string[]

  @ApiPropertyOptional()
  resolvedTimeBucket?: string

  @ApiPropertyOptional({ type: ExperimentResultWindowDto })
  resultWindow?: ExperimentResultWindowDto

  @ApiPropertyOptional()
  isSegmented?: boolean
}