Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const makeExperiment = (
id: 1,
metrics: [],
name: 'Test Experiment',
rollout_decision: null,
rollout_variant: null,
started_at: null,
status: overrides.status ?? 'running',
updated_at: '',
Expand Down
5 changes: 3 additions & 2 deletions frontend/common/services/useExperiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ export const experimentService = service
endpoints: (builder) => ({
completeExperiment: builder.mutation<
Res['experiment'],
Req['experimentAction']
Req['completeExperiment']
>({
invalidatesTags: (_res, _err, { experimentId }) => [
{ id: experimentId, type: 'Experiment' },
{ id: 'LIST', type: 'Experiment' },
],
query: ({ environmentId, experimentId }) => ({
query: ({ body, environmentId, experimentId }) => ({
body,
method: 'POST',
url: `environments/${environmentId}/experiments/${experimentId}/complete/`,
}),
Expand Down
11 changes: 11 additions & 0 deletions frontend/common/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ChangeRequest,
ExpectedDirection,
ExperimentAudienceMatch,
ExperimentRolloutDecision,
ExperimentStatus,
MetricAggregation,
MetricDirection,
Expand Down Expand Up @@ -180,6 +181,11 @@ export type ExperimentRolloutBody = {
audience?: ExperimentAudienceBody
}

export type CompleteExperimentBody = {
rollout_decision: ExperimentRolloutDecision
rollout_variant?: number
}

export type UsageGroupBy = 'project' | 'environment'

export type Req = {
Expand Down Expand Up @@ -1213,6 +1219,11 @@ export type Req = {
}
}
experimentAction: { environmentId: string; experimentId: number }
completeExperiment: {
environmentId: string
experimentId: number
body: CompleteExperimentBody
}
updateExperiment: {
environmentId: string
experimentId: number
Expand Down
4 changes: 4 additions & 0 deletions frontend/common/types/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ export type FeatureType = 'STANDARD' | 'MULTIVARIATE'

export type ExperimentStatus = 'created' | 'running' | 'paused' | 'completed'

export type ExperimentRolloutDecision = 'control' | 'variant' | 'disable'

export type ExperimentStatusCounts = Record<ExperimentStatus, number>

export type MetricAggregation = 'count' | 'sum' | 'mean' | 'occurrence'
Expand Down Expand Up @@ -744,6 +746,8 @@ export type Experiment = {
started_at: string | null
ended_at: string | null
experiment_rollout?: ExperimentRollout
rollout_decision: ExperimentRolloutDecision | null
rollout_variant: number | null
}

export type ExperimentAudienceMatch = 'any' | 'all'
Expand Down
26 changes: 20 additions & 6 deletions frontend/web/components/base/SelectableCard/SelectableCard.scss
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
.selectable-card {
display: flex;
align-items: flex-start;
justify-content: space-between;
flex-direction: column;
height: 100%;
padding: 16px;
border-radius: var(--radius-xl);
border: 2px solid transparent;
box-shadow: inset 0 0 0 1px var(--color-border-default);
background: transparent;
cursor: pointer;
text-align: left;
width: 100%;
transition:
border-color var(--duration-fast) var(--easing-standard),
Expand All @@ -20,6 +16,11 @@
box-shadow: inset 0 0 0 1px var(--color-border-strong);
}

&:has(.selectable-card__body:focus-visible) {
outline: 2px solid var(--color-border-action);
outline-offset: 2px;
}

&--selected {
border-color: var(--color-border-action);
box-shadow: none;
Expand All @@ -28,10 +29,23 @@

&--disabled {
opacity: 0.5;
cursor: default;
pointer-events: none;
}

&__body {
display: flex;
flex: 1;
align-items: flex-start;
justify-content: space-between;
width: 100%;
padding: 16px;
text-align: left;

.selectable-card &:focus-visible {
outline: none;
}
}

&__content {
display: flex;
flex-direction: column;
Expand Down
64 changes: 36 additions & 28 deletions frontend/web/components/base/SelectableCard/SelectableCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ type SelectableCardProps = {
selected?: boolean
onClick: () => void
icon?: ReactNode
title: string
description: string
title: ReactNode
description: ReactNode
badge?: { label: string; variant: BadgeVariant }
tags?: string[]
disabled?: boolean
className?: string
// Extra content below the description (e.g. an illustrative preview).
children?: ReactNode
// Content inside the card but outside its button (e.g. a form control).
footer?: ReactNode
}

const SelectableCard: FC<SelectableCardProps> = ({
Expand All @@ -27,14 +29,15 @@ const SelectableCard: FC<SelectableCardProps> = ({
className,
description,
disabled,
footer,
icon,
onClick,
selected,
tags,
title,
}) => {
return (
<BareButton
<div
className={cn(
'selectable-card',
{
Expand All @@ -43,34 +46,39 @@ const SelectableCard: FC<SelectableCardProps> = ({
},
className,
)}
onClick={onClick}
disabled={disabled}
>
<div className='selectable-card__content'>
{icon && <div className='selectable-card__icon'>{icon}</div>}
<span className='selectable-card__title'>{title}</span>
<span className='selectable-card__description'>{description}</span>
{!!tags?.length && (
<div className='selectable-card__tags'>
{tags.map((tag) => (
<span key={tag} className='selectable-card__tag'>
{tag}
</span>
))}
<BareButton
className='selectable-card__body'
onClick={onClick}
disabled={disabled}
>
<div className='selectable-card__content'>
{icon && <div className='selectable-card__icon'>{icon}</div>}
<span className='selectable-card__title'>{title}</span>
<span className='selectable-card__description'>{description}</span>
{!!tags?.length && (
<div className='selectable-card__tags'>
{tags.map((tag) => (
<span key={tag} className='selectable-card__tag'>
{tag}
</span>
))}
</div>
)}
{children}
</div>
{badge && (
<div className='selectable-card__aside'>
<span
className={`selectable-card__badge selectable-card__badge--${badge.variant}`}
>
{badge.label}
</span>
</div>
)}
{children}
</div>
{badge && (
<div className='selectable-card__aside'>
<span
className={`selectable-card__badge selectable-card__badge--${badge.variant}`}
>
{badge.label}
</span>
</div>
)}
</BareButton>
</BareButton>
{footer && <div className='px-3 pb-3'>{footer}</div>}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: A disabled card can still expose an interactive footer control to keyboard users because disabled only reaches the body button and pointer-events: none does not prevent sequential focus. Mark the footer inert when disabled is true.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/web/components/base/SelectableCard/SelectableCard.tsx, line 80:

<comment>A disabled card can still expose an interactive footer control to keyboard users because `disabled` only reaches the body button and `pointer-events: none` does not prevent sequential focus. Mark the footer inert when `disabled` is true.</comment>

<file context>
@@ -43,34 +46,39 @@ const SelectableCard: FC<SelectableCardProps> = ({
-      )}
-    </BareButton>
+      </BareButton>
+      {footer && <div className='px-3 pb-3'>{footer}</div>}
+    </div>
   )
</file context>
Suggested change
{footer && <div className='px-3 pb-3'>{footer}</div>}
{footer && (
<div className='px-3 pb-3' inert={disabled || undefined}>
{footer}
</div>
)}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The footer slot drops numeric zero because footer is checked for truthiness. Preserve valid ReactNode values such as 0 when deciding whether to render the footer wrapper.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/web/components/base/SelectableCard/SelectableCard.tsx, line 80:

<comment>The footer slot drops numeric zero because `footer` is checked for truthiness. Preserve valid ReactNode values such as `0` when deciding whether to render the footer wrapper.</comment>

<file context>
@@ -43,34 +46,39 @@ const SelectableCard: FC<SelectableCardProps> = ({
-      )}
-    </BareButton>
+      </BareButton>
+      {footer && <div className='px-3 pb-3'>{footer}</div>}
+    </div>
   )
</file context>
Suggested change
{footer && <div className='px-3 pb-3'>{footer}</div>}
{(footer || footer === 0) && <div className='px-3 pb-3'>{footer}</div>}

</div>
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { FC, useMemo, useState } from 'react'
import {
useCompleteExperimentMutation,
useGetExperimentBayesianResultsQuery,
useGetExperimentQuery,
} from 'common/services/useExperiment'
import { useGetEnvironmentQuery } from 'common/services/useEnvironment'
import Banner from 'components/base/Banner'
import Button from 'components/base/forms/Button'
import ErrorMessage from 'components/ErrorMessage'
import ModalHR from 'components/modals/ModalHR'
import { getVariantIdentities } from 'components/experiments/results/derive'
import RolloutDecisionPicker from './RolloutDecisionPicker'
import {
RolloutSelection,
buildCompleteExperimentBody,
getControlSelectedMessage,
getDefaultSelection,
getServedVariant,
getVariantChoices,
} from './utils'

type CompleteExperimentModalProps = {
environmentId: string
experimentId: number
}

const CompleteExperimentModal: FC<CompleteExperimentModalProps> = ({
environmentId,
experimentId,
}) => {
const params = { environmentId, experimentId }
const { data: experiment, isError } = useGetExperimentQuery(params)
const { data: bayesianResults, isLoading: isLoadingResults } =

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: A failed Bayesian-results request is treated as a successful no-results response, so the modal can silently roll out control during an irreversible completion. Handle the results error separately and block completion with an error state.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/web/components/experiments/CompleteExperimentModal/CompleteExperimentModal.tsx, line 34:

<comment>A failed Bayesian-results request is treated as a successful no-results response, so the modal can silently roll out control during an irreversible completion. Handle the results error separately and block completion with an error state.</comment>

<file context>
@@ -0,0 +1,144 @@
+}) => {
+  const params = { environmentId, experimentId }
+  const { data: experiment, isError } = useGetExperimentQuery(params)
+  const { data: bayesianResults, isLoading: isLoadingResults } =
+    useGetExperimentBayesianResultsQuery(params)
+  const { data: environment } = useGetEnvironmentQuery({ id: environmentId })
</file context>

useGetExperimentBayesianResultsQuery(params)
const { data: environment } = useGetEnvironmentQuery({ id: environmentId })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: A failed environment request leaves this modal showing a loader forever with no error or retry path. Destructure the query error state and render an error state when the environment cannot load.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At frontend/web/components/experiments/CompleteExperimentModal/CompleteExperimentModal.tsx, line 36:

<comment>A failed environment request leaves this modal showing a loader forever with no error or retry path. Destructure the query error state and render an error state when the environment cannot load.</comment>

<file context>
@@ -0,0 +1,144 @@
+  const { data: experiment, isError } = useGetExperimentQuery(params)
+  const { data: bayesianResults, isLoading: isLoadingResults } =
+    useGetExperimentBayesianResultsQuery(params)
+  const { data: environment } = useGetEnvironmentQuery({ id: environmentId })
+  const [completeExperiment, { isLoading: isCompleting }] =
+    useCompleteExperimentMutation()
</file context>

const [completeExperiment, { isLoading: isCompleting }] =
useCompleteExperimentMutation()
const [selection, setSelection] = useState<RolloutSelection>()

const results = bayesianResults?.payload ?? undefined
const choices = useMemo(
() => (experiment ? getVariantChoices(experiment, results) : []),
[experiment, results],
)

if (isError) {
return (
<div className='modal-body'>
<ErrorMessage
errorMessageClass='mb-0'
error="We couldn't load this experiment."
/>
</div>
)
}

if (!experiment || !environment || isLoadingResults) {
return (
<div className='modal-body text-center'>
<Loader />
</div>
)
}

const defaultSelection = getDefaultSelection(experiment, choices, results)
const activeSelection = selection ?? defaultSelection
const control = getVariantIdentities(experiment.feature)[0]
const servedVariant = getServedVariant(activeSelection, control, choices)
const hasAudience = !!experiment.experiment_rollout?.audience?.segments.length

const handleComplete = async () => {
try {
await completeExperiment({
...params,
body: buildCompleteExperimentBody(activeSelection),
}).unwrap()
toast('Experiment completed')
closeModal()
} catch {
toast('Failed to complete experiment', 'danger')
}
}

return (
<>
<div className='modal-body d-flex flex-column gap-3 mx-0'>
<p className='mb-0'>
Choose what <b>{experiment.feature.name}</b> serves in{' '}
<b>{environment.name}</b> after <b>{experiment.name}</b> ends. Results
stay available; this can't be undone.
</p>
{defaultSelection.decision === 'control' &&
activeSelection.decision === 'control' && (
<Banner>{getControlSelectedMessage(experiment, results)}</Banner>
)}
<RolloutDecisionPicker
choices={choices}
control={control}
environmentName={environment.name}
featureName={experiment.feature.name}
selection={activeSelection}
onChange={setSelection}
/>
<div className='bg-surface-muted rounded-md p-3'>
The experiment's rollout override is removed and{' '}
{servedVariant ? (
<>
<b>100% of users</b> get <b>{servedVariant.name}</b>.
</>
) : (
<>
<b>{experiment.feature.name}</b> is turned off for everyone.
</>
)}
{hasAudience && (
<>
{' '}
This applies to everyone in <b>{environment.name}</b>, not only
the experiment's audience.
</>
)}
</div>
</div>
<ModalHR />
<div className='modal-footer'>
<Button className='mr-2' onClick={closeModal} theme='secondary'>
Cancel
</Button>
<Button
disabled={
isCompleting ||
(!servedVariant && activeSelection.decision !== 'disable')
}
onClick={handleComplete}
>
Complete experiment
</Button>
</div>
</>
)
}

export default CompleteExperimentModal
Loading
Loading