2014-10-30 16:49:51 +00:00
|
|
|
import logging
|
|
|
|
|
2014-11-15 00:55:31 +00:00
|
|
|
from app import analytics, app, userevents
|
2014-10-30 16:49:51 +00:00
|
|
|
from data import model
|
|
|
|
from flask import request
|
|
|
|
from auth.auth_context import get_authenticated_user, get_validated_token, get_validated_oauth_token
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
def track_and_log(event_name, repo, **kwargs):
|
|
|
|
repository = repo.name
|
|
|
|
namespace = repo.namespace_user.username
|
|
|
|
metadata = {
|
|
|
|
'repo': repository,
|
|
|
|
'namespace': namespace,
|
|
|
|
}
|
|
|
|
metadata.update(kwargs)
|
|
|
|
|
|
|
|
analytics_id = 'anonymous'
|
|
|
|
|
2015-01-04 19:50:08 +00:00
|
|
|
authenticated_oauth_token = get_validated_oauth_token()
|
|
|
|
authenticated_user = get_authenticated_user()
|
|
|
|
authenticated_token = get_validated_token() if not authenticated_user else None
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Logging the %s to Mixpanel and the log system', event_name)
|
2015-01-04 19:50:08 +00:00
|
|
|
if authenticated_oauth_token:
|
|
|
|
metadata['oauth_token_id'] = authenticated_oauth_token.id
|
|
|
|
metadata['oauth_token_application_id'] = authenticated_oauth_token.application.client_id
|
|
|
|
metadata['oauth_token_application'] = authenticated_oauth_token.application.name
|
|
|
|
analytics_id = 'oauth:' + authenticated_oauth_token.id
|
|
|
|
elif authenticated_user:
|
|
|
|
metadata['username'] = authenticated_user.username
|
|
|
|
analytics_id = authenticated_user.username
|
|
|
|
elif authenticated_token:
|
|
|
|
metadata['token'] = authenticated_token.friendly_name
|
|
|
|
metadata['token_code'] = authenticated_token.code
|
2015-02-17 17:35:16 +00:00
|
|
|
|
|
|
|
if authenticated_token.kind:
|
|
|
|
metadata['token_type'] = authenticated_token.kind.name
|
|
|
|
|
2015-01-04 19:50:08 +00:00
|
|
|
analytics_id = 'token:' + authenticated_token.code
|
2014-10-30 16:49:51 +00:00
|
|
|
else:
|
|
|
|
metadata['public'] = True
|
|
|
|
analytics_id = 'anonymous'
|
|
|
|
|
|
|
|
extra_params = {
|
|
|
|
'repository': '%s/%s' % (namespace, repository),
|
|
|
|
}
|
|
|
|
|
2014-11-15 00:55:31 +00:00
|
|
|
# Publish the user event (if applicable)
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking publishing %s to the user events system', event_name)
|
2015-01-04 19:50:08 +00:00
|
|
|
if authenticated_user:
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Publishing %s to the user events system', event_name)
|
2014-11-15 00:55:31 +00:00
|
|
|
user_event_data = {
|
|
|
|
'action': event_name,
|
|
|
|
'repository': repository,
|
|
|
|
'namespace': namespace
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:50:08 +00:00
|
|
|
event = userevents.get_event(authenticated_user.username)
|
2014-11-15 00:55:31 +00:00
|
|
|
event.publish_event_data('docker-cli', user_event_data)
|
|
|
|
|
|
|
|
# Save the action to mixpanel.
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Logging the %s to Mixpanel', event_name)
|
2014-10-30 16:49:51 +00:00
|
|
|
analytics.track(analytics_id, event_name, extra_params)
|
2014-11-15 00:55:31 +00:00
|
|
|
|
|
|
|
# Log the action to the database.
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Logging the %s to logs system', event_name)
|
2014-10-30 16:49:51 +00:00
|
|
|
model.log_action(event_name, namespace,
|
2015-01-04 19:50:08 +00:00
|
|
|
performer=authenticated_user,
|
2014-10-30 16:49:51 +00:00
|
|
|
ip=request.remote_addr, metadata=metadata,
|
2014-11-24 21:07:38 +00:00
|
|
|
repository=repo)
|
2015-01-04 19:50:08 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Track and log of %s complete', event_name)
|