Skip to content

Commit

Permalink
feat(hogql): stickiness query runner (#19676)
Browse files Browse the repository at this point in the history
* WIP

* Added stickiness query runner
  • Loading branch information
Gilbert09 authored Jan 10, 2024
1 parent 1c62159 commit e4591cb
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 6 deletions.
1 change: 1 addition & 0 deletions frontend/src/lib/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export const FEATURE_FLAGS = {
HOGQL_INSIGHTS_LIFECYCLE: 'hogql-insights-lifecycle', // owner: @mariusandra
HOGQL_INSIGHTS_RETENTION: 'hogql-insights-retention', // owner: @webjunkie
HOGQL_INSIGHTS_TRENDS: 'hogql-insights-trends', // owner: @Gilbert09
HOGQL_INSIGHTS_STICKINESS: 'hogql-insights-stickiness', // owner: @Gilbert09
HOGQL_INSIGHT_LIVE_COMPARE: 'hogql-insight-live-compare', // owner: @mariusandra
BI_VIZ: 'bi_viz', // owner: @Gilbert09
WEBHOOKS_DENYLIST: 'webhooks-denylist', // owner: #team-pipeline
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/queries/nodes/DataNode/dataNodeLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
isInsightQueryNode,
isLifecycleQuery,
isPersonsNode,
isStickinessQuery,
isTrendsQuery,
} from '~/queries/utils'

Expand Down Expand Up @@ -126,6 +127,7 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
isInsightQueryNode(props.query) &&
!(values.hogQLInsightsLifecycleFlagEnabled && isLifecycleQuery(props.query)) &&
!(values.hogQLInsightsTrendsFlagEnabled && isTrendsQuery(props.query)) &&
!(values.hogQLInsightsStickinessFlagEnabled && isStickinessQuery(props.query)) &&
props.cachedResults &&
props.cachedResults['id'] &&
props.cachedResults['filters'] &&
Expand Down Expand Up @@ -357,6 +359,10 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
(s) => [s.featureFlags],
(featureFlags) => !!featureFlags[FEATURE_FLAGS.HOGQL_INSIGHTS_TRENDS],
],
hogQLInsightsStickinessFlagEnabled: [
(s) => [s.featureFlags],
(featureFlags) => !!featureFlags[FEATURE_FLAGS.HOGQL_INSIGHTS_STICKINESS],
],
query: [(_, p) => [p.query], (query) => query],
newQuery: [
(s, p) => [p.query, s.response],
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/queries/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
isLifecycleQuery,
isPersonsNode,
isRetentionQuery,
isStickinessQuery,
isTimeToSeeDataQuery,
isTimeToSeeDataSessionsNode,
isTimeToSeeDataSessionsQuery,
Expand Down Expand Up @@ -155,6 +156,9 @@ export async function query<N extends DataNode = DataNode>(
const hogQLInsightsTrendsFlagEnabled = Boolean(
featureFlagLogic.findMounted()?.values.featureFlags?.[FEATURE_FLAGS.HOGQL_INSIGHTS_TRENDS]
)
const hogQLInsightsStickinessFlagEnabled = Boolean(
featureFlagLogic.findMounted()?.values.featureFlags?.[FEATURE_FLAGS.HOGQL_INSIGHTS_STICKINESS]
)
const hogQLInsightsLiveCompareEnabled = Boolean(
featureFlagLogic.findMounted()?.values.featureFlags?.[FEATURE_FLAGS.HOGQL_INSIGHT_LIVE_COMPARE]
)
Expand Down Expand Up @@ -198,7 +202,8 @@ export async function query<N extends DataNode = DataNode>(
if (
(hogQLInsightsLifecycleFlagEnabled && isLifecycleQuery(queryNode)) ||
(hogQLInsightsRetentionFlagEnabled && isRetentionQuery(queryNode)) ||
(hogQLInsightsTrendsFlagEnabled && isTrendsQuery(queryNode))
(hogQLInsightsTrendsFlagEnabled && isTrendsQuery(queryNode)) ||
(hogQLInsightsStickinessFlagEnabled && isStickinessQuery(queryNode))
) {
if (hogQLInsightsLiveCompareEnabled) {
let legacyResponse
Expand Down
31 changes: 31 additions & 0 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3053,6 +3053,37 @@
"required": ["kind", "series"],
"type": "object"
},
"StickinessQueryResponse": {
"additionalProperties": false,
"properties": {
"hogql": {
"type": "string"
},
"is_cached": {
"type": "boolean"
},
"last_refresh": {
"type": "string"
},
"next_allowed_client_refresh": {
"type": "string"
},
"results": {
"items": {
"type": "object"
},
"type": "array"
},
"timings": {
"items": {
"$ref": "#/definitions/QueryTiming"
},
"type": "array"
}
},
"required": ["results"],
"type": "object"
},
"TimeToSeeDataJSONNode": {
"additionalProperties": false,
"properties": {
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ export type StickinessFilter = Omit<
StickinessFilterType & { hidden_legend_indexes?: number[] },
keyof FilterType | 'hidden_legend_keys' | 'stickiness_days' | 'shown_as'
>

export interface StickinessQueryResponse extends QueryResponse {
results: Record<string, any>[]
}

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` */
Expand Down
2 changes: 0 additions & 2 deletions mypy-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ posthog/hogql/query.py:0: error: Argument 1 to "get_default_limit_for_context" h
posthog/hogql/query.py:0: error: "SelectQuery" has no attribute "select_queries" [attr-defined]
posthog/hogql/query.py:0: error: Subclass of "SelectQuery" and "SelectUnionQuery" cannot exist: would have incompatible method signatures [unreachable]
posthog/hogql_queries/query_runner.py:0: error: Unused "type: ignore" comment [unused-ignore]
posthog/hogql_queries/query_runner.py:0: error: Argument "limit_context" to "TrendsQueryRunner" has incompatible type "LimitContext | None"; expected "bool | None" [arg-type]
posthog/hogql_queries/insights/trends/breakdown_values.py:0: error: Argument "chain" to "Field" has incompatible type "list[str]"; expected "list[str | int]" [arg-type]
posthog/hogql_queries/insights/trends/breakdown_values.py:0: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
posthog/hogql_queries/insights/trends/breakdown_values.py:0: note: Consider using "Sequence" instead, which is covariant
Expand Down Expand Up @@ -439,7 +438,6 @@ posthog/api/person.py:0: error: Unused "type: ignore" comment [unused-ignore]
posthog/api/person.py:0: error: Unused "type: ignore" comment [unused-ignore]
posthog/api/person.py:0: error: Unused "type: ignore" comment [unused-ignore]
posthog/hogql_queries/web_analytics/web_analytics_query_runner.py:0: error: Argument 1 to "append" of "list" has incompatible type "EventPropertyFilter"; expected "Expr" [arg-type]
posthog/hogql_queries/insights/trends/trends_query_runner.py:0: error: Argument "limit_context" to "__init__" of "QueryRunner" has incompatible type "bool | None"; expected "LimitContext | None" [arg-type]
posthog/hogql_queries/insights/trends/trends_query_runner.py:0: error: Return type "list[SelectQuery]" of "to_query" incompatible with return type "SelectQuery" in supertype "QueryRunner" [override]
posthog/hogql_queries/insights/trends/trends_query_runner.py:0: error: Incompatible return value type (got "list[SelectQuery | SelectUnionQuery]", expected "list[SelectQuery]") [return-value]
posthog/hogql_queries/insights/trends/trends_query_runner.py:0: error: Need type annotation for "timings" (hint: "timings: List[<type>] = ...") [var-annotated]
Expand Down
1 change: 1 addition & 0 deletions posthog/api/services/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"LifecycleQuery",
"RetentionQuery",
"TrendsQuery",
"StickinessQuery",
"WebOverviewQuery",
"WebTopSourcesQuery",
"WebTopClicksQuery",
Expand Down
4 changes: 2 additions & 2 deletions posthog/hogql/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ class ArithmeticOperation(Expr):

@dataclass(kw_only=True)
class And(Expr):
exprs: List[Expr]
type: Optional[ConstantType] = None
exprs: List[Expr]


@dataclass(kw_only=True)
Expand Down Expand Up @@ -566,8 +566,8 @@ class SelectQuery(Expr):

@dataclass(kw_only=True)
class SelectUnionQuery(Expr):
select_queries: List[SelectQuery]
type: Optional[SelectUnionQueryType] = None
select_queries: List[SelectQuery]


@dataclass(kw_only=True)
Expand Down
Loading

0 comments on commit e4591cb

Please sign in to comment.