Skip to content

Commit

Permalink
track anonymous personfull events in usage reports
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith committed Jul 3, 2024
1 parent c2a6c45 commit 71b551d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 78 deletions.
7 changes: 6 additions & 1 deletion posthog/models/event/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def create_event(
) -> str:
if properties is None:
properties = {}
if properties.get("$is_identified") is None:
properties["$is_identified"] = True
if not timestamp:
timestamp = timezone.now()
assert timestamp is not None
Expand Down Expand Up @@ -224,11 +226,14 @@ def bulk_create_events(

except Group.DoesNotExist:
continue
properties = event.get("properties", {})
if properties.get("$is_identified") is None:
properties["$is_identified"] = True

event = {
"uuid": str(event["event_uuid"]) if event.get("event_uuid") else str(uuid.uuid4()),
"event": event["event"],
"properties": json.dumps(event["properties"]) if event.get("properties") else "{}",
"properties": json.dumps(properties),
"timestamp": timestamp,
"team_id": team_id,
"distinct_id": str(event["distinct_id"]),
Expand Down
172 changes: 101 additions & 71 deletions posthog/tasks/test/__snapshots__/test_usage_report.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,50 @@
'''

SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
event,
JSONExtractBool(properties, '$is_identified') as is_identified,
person_mode,
properties
FROM events
WHERE timestamp between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND event != '$feature_flag_called'
AND event NOT IN ('survey sent',
'survey shown',
'survey dismissed')
AND person_mode IN ('full',
'force_upgrade')
GROUP BY team_id
AND JSONExtractBool(properties, '$is_identified') = 0
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.10
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
sum(read_rows) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['hogql_query', 'HogQLQuery'])
AND query_start_time between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND access_method = ''
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.11
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
sum(query_duration_ms) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['hogql_query', 'HogQLQuery'])
AND query_start_time between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND access_method = ''
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.12
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -46,7 +77,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.11
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.13
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -63,7 +94,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.12
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.14
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -80,7 +111,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.13
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.15
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -97,7 +128,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.14
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.16
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -114,7 +145,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.15
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.17
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -131,7 +162,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.16
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.18
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -148,7 +179,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.17
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.19
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -165,7 +196,23 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.18
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.2
'''

SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
WHERE timestamp between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND event != '$feature_flag_called'
AND event NOT IN ('survey sent',
'survey shown',
'survey dismissed')
AND person_mode IN ('full',
'force_upgrade')
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.20
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
Expand All @@ -182,7 +229,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.19
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.21
'''

SELECT team_id,
Expand All @@ -193,22 +240,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.2
'''

SELECT team_id,
count(1) as count
FROM events
WHERE timestamp between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND ($group_0 != ''
OR $group_1 != ''
OR $group_2 != ''
OR $group_3 != ''
OR $group_4 != '')
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.20
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.22
'''

SELECT team,
Expand All @@ -230,6 +262,38 @@
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.3
'''

SELECT team_id,
count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count
FROM events
WHERE timestamp between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND event != '$feature_flag_called'
AND event NOT IN ('survey sent',
'survey shown',
'survey dismissed')
AND person_mode IN ('full',
'force_upgrade')
AND JSONExtractBool(properties, '$is_identified') = 0
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.4
'''

SELECT team_id,
count(1) as count
FROM events
WHERE timestamp between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND ($group_0 != ''
OR $group_1 != ''
OR $group_2 != ''
OR $group_3 != ''
OR $group_4 != '')
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.5
'''

SELECT team_id,
count(distinct session_id) as count
FROM
Expand All @@ -247,7 +311,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.4
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.6
'''

SELECT team_id,
Expand All @@ -267,7 +331,7 @@
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.5
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.7
'''

SELECT distinct_id as team,
Expand All @@ -280,7 +344,7 @@
GROUP BY team
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.6
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.8
'''

SELECT distinct_id as team,
Expand All @@ -293,47 +357,13 @@
GROUP BY team
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.7
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
sum(read_bytes) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['hogql_query', 'HogQLQuery'])
AND query_start_time between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND access_method = ''
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.8
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
sum(read_rows) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
AND is_initial_query = 1
AND query_type IN (['hogql_query', 'HogQLQuery'])
AND query_start_time between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND access_method = ''
GROUP BY team_id
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.9
'''
WITH JSONExtractInt(log_comment, 'team_id') as team_id,
JSONExtractString(log_comment, 'query_type') as query_type,
JSONExtractString(log_comment, 'access_method') as access_method
SELECT team_id,
sum(query_duration_ms) as count
sum(read_bytes) as count
FROM clusterAllReplicas(posthog, system.query_log)
WHERE (type = 'QueryFinish'
OR type = 'ExceptionWhileProcessing')
Expand Down
Loading

0 comments on commit 71b551d

Please sign in to comment.