Skip to content

Commit

Permalink
Merge branch 'develop' into feature/fixed_income
Browse files Browse the repository at this point in the history
  • Loading branch information
JerBouma authored Feb 22, 2023
2 parents 1ba4f81 + 58d2ea1 commit a5129c4
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 56 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/nightly-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ jobs:
with:
workflow: docker.yml
token: ${{ secrets.GITHUB_TOKEN }}

trigger-pypi-build:
runs-on: ubuntu-latest
steps:
- name: Trigger PyPI Build
uses: aurelien-baudet/workflow-dispatch@v2
with:
workflow: nightly-pypi.yml
token: ${{ secrets.GITHUB_TOKEN }}
48 changes: 48 additions & 0 deletions .github/workflows/nightly-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Deploy to PyPI - Nightly

on: workflow_dispatch

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
deploy-nightly-pypi:
name: Build and publish nightly distributions 📦 to PyPI
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup Python 3.9
uses: actions/setup-python@v4
with:
python-version: "3.9"

- name: Update version in pyproject.toml and Edit docs
run: |
sed -i 's/name = ".*"/name = "openbb-nightly"/' pyproject.toml
sed -i "3s/version = \"\(.*\)\"/version = \"\1.dev$(date +%Y%m%d)\"/" pyproject.toml
head pyproject.toml
sed -i 's/pip install openbb/pip install openbb-nightly/g; s/from openbb_terminal.sdk import openbb/from openbb_terminal.sdk import openbb-nightly/g' ./website/content/sdk/quickstart/pypi.md
- name: Install pypa/build
run: >-
python -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: >-
python -m
build
--sdist
--wheel
--outdir dist/
.
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.NIGHTLY_PYPI_API_TOKEN }}
4 changes: 2 additions & 2 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ concurrency:

jobs:
deploy-test-pypi:
name: Build and publish distributions 📦 to PyPI and TestPyPI
name: Build and publish 📦 to TestPyPI
if: startsWith(github.ref, 'refs/heads/release/')
runs-on: ubuntu-latest

Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
repository_url: https://test.pypi.org/legacy/

deploy-pypi:
name: Build and publish distributions 📦 to PyPI and TestPyPI
name: Build and publish 📦 to PyPI
if: startsWith(github.ref, 'refs/heads/main')
runs-on: ubuntu-latest

Expand Down
2 changes: 1 addition & 1 deletion openbb_terminal/core/config/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_user_data_directory():
USER_CUSTOM_IMPORTS_DIRECTORY = USER_DATA_DIRECTORY / "custom_imports"
USER_PORTFOLIO_DATA_DIRECTORY = USER_DATA_DIRECTORY / "portfolio"
USER_ROUTINES_DIRECTORY = USER_DATA_DIRECTORY / "routines"
USER_DATA_SOURCES_DEFAULT_FILE = USER_DATA_DIRECTORY / "data_sources_default.json"
USER_DATA_SOURCES_DEFAULT_FILE = MISCELLANEOUS_DIRECTORY / "data_sources_default.json"
USER_PRESETS_DIRECTORY = USER_DATA_DIRECTORY / "presets"
USER_REPORTS_DIRECTORY = USER_DATA_DIRECTORY / "reports"
USER_CUSTOM_REPORTS_DIRECTORY = USER_DATA_DIRECTORY / "reports" / "custom reports"
Expand Down
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
1 change: 1 addition & 0 deletions openbb_terminal/economy/economy_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,7 @@ def call_eval(self, other_args):
type=str,
nargs="+",
dest="query",
required="-h" not in other_args,
help="Query to evaluate on loaded datasets",
)
if other_args and "-" not in other_args[0][0]:
Expand Down
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
9 changes: 9 additions & 0 deletions openbb_terminal/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from openbb_terminal import feature_flags as obbff
from openbb_terminal.session.user import User
from openbb_terminal.terminal_helper import is_auth_enabled

if User.is_guest():
load_dotenv_and_reload_configs()
Expand All @@ -48,6 +49,14 @@ def __init__(self):
self.logout = lib.sdk_session.logout
self.news = lib.common_feedparser_model.get_news
self.whoami = lib.sdk_session.whoami
self._try_to_login()

def _try_to_login(self):
if User.is_guest() and is_auth_enabled():
try:
self.login()
except Exception:
pass

@property
def alt(self):
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
15 changes: 11 additions & 4 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 @@ -13,7 +18,7 @@ def get_session(email: str, password: str, token: str, save: bool):
console.print("Creating session from token.")
session = session_model.create_session_from_token(token, save) # type: ignore

if not session:
if not session and email:
console.print("Creating session from email and password.")
session = session_model.create_session(email, password, save) # type: ignore

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 All @@ -49,11 +55,10 @@ def login(
>>> from openbb_terminal.sdk import openbb
>>> openbb.login(email="your_email", password="your_password")
"""

session = Local.get_session()
if not (email or token):
session = Local.get_session()

if not session:
console.print("No local session found. Creating new session.")
session = get_session(email, password, token, keep_session)
else:
console.print("Using local session to login.")
Expand All @@ -65,6 +70,7 @@ def login(
console.print("[green]Login successful.[/green]")


@log_start_end(log=logger)
def logout():
"""
Logout and clear session.
Expand All @@ -81,6 +87,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
Loading

0 comments on commit a5129c4

Please sign in to comment.