62 lines
No EOL
2 KiB
Python
62 lines
No EOL
2 KiB
Python
import logging
|
|
|
|
from app import analytics, app, userevents
|
|
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__)
|
|
profile = logging.getLogger('application.profiler')
|
|
|
|
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'
|
|
|
|
profile.debug('Logging the %s to Mixpanel and the log system', event_name)
|
|
if get_validated_oauth_token():
|
|
oauth_token = get_validated_oauth_token()
|
|
metadata['oauth_token_id'] = oauth_token.id
|
|
metadata['oauth_token_application_id'] = oauth_token.application.client_id
|
|
metadata['oauth_token_application'] = oauth_token.application.name
|
|
analytics_id = 'oauth:' + oauth_token.id
|
|
elif get_authenticated_user():
|
|
metadata['username'] = get_authenticated_user().username
|
|
analytics_id = get_authenticated_user().username
|
|
elif get_validated_token():
|
|
metadata['token'] = get_validated_token().friendly_name
|
|
metadata['token_code'] = get_validated_token().code
|
|
analytics_id = 'token:' + get_validated_token().code
|
|
else:
|
|
metadata['public'] = True
|
|
analytics_id = 'anonymous'
|
|
|
|
extra_params = {
|
|
'repository': '%s/%s' % (namespace, repository),
|
|
}
|
|
|
|
# Publish the user event (if applicable)
|
|
if get_authenticated_user():
|
|
user_event_data = {
|
|
'action': event_name,
|
|
'repository': repository,
|
|
'namespace': namespace
|
|
}
|
|
|
|
event = userevents.get_event(get_authenticated_user().username)
|
|
event.publish_event_data('docker-cli', user_event_data)
|
|
|
|
# Save the action to mixpanel.
|
|
analytics.track(analytics_id, event_name, extra_params)
|
|
|
|
# Log the action to the database.
|
|
model.log_action(event_name, namespace,
|
|
performer=get_authenticated_user(),
|
|
ip=request.remote_addr, metadata=metadata,
|
|
repository=repo) |