Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update settings logger #4597

Merged
merged 6 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 16 additions & 97 deletions openbb_terminal/core/log/generation/settings_logger.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# IMPORTATION STANDARD
import json
import logging
import platform
from types import FunctionType, ModuleType

# IMPORTATION THIRDPARTY
# IMPORTATION INTERNAL
from openbb_terminal import config_terminal as cfg
from openbb_terminal.core.log.generation.common import do_rollover
from openbb_terminal.core.models.credentials_model import CredentialsModel
from openbb_terminal.core.models.user_model import UserModel
from openbb_terminal.core.session.current_system import get_current_system
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.terminal_helper import is_installer

SENSITIVE_WORDS = [
"API",
Expand All @@ -30,103 +24,21 @@

def log_all_settings(with_rollover: bool = True) -> None:
"""Log all settings"""
log_settings()
log_config_terminal()
log_feature_flags()
log_keys()
log_system()
log_credentials()
log_preferences()

if with_rollover:
do_rollover()


def log_settings() -> None:
"""Log settings"""
current_user = get_current_user()
settings_dict = {}
settings_dict["tab"] = (
"True" if current_user.preferences.USE_TABULATE_DF else "False"
)
settings_dict["cls"] = (
"True" if current_user.preferences.USE_CLEAR_AFTER_CMD else "False"
)
settings_dict["color"] = "True" if current_user.preferences.USE_COLOR else "False"
settings_dict["promptkit"] = (
"True" if current_user.preferences.USE_PROMPT_TOOLKIT else "False"
)
settings_dict["thoughts"] = (
"True" if current_user.preferences.ENABLE_THOUGHTS_DAY else "False"
)
settings_dict["reporthtml"] = (
"True" if current_user.preferences.OPEN_REPORT_AS_HTML else "False"
)
settings_dict["exithelp"] = (
"True" if current_user.preferences.ENABLE_EXIT_AUTO_HELP else "False"
)
settings_dict["rcontext"] = (
"True" if current_user.preferences.REMEMBER_CONTEXTS else "False"
)
settings_dict["rich"] = "True" if current_user.preferences.ENABLE_RICH else "False"
settings_dict["richpanel"] = (
"True" if current_user.preferences.ENABLE_RICH_PANEL else "False"
)
settings_dict["ion"] = "True" if current_user.preferences.USE_ION else "False"
settings_dict["watermark"] = (
"True" if current_user.preferences.USE_WATERMARK else "False"
)
settings_dict["autoscaling"] = (
"True" if current_user.preferences.USE_PLOT_AUTOSCALING else "False"
)
settings_dict["dt"] = "True" if current_user.preferences.USE_DATETIME else "False"
settings_dict["packaged"] = "True" if is_installer() else "False"
settings_dict["python"] = str(platform.python_version())
settings_dict["os"] = str(platform.system())

logger.info("SETTINGS: %s ", json.dumps(settings_dict))

def log_system() -> None:
"""Log system"""
logger.info("SYSTEM: %s ", json.dumps(get_current_system().to_dict()))

def log_config_terminal() -> None:
"""Log config_terminal"""

config_terminal_dict = {}

for item in dir(cfg):
prop = getattr(cfg, item)
# pylint: disable=too-many-boolean-expressions
if (
not item.startswith("__")
and not isinstance(prop, FunctionType)
and not isinstance(prop, ModuleType)
and not isinstance(prop, UserModel)
and not isinstance(prop, CredentialsModel)
and not any(substring in item for substring in SENSITIVE_WORDS)
):
config_terminal_dict[item] = str(prop)

logger.info("CONFIG_TERMINAL: %s ", json.dumps(config_terminal_dict))


def log_feature_flags() -> None:
"""Log feature flags"""

current_user = get_current_user()
feature_flags_dict = {}

for item in dir(current_user.preferences):
prop = getattr(current_user.preferences, item)
if (
not item.startswith("__")
and not isinstance(prop, FunctionType)
and not isinstance(prop, ModuleType)
and not isinstance(prop, UserModel)
and not isinstance(prop, CredentialsModel)
):
feature_flags_dict[item] = str(prop)

logger.info("FEATURE_FLAGS: %s ", json.dumps(feature_flags_dict))


def log_keys() -> None:
"""Log keys"""
def log_credentials() -> None:
"""Log credentials"""

current_user = get_current_user()

Expand All @@ -143,3 +55,10 @@ def log_keys() -> None:
current_keys[cfg_var_name] = "not_defined"

logger.info("KEYS: %s ", json.dumps(current_keys))


def log_preferences() -> None:
"""Log preferences"""
logger.info(
"PREFERENCES: %s ", json.dumps(get_current_user().preferences.to_dict())
)
9 changes: 9 additions & 0 deletions openbb_terminal/core/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ def get_value(self, field: str) -> Optional[Any]:
return getattr(self, field)
return None

def to_dict(self) -> dict[str, Any]:
"""Convert model to dict."""
d = self.__dict__.copy()
keys = list(d.keys())
for key in keys:
if key.startswith("_"):
del d[key]
return d

def get_default(self, field: str) -> Optional[Any]:
"""Get default field value."""
if hasattr(self, field):
Expand Down
11 changes: 10 additions & 1 deletion openbb_terminal/core/models/system_model.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import platform
import sys
from typing import Literal

from pydantic.dataclasses import dataclass

from openbb_terminal.core.models import BaseModel

# pylint: disable=too-many-instance-attributes


@dataclass(config=dict(validate_assignment=True, frozen=True))
class SystemModel(BaseModel):
"""Data model for system variables and configurations."""

# System version
# System section
OS: str = str(platform.system())
PYTHON_VERSION: str = str(platform.python_version())

# OpenBB section
VERSION = "3.0.0rc1"
PACKAGED: bool = getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS")

# Logging section
LOGGING_APP_NAME: str = "gst"
Expand Down
6 changes: 4 additions & 2 deletions openbb_terminal/core/sdk/sdk_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ def __remove_key_and_log_state(func_module: str, function_args: dict) -> dict:

if func_module == "openbb_terminal.keys_model":
# pylint: disable=C0415
from openbb_terminal.core.log.generation.settings_logger import log_keys
from openbb_terminal.core.log.generation.settings_logger import (
log_credentials,
)

# remove key if defined
function_args.pop("key", None)
log_keys()
log_credentials()
return function_args

def log_after_call(
Expand Down