From a054e5d005d4aa2546a7bfb0b9363a096ef4fdfd Mon Sep 17 00:00:00 2001 From: Henrique Joaquim Date: Wed, 22 Feb 2023 14:22:05 +0000 Subject: [PATCH] User UUID to use the designated column instead (#4299) * token generation on the terminal * revoke and show commands * uuid on the right column * adding logs to the account commands * login info at this point does not make sense anymore * get_user_uuid function * log formatter to use get_user_uuid function * ruff + black * fix tests * fix tests --------- Co-authored-by: James Maslek --- openbb_terminal/core/library/operation.py | 15 --------------- .../core/log/generation/custom_logger.py | 3 +-- .../generation/formatter_with_exceptions.py | 5 +++-- .../core/log/generation/settings.py | 11 +++++++++++ .../core/log/generation/user_logger.py | 19 ++++++------------- openbb_terminal/loggers.py | 3 +++ openbb_terminal/sdk_core/sdk_helpers.py | 15 --------------- openbb_terminal/session/sdk_session.py | 8 ++++++++ tests/openbb_terminal/core/log/conftest.py | 1 + .../test_extract_log_extra.json | 2 +- .../test_formatter_with_exceptions.py | 1 + .../core/log/generation/test_settings.py | 4 ++++ tests/openbb_terminal/test_loggers.py | 1 + 13 files changed, 40 insertions(+), 48 deletions(-) diff --git a/openbb_terminal/core/library/operation.py b/openbb_terminal/core/library/operation.py index 596a406e8f66..1483be1398cf 100644 --- a/openbb_terminal/core/library/operation.py +++ b/openbb_terminal/core/library/operation.py @@ -7,7 +7,6 @@ import openbb_terminal.config_terminal as cfg from openbb_terminal.core.library.metadata import Metadata from openbb_terminal.core.library.trail_map import TrailMap -from openbb_terminal.session.sdk_session import login # pylint: disable=import-outside-toplevel @@ -249,9 +248,6 @@ def log_after_call( method_result=method_result, method_chosen=self.__method_chosen, ) - self.__log_if_login( - method_chosen=self.__method_chosen, - ) self.__log_end( logger=logger, method_chosen=self.__method_chosen, @@ -275,17 +271,6 @@ def __log_exception_if_any( extra={"func_name_override": method_chosen.__name__}, ) - @staticmethod - def __log_if_login( - method_chosen: Callable, - ): - if method_chosen.__name__ == login.__name__: - from openbb_terminal.core.log.generation.user_logger import ( # pylint: disable=import-outside-toplevel - log_user, - ) - - log_user(with_rollover=False) - @staticmethod def __log_end(logger: Logger, method_chosen: Callable): logger.info( diff --git a/openbb_terminal/core/log/generation/custom_logger.py b/openbb_terminal/core/log/generation/custom_logger.py index e27a3dd7741a..483265adc25e 100644 --- a/openbb_terminal/core/log/generation/custom_logger.py +++ b/openbb_terminal/core/log/generation/custom_logger.py @@ -1,7 +1,7 @@ # IMPORTATION STANDARD import logging -from openbb_terminal.core.log.generation import settings_logger, user_logger +from openbb_terminal.core.log.generation import settings_logger from openbb_terminal.core.log.generation.common import do_rollover # IMPORTATION THIRDPARTY @@ -19,5 +19,4 @@ def log_terminal(test_mode: bool): logger.info("START") settings_logger.log_all_settings(with_rollover=False) - user_logger.log_user(with_rollover=False) do_rollover() diff --git a/openbb_terminal/core/log/generation/formatter_with_exceptions.py b/openbb_terminal/core/log/generation/formatter_with_exceptions.py index 8e7d4ed6857f..1b569af582f4 100644 --- a/openbb_terminal/core/log/generation/formatter_with_exceptions.py +++ b/openbb_terminal/core/log/generation/formatter_with_exceptions.py @@ -6,6 +6,7 @@ # IMPORTATION INTERNAL from openbb_terminal.core.config.paths import HOME_DIRECTORY from openbb_terminal.core.log.generation.settings import AppSettings +from openbb_terminal.core.log.generation.user_logger import get_user_uuid class FormatterWithExceptions(logging.Formatter): @@ -36,8 +37,6 @@ def extract_log_extra(record: logging.LogRecord): record.funcName = record.func_name_override # type: ignore record.lineno = 0 - log_extra["userId"] = getattr(record, "user_id", "NA") - if hasattr(record, "session_id"): log_extra["sessionId"] = record.session_id # type: ignore @@ -149,7 +148,9 @@ def format(self, record: logging.LogRecord) -> str: "appId": app_settings.identifier, "sessionId": app_settings.session_id, "commitHash": app_settings.commit_hash, + "userId": get_user_uuid(), } + log_extra = self.extract_log_extra(record=record) log_prefix_content = {**log_prefix_content, **log_extra} log_prefix = self.LOGPREFIXFORMAT % log_prefix_content diff --git a/openbb_terminal/core/log/generation/settings.py b/openbb_terminal/core/log/generation/settings.py index 2c4486746759..fd637ada0a38 100644 --- a/openbb_terminal/core/log/generation/settings.py +++ b/openbb_terminal/core/log/generation/settings.py @@ -42,12 +42,21 @@ def identifier(self) -> str: def session_id(self) -> str: return self.__session_id + @property + def user_id(self) -> str: + return self.__user_id + + @user_id.setter + def user_id(self, value: str): + self.__user_id = value + def __init__( self, name: str, commit_hash: str, session_id: str, identifier: str, + user_id: str, ): """ Args: @@ -55,12 +64,14 @@ def __init__( commit_hash (str): Commit hash of the current running code. identifier (str): Unique key identifying a particular installation. session_id (str): Key identifying a particular running session. + user_id (str): Hash identifying a particular user. """ self.__name = name self.__commit_hash = commit_hash self.__identifier = identifier self.__session_id = session_id + self.__user_id = user_id class LogSettings: diff --git a/openbb_terminal/core/log/generation/user_logger.py b/openbb_terminal/core/log/generation/user_logger.py index 635af0b83f94..d8f2c3049fa1 100644 --- a/openbb_terminal/core/log/generation/user_logger.py +++ b/openbb_terminal/core/log/generation/user_logger.py @@ -1,26 +1,19 @@ # IMPORTATION STANDARD -import json import logging -from openbb_terminal.core.log.generation.common import do_rollover - # IMPORTATION THIRDPARTY # IMPORTATION INTERNAL from openbb_terminal.session.user import User logger = logging.getLogger(__name__) +NO_USER_PLACEHOLDER = "NA" -def log_user(with_rollover: bool = True): - """Log user""" - if not User.is_guest(): - _log_user_info() - if with_rollover: - do_rollover() +def get_user_uuid() -> str: + """Get user UUID""" + if not User.is_guest(): + return User.get_uuid() -def _log_user_info(): - """Log user info""" - user_info = {"user_uuid": User.get_uuid()} - logger.info("USER: %s ", json.dumps(user_info)) + return NO_USER_PLACEHOLDER diff --git a/openbb_terminal/loggers.py b/openbb_terminal/loggers.py index d4c0fe2a20bf..cdd09b0d4adf 100644 --- a/openbb_terminal/loggers.py +++ b/openbb_terminal/loggers.py @@ -40,6 +40,7 @@ LogSettings, Settings, ) +from openbb_terminal.core.log.generation.user_logger import get_user_uuid logging.getLogger("requests").setLevel(LOGGING_VERBOSITY) logging.getLogger("urllib3").setLevel(LOGGING_VERBOSITY) @@ -166,6 +167,7 @@ def setup_logging( name = app_name or LOGGING_APP_NAME identifier = get_app_id() session_id = session_id or str(START_TIMESTAMP) + user_id = get_user_uuid() # AWSSettings aws_access_key_id = LOGGING_AWS_ACCESS_KEY_ID @@ -184,6 +186,7 @@ def setup_logging( name=name, identifier=identifier, session_id=session_id, + user_id=user_id, ), aws_settings=AWSSettings( aws_access_key_id=aws_access_key_id, diff --git a/openbb_terminal/sdk_core/sdk_helpers.py b/openbb_terminal/sdk_core/sdk_helpers.py index 0d86bbc2fd3e..42924069b5f4 100644 --- a/openbb_terminal/sdk_core/sdk_helpers.py +++ b/openbb_terminal/sdk_core/sdk_helpers.py @@ -16,7 +16,6 @@ OPTIMIZATION_TOOLKIT_ENABLED, OPTIMIZATION_TOOLKIT_WARNING, ) -from openbb_terminal.session.sdk_session import login if not FORECASTING_TOOLKIT_ENABLED and not load_env_vars( "OPENBB_DISABLE_FORECASTING_WARNING", strtobool, False @@ -279,9 +278,6 @@ def log_after_call( method_result=method_result, method_chosen=self.__method_chosen, ) - self.__log_if_login( - method_chosen=self.__method_chosen, - ) self.__log_end( logger=logger, method_chosen=self.__method_chosen, @@ -305,17 +301,6 @@ def __log_exception_if_any( extra={"func_name_override": method_chosen.__name__}, ) - @staticmethod - def __log_if_login( - method_chosen: Callable, - ): - if method_chosen.__name__ == login.__name__: - from openbb_terminal.core.log.generation.user_logger import ( # pylint: disable=import-outside-toplevel - log_user, - ) - - log_user(with_rollover=False) - @staticmethod def __log_end(logger: Logger, method_chosen: Callable): logger.info( diff --git a/openbb_terminal/session/sdk_session.py b/openbb_terminal/session/sdk_session.py index 3606885662cc..51fa4008f4f3 100644 --- a/openbb_terminal/session/sdk_session.py +++ b/openbb_terminal/session/sdk_session.py @@ -1,3 +1,6 @@ +import logging + +from openbb_terminal.decorators import log_start_end from openbb_terminal.rich_config import console from openbb_terminal.session import ( local_model as Local, @@ -5,6 +8,8 @@ ) from openbb_terminal.session.user import User +logger = logging.getLogger(__name__) + def get_session(email: str, password: str, token: str, save: bool): session = "" @@ -23,6 +28,7 @@ def get_session(email: str, password: str, token: str, save: bool): return session +@log_start_end(log=logger) def login( email: str = "", password: str = "", token: str = "", keep_session: bool = False ): @@ -65,6 +71,7 @@ def login( console.print("[green]Login successful.[/green]") +@log_start_end(log=logger) def logout(): """ Logout and clear session. @@ -81,6 +88,7 @@ def logout(): ) +@log_start_end(log=logger) def whoami(): """ Display user info. diff --git a/tests/openbb_terminal/core/log/conftest.py b/tests/openbb_terminal/core/log/conftest.py index 705512921fa4..6069187d858b 100644 --- a/tests/openbb_terminal/core/log/conftest.py +++ b/tests/openbb_terminal/core/log/conftest.py @@ -16,6 +16,7 @@ def settings(tmp_path): name="MOCK_NAME", identifier="MOCK_IDENTIFIER", session_id="MOCK_SESSION_ID", + user_id="MOCK_USER_ID", ), aws_settings=AWSSettings( aws_access_key_id="MOCK_AWS_ACCESS_KEY_ID", diff --git a/tests/openbb_terminal/core/log/generation/json/test_formatter_with_exceptions/test_extract_log_extra.json b/tests/openbb_terminal/core/log/generation/json/test_formatter_with_exceptions/test_extract_log_extra.json index 75c63f4834cc..f8492ecf21e9 100644 --- a/tests/openbb_terminal/core/log/generation/json/test_formatter_with_exceptions/test_extract_log_extra.json +++ b/tests/openbb_terminal/core/log/generation/json/test_formatter_with_exceptions/test_extract_log_extra.json @@ -1 +1 @@ -{"userId": "MOCK_USER_ID", "sessionId": "MOCK_SESSION_ID"} \ No newline at end of file +{"sessionId": "MOCK_SESSION_ID"} \ No newline at end of file diff --git a/tests/openbb_terminal/core/log/generation/test_formatter_with_exceptions.py b/tests/openbb_terminal/core/log/generation/test_formatter_with_exceptions.py index dbfddc5539a8..0374f3b531d1 100644 --- a/tests/openbb_terminal/core/log/generation/test_formatter_with_exceptions.py +++ b/tests/openbb_terminal/core/log/generation/test_formatter_with_exceptions.py @@ -14,6 +14,7 @@ name="MOCK_COMMIT_HASH", identifier="MOCK_COMMIT_HASH", session_id="MOCK_SESSION_ID", + user_id="MOCK_USER_ID", ) formatter = FormatterWithExceptions(app_settings) diff --git a/tests/openbb_terminal/core/log/generation/test_settings.py b/tests/openbb_terminal/core/log/generation/test_settings.py index fb0916dde3e6..a02c06badf7b 100644 --- a/tests/openbb_terminal/core/log/generation/test_settings.py +++ b/tests/openbb_terminal/core/log/generation/test_settings.py @@ -24,12 +24,14 @@ def test_app_settings(): commit_hash = "MOCK_COMMIT_HASH" session_id = "MOCK_SESSION_ID" identifier = "MOCK_IDENTIFIER" + user_id = "MOCK_USER_ID" app_settings = AppSettings( name=name, commit_hash=commit_hash, session_id=session_id, identifier=identifier, + user_id=user_id, ) assert app_settings.name == name @@ -78,11 +80,13 @@ def test_settings(tmp_path): commit_hash = "MOCK_COMMIT_HASH" session_id = "MOCK_SESSION_ID" identifier = "MOCK_IDENTIFIER" + user_id = "MOCK_USER_ID" app_settings = AppSettings( name=name, commit_hash=commit_hash, session_id=session_id, identifier=identifier, + user_id=user_id, ) aws_access_key_id = "MOCK_AWS_ACCESS_KEY_ID" diff --git a/tests/openbb_terminal/test_loggers.py b/tests/openbb_terminal/test_loggers.py index f2739db88709..607dae3acf50 100644 --- a/tests/openbb_terminal/test_loggers.py +++ b/tests/openbb_terminal/test_loggers.py @@ -16,6 +16,7 @@ name="MOCK_COMMIT_HASH", identifier="MOCK_COMMIT_HASH", session_id="MOCK_SESSION_ID", + user_id="MOCK_USER_ID", ), aws_settings=AWSSettings( aws_access_key_id="MOCK_AWS_ACCESS_KEY_ID",