-
Notifications
You must be signed in to change notification settings - Fork 32
/
client.py
243 lines (197 loc) · 7.17 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Copyright (c) ZenML GmbH 2023. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
"""Analytics module for MLStacks."""
import datetime
import logging
import os
from types import TracebackType
from typing import Any, Dict, List, Optional, Type, cast
from uuid import uuid4
import click
from segment import analytics
from mlstacks.constants import (
ANALYTICS_OPT_IN_ENV_VARIABLE,
MLSTACKS_PACKAGE_NAME,
)
from mlstacks.enums import AnalyticsEventsEnum
from mlstacks.utils.analytics_utils import operating_system, python_version
from mlstacks.utils.environment_utils import handle_bool_env_var
from mlstacks.utils.yaml_utils import load_yaml_as_dict
logger = logging.getLogger(__name__)
logging.getLogger("segment").disabled = True
analytics.write_key = "tU9BJvF05TgC29xgiXuKF7CuYP0zhgnx"
analytics.max_retries = 1
CONFIG_FILENAME = "config.yaml"
def on_error(error: Exception, batch: List[Dict[str, Any]]) -> None:
"""Custom error handler for Segment analytics.
Args:
error: The error that occurred.
batch: Events processed when the error occurred.
"""
logger.debug("Analytics error: %s; Batch: %s", error, batch)
analytics.on_error = on_error
class MLStacksAnalyticsContext:
"""Analytics context manager for MLStacks."""
def __init__(self) -> None:
"""Initialization."""
self.analytics_opt_in = handle_bool_env_var(
var=ANALYTICS_OPT_IN_ENV_VARIABLE,
default=True,
)
self.user_id: Optional[str] = None
def __enter__(self) -> "MLStacksAnalyticsContext":
"""Enter the analytics context manager.
Returns:
MLStacksAnalyticsContext: Analytics context manager
"""
if self.analytics_opt_in and not self.get_analytics_user_id():
self.user_id = str(uuid4())
self.set_analytics_user_id(self.user_id)
analytics.identify(
self.user_id,
{
"created_at": datetime.datetime.now(
tz=datetime.timezone.utc,
),
},
)
else:
self.user_id = self.get_analytics_user_id()
return self
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> bool:
"""Exit context manager.
Args:
exc_type (Optional[Type[BaseException]]): Exception type
exc_val (Optional[BaseException]): Exception value
exc_tb (Optional[TracebackType]): Traceback type
Returns:
True if no exception occurred, False otherwise
"""
if exc_val:
logger.debug("Error occurred: %s", exc_val)
return True
def track(
self,
event: AnalyticsEventsEnum,
properties: Optional[Dict[Any, Any]] = None,
) -> Any:
"""Tracks event in Segment.
Args:
event (AnalyticsEventsEnum): Event to track
properties (Optional[Dict[Any, Any]]): Properties to track
Returns:
Result of the tracking
"""
if self.analytics_opt_in:
if properties is None:
properties = {}
return analytics.track(
self.user_id,
event.value,
{
"timestamp": datetime.datetime.now(
tz=datetime.timezone.utc,
),
"python_version": python_version(),
"operating_system": operating_system(),
**properties,
},
)
return None
@staticmethod
def get_analytics_user_id() -> Optional[str]:
"""Returns the user id for analytics.
Returns:
The user id for analytics.
"""
config_dir = click.get_app_dir(MLSTACKS_PACKAGE_NAME)
config_file = os.path.join(config_dir, CONFIG_FILENAME)
if os.path.exists(config_file):
yaml_dict = load_yaml_as_dict(config_file)
return cast(str, yaml_dict.get("analytics_user_id", None))
return None
@staticmethod
def set_analytics_user_id(user_id: str) -> None:
"""Sets the user id for analytics.
Args:
user_id: The user id for analytics.
"""
config_dir = click.get_app_dir(MLSTACKS_PACKAGE_NAME)
os.makedirs(config_dir, exist_ok=True)
config_file = os.path.join(config_dir, CONFIG_FILENAME)
with open(config_file, "w") as f:
f.write(f"analytics_user_id: {user_id}")
def track_event(
event: AnalyticsEventsEnum,
metadata: Optional[Dict[str, Any]] = None,
) -> bool:
"""Track segment event if user opted-in.
Args:
event: Name of event to track in segment.
metadata: Dict of metadata to track.
Returns:
True if event is sent successfully, False otherwise.
"""
if metadata is None:
metadata = {}
metadata.setdefault("event_success", True)
with MLStacksAnalyticsContext() as analytics_context:
return bool(analytics_context.track(event=event, properties=metadata))
return False
class EventHandler:
"""Context handler to enable tracking the success status of an event."""
def __init__(
self,
event: AnalyticsEventsEnum,
metadata: Optional[Dict[str, Any]] = None,
):
"""Initialization of the context manager.
Args:
event: The type of the analytics event
metadata: The metadata of the event.
"""
self.event: AnalyticsEventsEnum = event
self.metadata: Dict[str, Any] = metadata or {}
def __enter__(self) -> "EventHandler":
"""Enter function of the event handler.
Returns:
the handler instance.
"""
return self
def __exit__(
self,
type_: Optional[Any],
value: Optional[Any],
traceback: Optional[Any],
) -> Any:
"""Exit function of the event handler.
Checks whether there was a traceback and updates the metadata
accordingly. Following the check, it calls the function to track the
event.
Args:
type_: The class of the exception
value: The instance of the exception
traceback: The traceback of the exception
"""
if traceback is not None:
self.metadata.update({"event_success": False})
else:
self.metadata.update({"event_success": True})
if type_ is not None:
self.metadata.update({"event_error_type": type_.__name__})
track_event(self.event, self.metadata)