Skip to content

Commit

Permalink
chore(experiments): hogql migration cleanup - Goal (#27264)
Browse files Browse the repository at this point in the history
  • Loading branch information
andehen authored Jan 6, 2025
1 parent 86dfec3 commit baa6a18
Showing 1 changed file with 47 additions and 89 deletions.
136 changes: 47 additions & 89 deletions frontend/src/scenes/experiments/ExperimentView/Goal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { LemonButton, LemonDivider, Tooltip } from '@posthog/lemon-ui'
import { useActions, useValues } from 'kea'
import { InsightLabel } from 'lib/components/InsightLabel'
import { PropertyFilterButton } from 'lib/components/PropertyFilters/components/PropertyFilterButton'
import { EXPERIMENT_DEFAULT_DURATION, FEATURE_FLAGS } from 'lib/constants'
import { EXPERIMENT_DEFAULT_DURATION } from 'lib/constants'
import { dayjs } from 'lib/dayjs'
import { useState } from 'react'

import { ExperimentFunnelsQuery, ExperimentTrendsQuery, FunnelsQuery, NodeKind, TrendsQuery } from '~/queries/schema'
import { ActionFilter, AnyPropertyFilter, ChartDisplayType, Experiment, FilterType, InsightType } from '~/types'

import { experimentLogic, getDefaultFilters, getDefaultFunnelsMetric } from '../experimentLogic'
import { experimentLogic, getDefaultFunnelsMetric } from '../experimentLogic'
import { PrimaryTrendsExposureModal } from '../Metrics/PrimaryTrendsExposureModal'

export function MetricDisplayTrends({ query }: { query: TrendsQuery | undefined }): JSX.Element {
Expand Down Expand Up @@ -113,21 +113,14 @@ export function MetricDisplayOld({ filters }: { filters?: FilterType }): JSX.Ele
}

export function ExposureMetric({ experimentId }: { experimentId: Experiment['id'] }): JSX.Element {
const { experiment, featureFlags } = useValues(experimentLogic({ experimentId }))
const { experiment } = useValues(experimentLogic({ experimentId }))
const { updateExperimentGoal, loadExperiment, setExperiment, setEditingPrimaryMetricIndex } = useActions(
experimentLogic({ experimentId })
)
const [isModalOpen, setIsModalOpen] = useState(false)

const metricIdx = 0

// :FLAG: CLEAN UP AFTER MIGRATION
let hasCustomExposure = false
if (featureFlags[FEATURE_FLAGS.EXPERIMENTS_HOGQL]) {
hasCustomExposure = !!(experiment.metrics[metricIdx] as ExperimentTrendsQuery).exposure_query
} else {
hasCustomExposure = !!experiment.parameters?.custom_exposure_filter
}
const hasCustomExposure = !!(experiment.metrics[metricIdx] as ExperimentTrendsQuery).exposure_query

return (
<>
Expand All @@ -139,15 +132,8 @@ export function ExposureMetric({ experimentId }: { experimentId: Experiment['id'
<IconInfo className="ml-1 text-muted text-sm" />
</Tooltip>
</div>
{/* :FLAG: CLEAN UP AFTER MIGRATION */}
{featureFlags[FEATURE_FLAGS.EXPERIMENTS_HOGQL] ? (
hasCustomExposure ? (
<MetricDisplayTrends query={(experiment.metrics[0] as ExperimentTrendsQuery).exposure_query} />
) : (
<span className="description">Default via $feature_flag_called events</span>
)
) : hasCustomExposure ? (
<MetricDisplayOld filters={experiment.parameters.custom_exposure_filter} />
{hasCustomExposure ? (
<MetricDisplayTrends query={(experiment.metrics[0] as ExperimentTrendsQuery).exposure_query} />
) : (
<span className="description">Default via $feature_flag_called events</span>
)}
Expand All @@ -157,51 +143,39 @@ export function ExposureMetric({ experimentId }: { experimentId: Experiment['id'
type="secondary"
size="xsmall"
onClick={() => {
if (featureFlags[FEATURE_FLAGS.EXPERIMENTS_HOGQL]) {
if (!hasCustomExposure) {
setExperiment({
...experiment,
metrics: experiment.metrics.map((metric, idx) =>
idx === metricIdx
? {
...metric,
exposure_query: {
kind: NodeKind.TrendsQuery,
series: [
{
kind: NodeKind.EventsNode,
name: '$pageview',
event: '$pageview',
},
],
interval: 'day',
dateRange: {
date_from: dayjs()
.subtract(EXPERIMENT_DEFAULT_DURATION, 'day')
.format('YYYY-MM-DDTHH:mm'),
date_to: dayjs().endOf('d').format('YYYY-MM-DDTHH:mm'),
explicitDate: true,
},
trendsFilter: {
display: ChartDisplayType.ActionsLineGraph,
if (!hasCustomExposure) {
setExperiment({
...experiment,
metrics: experiment.metrics.map((metric, idx) =>
idx === metricIdx
? {
...metric,
exposure_query: {
kind: NodeKind.TrendsQuery,
series: [
{
kind: NodeKind.EventsNode,
name: '$pageview',
event: '$pageview',
},
filterTestAccounts: true,
],
interval: 'day',
dateRange: {
date_from: dayjs()
.subtract(EXPERIMENT_DEFAULT_DURATION, 'day')
.format('YYYY-MM-DDTHH:mm'),
date_to: dayjs().endOf('d').format('YYYY-MM-DDTHH:mm'),
explicitDate: true,
},
trendsFilter: {
display: ChartDisplayType.ActionsLineGraph,
},
}
: metric
),
})
}
} else {
if (!hasCustomExposure) {
setExperiment({
...experiment,
parameters: {
...experiment.parameters,
custom_exposure_filter: getDefaultFilters(InsightType.TRENDS, undefined),
},
})
}
filterTestAccounts: true,
},
}
: metric
),
})
}
setEditingPrimaryMetricIndex(metricIdx)
setIsModalOpen(true)
Expand Down Expand Up @@ -244,14 +218,12 @@ export function ExposureMetric({ experimentId }: { experimentId: Experiment['id'
}

export function Goal(): JSX.Element {
const { experiment, experimentId, _getMetricType, experimentMathAggregationForTrends, hasGoalSet, featureFlags } =
const { experiment, experimentId, _getMetricType, experimentMathAggregationForTrends, hasGoalSet } =
useValues(experimentLogic)
const { setExperiment, openPrimaryMetricModal } = useActions(experimentLogic)
const metricType = _getMetricType(experiment.metrics[0])

// :FLAG: CLEAN UP AFTER MIGRATION
const isDataWarehouseMetric =
featureFlags[FEATURE_FLAGS.EXPERIMENTS_HOGQL] &&
metricType === InsightType.TRENDS &&
(experiment.metrics[0] as ExperimentTrendsQuery).count_query?.series[0].kind === NodeKind.DataWarehouseNode

Expand Down Expand Up @@ -286,17 +258,10 @@ export function Goal(): JSX.Element {
size="small"
data-attr="add-experiment-goal"
onClick={() => {
if (featureFlags[FEATURE_FLAGS.EXPERIMENTS_HOGQL]) {
setExperiment({
...experiment,
metrics: [getDefaultFunnelsMetric()],
})
} else {
setExperiment({
...experiment,
filters: getDefaultFilters(InsightType.FUNNELS, undefined),
})
}
setExperiment({
...experiment,
metrics: [getDefaultFunnelsMetric()],
})
openPrimaryMetricModal(0)
}}
>
Expand All @@ -309,19 +274,12 @@ export function Goal(): JSX.Element {
<div className="card-secondary mb-2 mt-2">
{metricType === InsightType.FUNNELS ? 'Conversion goal steps' : 'Trend goal'}
</div>
{/* :FLAG: CLEAN UP AFTER MIGRATION */}
{featureFlags[FEATURE_FLAGS.EXPERIMENTS_HOGQL] ? (
metricType === InsightType.FUNNELS ? (
<MetricDisplayFunnels
query={(experiment.metrics[0] as ExperimentFunnelsQuery).funnels_query}
/>
) : (
<MetricDisplayTrends
query={(experiment.metrics[0] as ExperimentTrendsQuery).count_query}
/>
)
{metricType === InsightType.FUNNELS ? (
<MetricDisplayFunnels
query={(experiment.metrics[0] as ExperimentFunnelsQuery).funnels_query}
/>
) : (
<MetricDisplayOld filters={experiment.filters} />
<MetricDisplayTrends query={(experiment.metrics[0] as ExperimentTrendsQuery).count_query} />
)}
<LemonButton size="xsmall" type="secondary" onClick={() => openPrimaryMetricModal(0)}>
Change goal
Expand Down

0 comments on commit baa6a18

Please sign in to comment.