diff --git a/reflex/app_module_for_backend.py b/reflex/app_module_for_backend.py index 508488e44..4149c3fd2 100644 --- a/reflex/app_module_for_backend.py +++ b/reflex/app_module_for_backend.py @@ -2,11 +2,11 @@ Only the app attribute is explicitly exposed. """ +import time from concurrent.futures import ThreadPoolExecutor -import time from reflex import constants - +from reflex.utils import telemetry from reflex.utils.exec import is_prod_mode from reflex.utils.prerequisites import get_app @@ -20,17 +20,9 @@ app = getattr(app_module, constants.CompileVars.APP) app._apply_decorated_pages() start_time = time.perf_counter() -def compile_callback(f): - from reflex.utils import telemetry - try: - # Force background compile errors to print eagerly - f.result() - finally: - telemetry.send("test-compile", duration=time.perf_counter()- start_time) - del telemetry compile_future = ThreadPoolExecutor(max_workers=1).submit(app._compile) -compile_future.add_done_callback(compile_callback) +compile_future.add_done_callback(lambda f: telemetry.compile_callback(f, start_time)) # Wait for the compile to finish in prod mode to ensure all optional endpoints are mounted. if is_prod_mode(): @@ -41,6 +33,5 @@ del app_module del compile_future del get_app del is_prod_mode - del constants del ThreadPoolExecutor diff --git a/reflex/components/el/elements/metadata.py b/reflex/components/el/elements/metadata.py index fa73d40cf..82562a76a 100644 --- a/reflex/components/el/elements/metadata.py +++ b/reflex/components/el/elements/metadata.py @@ -44,7 +44,7 @@ class Meta(BaseHTML): # Inherits common attributes from BaseHTML """Display the meta element.""" tag = "meta" - + char_set: Var[Union[str, int, bool]] content: Var[Union[str, int, bool]] http_equiv: Var[Union[str, int, bool]] diff --git a/reflex/reflex.py b/reflex/reflex.py index 28234ccf8..000ee9df4 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -4,11 +4,11 @@ from __future__ import annotations import atexit import os +import time import webbrowser from pathlib import Path from typing import List, Optional -import time import typer import typer.core from reflex_cli.deployments import deployments_cli @@ -110,10 +110,10 @@ def _init( template = constants.Templates.DEFAULT start_time = time.perf_counter() - + # Check if the app is already initialized. reinit = os.path.exists(constants.Config.FILE) - + # Initialize the app. prerequisites.initialize_app(app_name, template, reinit=reinit) @@ -138,7 +138,8 @@ def _init( # Post telemetry event event_type = "reinit" if reinit else "init" telemetry.send(event_type, duration=time.perf_counter() - start_time) - + + @cli.command() def init( name: str = typer.Option( diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index e3c1458a4..12bc49c16 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -1425,6 +1425,7 @@ def initialize_app(app_name: str, template: str | None = None, reinit: bool = Fa Args: app_name: The name of the app. template: The name of the template to use. + reinit: Whether to reinitialize the app. Raises: Exit: If template is directly provided in the command flag and is invalid. @@ -1469,6 +1470,7 @@ def initialize_app(app_name: str, template: str | None = None, reinit: bool = Fa template_url=template_url, ) + def initialize_main_module_index_from_generation(app_name: str, generation_hash: str): """Overwrite the `index` function in the main module with reflex.build generated code. diff --git a/reflex/utils/telemetry.py b/reflex/utils/telemetry.py index dc015df3f..f71b9c920 100644 --- a/reflex/utils/telemetry.py +++ b/reflex/utils/telemetry.py @@ -4,9 +4,11 @@ from __future__ import annotations import asyncio import multiprocessing +import os import platform +import time import warnings -import os + try: from datetime import UTC, datetime except ImportError: @@ -69,7 +71,7 @@ def get_cpu_count() -> int: """ return multiprocessing.cpu_count() - + def get_memory() -> int: """Get the total memory in MB. @@ -80,7 +82,8 @@ def get_memory() -> int: return psutil.virtual_memory().total >> 20 except ValueError: # needed to pass ubuntu test return 0 - + + def get_folder_size(folder: str) -> int: """Get the total size of a folder in bytes, ignoring 'node_modules' folder. @@ -91,12 +94,11 @@ def get_folder_size(folder: str) -> int: The total size of the folder in bytes. """ total_files = 0 - for dirpath, dirnames, filenames in os.walk(folder): + for _, _, filenames in os.walk(folder): total_files += len(filenames) return total_files - def _raise_on_missing_project_hash() -> bool: """Check if an error should be raised when project hash is missing. @@ -113,6 +115,20 @@ def _raise_on_missing_project_hash() -> bool: return True +def compile_callback(f, start_time): + """Callback to send telemetry after compiling the app. + + Args: + f: The future object. + start_time: The start time of the compilation. + """ + try: + # Force background compile errors to print eagerly + f.result() + finally: + send("test-compile", duration=time.perf_counter() - start_time) + + def _prepare_event(event: str, **kwargs) -> dict: """Prepare the event to be sent to the PostHog server. @@ -160,7 +176,9 @@ def _prepare_event(event: str, **kwargs) -> dict: "cpu_count": get_cpu_count(), "memory": get_memory(), "cpu_info": dict(cpuinfo) if cpuinfo else {}, - "pages_count": get_folder_size(".web/pages") if event == "test-compile" or event == "run-dev" else None, + "pages_count": get_folder_size(".web/pages") + if event == "test-compile" or event == "run-dev" + else None, **additional_fields, }, "timestamp": stamp,