Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(experiments HogQL): add migration command #26271

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
74 changes: 74 additions & 0 deletions posthog/management/commands/migrate_experiments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from django.core.management.base import BaseCommand
from posthog.hogql_queries.legacy_compatibility.filter_to_query import filter_to_query
from posthog.models import Experiment
from posthog.schema import ExperimentTrendsQuery, ExperimentFunnelsQuery
from sentry_sdk import capture_exception
import logging
from datetime import datetime

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class Command(BaseCommand):
help = "Migrate experiment metrics to new schema"

def handle(self, *args, **options):
logger.info("Starting experiments migration")

experiments = Experiment.objects.iterator(chunk_size=100)
for experiment in experiments:
try:
# Check for valid insight type
if not experiment.filters.get("insight"):
logger.warning(f"Skipping experiment {experiment.id}: 'insight' type is missing or invalid")
continue

# Update main metric
main_experiment_query = self.create_experiment_query(
filters=experiment.filters,
custom_exposure_filter=experiment.parameters.get("custom_exposure_filter")
if experiment.parameters
else None,
)
experiment.metrics = [main_experiment_query.model_dump()]

# Update secondary metrics
experiment.metrics_secondary = []
for secondary_metric in experiment.secondary_metrics:
secondary_query = self.create_experiment_query(
filters=secondary_metric["filters"],
name=secondary_metric["name"],
)
experiment.metrics_secondary.append(secondary_query.model_dump())

experiment.filters["migrated_at"] = str(datetime.now())

experiment.save()
logger.info(f"ID: {experiment.id} updated")
except Exception as e:
capture_exception(e)
logger.exception(f"Error migrating experiment {experiment.id}")

logger.info("Experiment migration completed")

def create_experiment_query(self, filters, custom_exposure_filter=None, name=None):
filters["explicit_date"] = True

if filters.get("insight") == "TRENDS":
count_query = filter_to_query(filters)
exposure_query = None
if custom_exposure_filter:
exposure_query = filter_to_query(custom_exposure_filter)
return ExperimentTrendsQuery(
count_query=count_query,
exposure_query=exposure_query,
name=name,
)
elif filters.get("insight") == "FUNNELS":
funnels_query = filter_to_query(filters)
return ExperimentFunnelsQuery(
funnels_query=funnels_query,
name=name,
)
raise ValueError(f"Unsupported or missing insight type: {filters.get('insight')}")
Loading