move get_branch request to logging_settings (#5348)

* move get_branch request to logging_settings

* black

* add todo

* Revert "add todo"

This reverts commit 7e3af83e3d.

* add todo

* Update logging_settings.py
This commit is contained in:
montezdesousa
2023-08-18 16:56:03 +01:00
committed by GitHub
parent e0776b4e55
commit 07dd848679
3 changed files with 12 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
from pathlib import Path
from typing import List, Optional
from openbb_core.app.logs.utils.system_utils import get_branch
from openbb_core.app.logs.utils.utils import get_app_id, get_log_dir, get_session_id
from openbb_core.app.model.system_settings import SystemSettings
@@ -33,7 +34,10 @@ class LoggingSettings:
# System
self.commit_hash: Optional[str] = system_settings.logging_commit_hash
self.branch: Optional[str] = system_settings.logging_branch
# TODO: If we can avoid get_branch making a web request we can save up to 1s of import time.
self.branch: Optional[str] = (
get_branch(self.commit_hash) if self.commit_hash else None
)
self.app_name: str = system_settings.logging_app_name
self.app_id: str = get_app_id(user_data_directory)
self.session_id: str = get_session_id()

View File

@@ -19,7 +19,9 @@ GITHUB_PROJECT_URL = (
UNKNOWN_COMMIT_PLACEHOLDER = "unknown-commit"
UNKNOWN_BRANCH_PLACEHOLDER = "unknown-branch"
REPOSITORY_DIRECTORY = Path(__file__).parent.parent.parent.parent.parent
REPOSITORY_DIRECTORY = Path(
__file__
).parent.parent.parent.parent.parent.parent.parent.parent
def get_commit_hash() -> str:
@@ -41,6 +43,9 @@ def get_commit_hash() -> str:
def get_branch(commit_hash: str, request_timeout: int = 5) -> str:
"""Get Branch Name"""
if commit_hash == UNKNOWN_COMMIT_PLACEHOLDER:
return UNKNOWN_BRANCH_PLACEHOLDER
def get_branch_commit_hash(branch: str) -> str:
response = requests.get(
f"{GITHUB_PROJECT_URL}/{branch}", timeout=request_timeout

View File

@@ -13,7 +13,7 @@ from openbb_core.app.constants import (
SYSTEM_SETTINGS_PATH,
USER_SETTINGS_PATH,
)
from openbb_core.app.logs.utils.system_utils import get_branch, get_commit_hash
from openbb_core.app.logs.utils.system_utils import get_commit_hash
from openbb_core.app.model.abstract.tagged import Tagged
FrozenField = partial(Field, allow_mutation=False)
@@ -36,7 +36,6 @@ class SystemSettings(Tagged):
# Logging section
logging_app_name: str = FrozenField(default="gst")
logging_commit_hash: Optional[str] = FrozenField(default=None)
logging_branch: Optional[str] = FrozenField(default=None)
logging_frequency: Literal["D", "H", "M", "S"] = FrozenField(default="H")
logging_handlers: List[str] = FrozenField(default_factory=lambda: ["file"])
logging_rolling_clock: bool = FrozenField(default=False)
@@ -73,14 +72,6 @@ class SystemSettings(Tagged):
with open(path, mode="w") as file:
json.dump({}, file)
# TODO: Allow setting debug mode from environment variable
# @root_validator(allow_reuse=True)
# @classmethod
# def validate_debug_mode(cls, values):
# dm = os.getenv("DEBUG_MODE", "").lower() in ["true", "1"]
# values["debug_mode"] = bool(values["debug_mode"] or dm)
# return values
@root_validator(allow_reuse=True)
@classmethod
def create_openbb_directory(cls, values):
@@ -122,14 +113,3 @@ class SystemSettings(Tagged):
@classmethod
def validate_commit_hash(cls, v):
return v or get_commit_hash()
@root_validator(allow_reuse=True)
@classmethod
def validate_branch(cls, values):
branch = values["logging_branch"]
commit_hash = values["logging_commit_hash"]
if not branch and commit_hash:
values["logging_branch"] = get_branch(commit_hash)
return values