Skip to content

Commit

Permalink
feat: hogql trends (#17519)
Browse files Browse the repository at this point in the history
* Scaffolding to get trends working with new runner

* Handle multiple series

* Added date range filters

* Moved filters out of the main query

* Add hogql math operator

* Support hogql expressions in math operators

* Fixes for master branch changes

* Empty commit to trigger github checks

* Remove unused import

* Fixed type checking

* Updated schema.json with TrendsQueryResponse

* Update UI snapshots for `chromium` (1)

* Update UI snapshots for `chromium` (1)

* Updated schema and added series filters

* Update UI snapshots for `chromium` (1)

* Update UI snapshots for `chromium` (1)

* Update query snapshots

* Update UI snapshots for `chromium` (1)

* Update query snapshots

* Update UI snapshots for `chromium` (1)

* Update UI snapshots for `chromium` (1)

* Update UI snapshots for `chromium` (1)

* Added being able to compare to previous period

* Added sampling

* Move query runner caching logic outside of individual runners

* Added support for formulas

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Gilbert09 and github-actions[bot] authored Sep 22, 2023
1 parent be05d73 commit 9c1e31b
Show file tree
Hide file tree
Showing 12 changed files with 497 additions and 4 deletions.
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

0 comments on commit 9c1e31b

Please sign in to comment.