Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsobrmlr committed Sep 18, 2023
1 parent c57a870 commit 33807c8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
6 changes: 6 additions & 0 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,12 @@
"LifecycleQueryResponse": {
"additionalProperties": false,
"properties": {
"is_cached": {
"type": "boolean"
},
"last_refresh": {
"type": "string"
},
"result": {
"items": {
"type": "object"
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ export type LifecycleFilter = Omit<LifecycleFilterType, keyof FilterType> & {
export interface LifecycleQueryResponse {
result: Record<string, any>[]
timings?: QueryTiming[]
last_refresh?: string
is_cached?: boolean
}

export interface LifecycleQuery extends InsightsQueryBase {
Expand Down
25 changes: 10 additions & 15 deletions posthog/hogql_queries/query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from posthog.utils import generate_cache_key, get_safe_cache

QUERY_CACHE_WRITE_COUNTER = Counter(
"query_cache_write_total",
"posthog_query_cache_write_total",
"When a query result was persisted in the cache.",
labelnames=[LABEL_TEAM_ID],
)

QUERY_CACHE_HIT_COUNTER = Counter(
"query_cache_hit_total",
"posthog_query_cache_hit_total",
"Whether we could fetch the query from the cache or not.",
labelnames=[LABEL_TEAM_ID, "cache_hit"],
)
Expand All @@ -47,33 +47,28 @@ def __init__(self, query: InsightQueryNode | Dict[str, Any], team: Team, timings
def run(self) -> InsightQueryNode:
raise NotImplementedError()

@abstractmethod
def run_cached(self, refresh_requested: bool) -> InsightQueryNode:
cache_key = self.cache_key()
tag_queries(cache_key=cache_key)

if not refresh_requested:
cached_result_package = get_safe_cache(cache_key)

if cached_result_package and cached_result_package.get("result"):
if cached_result_package and cached_result_package.result:
if not self.is_stale(cached_result_package):
cached_result_package["is_cached"] = True
QUERY_CACHE_HIT_COUNTER.labels(team_id=self.team.pk, cache_hit=True).inc()
QUERY_CACHE_HIT_COUNTER.labels(team_id=self.team.pk, cache_hit="hit").inc()
return cached_result_package
else:
QUERY_CACHE_HIT_COUNTER.labels(team_id=self.team.pk, cache_hit=False).inc()
QUERY_CACHE_HIT_COUNTER.labels(team_id=self.team.pk, cache_hit="stale").inc()
else:
QUERY_CACHE_HIT_COUNTER.labels(team_id=self.team.pk, cache_hit=False).inc()
QUERY_CACHE_HIT_COUNTER.labels(team_id=self.team.pk, cache_hit="miss").inc()

fresh_result_package = self.run()
if isinstance(fresh_result_package, dict):
result = fresh_result_package.get("result")
if not isinstance(result, dict) or not result.get("loading"):
timestamp = now()
fresh_result_package["last_refresh"] = timestamp
fresh_result_package["is_cached"] = False
cache.set(cache_key, result, settings.CACHED_RESULTS_TTL)
QUERY_CACHE_WRITE_COUNTER.inc()
fresh_result_package.last_refresh = now()
fresh_result_package.is_cached = False
cache.set(cache_key, fresh_result_package, settings.CACHED_RESULTS_TTL)
QUERY_CACHE_WRITE_COUNTER.labels(team_id=self.team.pk).inc()

return fresh_result_package

Expand Down
2 changes: 2 additions & 0 deletions posthog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,8 @@ class LifecycleQueryResponse(BaseModel):
model_config = ConfigDict(
extra="forbid",
)
is_cached: Optional[bool] = None
last_refresh: Optional[str] = None
result: List[Dict[str, Any]]
timings: Optional[List[QueryTiming]] = None

Expand Down

0 comments on commit 33807c8

Please sign in to comment.