22import inspect
33import sys
44import traceback
5- from json import dump
65from pathlib import Path
76from typing import Callable
87from uuid import uuid4
1413ASSEMBLIES_DIR = Path (r"C:\ProgramData\BHoM\Assemblies" )
1514LOGGING_DIR = Path (r"C:\ProgramData\BHoM\Logs" )
1615LOGGING_DIR .mkdir (exist_ok = True , parents = True )
16+ BHOM_VERSION = "." .join ([str (i ) for i in version ((ASSEMBLIES_DIR / "BHoM.dll" ).as_posix ())][:2 ])
17+ UI_NAME = "PythonBHoM"
18+ TICKS = ticks ()
1719
1820
1921def analytics (f : Callable ):
2022 """A wrapper used to capture usage analytics of the decorated function."""
2123
2224 @functools .wraps (f )
2325 def wrapper (* args , ** kwargs ):
24-
25- bhom_version = "." .join ([str (i ) for i in version ((ASSEMBLIES_DIR / "BHoM.dll" ).as_posix ())][:2 ])
26- ui_name = "PythonBHoM"
27- ticks_value = ticks ()
2826
2927 _ , module_stack , _ , file_path = str (inspect .getmodule (f )).split (" " )
3028 module_stack = module_stack [1 :- 1 ]
3129
32- log_file = LOGGING_DIR / f"Usage_{ ui_name } _{ ticks_value } .log"
30+ log_file = LOGGING_DIR / f"Usage_{ UI_NAME } _{ TICKS } .log"
3331
3432 # gather metadata around current callable instance
3533 d = {
36- "BHoMVersion" : bhom_version ,
34+ "BHoMVersion" : BHOM_VERSION ,
3735 "BHoM_Guid" : str (uuid4 ()),
3836 "CallerName" : f"{ module_stack } " ,
3937 "ComponentId" : "" ,
@@ -47,17 +45,17 @@ def wrapper(*args, **kwargs):
4745 "SelectedItem" :
4846 {
4947 "Name" : f .__name__ ,
50- "_bhomVersion" : bhom_version ,
48+ "_bhomVersion" : BHOM_VERSION ,
5149 "_t" : "Python"
5250 },
5351 "Tags" : [],
5452 "Time" :
5553 {
56- "$date" : ticks_value
54+ "$date" : TICKS
5755 },
58- "UI" : ui_name ,
56+ "UI" : UI_NAME ,
5957 "UiVersion" : sys .version ,
60- "_bhomVersion" : bhom_version ,
58+ "_bhomVersion" : BHOM_VERSION ,
6159 "_t" : "BH.oM.UI.UsageLogEntry"
6260 }
6361
@@ -67,11 +65,11 @@ def wrapper(*args, **kwargs):
6765 except Exception :
6866 d ["Errors" ].append (traceback .format_exc ())
6967
70- with open (log_file , "w +" ) as fp :
71- dump ( d , fp , cls = Encoder )
68+ with open (log_file , "a +" ) as fp :
69+ fp . write ( str ( d ) + " \n " )
7270
7371 # print function here to enable downstream processes to access the location where the data is stored for loading into BHoM.
74- print (log_file , file = sys .stdout )
72+ # print(log_file, file=sys.stdout)
7573
7674 return f (* args , ** kwargs )
7775
0 commit comments