def _create_anonymous_users(team: Team, base_url: str) -> None: with open(Path('posthog/demo_data.json').resolve(), 'r') as demo_data_file: demo_data = json.load(demo_data_file) Person.objects.bulk_create([ Person(team=team, properties={'is_demo': True}) for _ in range(0, 100) ]) distinct_ids: List[PersonDistinctId] = [] events: List[Event] = [] days_ago = 7 demo_data_index = 0 for index, person in enumerate(Person.objects.filter(team=team)): if index > 0 and index % 14 == 0: days_ago -= 1 distinct_id = str(uuid.uuid4()) distinct_ids.append(PersonDistinctId(team=team, person=person, distinct_id=distinct_id)) date = now() - relativedelta(days=days_ago) browser = random.choice(['Chrome', 'Safari', 'Firefox']) events.append(Event(team=team, event='$pageview', distinct_id=distinct_id, properties={'$current_url': base_url, '$browser': browser, '$lib': 'web'}, timestamp=date)) if index % 3 == 0: person.properties.update(demo_data[demo_data_index]) person.save() demo_data_index += 1 Event.objects.create( team=team, distinct_id=distinct_id, event='$autocapture', properties={'$current_url': base_url}, timestamp=date + relativedelta(seconds=14), elements=[ Element(tag_name='a', href='/demo/1', attr_class=['btn', 'btn-success'], attr_id='sign-up', text='Sign up') ]) events.append(Event(event='$pageview', team=team, distinct_id=distinct_id, properties={'$current_url': '%s1/' % base_url, '$browser': browser, '$lib': 'web'}, timestamp=date + relativedelta(seconds=15))) if index % 4 == 0: Event.objects.create( team=team, event='$autocapture', distinct_id=distinct_id, properties={'$current_url': '%s1/' % base_url}, timestamp=date + relativedelta(seconds=29), elements=[ Element(tag_name='button', attr_class=['btn', 'btn-success'], text='Sign up!') ]) events.append(Event(event='$pageview', team=team, distinct_id=distinct_id, properties={'$current_url': '%s2/' % base_url, '$browser': browser, '$lib': 'web'}, timestamp=date + relativedelta(seconds=30))) if index % 5 == 0: Event.objects.create( team=team, event='$autocapture', distinct_id=distinct_id, properties={'$current_url': '%s2/' % base_url}, timestamp=date + relativedelta(seconds=59), elements=[ Element(tag_name='button', attr_class=['btn', 'btn-success'], text='Pay $10') ]) events.append(Event(event='$pageview', team=team, distinct_id=distinct_id, properties={'$current_url': '%s3/' % base_url, '$browser': browser, '$lib': 'web'}, timestamp=date + relativedelta(seconds=60))) PersonDistinctId.objects.bulk_create(distinct_ids) Event.objects.bulk_create(events)
def test_get_event_by_id(self): event_id: Union[str, int] = 12345 if settings.PRIMARY_DB == AnalyticsDBMS.CLICKHOUSE: from ee.clickhouse.models.event import create_event event_id = "01793986-dc4b-0000-93e8-1fb646df3a93" Event( pk=create_event( team=self.team, event="event", distinct_id="1", timestamp=timezone.now(), event_uuid=uuid.UUID(event_id), ) ) else: event_factory(team=self.team, event="event", distinct_id="1", timestamp=timezone.now(), id=event_id) response = self.client.get(f"/api/projects/{self.team.id}/events/{event_id}",) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json()["event"], "event") response = self.client.get(f"/api/projects/{self.team.id}/events/123456",) # EE will inform the user the ID passed is not a valid UUID self.assertIn(response.status_code, [status.HTTP_404_NOT_FOUND, status.HTTP_400_BAD_REQUEST]) response = self.client.get(f"/api/projects/{self.team.id}/events/im_a_string_not_an_integer",) self.assertIn(response.status_code, [status.HTTP_404_NOT_FOUND, status.HTTP_400_BAD_REQUEST])
def _generate_psql_data(self, team, n_events, n_days): distinct_ids = [] for i in range(0, n_events): distinct_id = str(UUIDT()) distinct_ids.append(distinct_id) Person.objects.create(team=team, distinct_ids=[distinct_id], properties={"is_demo": True}) Event.objects.bulk_create( Event( event="$purchase", team=team, distinct_id=distinct_ids[i], properties={ "plan": PRICING_TIERS[_deterministic_random_value( distinct_ids[i])][0], "purchase_value": PRICING_TIERS[_deterministic_random_value(distinct_ids[i])] [1], }, timestamp=now() - relativedelta(days=random.randint(0, n_days)), ) for i in range(0, n_events))
def test_pagination(self): events = [] for index in range(0, 150): events.append(Event(team=self.team, event="some event", distinct_id="1")) Event.objects.bulk_create(events) response = self.client.get("/api/event/?distinct_id=1").json() self.assertIn("http://testserver/api/event/?distinct_id=1&before=", response["next"]) page2 = self.client.get(response["next"]).json() self.assertEqual(len(page2["results"]), 50)
def test_pagination(self): events = [] for index in range(0, 150): events.append( Event(team=self.team, event='some event', distinct_id='1')) Event.objects.bulk_create(events) response = self.client.get('/api/event/?distinct_id=1').json() self.assertIn('distinct_id=1', response['next']) page2 = self.client.get(response['next']).json() self.assertEqual(len(page2['results']), 50)
def bulk_import_events(self): if is_clickhouse_enabled(): from ee.clickhouse.demo import bulk_create_events, bulk_create_session_recording_events bulk_create_events(self.events, team=self.team) bulk_create_session_recording_events(self.snapshots, team_id=self.team.pk) else: Event.objects.bulk_create([Event(**kw, team=self.team) for kw in self.events]) SessionRecordingEvent.objects.bulk_create( [SessionRecordingEvent(**kw, team=self.team) for kw in self.snapshots] )
def _create_anonymous_users(team: Team, base_url: str) -> None: with open(Path("posthog/demo_data.json").resolve(), "r") as demo_data_file: demo_data = json.load(demo_data_file) Person.objects.bulk_create([Person(team=team, properties={"is_demo": True}) for _ in range(0, 100)]) distinct_ids: List[PersonDistinctId] = [] events: List[Event] = [] days_ago = 7 demo_data_index = 0 for index, person in enumerate(Person.objects.filter(team=team)): if index > 0 and index % 14 == 0: days_ago -= 1 distinct_id = str(UUIDT()) distinct_ids.append(PersonDistinctId(team=team, person=person, distinct_id=distinct_id)) # Add first user more 3 distinct id's if index == 0: for _ in range(0, 3): distinct_ids.append(PersonDistinctId(team=team, person=person, distinct_id=str(UUIDT()))) date = now() - relativedelta(days=days_ago) browser = random.choice(["Chrome", "Safari", "Firefox"]) events.append( Event( team=team, event="$pageview", distinct_id=distinct_id, properties={"$current_url": base_url, "$browser": browser, "$lib": "web"}, timestamp=date, ) ) if index % 3 == 0: person.properties.update(demo_data[demo_data_index]) person.is_identified = True person.save() demo_data_index += 1 Event.objects.create( team=team, distinct_id=distinct_id, event="$autocapture", properties={"$current_url": base_url, "$browser": browser, "$lib": "web", "$event_type": "click",}, timestamp=date + relativedelta(seconds=14), elements=[ Element( tag_name="a", href="/demo/1", attr_class=["btn", "btn-success"], attr_id="sign-up", text="Sign up", ), Element(tag_name="form", attr_class=["form"]), Element(tag_name="div", attr_class=["container"]), Element(tag_name="body"), Element(tag_name="html"), ], ) events.append( Event( event="$pageview", team=team, distinct_id=distinct_id, properties={"$current_url": "%s/1" % base_url, "$browser": browser, "$lib": "web",}, timestamp=date + relativedelta(seconds=15), ) ) if index % 4 == 0: Event.objects.create( team=team, event="$autocapture", distinct_id=distinct_id, properties={ "$current_url": "%s/1" % base_url, "$browser": browser, "$lib": "web", "$event_type": "click", }, timestamp=date + relativedelta(seconds=29), elements=[ Element(tag_name="button", attr_class=["btn", "btn-success"], text="Sign up!",), Element(tag_name="form", attr_class=["form"]), Element(tag_name="div", attr_class=["container"]), Element(tag_name="body"), Element(tag_name="html"), ], ) events.append( Event( event="$pageview", team=team, distinct_id=distinct_id, properties={"$current_url": "%s/2" % base_url, "$browser": browser, "$lib": "web",}, timestamp=date + relativedelta(seconds=30), ) ) if index % 5 == 0: Event.objects.create( team=team, event="$autocapture", distinct_id=distinct_id, properties={ "$current_url": "%s/2" % base_url, "$browser": browser, "$lib": "web", "$event_type": "click", }, timestamp=date + relativedelta(seconds=59), elements=[ Element(tag_name="button", attr_class=["btn", "btn-success"], text="Pay $10",), Element(tag_name="form", attr_class=["form"]), Element(tag_name="div", attr_class=["container"]), Element(tag_name="body"), Element(tag_name="html"), ], ) events.append( Event( event="purchase", team=team, distinct_id=distinct_id, properties={"price": 10}, timestamp=date + relativedelta(seconds=60), ) ) events.append( Event( event="$pageview", team=team, distinct_id=distinct_id, properties={"$current_url": "%s/3" % base_url, "$browser": browser, "$lib": "web",}, timestamp=date + relativedelta(seconds=60), ) ) team.event_properties_numerical.append("purchase") team.save() PersonDistinctId.objects.bulk_create(distinct_ids) Event.objects.bulk_create(events)
def _create_event(**kwargs): kwargs.update({"event_uuid": uuid4()}) return Event(pk=create_event(**kwargs))