Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hogql trends #17519

Merged
merged 28 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bb9c96a
Scaffolding to get trends working with new runner
Gilbert09 Sep 18, 2023
36ff530
Handle multiple series
Gilbert09 Sep 19, 2023
ee004bd
Added date range filters
Gilbert09 Sep 19, 2023
3d73a9a
Moved filters out of the main query
Gilbert09 Sep 19, 2023
6d0b825
Add hogql math operator
Gilbert09 Sep 19, 2023
1251cb4
Support hogql expressions in math operators
Gilbert09 Sep 20, 2023
c8fa737
Fixes for master branch changes
Gilbert09 Sep 20, 2023
6b85aa7
Empty commit to trigger github checks
Gilbert09 Sep 20, 2023
9cca31d
Remove unused import
Gilbert09 Sep 20, 2023
167f393
Fixed type checking
Gilbert09 Sep 20, 2023
8e4e229
Updated schema.json with TrendsQueryResponse
Gilbert09 Sep 20, 2023
5cfd10c
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 20, 2023
966f041
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 20, 2023
bdd9169
Updated schema and added series filters
Gilbert09 Sep 20, 2023
fa5c597
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 20, 2023
f132cce
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 20, 2023
7b1d58b
Update query snapshots
github-actions[bot] Sep 20, 2023
69be7bc
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 20, 2023
c173374
Update query snapshots
github-actions[bot] Sep 20, 2023
857e4ad
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 20, 2023
f8bdf60
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 20, 2023
8a86fef
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 20, 2023
92ef0f1
Added being able to compare to previous period
Gilbert09 Sep 21, 2023
11e8111
Added sampling
Gilbert09 Sep 21, 2023
c73c753
Move query runner caching logic outside of individual runners
Gilbert09 Sep 21, 2023
c871b48
Added support for formulas
Gilbert09 Sep 22, 2023
989f0a2
Revert moving caching logic and fix type gaurd errors
Gilbert09 Sep 22, 2023
a7c67c5
Add back in abstract parent methods
Gilbert09 Sep 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,9 @@
],
"description": "Property filters for all series"
},
"response": {
"$ref": "#/definitions/TrendsQueryResponse"
},
"samplingFactor": {
"description": "Sampling rate",
"type": ["number", "null"]
Expand All @@ -2308,6 +2311,34 @@
},
"required": ["kind", "series"],
"type": "object"
},
"TrendsQueryResponse": {
"additionalProperties": false,
"properties": {
"is_cached": {
"type": "boolean"
},
"last_refresh": {
"type": "string"
},
"next_allowed_client_refresh": {
"type": "string"
},
"result": {
"items": {
"type": "object"
},
"type": "array"
},
"timings": {
"items": {
"$ref": "#/definitions/QueryTiming"
},
"type": "array"
}
},
"required": ["result"],
"type": "object"
}
}
}
6 changes: 6 additions & 0 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ export type TrendsFilter = Omit<
TrendsFilterType & { hidden_legend_indexes?: number[] },
keyof FilterType | 'hidden_legend_keys'
>

export interface TrendsQueryResponse extends QueryResponse {
result: Record<string, any>[]
}

export interface TrendsQuery extends InsightsQueryBase {
kind: NodeKind.TrendsQuery
/** Granularity of the response. Can be one of `hour`, `day`, `week` or `month` */
Expand All @@ -384,6 +389,7 @@ export interface TrendsQuery extends InsightsQueryBase {
trendsFilter?: TrendsFilter
/** Breakdown of the events and actions */
breakdown?: BreakdownFilter
response?: TrendsQueryResponse
}

/** `FunnelsFilterType` minus everything inherited from `FilterType` and
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/queries/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function isLifecycleQuery(node?: Node | null): node is LifecycleQuery {
}

export function isQueryWithHogQLSupport(node?: Node | null): node is LifecycleQuery {
return isLifecycleQuery(node)
return isLifecycleQuery(node) || isTrendsQuery(node)
}

export function isInsightQueryWithDisplay(node?: Node | null): node is TrendsQuery | StickinessQuery {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions posthog/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from posthog.hogql.query import execute_hogql_query

from posthog.hogql_queries.lifecycle_query_runner import LifecycleQueryRunner
from posthog.hogql_queries.trends_query_runner import TrendsQueryRunner
from posthog.models import Team
from posthog.models.event.events_query import run_events_query
from posthog.models.user import User
Expand Down Expand Up @@ -227,6 +228,10 @@ def process_query(
refresh_requested = refresh_requested_by_client(request) if request else False
lifecycle_query_runner = LifecycleQueryRunner(query_json, team)
return _unwrap_pydantic_dict(lifecycle_query_runner.run(refresh_requested=refresh_requested))
elif query_kind == "TrendsQuery":
refresh_requested = refresh_requested_by_client(request) if request else False
trends_query_runner = TrendsQueryRunner(query_json, team)
return _unwrap_pydantic_dict(trends_query_runner.run(refresh_requested=refresh_requested))
elif query_kind == "DatabaseSchemaQuery":
database = create_hogql_database(team.pk)
return serialize_database(database)
Expand Down
6 changes: 3 additions & 3 deletions posthog/hogql_queries/query_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
from datetime import datetime
from typing import Any, Generic, List, Optional, Type, Dict, TypeVar

from prometheus_client import Counter
Expand Down Expand Up @@ -119,9 +119,9 @@ def _cache_key(self) -> str:
return generate_cache_key(f"query_{self.toJSON()}_{self.team.pk}_{self.team.timezone}")

@abstractmethod
def _is_stale(self, cached_result_package) -> bool:
def _is_stale(self, cached_result_package):
raise NotImplementedError()

@abstractmethod
def _refresh_frequency(self) -> timedelta:
def _refresh_frequency(self):
raise NotImplementedError()
Loading