2
2
import inspect
3
3
import sys
4
4
import traceback
5
- from json import dump
6
5
from pathlib import Path
7
6
from typing import Callable
8
7
from uuid import uuid4
14
13
ASSEMBLIES_DIR = Path (r"C:\ProgramData\BHoM\Assemblies" )
15
14
LOGGING_DIR = Path (r"C:\ProgramData\BHoM\Logs" )
16
15
LOGGING_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 ()
17
19
18
20
19
21
def analytics (f : Callable ):
20
22
"""A wrapper used to capture usage analytics of the decorated function."""
21
23
22
24
@functools .wraps (f )
23
25
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 ()
28
26
29
27
_ , module_stack , _ , file_path = str (inspect .getmodule (f )).split (" " )
30
28
module_stack = module_stack [1 :- 1 ]
31
29
32
- log_file = LOGGING_DIR / f"Usage_{ ui_name } _{ ticks_value } .log"
30
+ log_file = LOGGING_DIR / f"Usage_{ UI_NAME } _{ TICKS } .log"
33
31
34
32
# gather metadata around current callable instance
35
33
d = {
36
- "BHoMVersion" : bhom_version ,
34
+ "BHoMVersion" : BHOM_VERSION ,
37
35
"BHoM_Guid" : str (uuid4 ()),
38
36
"CallerName" : f"{ module_stack } " ,
39
37
"ComponentId" : "" ,
@@ -47,17 +45,17 @@ def wrapper(*args, **kwargs):
47
45
"SelectedItem" :
48
46
{
49
47
"Name" : f .__name__ ,
50
- "_bhomVersion" : bhom_version ,
48
+ "_bhomVersion" : BHOM_VERSION ,
51
49
"_t" : "Python"
52
50
},
53
51
"Tags" : [],
54
52
"Time" :
55
53
{
56
- "$date" : ticks_value
54
+ "$date" : TICKS
57
55
},
58
- "UI" : ui_name ,
56
+ "UI" : UI_NAME ,
59
57
"UiVersion" : sys .version ,
60
- "_bhomVersion" : bhom_version ,
58
+ "_bhomVersion" : BHOM_VERSION ,
61
59
"_t" : "BH.oM.UI.UsageLogEntry"
62
60
}
63
61
@@ -67,11 +65,11 @@ def wrapper(*args, **kwargs):
67
65
except Exception :
68
66
d ["Errors" ].append (traceback .format_exc ())
69
67
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 " )
72
70
73
71
# 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)
75
73
76
74
return f (* args , ** kwargs )
77
75
0 commit comments