Skip to content

Commit

Permalink
Add @tel_logTime decorator, update tests, version and fix minor bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolphpienaar committed Mar 22, 2023
1 parent a21eb99 commit fcff769
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
37 changes: 26 additions & 11 deletions pflog/pflog.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
__version__ = '1.2.5'
__version__ = '1.2.6'

from pathlib import Path

Expand Down Expand Up @@ -331,15 +331,27 @@ def __call__(self, message:str, *args: Any, **kwds: Any) -> Any:
self.options.log = message
return self.run()

def tel_logTime(_func: Callable = None, *,
someother:str = '',
pftelDB:str = '',
name:str = '',
log:str = '',
isthis:str = ''):
def tel_logTime(_func:Callable = None, *,
pftelDB:str = '',
event:str = '',
log:str = ''):
"""A decorator that times (and logs) a method.
If called without any arguments, this decorator simply prints the time
(seconds) that a method took to execute. If called with named args, will
transmit the time (and optional log message) as an event to a pftelDB
server.
Args:
_func (Callable, optional): the function to wrap. Defaults to None.
pftelDB (str, optional): telemetry server address. Defaults to ''.
event (str, optional): the name of this event. Defaults to ''.
log (str, optional): optional log message. Defaults to ''.
"""
def decorator_time(func: Callable) -> Callable:
@functools.wraps(func)
def wrapped(*args, **kwargs) -> Any:
pftelDBlocal:str = ''
d_log:dict = {}
tagger:pftag.Pftag = pftag.Pftag({})
str_event:str = 'analysisEvent'
Expand All @@ -350,17 +362,19 @@ def wrapped(*args, **kwargs) -> Any:
print(f"{func} executed in {ft} second(s).")
try:
if pftelDB:
if name:
str_event = name
pftelDB = '/'.join(pftelDB.split('/')[:-1] + [str_event])
if event:
str_event = event
pftelDBlocal = pftelDB
pftelDBlocal = '/'.join(pftelDB.split('/')[:-1] + [str_event])
d_log:dict = pfprint(
options.pftelDB,
pftelDBlocal,
log,
appName = str_event,
execTime = ft
)
except:
pass
return f_ret, d_log
return wrapped
if _func is None:
return decorator_time
Expand Down Expand Up @@ -399,6 +413,7 @@ def pfprint(pftelspec:str, message:str, **kwargs) -> str:
logObject:str = ""
logCollection:str = ""
logEvent:str = ""
pftelspec = pftelspec.strip()
d_log = {
"status": False,
"reply": None,
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -2,4 +2,5 @@ pudb
loguru
pftel-client
pftag
pytest-mock

2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -30,7 +30,7 @@ def get_version(rel_path: str) -> str:
author = 'FNNDSC',
author_email = 'rudolph.pienaar@childrens.harvard.edu',
url = 'https://github.com/FNNDSC/pflog',
install_requires = ['pudb', 'loguru', 'pftag', 'pftel-client'],
install_requires = ['pudb', 'loguru', 'pftag', 'pftel-client', 'pytest-mock'],
packages = ['pflog'],
license = 'MIT',
entry_points = {
Expand Down
29 changes: 18 additions & 11 deletions tests/test_pflog.py
Expand Up @@ -11,6 +11,7 @@
def test_pfprint() -> None:
collection:str = '%timestamp_chrplc|:|-_'
IP:str = socket.gethostbyname(socket.gethostname())
# This test will fail if a local `pftel` server has not been started!
pftelURL:str = f"http://{IP}:22223/api/v1/new/{collection}/event"

d_log:dict = pflog.pfprint(pftelURL, "hello, world!" , appName = "testApp", execTime = 2.0)
Expand All @@ -28,22 +29,28 @@ def test_pfprint_validSpecInvalidURL() -> None:
d_log:dict = pflog.pfprint(pftelURL, "hello, world!" , appName = "testApp", execTime = 2.0)
assert d_log['status'] is False

def test_mocpftelTimed() -> None:
def testmocptelTimedNoArgsDecorator(mocker) -> None:

mock_print = mocker.patch('builtins.print')
@pflog.tel_logTime
def wait(seconds:float) -> None:
time.sleep(seconds)
wait(2)
assert mock_print.call_count is 1

def test_mocpftelTimed(mocker) -> None:
"""
Send message to the moc listener with a log time
"""
pudb.set_trace()
pftelURL:str = r' https://pftel-chris-public.apps.ocp-prod.massopen.cloud/api/v1/timetest/%timestamp/analysis'
mock_print = mocker.patch('builtins.print')
pftelURL:str = r'https://pftel-chris-public.apps.ocp-prod.massopen.cloud/api/v1/timetest/%timestamp/analysis'

@pflog.tel_logTime(
someother = 'something',
pftelDB = pftelURL,
name = 'pytest',
log = 'A two second delay logger',
isthis = pftelURL
event = 'pytest',
log = 'A two second delay logger'
)
# @pflog.tel_logTime
def wait(seconds:float):
def wait(seconds:float) -> None:
time.sleep(seconds)
wait(2)
assert True == True

assert mock_print.call_count is 2

0 comments on commit fcff769

Please sign in to comment.