Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(billing): prefilter usage report event metrics (#64281)

Pawel Cebula168d86cce44b
diff --git a/posthog/tasks/test/__snapshots__/test_usage_report.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
index 00f8956078b1..5a0c11c24254 100644
--- a/posthog/tasks/test/__snapshots__/test_usage_report.ambr
+++ b/posthog/tasks/test/__snapshots__/test_usage_report.ambr
@@ -7,12 +7,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 00:00:00'
- AND timestamp < '2022-01-10 02:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 00:00:00'
+ AND timestamp < '2022-01-10 02:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.1
@@ -23,12 +44,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 02:00:00'
- AND timestamp < '2022-01-10 04:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 02:00:00'
+ AND timestamp < '2022-01-10 04:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.10
@@ -39,12 +81,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 20:00:00'
- AND timestamp < '2022-01-10 22:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 20:00:00'
+ AND timestamp < '2022-01-10 22:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.11
@@ -55,12 +118,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 22:00:00'
- AND timestamp < '2022-01-10 23:59:59'
+ FROM events PREWHERE timestamp >= '2022-01-10 22:00:00'
+ AND timestamp < '2022-01-10 23:59:59'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.12
@@ -179,12 +263,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 04:00:00'
- AND timestamp < '2022-01-10 06:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 04:00:00'
+ AND timestamp < '2022-01-10 06:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.20
@@ -319,12 +424,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 06:00:00'
- AND timestamp < '2022-01-10 08:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 06:00:00'
+ AND timestamp < '2022-01-10 08:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.30
@@ -475,12 +601,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 08:00:00'
- AND timestamp < '2022-01-10 10:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 08:00:00'
+ AND timestamp < '2022-01-10 10:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.40
@@ -698,12 +845,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 10:00:00'
- AND timestamp < '2022-01-10 12:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 10:00:00'
+ AND timestamp < '2022-01-10 12:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.50
@@ -909,12 +1077,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 12:00:00'
- AND timestamp < '2022-01-10 14:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 12:00:00'
+ AND timestamp < '2022-01-10 14:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.60
@@ -1079,12 +1268,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 14:00:00'
- AND timestamp < '2022-01-10 16:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 14:00:00'
+ AND timestamp < '2022-01-10 16:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.70
@@ -1108,12 +1318,33 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 16:00:00'
- AND timestamp < '2022-01-10 18:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 16:00:00'
+ AND timestamp < '2022-01-10 18:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.9
@@ -1124,11 +1355,32 @@
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = 'posthog-openclaw', 'openclaw_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node'
AND replaceRegexpAll(JSONExtractRaw(properties, '$ai_lib'), '^"|"$', '') = '@posthog/pi', 'posthog_pi_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-node', 'node_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-edge', 'edge_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-convex', 'convex_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-android', 'android_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-flutter', 'flutter_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ios', 'ios_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-go', 'go_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-java', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-server', 'java_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-react-native', 'react_native_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-ruby', 'ruby_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-python', 'python_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-php', 'php_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-dotnet', 'dotnet_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-elixir', 'elixir_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-unity', 'unity_events', replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') = 'posthog-rs', 'rust_events', 'other') AS metric,
count(1) as count
- FROM events
- WHERE timestamp >= '2022-01-10 18:00:00'
- AND timestamp < '2022-01-10 20:00:00'
+ FROM events PREWHERE timestamp >= '2022-01-10 18:00:00'
+ AND timestamp < '2022-01-10 20:00:00'
+ WHERE (event LIKE 'helicone%'
+ OR event LIKE 'langfuse%'
+ OR event LIKE 'keywords_ai%'
+ OR event LIKE 'traceloop%'
+ OR replaceRegexpAll(JSONExtractRaw(properties, '$lib'), '^"|"$', '') IN ('web',
+ 'js',
+ 'posthog-node',
+ 'posthog-edge',
+ 'posthog-convex',
+ 'posthog-android',
+ 'posthog-flutter',
+ 'posthog-ios',
+ 'posthog-go',
+ 'posthog-java',
+ 'posthog-server',
+ 'posthog-react-native',
+ 'posthog-ruby',
+ 'posthog-python',
+ 'posthog-php',
+ 'posthog-dotnet',
+ 'posthog-elixir',
+ 'posthog-unity',
+ 'posthog-rs'))
GROUP BY team_id,
metric
- HAVING metric != 'other'
'''
# ---
diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py
index cc48a9e36d52..3481b1eae2f2 100644
--- a/posthog/tasks/test/test_usage_report.py
+++ b/posthog/tasks/test/test_usage_report.py
@@ -58,6 +58,7 @@ from posthog.tasks.usage_report import (
_get_team_report,
_get_teams_for_usage_reports,
capture_event,
+ get_all_event_metrics_in_period,
get_instance_metadata,
get_teams_with_query_metric,
has_non_zero_usage,
@@ -1363,6 +1364,32 @@ class TestQueryUsageReportSQL:
assert params["end"] == end
assert params["event_time_end"] == end + timedelta(hours=6)
+ @patch("posthog.tasks.usage_report._execute_split_query", return_value={})
+ @patch("posthog.tasks.usage_report.get_property_string_expr")
+ def test_get_all_event_metrics_prefilters_tracked_metrics(
+ self,
+ mock_get_property_string_expr: MagicMock,
+ mock_execute_split_query: MagicMock,
+ ) -> None:
+ mock_get_property_string_expr.side_effect = [("lib_expr", True), ("ai_lib_expr", True)]
+ begin = datetime(2026, 6, 15, tzinfo=tzutc())
+ end = begin + timedelta(days=1)
+
+ get_all_event_metrics_in_period(begin, end)
+
+ query = mock_execute_split_query.call_args.kwargs["query_template"]
+ assert "PREWHERE timestamp >= %(begin)s AND timestamp < %(end)s" in query
+ assert "WHERE" in query
+ assert "event LIKE 'helicone%%'" in query
+ assert "event LIKE 'langfuse%%'" in query
+ assert "event LIKE 'keywords_ai%%'" in query
+ assert "event LIKE 'traceloop%%'" in query
+ assert "OR lib_expr IN (" in query
+ assert "'posthog-node'" in query
+ assert "'posthog-rs'" in query
+ assert "HAVING metric != 'other'" not in query
+ assert mock_execute_split_query.call_args.kwargs["num_splits"] == 12
+
@freeze_time("2022-01-10T00:01:00Z")
class TestFeatureFlagsUsageReport(ClickhouseDestroyTablesMixin, TestCase, ClickhouseTestMixin):
diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py
index d773db09c4a6..1a3d308fc51f 100644
--- a/posthog/tasks/usage_report.py
+++ b/posthog/tasks/usage_report.py
@@ -659,43 +659,62 @@ def get_all_event_metrics_in_period(begin: datetime, end: datetime) -> dict[str,
# Check if $lib and $ai_lib are materialized
lib_expression, _ = get_property_string_expr("events", "$lib", "'$lib'", "properties")
ai_lib_expression, _ = get_property_string_expr("events", "$ai_lib", "'$ai_lib'", "properties")
+ event_prefix_metrics = (
+ ("helicone%%", "helicone_events"),
+ ("langfuse%%", "langfuse_events"),
+ ("keywords_ai%%", "keywords_ai_events"),
+ ("traceloop%%", "traceloop_events"),
+ )
+ sdk_metrics = (
+ ("web", None, "web_events"),
+ ("js", None, "web_lite_events"),
+ ("posthog-node", "posthog-openclaw", "openclaw_events"),
+ ("posthog-node", "@posthog/pi", "posthog_pi_events"),
+ ("posthog-node", None, "node_events"),
+ ("posthog-edge", None, "edge_events"),
+ ("posthog-convex", None, "convex_events"),
+ ("posthog-android", None, "android_events"),
+ ("posthog-flutter", None, "flutter_events"),
+ ("posthog-ios", None, "ios_events"),
+ ("posthog-go", None, "go_events"),
+ ("posthog-java", None, "java_events"),
+ ("posthog-server", None, "java_events"),
+ ("posthog-react-native", None, "react_native_events"),
+ ("posthog-ruby", None, "ruby_events"),
+ ("posthog-python", None, "python_events"),
+ ("posthog-php", None, "php_events"),
+ ("posthog-dotnet", None, "dotnet_events"),
+ ("posthog-elixir", None, "elixir_events"),
+ ("posthog-unity", None, "unity_events"),
+ ("posthog-rs", None, "rust_events"),
+ )
+ tracked_libs = tuple(dict.fromkeys(lib for lib, _ai_lib, _metric in sdk_metrics))
+ quoted_tracked_libs = ", ".join(f"'{lib}'" for lib in tracked_libs)
+ metric_filter_conditions = [f"event LIKE '{event_prefix}'" for event_prefix, _metric in event_prefix_metrics]
+ metric_filter_conditions.append(f"{lib_expression} IN ({quoted_tracked_libs})")
+ metric_filter = "\n OR ".join(metric_filter_conditions)
+ metric_conditions = [f"event LIKE '{event_prefix}', '{metric}'" for event_prefix, metric in event_prefix_metrics]
+ for lib, ai_lib, metric in sdk_metrics:
+ if ai_lib is None:
+ metric_conditions.append(f"{lib_expression} = '{lib}', '{metric}'")
+ else:
+ metric_conditions.append(f"{lib_expression} = '{lib}' AND {ai_lib_expression} = '{ai_lib}', '{metric}'")
+ metric_conditions.append("'other'")
+ metric_expression = ",\n ".join(metric_conditions)
query_template = f"""
SELECT
team_id,
multiIf(
- event LIKE 'helicone%%', 'helicone_events',
- event LIKE 'langfuse%%', 'langfuse_events',
- event LIKE 'keywords_ai%%', 'keywords_ai_events',
- event LIKE 'traceloop%%', 'traceloop_events',
- {lib_expression} = 'web', 'web_events',
- {lib_expression} = 'js', 'web_lite_events',
- {lib_expression} = 'posthog-node' AND {ai_lib_expression} = 'posthog-openclaw', 'openclaw_events',
- {lib_expression} = 'posthog-node' AND {ai_lib_expression} = '@posthog/pi', 'posthog_pi_events',
- {lib_expression} = 'posthog-node', 'node_events',
- {lib_expression} = 'posthog-edge', 'edge_events',
- {lib_expression} = 'posthog-convex', 'convex_events',
- {lib_expression} = 'posthog-android', 'android_events',
- {lib_expression} = 'posthog-flutter', 'flutter_events',
- {lib_expression} = 'posthog-ios', 'ios_events',
- {lib_expression} = 'posthog-go', 'go_events',
- {lib_expression} = 'posthog-java', 'java_events',
- {lib_expression} = 'posthog-server', 'java_events',
- {lib_expression} = 'posthog-react-native', 'react_native_events',
- {lib_expression} = 'posthog-ruby', 'ruby_events',
- {lib_expression} = 'posthog-python', 'python_events',
- {lib_expression} = 'posthog-php', 'php_events',
- {lib_expression} = 'posthog-dotnet', 'dotnet_events',
- {lib_expression} = 'posthog-elixir', 'elixir_events',
- {lib_expression} = 'posthog-unity', 'unity_events',
- {lib_expression} = 'posthog-rs', 'rust_events',
- 'other'
+ {metric_expression}
) AS metric,
count(1) as count
FROM events
- WHERE timestamp >= %(begin)s AND timestamp < %(end)s
+ PREWHERE timestamp >= %(begin)s AND timestamp < %(end)s
+ WHERE (
+ {metric_filter}
+ )
GROUP BY team_id, metric
- HAVING metric != 'other'
"""
# Define a custom function to combine results from multiple queries