Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
andehen committed Dec 9, 2024
1 parent 5da0de4 commit a2e97c1
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions posthog/management/commands/generate_experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import random
import secrets
import time
import uuid

from django.conf import settings
Expand All @@ -25,15 +26,18 @@ def handle(self, *args, **options):
raise ValueError("This command should only be run in development! DEBUG must be True.")

experiment_id = options.get("experiment_id")

# TODO: actually implement a seed
seed = options.get("seed") or secrets.token_hex(16)

if not experiment_id:
raise ValueError("Experiment ID is required")

# TODO: this can be a config file taken as an argument
experiment_config = {
"experiment_id": experiment_id,
"seed": seed,
"number_of_users": 10,
"number_of_users": 1000,
"start_timestamp": datetime.now() - timedelta(days=7),
"end_timestamp": datetime.now(),
"variants": {
Expand All @@ -52,31 +56,39 @@ def handle(self, *args, **options):
},
}

variants = list(experiment_config["variants"].keys())
variant_counts = {variant: 0 for variant in variants}
for _ in range(experiment_config["number_of_users"]):
variant = random.choices(
list(experiment_config["variants"].keys()),
variants,
weights=[v["weight"] for v in experiment_config["variants"].values()],
)[0]
variant_counts[variant] += 1
distinct_id = uuid.uuid4()
random_timestamp = random.uniform(
experiment_config["start_timestamp"], experiment_config["end_timestamp"] - timedelta(hours=1)
)
print(f"Generating data for user {distinct_id} at {random_timestamp} with variant {variant}")
posthoganalytics.capture(
distinct_id=distinct_id,

Check failure on line 72 in posthog/management/commands/generate_experiment_data.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "distinct_id" to "capture" has incompatible type "UUID"; expected "str"
event="$feature_flag_called",
timestamp=random_timestamp,

Check failure on line 74 in posthog/management/commands/generate_experiment_data.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "timestamp" to "capture" has incompatible type "float"; expected "datetime | None"
properties={
"feature_flag": experiment_config["experiment_id"],
f"feature/{experiment_config['experiment_id']}": variant,
"$feature_flag": experiment_config["experiment_id"],
f"$feature/{experiment_config['experiment_id']}": variant,
},
)

for action in experiment_config["variants"][variant]["actions"]:
if random.random() < action["probability"]:
print(f"Generating data for user {distinct_id} at {random_timestamp + timedelta(minutes=1)} with event {action['event']}")
posthoganalytics.capture(
distinct_id=distinct_id,

Check failure on line 84 in posthog/management/commands/generate_experiment_data.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "distinct_id" to "capture" has incompatible type "UUID"; expected "str"
event=action["event"],
timestamp=random_timestamp + timedelta(minutes=1),

Check failure on line 86 in posthog/management/commands/generate_experiment_data.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Unsupported operand types for + ("float" and "timedelta")
)

logging.info(f"Generated data for {experiment_config['experiment_id']} with seed {seed}")
logging.info(f"Variant counts: {variant_counts}")

# TODO: need to figure out how to wait for the data to be flushed. shutdown() doesn't work as expected.
time.sleep(10)
posthoganalytics.shutdown()

0 comments on commit a2e97c1

Please sign in to comment.