Skip to content

Commit

Permalink
User UUID to use the designated column instead (#4299)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hjoaquim and jmaslek authored Feb 22, 2023
1 parent 2079649 commit a054e5d
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 48 deletions.
15 changes: 0 additions & 15 deletions openbb_terminal/core/library/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions openbb_terminal/core/log/generation/custom_logger.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions openbb_terminal/core/log/generation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,36 @@ 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:
name (str): Source of the application.
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:
Expand Down
19 changes: 6 additions & 13 deletions openbb_terminal/core/log/generation/user_logger.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions openbb_terminal/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
15 changes: 0 additions & 15 deletions openbb_terminal/sdk_core/sdk_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions openbb_terminal/session/sdk_session.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
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,
session_model,
)
from openbb_terminal.session.user import User

logger = logging.getLogger(__name__)


def get_session(email: str, password: str, token: str, save: bool):
session = ""
Expand All @@ -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
):
Expand Down Expand Up @@ -65,6 +71,7 @@ def login(
console.print("[green]Login successful.[/green]")


@log_start_end(log=logger)
def logout():
"""
Logout and clear session.
Expand All @@ -81,6 +88,7 @@ def logout():
)


@log_start_end(log=logger)
def whoami():
"""
Display user info.
Expand Down
1 change: 1 addition & 0 deletions tests/openbb_terminal/core/log/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"userId": "MOCK_USER_ID", "sessionId": "MOCK_SESSION_ID"}
{"sessionId": "MOCK_SESSION_ID"}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions tests/openbb_terminal/core/log/generation/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions tests/openbb_terminal/test_loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a054e5d

Please sign in to comment.