Merge pull request #231 from topoteretes/COG-597-refactor-analytics

Cog 597 refactor analytics
This commit is contained in:
Vasilije 2024-11-16 13:56:56 +01:00 committed by GitHub
commit c2e265fbf8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,6 @@
""" This module contains utility functions for the cognee. """ """ This module contains utility functions for the cognee. """
import os import os
import requests
from datetime import datetime, timezone from datetime import datetime, timezone
import graphistry import graphistry
import networkx as nx import networkx as nx
@ -8,7 +9,6 @@ import pandas as pd
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import tiktoken import tiktoken
import nltk import nltk
from posthog import Posthog
from cognee.base_config import get_base_config from cognee.base_config import get_base_config
from cognee.infrastructure.databases.graph import get_graph_engine from cognee.infrastructure.databases.graph import get_graph_engine
@ -16,6 +16,9 @@ from cognee.infrastructure.databases.graph import get_graph_engine
from uuid import uuid4 from uuid import uuid4
import pathlib import pathlib
# Analytics Proxy Url, currently hosted by Vercel
vercel_url = "https://proxyanalytics.vercel.app"
def get_anonymous_id(): def get_anonymous_id():
"""Creates or reads a anonymous user id""" """Creates or reads a anonymous user id"""
home_dir = str(pathlib.Path(pathlib.Path(__file__).parent.parent.parent.resolve())) home_dir = str(pathlib.Path(pathlib.Path(__file__).parent.parent.parent.resolve()))
@ -40,25 +43,24 @@ def send_telemetry(event_name: str, user_id, additional_properties: dict = {}):
if env in ["test", "dev"]: if env in ["test", "dev"]:
return return
posthog = Posthog(
project_api_key = "phc_UB1YVere1KtJg1MFxAo6ABfpkwN3OxCvGNDkMTjvH0",
host = "https://eu.i.posthog.com"
)
current_time = datetime.now(timezone.utc) current_time = datetime.now(timezone.utc)
properties = { payload = {
"time": current_time.strftime("%m/%d/%Y"), "anonymous_id": str(get_anonymous_id()),
"user_id": user_id, "event_name": event_name,
**additional_properties, "user_properties": {
"user_id": str(user_id),
},
"properties": {
"time": current_time.strftime("%m/%d/%Y"),
"user_id": str(user_id),
**additional_properties
},
} }
# Needed to forward properties to PostHog along with id response = requests.post(vercel_url, json=payload)
posthog.identify(get_anonymous_id(), properties)
try: if response.status_code != 200:
posthog.capture(get_anonymous_id(), event_name, properties) print(f"Error sending telemetry through proxy: {response.status_code}")
except Exception as e:
print("ERROR sending telemetric data to Posthog. See exception: %s", e)
def num_tokens_from_string(string: str, encoding_name: str) -> int: def num_tokens_from_string(string: str, encoding_name: str) -> int:
"""Returns the number of tokens in a text string.""" """Returns the number of tokens in a text string."""