Rework telemetry to support installation_id (#2480)

This commit is contained in:
jackie-pc 2024-01-31 11:39:48 -08:00 committed by GitHub
parent 80dce21ac0
commit 80c9eb34e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 54 additions and 6 deletions

View File

@ -80,6 +80,10 @@ def _init(
prerequisites.check_latest_package_version(constants.Reflex.MODULE_NAME)
prerequisites.initialize_reflex_user_directory()
prerequisites.ensure_reflex_installation_id()
# Set up the app directory, only if the config doesn't exist.
if not os.path.exists(constants.Config.FILE):
if template is None:

View File

@ -16,7 +16,7 @@ import zipfile
from fileinput import FileInput
from pathlib import Path
from types import ModuleType
from typing import Callable
from typing import Callable, Optional
import httpx
import pkg_resources
@ -824,10 +824,48 @@ def validate_frontend_dependencies(init=True):
validate_bun()
def initialize_frontend_dependencies():
"""Initialize all the frontend dependencies."""
def ensure_reflex_installation_id() -> Optional[int]:
"""Ensures that a reflex distinct id has been generated and stored in the reflex directory.
Returns:
Distinct id.
"""
try:
initialize_reflex_user_directory()
installation_id_file = os.path.join(constants.Reflex.DIR, "installation_id")
installation_id = None
if os.path.exists(installation_id_file):
try:
with open(installation_id_file, "r") as f:
installation_id = int(f.read())
except Exception:
# If anything goes wrong at all... just regenerate.
# Like what? Examples:
# - file not exists
# - file not readable
# - content not parseable as an int
pass
if installation_id is None:
installation_id = random.getrandbits(128)
with open(installation_id_file, "w") as f:
f.write(str(installation_id))
# If we get here, installation_id is definitely set
return installation_id
except Exception as e:
console.debug(f"Failed to ensure reflex installation id: {e}")
return None
def initialize_reflex_user_directory():
"""Initialize the reflex user directory."""
# Create the reflex directory.
path_ops.mkdir(constants.Reflex.DIR)
def initialize_frontend_dependencies():
"""Initialize all the frontend dependencies."""
# validate dependencies before install
validate_frontend_dependencies()
# Install the frontend dependencies.

View File

@ -10,6 +10,7 @@ from datetime import datetime
import psutil
from reflex import constants
from reflex.utils.prerequisites import ensure_reflex_installation_id
def get_os() -> str:
@ -79,15 +80,20 @@ def send(event: str, telemetry_enabled: bool | None = None) -> bool:
if not telemetry_enabled:
return False
installation_id = ensure_reflex_installation_id()
if installation_id is None:
return False
try:
with open(constants.Dirs.REFLEX_JSON) as f:
reflex_json = json.load(f)
distinct_id = reflex_json["project_hash"]
project_hash = reflex_json["project_hash"]
post_hog = {
"api_key": "phc_JoMo0fOyi0GQAooY3UyO9k0hebGkMyFJrrCw1Gt5SGb",
"event": event,
"properties": {
"distinct_id": distinct_id,
"distinct_id": installation_id,
"distinct_app_id": project_hash,
"user_os": get_os(),
"reflex_version": get_reflex_version(),
"python_version": get_python_version(),

View File

@ -485,7 +485,7 @@ def test_create_reflex_dir(mocker, is_windows):
"reflex.utils.prerequisites.path_ops.mkdir", mocker.Mock()
)
prerequisites.initialize_frontend_dependencies()
prerequisites.initialize_reflex_user_directory()
assert create_cmd.called