mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 20:42:26 +00:00
Filtering duplicate logs (#3445)
* filtering duplicate logs * removes the logging on the pyth model * checks if the log file is bigger than 2mb * check for a trail stack to know if should log or not * using method_stack only on the OperationLogger * getting rid of deque * linting * removed unusded import * removed redundant method * ype annotation
This commit is contained in:
@@ -84,7 +84,6 @@ class Operation:
|
||||
self._trail = trail
|
||||
self._trail_map = trail_map
|
||||
self._method = method
|
||||
|
||||
self.__doc__ = metadata.docstring
|
||||
self.__signature__ = signature(method)
|
||||
|
||||
@@ -116,6 +115,9 @@ class Operation:
|
||||
|
||||
|
||||
class OperationLogger:
|
||||
|
||||
last_method: Dict[Any, Any] = {}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
trail: str,
|
||||
@@ -133,7 +135,7 @@ class OperationLogger:
|
||||
def log_before_call(
|
||||
self,
|
||||
):
|
||||
if not cfg.LOGGING_SUPPRESS:
|
||||
if self.__check_logging_conditions():
|
||||
logger = self.__logger
|
||||
self.__log_start(logger=logger, method_chosen=self.__method_chosen)
|
||||
self.__log_method_info(
|
||||
@@ -151,17 +153,16 @@ class OperationLogger:
|
||||
extra={"func_name_override": method_chosen.__name__},
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __log_method_info(
|
||||
cls,
|
||||
self,
|
||||
logger: Logger,
|
||||
trail: str,
|
||||
method_chosen: Callable,
|
||||
args: Any,
|
||||
kwargs: Any,
|
||||
):
|
||||
merged_args = cls.__merge_function_args(method_chosen, args, kwargs)
|
||||
merged_args = cls.__remove_key_and_log_state(
|
||||
merged_args = self.__merge_function_args(method_chosen, args, kwargs)
|
||||
merged_args = self.__remove_key_and_log_state(
|
||||
method_chosen.__module__, merged_args
|
||||
)
|
||||
|
||||
@@ -244,7 +245,7 @@ class OperationLogger:
|
||||
self,
|
||||
method_result: Any,
|
||||
):
|
||||
if not cfg.LOGGING_SUPPRESS:
|
||||
if self.__check_logging_conditions():
|
||||
logger = self.__logger
|
||||
self.__log_exception_if_any(
|
||||
logger=logger,
|
||||
@@ -255,6 +256,12 @@ class OperationLogger:
|
||||
logger=logger,
|
||||
method_chosen=self.__method_chosen,
|
||||
)
|
||||
OperationLogger.last_method = {
|
||||
f"{self.__method_chosen.__module__}.{self.__method_chosen.__name__}": {
|
||||
"args": self.__args,
|
||||
"kwargs": self.__kwargs,
|
||||
}
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def __log_exception_if_any(
|
||||
@@ -274,3 +281,15 @@ class OperationLogger:
|
||||
"END",
|
||||
extra={"func_name_override": method_chosen.__name__},
|
||||
)
|
||||
|
||||
def __check_logging_conditions(self) -> bool:
|
||||
return not cfg.LOGGING_SUPPRESS and not self.__check_last_method()
|
||||
|
||||
def __check_last_method(self) -> bool:
|
||||
current_method = {
|
||||
f"{self.__method_chosen.__module__}.{self.__method_chosen.__name__}": {
|
||||
"args": self.__args,
|
||||
"kwargs": self.__kwargs,
|
||||
}
|
||||
}
|
||||
return OperationLogger.last_method == current_method
|
||||
|
||||
@@ -72,7 +72,7 @@ class LogSender(Thread):
|
||||
file = item.path
|
||||
last = item.last
|
||||
|
||||
if self.check_sending_conditions():
|
||||
if self.check_sending_conditions(file=file):
|
||||
archives_file = file.parent / ARCHIVES_FOLDER_NAME / f"{file.stem}.log"
|
||||
object_key = (
|
||||
f"{app_name}{S3_FOLDER_SUFFIX}/logs/{identifier}/{file.stem}.log"
|
||||
@@ -110,10 +110,14 @@ class LogSender(Thread):
|
||||
self.__fails: int = 0 # type: ignore
|
||||
self.__queue: SimpleQueue = SimpleQueue()
|
||||
|
||||
def check_sending_conditions(self):
|
||||
def check_sending_conditions(self, file: Path) -> bool:
|
||||
"""Check if the condition are met to send data."""
|
||||
|
||||
return self.start_required() and not self.max_fails_reached()
|
||||
return (
|
||||
self.start_required()
|
||||
and not self.max_fails_reached()
|
||||
and not self.max_size_exceeded(file=file)
|
||||
)
|
||||
|
||||
def fails_increment(self):
|
||||
self.__fails += 1
|
||||
@@ -124,6 +128,11 @@ class LogSender(Thread):
|
||||
def max_fails_reached(self) -> bool:
|
||||
return self.__fails > self.MAX_FAILS
|
||||
|
||||
def max_size_exceeded(self, file: Path) -> bool:
|
||||
"""Check if the log file is bigger than 2MB."""
|
||||
|
||||
return file.stat().st_size > 2 * 1024 * 1024
|
||||
|
||||
def send_path(self, path: Path, last: bool = False):
|
||||
"""Only closed files should be sent."""
|
||||
|
||||
|
||||
@@ -169,7 +169,9 @@ class PathTrackingFileHandler(TimedRotatingFileHandler):
|
||||
with self.lock:
|
||||
super().doRollover()
|
||||
|
||||
if log_sender.check_sending_conditions():
|
||||
log_path = Path(self.baseFilename)
|
||||
|
||||
if log_sender.check_sending_conditions(file=log_path):
|
||||
to_delete_path_list = self.getFilesToDelete()
|
||||
for path in to_delete_path_list:
|
||||
log_sender.send_path(path=Path(path))
|
||||
@@ -181,7 +183,7 @@ class PathTrackingFileHandler(TimedRotatingFileHandler):
|
||||
log_sender = self.__log_sender
|
||||
closed_log_path = Path(self.baseFilename)
|
||||
|
||||
if log_sender.check_sending_conditions():
|
||||
if log_sender.check_sending_conditions(file=closed_log_path):
|
||||
log_sender.send_path(path=closed_log_path, last=True)
|
||||
try:
|
||||
log_sender.join(timeout=3)
|
||||
@@ -190,5 +192,5 @@ class PathTrackingFileHandler(TimedRotatingFileHandler):
|
||||
|
||||
super().close()
|
||||
|
||||
if log_sender.check_sending_conditions():
|
||||
if log_sender.check_sending_conditions(file=closed_log_path):
|
||||
closed_log_path.unlink(missing_ok=True)
|
||||
|
||||
@@ -9,7 +9,6 @@ from pythclient.solana import (
|
||||
SOLANA_DEVNET_WS_ENDPOINT,
|
||||
)
|
||||
|
||||
from openbb_terminal.decorators import log_start_end
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -74,7 +73,6 @@ ASSETS = {
|
||||
}
|
||||
|
||||
|
||||
@log_start_end(log=logger)
|
||||
async def get_price(symbol: str) -> Tuple[float, float, float]:
|
||||
"""Returns price and confidence interval from pyth live feed. [Source: Pyth]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user