From 02982baf87fe26cd4878e1c3442a3e461fd01cdc Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Fri, 8 Nov 2024 17:35:05 +0100 Subject: [PATCH 1/3] feat: Add unique identifier for reusing cognee-lib for posthog an .anonymous_id file is created in the cognee directory which stores unique identifier that is reused in further cognee runs Feature #COG-492 --- cognee/shared/utils.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/cognee/shared/utils.py b/cognee/shared/utils.py index f6b75f4e0..9a497b233 100644 --- a/cognee/shared/utils.py +++ b/cognee/shared/utils.py @@ -9,9 +9,30 @@ import matplotlib.pyplot as plt import tiktoken import nltk from posthog import Posthog + +import cognee from cognee.base_config import get_base_config from cognee.infrastructure.databases.graph import get_graph_engine +from uuid import uuid4 +import pathlib + +def get_anonymous_id(): + """Creates or reads a anonymous user id""" + home_dir = str(pathlib.Path(pathlib.Path(__file__).parent.parent.parent.resolve())) + + if not os.path.isdir(home_dir): + os.makedirs(home_dir, exist_ok=True) + anonymous_id_file = os.path.join(home_dir, ".anonymous_id") + if not os.path.isfile(anonymous_id_file): + anonymous_id = str(uuid4()) + with open(anonymous_id_file, "w", encoding="utf-8") as f: + f.write(anonymous_id) + else: + with open(anonymous_id_file, "r", encoding="utf-8") as f: + anonymous_id = f.read() + return anonymous_id + def send_telemetry(event_name: str, user_id, additional_properties: dict = {}): if os.getenv("TELEMETRY_DISABLED"): return @@ -28,11 +49,14 @@ def send_telemetry(event_name: str, user_id, additional_properties: dict = {}): current_time = datetime.datetime.now() properties = { "time": current_time.strftime("%m/%d/%Y"), + "user_id": user_id, **additional_properties, } + posthog.identify(get_anonymous_id(), properties) + try: - posthog.capture(user_id, event_name, properties) + posthog.capture(get_anonymous_id(), event_name, properties) except Exception as e: print("ERROR sending telemetric data to Posthog. See exception: %s", e) From 9277363c24343077a524b3e437f1b5678e9b756d Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Mon, 11 Nov 2024 10:04:05 +0100 Subject: [PATCH 2/3] refactor: Remove unused dependency Remove unused cognee dependency Refactor #COG-492 --- cognee/shared/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cognee/shared/utils.py b/cognee/shared/utils.py index 9a497b233..1b5fb9de8 100644 --- a/cognee/shared/utils.py +++ b/cognee/shared/utils.py @@ -10,7 +10,6 @@ import tiktoken import nltk from posthog import Posthog -import cognee from cognee.base_config import get_base_config from cognee.infrastructure.databases.graph import get_graph_engine From 33c3748d1ecd215c084830756b1c4fd6a6b90150 Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Mon, 11 Nov 2024 11:53:09 +0100 Subject: [PATCH 3/3] refactor: Renamed .anonymous_id file to anon_id Renamed .anonymous_id file to anon_id Refactor #COG-492 --- .gitignore | 1 + cognee/shared/utils.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fef10cd63..f447655cf 100644 --- a/.gitignore +++ b/.gitignore @@ -177,5 +177,6 @@ cognee/cache/ # Default cognee system directory, used in development .cognee_system/ .data_storage/ +.anon_id node_modules/ diff --git a/cognee/shared/utils.py b/cognee/shared/utils.py index 1b5fb9de8..41f3928ca 100644 --- a/cognee/shared/utils.py +++ b/cognee/shared/utils.py @@ -22,7 +22,7 @@ def get_anonymous_id(): if not os.path.isdir(home_dir): os.makedirs(home_dir, exist_ok=True) - anonymous_id_file = os.path.join(home_dir, ".anonymous_id") + anonymous_id_file = os.path.join(home_dir, ".anon_id") if not os.path.isfile(anonymous_id_file): anonymous_id = str(uuid4()) with open(anonymous_id_file, "w", encoding="utf-8") as f: @@ -52,6 +52,7 @@ def send_telemetry(event_name: str, user_id, additional_properties: dict = {}): **additional_properties, } + # Needed to forward properties to PostHog along with id posthog.identify(get_anonymous_id(), properties) try: