|
@@ -1,5 +1,7 @@
|
|
|
"""Anonymous telemetry for Reflex."""
|
|
|
|
|
|
+from __future__ import annotations
|
|
|
+
|
|
|
import json
|
|
|
import multiprocessing
|
|
|
import platform
|
|
@@ -10,6 +12,7 @@ import psutil
|
|
|
|
|
|
from reflex import constants
|
|
|
from reflex.base import Base
|
|
|
+from reflex.config import get_config
|
|
|
|
|
|
|
|
|
def get_os() -> str:
|
|
@@ -67,32 +70,43 @@ class Telemetry(Base):
|
|
|
python_version: str = get_python_version()
|
|
|
|
|
|
|
|
|
-def send(event: str, telemetry_enabled: bool) -> None:
|
|
|
+def send(event: str, telemetry_enabled: bool | None = None) -> bool:
|
|
|
"""Send anonymous telemetry for Reflex.
|
|
|
|
|
|
Args:
|
|
|
event: The event name.
|
|
|
- telemetry_enabled: Whether to send the telemetry.
|
|
|
+ telemetry_enabled: Whether to send the telemetry (If None, get from config).
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Whether the telemetry was sent successfully.
|
|
|
"""
|
|
|
+ # Get the telemetry_enabled from the config if it is not specified.
|
|
|
+ if telemetry_enabled is None:
|
|
|
+ telemetry_enabled = get_config().telemetry_enabled
|
|
|
+
|
|
|
+ # Return if telemetry is disabled.
|
|
|
+ if not telemetry_enabled:
|
|
|
+ return False
|
|
|
+
|
|
|
try:
|
|
|
- if telemetry_enabled:
|
|
|
- telemetry = Telemetry()
|
|
|
- with open(constants.REFLEX_JSON) as f: # type: ignore
|
|
|
- reflex_json = json.load(f)
|
|
|
- distinct_id = reflex_json["project_hash"]
|
|
|
- post_hog = {
|
|
|
- "api_key": "phc_JoMo0fOyi0GQAooY3UyO9k0hebGkMyFJrrCw1Gt5SGb",
|
|
|
- "event": event,
|
|
|
- "properties": {
|
|
|
- "distinct_id": distinct_id,
|
|
|
- "user_os": telemetry.user_os,
|
|
|
- "reflex_version": telemetry.reflex_version,
|
|
|
- "python_version": telemetry.python_version,
|
|
|
- "cpu_count": telemetry.cpu_count,
|
|
|
- "memory": telemetry.memory,
|
|
|
- },
|
|
|
- "timestamp": datetime.utcnow().isoformat(),
|
|
|
- }
|
|
|
- httpx.post("https://app.posthog.com/capture/", json=post_hog)
|
|
|
+ telemetry = Telemetry()
|
|
|
+ with open(constants.REFLEX_JSON) as f: # type: ignore
|
|
|
+ reflex_json = json.load(f)
|
|
|
+ distinct_id = reflex_json["project_hash"]
|
|
|
+ post_hog = {
|
|
|
+ "api_key": "phc_JoMo0fOyi0GQAooY3UyO9k0hebGkMyFJrrCw1Gt5SGb",
|
|
|
+ "event": event,
|
|
|
+ "properties": {
|
|
|
+ "distinct_id": distinct_id,
|
|
|
+ "user_os": telemetry.user_os,
|
|
|
+ "reflex_version": telemetry.reflex_version,
|
|
|
+ "python_version": telemetry.python_version,
|
|
|
+ "cpu_count": telemetry.cpu_count,
|
|
|
+ "memory": telemetry.memory,
|
|
|
+ },
|
|
|
+ "timestamp": datetime.utcnow().isoformat(),
|
|
|
+ }
|
|
|
+ httpx.post("https://app.posthog.com/capture/", json=post_hog)
|
|
|
+ return True
|
|
|
except Exception:
|
|
|
- pass
|
|
|
+ return False
|