: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(customer-analytics): filter assigned accounts correctly (#90914)
Arthur Moreira de Deus574dd011295c
diff --git a/frontend/src/queries/nodes/DataNode/dataNodeLogic.test.ts b/frontend/src/queries/nodes/DataNode/dataNodeLogic.test.tsindex 70ba81d3bb4b..24aace056ffa 100644--- a/frontend/src/queries/nodes/DataNode/dataNodeLogic.test.ts+++ b/frontend/src/queries/nodes/DataNode/dataNodeLogic.test.ts@@ -93,6 +93,38 @@ describe('dataNodeLogic', () => {.toMatchValues({ responseLoading: false, response: partial({ results: results3 }) })})+ it('force refreshes account table results when filters change', async () => {+ const assignedQuery = {+ kind: NodeKind.AccountsTableQuery,+ columns: [],+ filters: [{ kind: 'assigned' as const }],+ }+ mockedQuery.mockResolvedValue({ results: [] })+ logic = dataNodeLogic({ key: testUniqueKey, query: assignedQuery })+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadDataSuccess'])++ const unassignedQuery = {+ kind: NodeKind.AccountsTableQuery,+ columns: [],+ filters: [{ kind: 'unassigned' as const }],+ }+ mockedQuery.mockClear()+ dataNodeLogic({ key: testUniqueKey, query: unassignedQuery })++ expect(performQuery).toHaveBeenCalledWith(+ unassignedQuery,+ expect.anything(),+ 'force_blocking',+ expect.any(String),+ expect.any(Function),+ undefined,+ undefined,+ false,+ undefined+ )+ })+it('can load new data if EventsQuery sorted by timestamp', async () => {const results = [[diff --git a/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts b/frontend/src/queries/nodes/DataNode/dataNodeLogic.tsindex d243038cc6b3..2f965b6c45ab 100644--- a/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts+++ b/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts@@ -920,7 +920,7 @@ export const dataNodeLogic = kea<dataNodeLogicType>([) {// For normal loads, use appropriate refresh typelet refreshType: RefreshType- if (queryVarsHaveChanged) {+ if (queryVarsHaveChanged || isAccountsTableQuery(props.query)) {refreshType =isInsightQueryNode(props.query) || isHogQLQuery(props.query) ? 'force_async' : 'force_blocking'} else {diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex 421f5f0c3bc0..f2b4871eea61 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -348,6 +348,17 @@"enum": ["sum", "avg", "min", "max", "median"],"type": "string"},+ "AccountsTableAssignedFilter": {+ "additionalProperties": false,+ "properties": {+ "kind": {+ "const": "assigned",+ "type": "string"+ }+ },+ "required": ["kind"],+ "type": "object"+ },"AccountsTableAssignedToFilter": {"additionalProperties": false,"properties": {@@ -536,6 +547,9 @@{"$ref": "#/definitions/AccountsTableAssignedToFilter"},+ {+ "$ref": "#/definitions/AccountsTableAssignedFilter"+ },{"$ref": "#/definitions/AccountsTableUnassignedFilter"},diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.tsindex a68482ba834e..56b15ca341aa 100644--- a/frontend/src/queries/schema/schema-general.ts+++ b/frontend/src/queries/schema/schema-general.ts@@ -3032,6 +3032,10 @@ export interface AccountsTableAssignedToFilter {userIds: integer[]}+export interface AccountsTableAssignedFilter {+ kind: 'assigned'+}+export interface AccountsTableUnassignedFilter {kind: 'unassigned'}@@ -3094,6 +3098,7 @@ export type AccountsTableFilter =| AccountsTableSearchFilter| AccountsTableTagsFilter| AccountsTableAssignedToFilter+ | AccountsTableAssignedFilter| AccountsTableUnassignedFilter| AccountsTableAccountIdFilter| AccountsTableAccountFieldFilterdiff --git a/posthog/schema.py b/posthog/schema.pyindex d1e8af0093f9..8bbaf0a0d574 100644--- a/posthog/schema.py+++ b/posthog/schema.py@@ -340,6 +340,13 @@ class AccountsTableAccountIdFilter(BaseModel):kind: Literal["account_id"] = "account_id"+class AccountsTableAssignedFilter(BaseModel):+ model_config = ConfigDict(+ extra="forbid",+ )+ kind: Literal["assigned"] = "assigned"++class AccountsTableCountMetric(BaseModel):model_config = ConfigDict(extra="forbid",@@ -24589,6 +24596,7 @@ class AccountsTableQuery(BaseModel):AccountsTableSearchFilter| AccountsTableTagsFilter| AccountsTableAssignedToFilter+ | AccountsTableAssignedFilter| AccountsTableUnassignedFilter| AccountsTableAccountIdFilter| AccountsTableAccountFieldFilterdiff --git a/products/customer_analytics/backend/facade/contracts.py b/products/customer_analytics/backend/facade/contracts.pyindex f7cbe2b3b213..126489cad223 100644--- a/products/customer_analytics/backend/facade/contracts.py+++ b/products/customer_analytics/backend/facade/contracts.py@@ -227,6 +227,11 @@ class AccountTableAssignedToFilter:user_ids: tuple[int, ...]+@dataclass(frozen=True, kw_only=True)+class AccountTableAssignedFilter:+ pass++@dataclass(frozen=True, kw_only=True)class AccountTableUnassignedFilter:pass@@ -285,6 +290,7 @@ AccountTableFilter = (AccountTableSearchFilter| AccountTableTagsFilter| AccountTableAssignedToFilter+ | AccountTableAssignedFilter| AccountTableUnassignedFilter| AccountTableAccountIdFilter| AccountTableFieldFilterdiff --git a/products/customer_analytics/backend/hogql_queries/accounts_table_query_runner.py b/products/customer_analytics/backend/hogql_queries/accounts_table_query_runner.pyindex 7c3fd14e6c05..4d5b742aa92d 100644--- a/products/customer_analytics/backend/hogql_queries/accounts_table_query_runner.py+++ b/products/customer_analytics/backend/hogql_queries/accounts_table_query_runner.py@@ -8,6 +8,7 @@ from posthog.schema import (AccountsTableAccountFieldFilter,AccountsTableAccountIdFilter,AccountsTableAggregateMetric,+ AccountsTableAssignedFilter,AccountsTableAssignedToFilter,AccountsTableCountMetric,AccountsTableCountThresholdMetric,@@ -135,6 +136,8 @@ class AccountsTableQueryRunner(AnalyticsQueryRunner[AccountsTableQueryResponse])filters.append(contracts.AccountTableTagsFilter(tag_names=tuple(filter_.tagNames)))elif isinstance(filter_, AccountsTableAssignedToFilter):filters.append(contracts.AccountTableAssignedToFilter(user_ids=tuple(filter_.userIds)))+ elif isinstance(filter_, AccountsTableAssignedFilter):+ filters.append(contracts.AccountTableAssignedFilter())elif isinstance(filter_, AccountsTableUnassignedFilter):filters.append(contracts.AccountTableUnassignedFilter())elif isinstance(filter_, AccountsTableAccountIdFilter):diff --git a/products/customer_analytics/backend/hogql_queries/test/test_accounts_table_query_runner.py b/products/customer_analytics/backend/hogql_queries/test/test_accounts_table_query_runner.pyindex 06d3a8fe8bc6..4fad82957f34 100644--- a/products/customer_analytics/backend/hogql_queries/test/test_accounts_table_query_runner.py+++ b/products/customer_analytics/backend/hogql_queries/test/test_accounts_table_query_runner.py@@ -19,6 +19,7 @@ from posthog.schema import (AccountsTableAccountIdFilter,AccountsTableAggregateMetric,AccountsTableAggregation,+ AccountsTableAssignedFilter,AccountsTableAssignedToFilter,AccountsTableCountMetric,AccountsTableCountThresholdMetric,@@ -456,6 +457,7 @@ class TestAccountsTableQueryRunner(BaseTest):user=self.user,)+ assigned_response = self._run(AccountsTableQuery(columns=[], filters=[AccountsTableAssignedFilter()]))unassigned_response = self._run(AccountsTableQuery(columns=[], filters=[AccountsTableUnassignedFilter()]))account_response = self._run(AccountsTableQuery(@@ -464,6 +466,7 @@ class TestAccountsTableQueryRunner(BaseTest):))+ assert [row.id for row in assigned_response.results] == [str(assigned_account.id)]assert [row.id for row in unassigned_response.results] == [str(unassigned_account.id)]assert [row.id for row in account_response.results] == [str(assigned_account.id)]diff --git a/products/customer_analytics/backend/logic/account_filters.py b/products/customer_analytics/backend/logic/account_filters.pyindex 8f673d2ff331..03f7278a2491 100644--- a/products/customer_analytics/backend/logic/account_filters.py+++ b/products/customer_analytics/backend/logic/account_filters.py@@ -279,6 +279,8 @@ def apply_account_filters(elif isinstance(filter_, contracts.AccountTableAssignedToFilter):if filter_.user_ids:queryset = queryset.filter(Exists(active_relationships.filter(user_id__in=filter_.user_ids)))+ elif isinstance(filter_, contracts.AccountTableAssignedFilter):+ queryset = queryset.filter(Exists(active_relationships))elif isinstance(filter_, contracts.AccountTableUnassignedFilter):queryset = queryset.filter(~Exists(active_relationships))elif isinstance(filter_, contracts.AccountTableAccountIdFilter):diff --git a/products/customer_analytics/frontend/components/Accounts/accountsTableQuery.test.ts b/products/customer_analytics/frontend/components/Accounts/accountsTableQuery.test.tsindex 8c99ea0ff461..de65ae2a6e41 100644--- a/products/customer_analytics/frontend/components/Accounts/accountsTableQuery.test.ts+++ b/products/customer_analytics/frontend/components/Accounts/accountsTableQuery.test.ts@@ -124,6 +124,7 @@ describe('accountsTableQuery', () => {)expect(plan.query.filters).toEqual([+ { kind: 'assigned' },{kind: 'account_field',field: AccountsTableAccountField.IgnoredAt,@@ -152,7 +153,13 @@ describe('accountsTableQuery', () => {}))- expect(plan.query.filters).toEqual([])+ expect(plan.query.filters).toEqual([{ kind: 'assigned' }])+ })++ it('filters the default account list to accounts assigned to someone', () => {+ const plan = buildAccountsTableQueryPlan(queryInput())++ expect(plan.query.filters).toEqual([{ kind: 'assigned' }])})it('translates saved custom-property history display configuration', () => {@@ -223,6 +230,7 @@ describe('accountsTableQuery', () => {)expect(plan.query.filters).toEqual([+ { kind: 'assigned' },{kind: 'custom_property',definitionId: CUSTOM_PROPERTY_ID,@@ -248,6 +256,7 @@ describe('accountsTableQuery', () => {)expect(plan.query.filters).toEqual([+ { kind: 'assigned' },{kind: 'custom_property',definitionId: CUSTOM_PROPERTY_ID,@@ -277,7 +286,7 @@ describe('accountsTableQuery', () => {}))- expect(plan.query.filters).toEqual([])+ expect(plan.query.filters).toEqual([{ kind: 'assigned' }])})it('keeps contains filters for link properties', () => {@@ -290,6 +299,7 @@ describe('accountsTableQuery', () => {)expect(plan.query.filters).toEqual([+ { kind: 'assigned' },{kind: 'custom_property',definitionId: CUSTOM_PROPERTY_ID,diff --git a/products/customer_analytics/frontend/components/Accounts/accountsTableQuery.ts b/products/customer_analytics/frontend/components/Accounts/accountsTableQuery.tsindex 44af1e227981..5fc389291396 100644--- a/products/customer_analytics/frontend/components/Accounts/accountsTableQuery.ts+++ b/products/customer_analytics/frontend/components/Accounts/accountsTableQuery.ts@@ -5,6 +5,7 @@ import {AccountsTableAccountFieldFilter,AccountsTableAccountFieldOperator,AccountsTableAccountIdFilter,+ AccountsTableAssignedFilter,AccountsTableAssignedToFilter,AccountsTableColumn,AccountsTableCustomPropertyFilter,@@ -262,9 +263,10 @@ function queryFilters(input: BuildAccountsTableQueryPlanInput): AccountsTableFil}if (input.allRolesUnassigned) {filters.push({ kind: 'unassigned' } satisfies AccountsTableUnassignedFilter)- }- if (input.assignedToFilter.length > 0) {+ } else if (input.assignedToFilter.length > 0) {filters.push({ kind: 'assigned_to', userIds: input.assignedToFilter } satisfies AccountsTableAssignedToFilter)+ } else {+ filters.push({ kind: 'assigned' } satisfies AccountsTableAssignedFilter)}for (const filter of input.accountFilters) {const translatedFilter = isAccountPropertyFilter(filter)diff --git a/products/dashboards/frontend/generated/api.schemas.ts b/products/dashboards/frontend/generated/api.schemas.tsindex d626b8c5a219..820bb3d8ec8c 100644--- a/products/dashboards/frontend/generated/api.schemas.ts+++ b/products/dashboards/frontend/generated/api.schemas.ts@@ -8344,6 +8344,11 @@ export interface AccountsTableAssignedToFilterApi {userIds: number[]}+export const AccountsTableAssignedFilterApiValue = {+ kind: 'assigned',+} as const+export type AccountsTableAssignedFilterApi = typeof AccountsTableAssignedFilterApiValue+export const AccountsTableUnassignedFilterApiValue = {kind: 'unassigned',} as const@@ -8509,6 +8514,7 @@ export interface AccountsTableQueryApi {| AccountsTableSearchFilterApi| AccountsTableTagsFilterApi| AccountsTableAssignedToFilterApi+ | AccountsTableAssignedFilterApi| AccountsTableUnassignedFilterApi| AccountsTableAccountIdFilterApi| AccountsTableAccountFieldFilterApidiff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.tsindex 5f0e1578cc32..021b6c787c98 100644--- a/products/product_analytics/frontend/generated/api.schemas.ts+++ b/products/product_analytics/frontend/generated/api.schemas.ts@@ -7506,6 +7506,11 @@ export interface AccountsTableAssignedToFilterApi {userIds: number[]}+export const AccountsTableAssignedFilterApiValue = {+ kind: 'assigned',+} as const+export type AccountsTableAssignedFilterApi = typeof AccountsTableAssignedFilterApiValue+export const AccountsTableUnassignedFilterApiValue = {kind: 'unassigned',} as const@@ -7671,6 +7676,7 @@ export interface AccountsTableQueryApi {| AccountsTableSearchFilterApi| AccountsTableTagsFilterApi| AccountsTableAssignedToFilterApi+ | AccountsTableAssignedFilterApi| AccountsTableUnassignedFilterApi| AccountsTableAccountIdFilterApi| AccountsTableAccountFieldFilterApidiff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex d00b73ec4130..1232e034d5d2 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -1224,6 +1224,11 @@ export namespace Schemas {scale?: number | null;}+ export const AccountsTableAssignedFilterValue = {+ kind: 'assigned',+ } as const;+ export type AccountsTableAssignedFilter = typeof AccountsTableAssignedFilterValue;+export interface AccountsTableAssignedToFilter {kind?: 'assigned_to';/** Match accounts where any listed user actively holds any relationship. */@@ -1424,7 +1429,7 @@ export namespace Schemas {/** Columns to load for each account. Account identity fields are always returned. */columns: (AccountsTableAccountFieldColumn | AccountsTableTagsColumn | AccountsTableNoteCountColumn | AccountsTableRelationshipColumn | AccountsTableCustomPropertyColumn | AccountsTableCustomPropertyHistoryColumn)[];/** Filters are combined with AND. Values within tag and assignment filters use OR. */- filters?: (AccountsTableSearchFilter | AccountsTableTagsFilter | AccountsTableAssignedToFilter | AccountsTableUnassignedFilter | AccountsTableAccountIdFilter | AccountsTableAccountFieldFilter | AccountsTableCustomPropertyFilter)[] | null;+ filters?: (AccountsTableSearchFilter | AccountsTableTagsFilter | AccountsTableAssignedToFilter | AccountsTableAssignedFilter | AccountsTableUnassignedFilter | AccountsTableAccountIdFilter | AccountsTableAccountFieldFilter | AccountsTableCustomPropertyFilter)[] | null;/** Include churned accounts. Churned accounts are hidden by default. */includeChurned?: boolean | null;/** Include ignored accounts. Ignored accounts are hidden by default. */