|
@@ -2,15 +2,16 @@
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
-import json
|
|
|
import multiprocessing
|
|
|
import platform
|
|
|
from datetime import datetime
|
|
|
|
|
|
+import httpx
|
|
|
import psutil
|
|
|
|
|
|
from reflex import constants
|
|
|
-from reflex.utils.prerequisites import ensure_reflex_installation_id
|
|
|
+from reflex.utils import console
|
|
|
+from reflex.utils.prerequisites import ensure_reflex_installation_id, get_project_hash
|
|
|
|
|
|
POSTHOG_API_URL: str = "https://app.posthog.com/capture/"
|
|
|
|
|
@@ -57,7 +58,52 @@ def get_memory() -> int:
|
|
|
Returns:
|
|
|
The total memory in MB.
|
|
|
"""
|
|
|
- return psutil.virtual_memory().total >> 20
|
|
|
+ try:
|
|
|
+ return psutil.virtual_memory().total >> 20
|
|
|
+ except ValueError: # needed to pass ubuntu test
|
|
|
+ return 0
|
|
|
+
|
|
|
+
|
|
|
+def _prepare_event(event: str) -> dict:
|
|
|
+ """Prepare the event to be sent to the PostHog server.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ event: The event name.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ The event data.
|
|
|
+ """
|
|
|
+ installation_id = ensure_reflex_installation_id()
|
|
|
+ project_hash = get_project_hash(raise_on_fail=True)
|
|
|
+
|
|
|
+ if installation_id is None or project_hash is None:
|
|
|
+ console.debug(
|
|
|
+ f"Could not get installation_id or project_hash: {installation_id}, {project_hash}"
|
|
|
+ )
|
|
|
+ return {}
|
|
|
+
|
|
|
+ return {
|
|
|
+ "api_key": "phc_JoMo0fOyi0GQAooY3UyO9k0hebGkMyFJrrCw1Gt5SGb",
|
|
|
+ "event": event,
|
|
|
+ "properties": {
|
|
|
+ "distinct_id": installation_id,
|
|
|
+ "distinct_app_id": project_hash,
|
|
|
+ "user_os": get_os(),
|
|
|
+ "reflex_version": get_reflex_version(),
|
|
|
+ "python_version": get_python_version(),
|
|
|
+ "cpu_count": get_cpu_count(),
|
|
|
+ "memory": get_memory(),
|
|
|
+ },
|
|
|
+ "timestamp": datetime.utcnow().isoformat(),
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def _send_event(event_data: dict) -> bool:
|
|
|
+ try:
|
|
|
+ httpx.post(POSTHOG_API_URL, json=event_data)
|
|
|
+ return True
|
|
|
+ except Exception:
|
|
|
+ return False
|
|
|
|
|
|
|
|
|
def send(event: str, telemetry_enabled: bool | None = None) -> bool:
|
|
@@ -70,8 +116,6 @@ def send(event: str, telemetry_enabled: bool | None = None) -> bool:
|
|
|
Returns:
|
|
|
Whether the telemetry was sent successfully.
|
|
|
"""
|
|
|
- import httpx
|
|
|
-
|
|
|
from reflex.config import get_config
|
|
|
|
|
|
# Get the telemetry_enabled from the config if it is not specified.
|
|
@@ -82,29 +126,8 @@ 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:
|
|
|
+ event_data = _prepare_event(event)
|
|
|
+ if not event_data:
|
|
|
return False
|
|
|
|
|
|
- try:
|
|
|
- with open(constants.Dirs.REFLEX_JSON) as f:
|
|
|
- reflex_json = json.load(f)
|
|
|
- project_hash = reflex_json["project_hash"]
|
|
|
- post_hog = {
|
|
|
- "api_key": "phc_JoMo0fOyi0GQAooY3UyO9k0hebGkMyFJrrCw1Gt5SGb",
|
|
|
- "event": event,
|
|
|
- "properties": {
|
|
|
- "distinct_id": installation_id,
|
|
|
- "distinct_app_id": project_hash,
|
|
|
- "user_os": get_os(),
|
|
|
- "reflex_version": get_reflex_version(),
|
|
|
- "python_version": get_python_version(),
|
|
|
- "cpu_count": get_cpu_count(),
|
|
|
- "memory": get_memory(),
|
|
|
- },
|
|
|
- "timestamp": datetime.utcnow().isoformat(),
|
|
|
- }
|
|
|
- httpx.post(POSTHOG_API_URL, json=post_hog)
|
|
|
- return True
|
|
|
- except Exception:
|
|
|
- return False
|
|
|
+ return _send_event(event_data)
|