mirror of
https://github.com/PostHog/posthog.git
synced 2024-11-28 18:26:15 +01:00
10a26d3e17
* Avoid naming collisions when materializing columns 1. Prefix person properties differently. Mixing them up can break ambigious column issues 2. When name already exists, suffix with random junk :) * Implement analyze.py * Add `suggested_columns_to_materialize` * Add code to backfill a materialized column * Add tests for backfilling data * Cleanup * Add tests for analyze * WIP: Crontab for materializing columns * Nooped task for materializing properties * Use mutations_sync=0 for column tests * Add comment
42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
from datetime import timedelta
|
|
from functools import wraps
|
|
from typing import no_type_check
|
|
|
|
from django.utils.timezone import now
|
|
|
|
from posthog.settings import TEST
|
|
|
|
|
|
def cache_for(cache_time: timedelta):
|
|
def wrapper(fn):
|
|
@wraps(fn)
|
|
@no_type_check
|
|
def memoized_fn(*args, use_cache=not TEST):
|
|
if not use_cache:
|
|
return fn(*args)
|
|
|
|
current_time = now()
|
|
if args not in memoized_fn.__cache or current_time - memoized_fn.__cache[args][0] > cache_time:
|
|
memoized_fn.__cache[args] = (current_time, fn(*args))
|
|
return memoized_fn.__cache[args][1]
|
|
|
|
memoized_fn.__cache = {}
|
|
return memoized_fn
|
|
|
|
return wrapper
|
|
|
|
|
|
def instance_memoize(callback):
|
|
name = f"_{callback.__name__}_memo"
|
|
|
|
def _inner(self, *args):
|
|
if not hasattr(self, name):
|
|
setattr(self, name, {})
|
|
|
|
memo = getattr(self, name)
|
|
if args not in memo:
|
|
memo[args] = callback(self, *args)
|
|
return memo[args]
|
|
|
|
return _inner
|