|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import ssl |
| 4 | +import uuid |
| 5 | +from base64 import b64encode |
| 6 | +from functools import cached_property, wraps |
| 7 | +from hashlib import blake2b |
| 8 | +from urllib.parse import urlencode |
| 9 | +from urllib.request import Request, urlopen |
| 10 | + |
| 11 | +from testgen import settings |
| 12 | +from testgen.ui.session import session |
| 13 | +from testgen.utils.singleton import Singleton |
| 14 | + |
| 15 | +LOG = logging.getLogger("testgen") |
| 16 | + |
| 17 | + |
| 18 | +def safe_method(method): |
| 19 | + @wraps(method) |
| 20 | + def wrapped(*args, **kwargs): |
| 21 | + if settings.ANALYTICS_ENABLED: |
| 22 | + try: |
| 23 | + method(*args, **kwargs) |
| 24 | + except Exception: |
| 25 | + LOG.exception("Error processing analytics data") |
| 26 | + |
| 27 | + return wrapped |
| 28 | + |
| 29 | + |
| 30 | +class MixpanelService(Singleton): |
| 31 | + |
| 32 | + @cached_property |
| 33 | + def instance_id(self): |
| 34 | + return settings.INSTANCE_ID or blake2b(uuid.getnode().to_bytes(8), digest_size=8).hexdigest() |
| 35 | + |
| 36 | + @cached_property |
| 37 | + def distinct_id(self): |
| 38 | + return self._hash_value(session.username) |
| 39 | + |
| 40 | + def _hash_value(self, value: bytes | str, digest_size: int = 8) -> str: |
| 41 | + if isinstance(value, str): |
| 42 | + value = value.encode() |
| 43 | + return blake2b(value, salt=self.instance_id.encode(), digest_size=digest_size).hexdigest() |
| 44 | + |
| 45 | + @safe_method |
| 46 | + def send_event(self, event_name, **properties): |
| 47 | + properties.setdefault("instance_id", self.instance_id) |
| 48 | + properties.setdefault("version", settings.VERSION) |
| 49 | + properties.setdefault("distinct_id", self.distinct_id) |
| 50 | + |
| 51 | + track_payload = { |
| 52 | + "event": event_name, |
| 53 | + "properties": { |
| 54 | + "token": settings.MIXPANEL_TOKEN, |
| 55 | + **properties, |
| 56 | + } |
| 57 | + } |
| 58 | + self.send_mp_request("track?ip=1", track_payload) |
| 59 | + |
| 60 | + def get_ssl_context(self): |
| 61 | + ssl_context = ssl.create_default_context() |
| 62 | + ssl_context.check_hostname = False |
| 63 | + ssl_context.verify_mode = ssl.CERT_NONE |
| 64 | + return ssl_context |
| 65 | + |
| 66 | + def send_mp_request(self, endpoint, payload): |
| 67 | + try: |
| 68 | + post_data = urlencode( |
| 69 | + {"data": b64encode(json.dumps(payload).encode()).decode()} |
| 70 | + ).encode() |
| 71 | + |
| 72 | + req = Request(f"{settings.MIXPANEL_URL}/{endpoint}", data=post_data, method="POST") # noqa: S310 |
| 73 | + req.add_header("Content-Type", "application/x-www-form-urlencoded") |
| 74 | + |
| 75 | + urlopen(req, context=self.get_ssl_context(), timeout=settings.MIXPANEL_TIMEOUT) # noqa: S310 |
| 76 | + except Exception: |
| 77 | + LOG.exception("Failed to send analytics data") |
0 commit comments