Skip to content

Commit

Permalink
Merge branch 'master' into hogql-lifecycle-comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Nov 16, 2023
2 parents 9eeaf3c + e83de60 commit 61f2c2b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export const queryNodeToFilter = (query: InsightQueryNode): Partial<FilterType>
date_from: query.dateRange?.date_from,
entity_type: 'events',
sampling_factor: query.samplingFactor,
aggregation_group_type_index: query.aggregation_group_type_index,
})

if (!isRetentionQuery(query) && !isPathsQuery(query)) {
Expand All @@ -107,6 +106,15 @@ export const queryNodeToFilter = (query: InsightQueryNode): Partial<FilterType>
Object.assign(filters, objectClean<Partial<Record<keyof BreakdownFilter, unknown>>>(query.breakdown))
}

if (!isLifecycleQuery(query) && !isStickinessQuery(query)) {
Object.assign(
filters,
objectClean({
aggregation_group_type_index: query.aggregation_group_type_index,
})
)
}

if (isTrendsQuery(query) || isStickinessQuery(query) || isLifecycleQuery(query) || isFunnelsQuery(query)) {
filters.interval = query.interval
}
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2757,10 +2757,6 @@
"StickinessQuery": {
"additionalProperties": false,
"properties": {
"aggregation_group_type_index": {
"description": "Groups aggregation",
"type": "number"
},
"dateRange": {
"$ref": "#/definitions/DateRange",
"description": "Date range for the query"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export type StickinessFilter = Omit<
StickinessFilterType & { hidden_legend_indexes?: number[] },
keyof FilterType | 'hidden_legend_keys' | 'stickiness_days' | 'shown_as'
>
export interface StickinessQuery extends InsightsQueryBase {
export interface StickinessQuery extends Omit<InsightsQueryBase, 'aggregation_group_type_index'> {
kind: NodeKind.StickinessQuery
/** Granularity of the response. Can be one of `hour`, `day`, `week` or `month` */
interval?: IntervalType
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/scenes/insights/filters/AggregationSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LemonSelect, LemonSelectSection } from '@posthog/lemon-ui'
import { groupsAccessLogic } from 'lib/introductions/groupsAccessLogic'
import { GroupIntroductionFooter } from 'scenes/groups/GroupsIntroduction'
import { InsightLogicProps } from '~/types'
import { isFunnelsQuery, isInsightQueryNode } from '~/queries/utils'
import { isFunnelsQuery, isInsightQueryNode, isLifecycleQuery, isStickinessQuery } from '~/queries/utils'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { FunnelsQuery } from '~/queries/schema'
import { HogQLEditor } from 'lib/components/HogQLEditor/HogQLEditor'
Expand Down Expand Up @@ -51,7 +51,9 @@ export function AggregationSelect({
}

const value = getHogQLValue(
querySource.aggregation_group_type_index,
isLifecycleQuery(querySource) || isStickinessQuery(querySource)
? undefined
: querySource.aggregation_group_type_index,
isFunnelsQuery(querySource) ? querySource.funnelsFilter?.funnel_aggregate_by_hogql : undefined
)
const onChange = (value: string): void => {
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/scenes/retention/retentionLineGraphLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { retentionLogic } from './retentionLogic'

import type { retentionLineGraphLogicType } from './retentionLineGraphLogicType'
import { isLifecycleQuery, isStickinessQuery } from '~/queries/utils'

const DEFAULT_RETENTION_LOGIC_KEY = 'default_retention_key'

Expand Down Expand Up @@ -117,7 +118,11 @@ export const retentionLineGraphLogic = kea<retentionLineGraphLogicType>([
aggregationGroupTypeIndex: [
(s) => [s.querySource],
(querySource) => {
return querySource?.aggregation_group_type_index ?? 'people'
return (
(isLifecycleQuery(querySource) || isStickinessQuery(querySource)
? null
: querySource?.aggregation_group_type_index) ?? 'people'
)
},
],
}),
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/scenes/retention/retentionModalLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { retentionPeopleLogic } from './retentionPeopleLogic'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'

import type { retentionModalLogicType } from './retentionModalLogicType'
import { isLifecycleQuery, isStickinessQuery } from '~/queries/utils'

const DEFAULT_RETENTION_LOGIC_KEY = 'default_retention_key'

Expand Down Expand Up @@ -35,7 +36,10 @@ export const retentionModalLogic = kea<retentionModalLogicType>([
aggregationTargetLabel: [
(s) => [s.querySource, s.aggregationLabel],
(querySource, aggregationLabel): Noun => {
const { aggregation_group_type_index } = querySource || {}
const aggregation_group_type_index =
isLifecycleQuery(querySource) || isStickinessQuery(querySource)
? undefined
: querySource?.aggregation_group_type_index
return aggregationLabel(aggregation_group_type_index)
},
],
Expand Down
1 change: 0 additions & 1 deletion posthog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,6 @@ class StickinessQuery(BaseModel):
model_config = ConfigDict(
extra="forbid",
)
aggregation_group_type_index: Optional[float] = Field(default=None, description="Groups aggregation")
dateRange: Optional[DateRange] = Field(default=None, description="Date range for the query")
filterTestAccounts: Optional[bool] = Field(
default=None, description="Exclude internal and test users by applying the respective filters"
Expand Down
28 changes: 27 additions & 1 deletion posthog/temporal/workflows/redshift_batch_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime as dt
import itertools
import json
import os
import typing
from dataclasses import dataclass

Expand Down Expand Up @@ -32,6 +33,31 @@
)


@contextlib.asynccontextmanager
async def redshift_connection(inputs) -> typing.AsyncIterator[psycopg.AsyncConnection]:
"""Manage a Redshift connection.
This just yields a Postgres connection but we adjust a couple of things required for
psycopg to work with Redshift:
1. Set PGCLIENTENCODING to utf-8 as Redshift reports back UNICODE.
2. Set prepare_threshold to None on the connection as psycopg attempts to run DEALLOCATE ALL otherwise
which is not supported on Redshift.
"""
old_value = os.environ.get("PGCLIENTENCODING", None)
os.environ["PGCLIENTENCODING"] = "utf-8"

try:
async with postgres_connection(inputs) as connection:
connection.prepare_threshold = None
yield connection

finally:
if old_value is None:
del os.environ["PGCLIENTENCODING"]
else:
os.environ["PGCLIENTENCODING"] = old_value


async def insert_records_to_redshift(
records: collections.abc.Iterator[dict[str, typing.Any]],
redshift_connection: psycopg.AsyncConnection,
Expand Down Expand Up @@ -186,7 +212,7 @@ async def insert_into_redshift_activity(inputs: RedshiftInsertInputs):
)
properties_type = "VARCHAR(65535)" if inputs.properties_data_type == "varchar" else "SUPER"

async with postgres_connection(inputs) as connection:
async with redshift_connection(inputs) as connection:
await create_table_in_postgres(
connection,
schema=inputs.schema,
Expand Down

0 comments on commit 61f2c2b

Please sign in to comment.