From 237defad95d4b621220c14818e5db927f4aec3e0 Mon Sep 17 00:00:00 2001 From: teh_coderer Date: Fri, 19 Jan 2024 16:31:03 -0500 Subject: [PATCH] hotfix/ allow multiple `identify` calls (#5976) * allow multiple `identify` calls * Update posthog_handler.py --- .../app/logs/handlers/posthog_handler.py | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/openbb_platform/core/openbb_core/app/logs/handlers/posthog_handler.py b/openbb_platform/core/openbb_core/app/logs/handlers/posthog_handler.py index b7f8ebeb3a27..8ca2b2c8ad66 100644 --- a/openbb_platform/core/openbb_core/app/logs/handlers/posthog_handler.py +++ b/openbb_platform/core/openbb_core/app/logs/handlers/posthog_handler.py @@ -40,6 +40,7 @@ def settings(self) -> LoggingSettings: @settings.setter def settings(self, settings: LoggingSettings) -> None: + """Set logging settings.""" self._settings = settings def emit(self, record: logging.LogRecord): @@ -51,6 +52,29 @@ def emit(self, record: logging.LogRecord): if Env().DEBUG_MODE: raise e + def distinct_id(self) -> str: + """Get distinct id.""" + return self._settings.user_id or self._settings.app_id + + def identify(self) -> None: + """Identify user.""" + if self.logged_in or not self._settings.user_id: + return + + posthog.identify( + self._settings.user_id, + { + "email": self._settings.user_email, + "primaryUsage": self._settings.user_primary_usage, + }, + ) + + if self._settings.sub_app_name == "pro": + return + + self.logged_in = True + posthog.alias(self._settings.user_id, self._settings.app_id) + def log_to_dict(self, log_info: str) -> dict: """Log to dict.""" log_regex = r"(STARTUP|CMD|ERROR): (.*)" @@ -83,22 +107,8 @@ def send(self, record: logging.LogRecord): if event_name not in [e.value for e in self.AllowedEvents]: return - if not self.logged_in and self._settings.user_id: - self.logged_in = True - posthog.identify( - self._settings.user_id, - { - "email": self._settings.user_email, - "primaryUsage": self._settings.user_primary_usage, - }, - ) - posthog.alias(self._settings.user_id, self._settings.app_id) - - posthog.capture( - self._settings.app_id, - event_name, - properties=log_extra, - ) + self.identify() + posthog.capture(self.distinct_id(), event_name, properties=log_extra) def extract_log_extra(self, record: logging.LogRecord) -> Dict[str, Any]: """Extract log extra from record."""