Skip to content

Commit

Permalink
fix(experiments): Avoid overriding feature flag payloads (#27102)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbachhuber authored Dec 20, 2024
1 parent 25ebebc commit 02ad064
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import { VariantScreenshot } from './VariantScreenshot'

export function DistributionModal({ experimentId }: { experimentId: Experiment['id'] }): JSX.Element {
const { experiment, experimentLoading, isDistributionModalOpen } = useValues(experimentLogic({ experimentId }))
const { closeDistributionModal, updateExperiment } = useActions(experimentLogic({ experimentId }))
const { closeDistributionModal, updateDistributionModal } = useActions(experimentLogic({ experimentId }))

const _featureFlagLogic = featureFlagLogic({ id: experiment.feature_flag?.id ?? null } as FeatureFlagLogicProps)
const { featureFlag, areVariantRolloutsValid, variantRolloutSum } = useValues(_featureFlagLogic)
const { setFeatureFlagFilters, distributeVariantsEqually, saveSidebarExperimentFeatureFlag } =
useActions(_featureFlagLogic)
const { setFeatureFlagFilters, distributeVariantsEqually } = useActions(_featureFlagLogic)

const handleRolloutPercentageChange = (index: number, value: number | undefined): void => {
if (!featureFlag?.filters?.multivariate || !value) {
Expand Down Expand Up @@ -61,13 +60,7 @@ export function DistributionModal({ experimentId }: { experimentId: Experiment['
</LemonButton>
<LemonButton
onClick={() => {
saveSidebarExperimentFeatureFlag(featureFlag)
updateExperiment({
holdout_id: experiment.holdout_id,
parameters: {
feature_flag_variants: featureFlag?.filters?.multivariate?.variants ?? [],
},
})
updateDistributionModal(featureFlag)
closeDistributionModal()
}}
type="primary"
Expand Down
25 changes: 25 additions & 0 deletions frontend/src/scenes/experiments/experimentLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { hasFormErrors, toParams } from 'lib/utils'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import {
indexToVariantKeyFeatureFlagPayloads,
variantKeyToIndexFeatureFlagPayloads,
} from 'scenes/feature-flags/featureFlagLogic'
import { validateFeatureFlagKey } from 'scenes/feature-flags/featureFlagLogic'
import { featureFlagsLogic } from 'scenes/feature-flags/featureFlagsLogic'
import { funnelDataLogic } from 'scenes/funnels/funnelDataLogic'
import { insightDataLogic } from 'scenes/insights/insightDataLogic'
import { cleanFilters, getDefaultEvent } from 'scenes/insights/utils/cleanFilters'
Expand Down Expand Up @@ -165,6 +170,8 @@ export const experimentLogic = kea<experimentLogicType>([
],
teamLogic,
['addProductIntent'],
featureFlagsLogic,
['updateFlag'],
],
})),
actions({
Expand Down Expand Up @@ -262,6 +269,7 @@ export const experimentLogic = kea<experimentLogicType>([
openPrimaryMetricModal: (index: number) => ({ index }),
closePrimaryMetricModal: true,
setPrimaryMetricsResultErrors: (errors: any[]) => ({ errors }),
updateDistributionModal: (featureFlag: FeatureFlagType) => ({ featureFlag }),
}),
reducers({
experiment: [
Expand Down Expand Up @@ -795,6 +803,23 @@ export const experimentLogic = kea<experimentLogicType>([
lemonToast.error('Failed to update experiment variant images')
}
},
updateDistributionModal: async ({ featureFlag }) => {
const { created_at, id, ...flag } = featureFlag

const preparedFlag = indexToVariantKeyFeatureFlagPayloads(flag)

const savedFlag = await api.update(
`api/projects/${values.currentProjectId}/feature_flags/${id}`,
preparedFlag
)

const updatedFlag = variantKeyToIndexFeatureFlagPayloads(savedFlag)
actions.updateFlag(updatedFlag)

actions.updateExperiment({
holdout_id: values.experiment.holdout_id,
})
},
})),
loaders(({ actions, props, values }) => ({
experiment: {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/scenes/feature-flags/featureFlagLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const variantKeyToIndexFeatureFlagPayloads = (flag: FeatureFlagType): Fea
}
}

const indexToVariantKeyFeatureFlagPayloads = (flag: Partial<FeatureFlagType>): Partial<FeatureFlagType> => {
export const indexToVariantKeyFeatureFlagPayloads = (flag: Partial<FeatureFlagType>): Partial<FeatureFlagType> => {
if (flag.filters?.multivariate) {
const newPayloads: Record<string, JsonType> = {}
flag.filters.multivariate.variants.forEach(({ key }, index) => {
Expand Down

0 comments on commit 02ad064

Please sign in to comment.