diff --git a/data/userevent.py b/data/userevent.py index 47e433c89..a5b7671ca 100644 --- a/data/userevent.py +++ b/data/userevent.py @@ -1,8 +1,9 @@ -import redis import json import threading import logging +import redis + logger = logging.getLogger(__name__) class CannotReadUserEventsException(Exception): @@ -56,7 +57,7 @@ class UserEvent(object): as backed by Redis. """ def __init__(self, redis_config, username): - self._redis = redis.StrictRedis(socket_connect_timeout=5, **redis_config) + self._redis = redis.StrictRedis(socket_connect_timeout=2, **redis_config) self._username = username @staticmethod @@ -75,7 +76,7 @@ class UserEvent(object): try: self.publish_event_data_sync(event_id, data_obj) logger.debug('Published user event %s: %s', event_id, data_obj) - except Exception: + except redis.RedisError: logger.exception('Could not publish user event') thread = threading.Thread(target=conduct) @@ -87,7 +88,8 @@ class UserEventListener(object): Defines a helper class for subscribing to realtime user events as backed by Redis. """ - def __init__(self, redis_config, username, events=set([])): + def __init__(self, redis_config, username, events=None): + events = events or set([]) channels = [self._user_event_key(username, e) for e in events] try: @@ -114,8 +116,8 @@ class UserEventListener(object): try: data = json.loads(item['data'] or '{}') - except: - pass + except ValueError: + continue if data: yield event_id, data diff --git a/endpoints/trackhelper.py b/endpoints/trackhelper.py index f1bc708e5..0aa66cefa 100644 --- a/endpoints/trackhelper.py +++ b/endpoints/trackhelper.py @@ -56,16 +56,9 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1, metadata['public'] = True analytics_id = 'anonymous' - request_parsed = urlparse(request.url_root) - extra_params = { - 'repository': '%s/%s' % (namespace_name, repo_name), - 'user-agent': request.user_agent.string, - 'hostname': request_parsed.hostname, - } - # Publish the user event (if applicable) logger.debug('Checking publishing %s to the user events system', event_name) - if authenticated_user: + if authenticated_user and not authenticated_user.robot: logger.debug('Publishing %s to the user events system', event_name) user_event_data = { 'action': event_name, @@ -80,7 +73,16 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1, if random.random() < analytics_sample: if analytics_name is None: analytics_name = event_name + logger.debug('Logging the %s to Mixpanel', analytics_name) + + request_parsed = urlparse(request.url_root) + extra_params = { + 'repository': '%s/%s' % (namespace_name, repo_name), + 'user-agent': request.user_agent.string, + 'hostname': request_parsed.hostname, + } + analytics.track(analytics_id, analytics_name, extra_params) # Log the action to the database.